table.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. namespace Juju\PDO;
  3. require_once(realpath(dirname(__DIR__).'/pdo.class.php'));
  4. use Juju\{PDO, PDO\Transaction};
  5. class Table {
  6. const COLUMN = 'column';
  7. const INDEX = 'index';
  8. const FOREIGN_KEY = 'foreignkey';
  9. private $pdo;
  10. private $name;
  11. private $exists;
  12. private $primaryKey;
  13. private $columns;
  14. private $index;
  15. private $foreignKey;
  16. private $columns_removed;
  17. private $index_removed;
  18. private $foreignKey_removed;
  19. public function __construct($pdo, string $name){
  20. if($pdo instanceof PDO || $pdo instanceof Transaction){
  21. $this->pdo = $pdo;
  22. $this->name = $name;
  23. $this->rollback();
  24. }else{
  25. throw new \Exception("Invalid pdo argument");
  26. }
  27. }
  28. public function __get($name){
  29. switch($name){
  30. case 'exists':case 'name':case 'columns':
  31. return $this->$name;
  32. break;
  33. default:
  34. throw new \Exception("Invalid property {$name}");
  35. }
  36. }
  37. public function exists(){
  38. $pdo = $this->pdo;
  39. try{
  40. $count = $pdo->exec("select count(1) from `{$this->name}` limit 1");
  41. $this->exists = $count > 0;
  42. }catch(\Exception $e){
  43. $this->exists = false;
  44. }
  45. return $this->exists;
  46. }
  47. public function describe(){
  48. $this->columns = [];
  49. $this->primaryKey = [];
  50. $this->index = [];
  51. $this->foreignKeys = [];
  52. $this->columns_removed = [];
  53. $this->index_removed = [];
  54. $this->foreignKey_removed = [];
  55. if($this->exists){
  56. $pdo = $this->pdo;
  57. $query = $pdo->query("show create table `{$this->name}`");
  58. $sql = $query->fetch()['create table'];
  59. $query->closeCursor();
  60. if(preg_match_all('/ PRIMARY KEY \(((?:`[^`]+`,?)+)\)/s', $sql, $primaryKey, PREG_SET_ORDER) > 0){
  61. $this->primaryKey = array_map(
  62. function($item){
  63. return preg_replace('/`([^`]+)`/', '\\1', $item);
  64. },
  65. array_merge(
  66. ...array_map(
  67. function($item){
  68. return explode(',', $item[1]);
  69. }, $primaryKey
  70. )
  71. )
  72. );
  73. }
  74. unset($primaryKey);
  75. if(preg_match_all('/ ((?:UNIQUE)? ?KEY) `([^`]+)` \(((?:`[^`]+`,?)+)\)/s', $sql, $indexes, PREG_SET_ORDER) > 0){
  76. $this->indexes = array_reduce($indexes, function($indexes, $item){
  77. $indexes[$item[2]] = [
  78. 'unique'=> $item[1] == 'UNIQUE KEY',
  79. 'columns'=> array_map(function($item){
  80. return preg_replace('/`([^`]+)`/', '\\1', $item);
  81. }, explode(',', $item[3])),
  82. 'dirty'=> false
  83. ];
  84. return $indexes;
  85. });
  86. }
  87. unset($indexes);
  88. if(preg_match_all('/ CONSTRAINT `([^`]+)` FOREIGN KEY \(((?:`[^`]+`(?:, )?)+)\) REFERENCES `([^`]+)` \(((?:`[^`]+`(?:, )?)+)\)/s', $sql, $foreignKeys, PREG_SET_ORDER, 0) > 0){
  89. $this->foreignKeys = array_reduce($foreignKeys, function($foreignKeys, $item) use($sql){
  90. $columns = explode(',', $item[2]);
  91. $matches = explode(',', $item[4]);
  92. foreach($columns as $key => &$column){
  93. $column = [
  94. preg_replace('/`([^`]+)`/', '\\1', $column),
  95. preg_replace('/`([^`]+)`/', '\\1', $matches[$key])
  96. ];
  97. }
  98. $foreignKeys[$item[1]] = [
  99. 'references'=> $item[3],
  100. 'columns'=> $columns,
  101. 'dirty'=> false
  102. ];
  103. return $foreignKeys;
  104. });
  105. }
  106. unset($foreignKeys);
  107. if(preg_match_all('/ `([^`]+)` ([^\(]+\([^\)]+\))[^,\n]*,?/s', $sql, $columns, PREG_SET_ORDER) > 0){
  108. foreach($columns as $column){
  109. $default = null;
  110. $line = $column[0];
  111. $line .= substr($line, -1, 1) === ',' ? '' : ',';
  112. if(preg_match('/ `[^`]+` [^\(]+\([^\)]+\)[^D$]+DEFAULT (?!NULL)(.+)(?: AUTO_INCREMENT|UNIQUE|KEY|PRIMARY|COMMENT|COLUMN_FORMAT|STORAGE|REFERENCES|,)/u', $line, $match) == 1){
  113. $default = preg_replace("/'(.+)'/", "\\1", $match[1]);
  114. }
  115. $this->column(
  116. $column[1], // name
  117. $column[2], // type
  118. $default, // default
  119. preg_match('/`[^`]+` [^\(]+\([^\)]+\) NOT NULL/', $column[0]) === 1, // null
  120. preg_match('/ `[^`]+` [^\(]+\([^\)]+\)[^A$]+AUTO_INCREMENT,?/u', $line) == 1 // increment
  121. );
  122. $this->columns[$column[1]]['dirty'] = false;
  123. }
  124. }
  125. unset($columns);
  126. }
  127. }
  128. public function commit(){
  129. $pdo = $this->pdo;
  130. if(!$this->exists){
  131. $columns = '';
  132. foreach($this->columns as $name => $column){
  133. $columns .= "{$pdo->stringColumn($name, $column)},";
  134. }
  135. if(count($columns) > 0){
  136. $columns = rtrim($columns, ',');
  137. }
  138. $pk = $this->primaryKey();
  139. if(count($pk)){
  140. $pk = ", primary key (".implode(',', $pk).")";
  141. }
  142. $index = '';
  143. foreach($this->index as $name => $idx){
  144. $index .=", {$pdo->stringIndex($name, $idx)}";
  145. }
  146. $fk = '';
  147. foreach($this->foreignKeys as $name => $k){
  148. $fk .= ", {$pdo->stringForeignKey($name, $k)}";
  149. }
  150. $sql = "create table `{$this->name}` ({$columns}{$pk}{$index}{$fk})";
  151. $count = $pdo->exec($sql);
  152. if($count === false){
  153. throw $pdo->getError();
  154. }
  155. if(!$this->exists()){
  156. throw new \Exception("Unable to create table {$this->name}. Generated SQL: {$sql}");
  157. }
  158. unset($sql);
  159. $this->exists();
  160. }else{
  161. $columns = array_filter($this->columns, function($item){
  162. return (bool)$item['dirty'];
  163. });
  164. if(count($columns) + count($this->columns_removed) > 0){
  165. $sql = "alter table `{$this->name}`";
  166. foreach($this->index_removed as $name){
  167. if($pdo->exec("show index from `{$this->name}` where KEY_name = ".$pdo->quote($name)) > 0){
  168. $sql .= " drop index {$name},";
  169. }
  170. }
  171. foreach($this->foreignKey_removed as $name){
  172. $sql .= " drop foreign key {$name},";
  173. }
  174. foreach($this->columns_removed as $name){
  175. $sql .= " drop column {$name},";
  176. }
  177. if(count($columns) > 0){
  178. $sql .= " add (";
  179. foreach($columns as $name => $column){
  180. $sql .= "{$pdo->stringColumn($name, $column)},";
  181. }
  182. $sql = rtrim($sql , ',') . "),";
  183. }
  184. unset($columns);
  185. $sql = rtrim($sql, ',');
  186. if($pdo->exec($sql) === false){
  187. throw new \Exception("Unable to update table {$this->name}\n{$this->pdo->getError()}");
  188. }
  189. }
  190. $sql = '';
  191. if($pdo->exec("show index from `{$this->name}` where KEY_name = 'PRIMARY'") > 0){
  192. $sql .= " drop primary key,";
  193. }
  194. if(count($this->primaryKey) > 0){
  195. $sql .= " add primary key (".implode(',', $this->primaryKey)."),";
  196. }
  197. foreach(array_filter($this->index, function($item){
  198. return (bool)$item['dirty'];
  199. }) as $name => $idx){
  200. $sql .= " add {$pdo->stringIndex($name, $idx)},";
  201. }
  202. if(count($sql) > 0){
  203. $sql = "alter table `{$this->name}`".rtrim($sql, ',');
  204. if($pdo->exec($sql) === false){
  205. throw new \Exception("Unable to update table {$this->name}\n{$this->pdo->getError()}");
  206. }
  207. }
  208. $foreignKeys =array_filter($this->foreignKeys, function($item){
  209. return (bool)$item['dirty'];
  210. });
  211. if(count($foreignKeys) > 0){
  212. $sql = "alter table `{$this->name}`";
  213. foreach($foreignKeys as $name => $fk){
  214. $sql .= " add {$pdo->stringForeignKey($name, $fk)},";
  215. }
  216. $sql = rtrim($sql, ',');
  217. if($pdo->exec($sql) === false){
  218. throw new \Exception("Unable to update table {$this->name}\n{$this->pdo->getError()}");
  219. }
  220. }
  221. unset($foreignKeys);
  222. }
  223. $this->describe();
  224. }
  225. public function rollback(){
  226. $this->exists();
  227. $this->describe();
  228. }
  229. public function drop(string $type = null, string $name = null){
  230. if(!is_null($type)){
  231. switch($type){
  232. case self::COLUMN:
  233. if(isset($this->columns[$name])){
  234. if($this->exists){
  235. $this->columns_removed = array_merge($this->columns_removed, [$name]);
  236. }
  237. unset($this->columns[$name]);
  238. }
  239. break;
  240. case self::INDEX:
  241. if(isset($this->index[$name])){
  242. if($this->exists){
  243. $this->index_removed = array_merge($this->index_removed, [$name]);
  244. }
  245. unset($this->index[$name]);
  246. }
  247. break;
  248. case self::FOREIGN_KEY:
  249. if(isset($this->foreignKey[$name])){
  250. if($this->exists){
  251. $this->foreignKey_removed = array_merge($this->foreignKey_removed, [$name]);
  252. }
  253. unset($this->foreignKey[$name]);
  254. }
  255. break;
  256. default:
  257. throw new \Exception("Cannot drop {$name}. Unknown type {$type}");
  258. }
  259. }else{
  260. if($this->exists){
  261. $this->pdo->exec("drop table `{$this->name}`");
  262. }
  263. $this->rollback();
  264. }
  265. return $this;
  266. }
  267. public function column(string $name, string $type = null, $default = null, bool $null = false, bool $increment = false){
  268. if(!is_null($type)){
  269. $new = [
  270. 'type'=> $type,
  271. 'default'=> $default,
  272. 'null'=> $null,
  273. 'increment'=> $increment,
  274. 'dirty'=> false
  275. ];
  276. if(isset($this->columns[$name])){
  277. $old = $this->columns[$name];
  278. foreach($new as $key => $val){
  279. if($key != 'dirty' && $old[$key] !== $val){
  280. $new['dirty'] = true;
  281. break;
  282. }
  283. }
  284. }else{
  285. $new['dirty'] = true;
  286. }
  287. $this->columns[$name] = $new;
  288. return $this;
  289. }else{
  290. return isset($this->columns[$name]) ? $this->columns[$name] : null;
  291. }
  292. }
  293. public function index(string $name, array $columns = null, bool $unique = false){
  294. if(!is_null($columns)){
  295. foreach($columns as $column){
  296. if(!isset($this->columns[$column])){
  297. throw new \Exception("Can't add index. Column {$this->name}.{$column} doesn't exist");
  298. }
  299. }
  300. $this->index[$name] = [
  301. 'columns'=> $columns,
  302. 'unique'=> $unique,
  303. 'dirty'=>true
  304. ];
  305. return $this;
  306. }else{
  307. return isset($this->index[$name]) ? $this->index[$name] : null;
  308. }
  309. }
  310. public function addToIndex(string $name, string $column){
  311. if(!isset($this->index[$name])){
  312. throw new \Exception("Can't add column to index. Index {$this->name}.{$name} doesn't exist");
  313. }
  314. if(!isset($this->columns[$column])){
  315. throw new \Exception("Can't add column to index. Column {$this->name}.{$column} doesn't exist");
  316. }
  317. $this->index[$name]['columns'] = array_merge($this->index[$name]['columns'], [$column]);
  318. }
  319. public function foreignKey(string $name, string $references = null, array $columns = []){
  320. if(!is_null($references)){
  321. $table = $this->pdo->table($references);
  322. if(!$table->exists){
  323. throw new \Exception("Can't create foreign key {$name}. Table {$references} does not exist.");
  324. }
  325. foreach($columns as $column){
  326. if(is_null($this->column($column[0]))){
  327. throw new \Exception("Can't create foreign key {$name}. Column {$this->name}.{$column[0]} does not exist");
  328. }
  329. if(is_null($table->column($column[1]))){
  330. throw new \Exception("Can't create foreign key {$name}. Column {$references}.{$column[1]} does not exist");
  331. }
  332. }
  333. $this->foreignKeys[$name] = [
  334. 'references'=> $references,
  335. 'columns'=> $columns,
  336. 'dirty'=> true
  337. ];
  338. return $this;
  339. }else{
  340. return isset($this->foreignKeys[$name]) ? $this->foreignKeys[$name] : null;
  341. }
  342. }
  343. public function primaryKey(...$columns){
  344. if(count($columns) > 0){
  345. foreach($columns as $column){
  346. if(!isset($this->columns[$column])){
  347. throw new \Exception("Can't add Primary key. Column {$this->name}.{$column} doesn't exist");
  348. }
  349. }
  350. $this->primaryKey = $columns;
  351. return $this;
  352. }else{
  353. return $this->primaryKey;
  354. }
  355. }
  356. public function insert(array $data){
  357. return $this->exists ? $this->pdo->exec("insert into `{$this->name}` {$this->pdo->stringSet($data)}") : 0;
  358. }
  359. public function update(array $data, array $filter = null){
  360. return $this->exists ? $this->pdo->exec("update `{$this->name}` {$this->pdo->stringSet($data)} {$this->pdo->stringFilter($filter)}") : 0;
  361. }
  362. public function delete(array $filter = null){
  363. return $this->exists ? $this->pdo->exec("delete from `{$this->name}` {$this->pdo->stringFilter($filter)}") : 0;
  364. }
  365. public function fetch(array $columns = null, array $filter = null, int $start = null, int $amount = null){
  366. $results = [];
  367. $this->each(function($row) use($results){
  368. $results[] = $row;
  369. }, $columns, $filter, $start, $amount);
  370. return $results;
  371. }
  372. public function each(callable $fn, array $columns = null, array $filter = null, int $start = null, int $amount = null){
  373. if($this->exists){
  374. $limit = '';
  375. if(!is_null($start) && !is_null($amount)){
  376. $limit .= " limit {$start}, {$amount}";
  377. }
  378. $columns = $columns ?? ['*'];
  379. $cols = '';
  380. foreach($columns as $column){
  381. $cols .= "{$column},";
  382. }
  383. $cols = rtrim($cols, ',');
  384. $query = $this->pdo->query("select {$cols} from `{$this->name}` {$this->pdo->stringFilter($filter)} {$limit}");
  385. while($row = $query->fetch()){
  386. $fn($row);
  387. }
  388. $query->closeCursor();
  389. }
  390. return $this;
  391. }
  392. public function count(array $filter = null){
  393. return $this->exists ? $this->pdo->exec("select 1 from `{$this->name}` {$this->pdo->stringFilter($filter)}") : 0;
  394. }
  395. }
  396. ?>