api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GenericDefinition.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. namespace Tqdev\PhpCrudApi\Database;
  3. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedColumn;
  4. use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
  5. use Tqdev\PhpCrudApi\Database\LazyPdo;
  6. class GenericDefinition
  7. {
  8. private $pdo;
  9. private $driver;
  10. private $database;
  11. private $typeConverter;
  12. private $reflection;
  13. public function __construct(LazyPdo $pdo, string $driver, string $database)
  14. {
  15. $this->pdo = $pdo;
  16. $this->driver = $driver;
  17. $this->database = $database;
  18. $this->typeConverter = new TypeConverter($driver);
  19. $this->reflection = new GenericReflection($pdo, $driver, $database);
  20. }
  21. private function quote(string $identifier): string
  22. {
  23. return '"' . str_replace('"', '', $identifier) . '"';
  24. }
  25. public function getColumnType(ReflectedColumn $column, bool $update): string
  26. {
  27. if ($this->driver == 'pgsql' && !$update && $column->getPk() && $this->canAutoIncrement($column)) {
  28. return 'serial';
  29. }
  30. $type = $this->typeConverter->fromJdbc($column->getType());
  31. if ($column->hasPrecision() && $column->hasScale()) {
  32. $size = '(' . $column->getPrecision() . ',' . $column->getScale() . ')';
  33. } elseif ($column->hasPrecision()) {
  34. $size = '(' . $column->getPrecision() . ')';
  35. } elseif ($column->hasLength()) {
  36. $size = '(' . $column->getLength() . ')';
  37. } else {
  38. $size = '';
  39. }
  40. $null = $this->getColumnNullType($column, $update);
  41. $auto = $this->getColumnAutoIncrement($column, $update);
  42. return $type . $size . $null . $auto;
  43. }
  44. private function getPrimaryKey(string $tableName): string
  45. {
  46. $pks = $this->reflection->getTablePrimaryKeys($tableName);
  47. if (count($pks) == 1) {
  48. return $pks[0];
  49. }
  50. return "";
  51. }
  52. private function canAutoIncrement(ReflectedColumn $column): bool
  53. {
  54. return in_array($column->getType(), ['integer', 'bigint']);
  55. }
  56. private function getColumnAutoIncrement(ReflectedColumn $column, bool $update): string
  57. {
  58. if (!$this->canAutoIncrement($column)) {
  59. return '';
  60. }
  61. switch ($this->driver) {
  62. case 'mysql':
  63. return $column->getPk() ? ' AUTO_INCREMENT' : '';
  64. case 'pgsql':
  65. case 'sqlsrv':
  66. return '';
  67. }
  68. }
  69. private function getColumnNullType(ReflectedColumn $column, bool $update): string
  70. {
  71. if ($this->driver == 'pgsql' && $update) {
  72. return '';
  73. }
  74. return $column->getNullable() ? ' NULL' : ' NOT NULL';
  75. }
  76. private function getTableRenameSQL(string $tableName, string $newTableName): string
  77. {
  78. $p1 = $this->quote($tableName);
  79. $p2 = $this->quote($newTableName);
  80. switch ($this->driver) {
  81. case 'mysql':
  82. return "RENAME TABLE $p1 TO $p2";
  83. case 'pgsql':
  84. return "ALTER TABLE $p1 RENAME TO $p2";
  85. case 'sqlsrv':
  86. return "EXEC sp_rename $p1, $p2";
  87. }
  88. }
  89. private function getColumnRenameSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  90. {
  91. $p1 = $this->quote($tableName);
  92. $p2 = $this->quote($columnName);
  93. $p3 = $this->quote($newColumn->getName());
  94. switch ($this->driver) {
  95. case 'mysql':
  96. $p4 = $this->getColumnType($newColumn, true);
  97. return "ALTER TABLE $p1 CHANGE $p2 $p3 $p4";
  98. case 'pgsql':
  99. return "ALTER TABLE $p1 RENAME COLUMN $p2 TO $p3";
  100. case 'sqlsrv':
  101. $p4 = $this->quote($tableName . '.' . $columnName);
  102. return "EXEC sp_rename $p4, $p3, 'COLUMN'";
  103. }
  104. }
  105. private function getColumnRetypeSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  106. {
  107. $p1 = $this->quote($tableName);
  108. $p2 = $this->quote($columnName);
  109. $p3 = $this->quote($newColumn->getName());
  110. $p4 = $this->getColumnType($newColumn, true);
  111. switch ($this->driver) {
  112. case 'mysql':
  113. return "ALTER TABLE $p1 CHANGE $p2 $p3 $p4";
  114. case 'pgsql':
  115. return "ALTER TABLE $p1 ALTER COLUMN $p3 TYPE $p4";
  116. case 'sqlsrv':
  117. return "ALTER TABLE $p1 ALTER COLUMN $p3 $p4";
  118. }
  119. }
  120. private function getSetColumnNullableSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  121. {
  122. $p1 = $this->quote($tableName);
  123. $p2 = $this->quote($columnName);
  124. $p3 = $this->quote($newColumn->getName());
  125. $p4 = $this->getColumnType($newColumn, true);
  126. switch ($this->driver) {
  127. case 'mysql':
  128. return "ALTER TABLE $p1 CHANGE $p2 $p3 $p4";
  129. case 'pgsql':
  130. $p5 = $newColumn->getNullable() ? 'DROP NOT NULL' : 'SET NOT NULL';
  131. return "ALTER TABLE $p1 ALTER COLUMN $p2 $p5";
  132. case 'sqlsrv':
  133. return "ALTER TABLE $p1 ALTER COLUMN $p2 $p4";
  134. }
  135. }
  136. private function getSetColumnPkConstraintSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  137. {
  138. $p1 = $this->quote($tableName);
  139. $p2 = $this->quote($columnName);
  140. $p3 = $this->quote($tableName . '_pkey');
  141. switch ($this->driver) {
  142. case 'mysql':
  143. $p4 = $newColumn->getPk() ? "ADD PRIMARY KEY ($p2)" : 'DROP PRIMARY KEY';
  144. return "ALTER TABLE $p1 $p4";
  145. case 'pgsql':
  146. case 'sqlsrv':
  147. $p4 = $newColumn->getPk() ? "ADD CONSTRAINT $p3 PRIMARY KEY ($p2)" : "DROP CONSTRAINT $p3";
  148. return "ALTER TABLE $p1 $p4";
  149. }
  150. }
  151. private function getSetColumnPkSequenceSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  152. {
  153. $p1 = $this->quote($tableName);
  154. $p2 = $this->quote($columnName);
  155. $p3 = $this->quote($tableName . '_' . $columnName . '_seq');
  156. switch ($this->driver) {
  157. case 'mysql':
  158. return "select 1";
  159. case 'pgsql':
  160. return $newColumn->getPk() ? "CREATE SEQUENCE $p3 OWNED BY $p1.$p2" : "DROP SEQUENCE $p3";
  161. case 'sqlsrv':
  162. return $newColumn->getPk() ? "CREATE SEQUENCE $p3" : "DROP SEQUENCE $p3";
  163. }
  164. }
  165. private function getSetColumnPkSequenceStartSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  166. {
  167. $p1 = $this->quote($tableName);
  168. $p2 = $this->quote($columnName);
  169. switch ($this->driver) {
  170. case 'mysql':
  171. return "select 1";
  172. case 'pgsql':
  173. $p3 = $this->pdo->quote($tableName . '_' . $columnName . '_seq');
  174. return "SELECT setval($p3, (SELECT max($p2)+1 FROM $p1));";
  175. case 'sqlsrv':
  176. $p3 = $this->quote($tableName . '_' . $columnName . '_seq');
  177. $p4 = $this->pdo->query("SELECT max($p2)+1 FROM $p1")->fetchColumn();
  178. return "ALTER SEQUENCE $p3 RESTART WITH $p4";
  179. }
  180. }
  181. private function getSetColumnPkDefaultSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  182. {
  183. $p1 = $this->quote($tableName);
  184. $p2 = $this->quote($columnName);
  185. switch ($this->driver) {
  186. case 'mysql':
  187. $p3 = $this->quote($newColumn->getName());
  188. $p4 = $this->getColumnType($newColumn, true);
  189. return "ALTER TABLE $p1 CHANGE $p2 $p3 $p4";
  190. case 'pgsql':
  191. if ($newColumn->getPk()) {
  192. $p3 = $this->pdo->quote($tableName . '_' . $columnName . '_seq');
  193. $p4 = "SET DEFAULT nextval($p3)";
  194. } else {
  195. $p4 = 'DROP DEFAULT';
  196. }
  197. return "ALTER TABLE $p1 ALTER COLUMN $p2 $p4";
  198. case 'sqlsrv':
  199. $p3 = $this->quote($tableName . '_' . $columnName . '_seq');
  200. $p4 = $this->quote($tableName . '_' . $columnName . '_def');
  201. if ($newColumn->getPk()) {
  202. return "ALTER TABLE $p1 ADD CONSTRAINT $p4 DEFAULT NEXT VALUE FOR $p3 FOR $p2";
  203. } else {
  204. return "ALTER TABLE $p1 DROP CONSTRAINT $p4";
  205. }
  206. }
  207. }
  208. private function getAddColumnFkConstraintSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  209. {
  210. $p1 = $this->quote($tableName);
  211. $p2 = $this->quote($columnName);
  212. $p3 = $this->quote($tableName . '_' . $columnName . '_fkey');
  213. $p4 = $this->quote($newColumn->getFk());
  214. $p5 = $this->quote($this->getPrimaryKey($newColumn->getFk()));
  215. return "ALTER TABLE $p1 ADD CONSTRAINT $p3 FOREIGN KEY ($p2) REFERENCES $p4 ($p5)";
  216. }
  217. private function getRemoveColumnFkConstraintSQL(string $tableName, string $columnName, ReflectedColumn $newColumn): string
  218. {
  219. $p1 = $this->quote($tableName);
  220. $p2 = $this->quote($tableName . '_' . $columnName . '_fkey');
  221. switch ($this->driver) {
  222. case 'mysql':
  223. return "ALTER TABLE $p1 DROP FOREIGN KEY $p2";
  224. case 'pgsql':
  225. case 'sqlsrv':
  226. return "ALTER TABLE $p1 DROP CONSTRAINT $p2";
  227. }
  228. }
  229. private function getAddTableSQL(ReflectedTable $newTable): string
  230. {
  231. $tableName = $newTable->getName();
  232. $p1 = $this->quote($tableName);
  233. $fields = [];
  234. $constraints = [];
  235. foreach ($newTable->getColumnNames() as $columnName) {
  236. $pkColumn = $this->getPrimaryKey($tableName);
  237. $newColumn = $newTable->getColumn($columnName);
  238. $f1 = $this->quote($columnName);
  239. $f2 = $this->getColumnType($newColumn, false);
  240. $f3 = $this->quote($tableName . '_' . $columnName . '_fkey');
  241. $f4 = $this->quote($newColumn->getFk());
  242. $f5 = $this->quote($this->getPrimaryKey($newColumn->getFk()));
  243. $f6 = $this->quote($tableName . '_' . $pkColumn . '_pkey');
  244. $fields[] = "$f1 $f2";
  245. if ($newColumn->getPk()) {
  246. $constraints[] = "CONSTRAINT $f6 PRIMARY KEY ($f1)";
  247. }
  248. if ($newColumn->getFk()) {
  249. $constraints[] = "CONSTRAINT $f3 FOREIGN KEY ($f1) REFERENCES $f4 ($f5)";
  250. }
  251. }
  252. $p2 = implode(',', array_merge($fields, $constraints));
  253. return "CREATE TABLE $p1 ($p2);";
  254. }
  255. private function getAddColumnSQL(string $tableName, ReflectedColumn $newColumn): string
  256. {
  257. $p1 = $this->quote($tableName);
  258. $p2 = $this->quote($newColumn->getName());
  259. $p3 = $this->getColumnType($newColumn, false);
  260. switch ($this->driver) {
  261. case 'mysql':
  262. case 'pgsql':
  263. return "ALTER TABLE $p1 ADD COLUMN $p2 $p3";
  264. case 'sqlsrv':
  265. return "ALTER TABLE $p1 ADD $p2 $p3";
  266. }
  267. }
  268. private function getRemoveTableSQL(string $tableName): string
  269. {
  270. $p1 = $this->quote($tableName);
  271. switch ($this->driver) {
  272. case 'mysql':
  273. case 'pgsql':
  274. return "DROP TABLE $p1 CASCADE;";
  275. case 'sqlsrv':
  276. return "DROP TABLE $p1;";
  277. }
  278. }
  279. private function getRemoveColumnSQL(string $tableName, string $columnName): string
  280. {
  281. $p1 = $this->quote($tableName);
  282. $p2 = $this->quote($columnName);
  283. switch ($this->driver) {
  284. case 'mysql':
  285. case 'pgsql':
  286. return "ALTER TABLE $p1 DROP COLUMN $p2 CASCADE;";
  287. case 'sqlsrv':
  288. return "ALTER TABLE $p1 DROP COLUMN $p2;";
  289. }
  290. }
  291. public function renameTable(string $tableName, string $newTableName)
  292. {
  293. $sql = $this->getTableRenameSQL($tableName, $newTableName);
  294. return $this->query($sql);
  295. }
  296. public function renameColumn(string $tableName, string $columnName, ReflectedColumn $newColumn)
  297. {
  298. $sql = $this->getColumnRenameSQL($tableName, $columnName, $newColumn);
  299. return $this->query($sql);
  300. }
  301. public function retypeColumn(string $tableName, string $columnName, ReflectedColumn $newColumn)
  302. {
  303. $sql = $this->getColumnRetypeSQL($tableName, $columnName, $newColumn);
  304. return $this->query($sql);
  305. }
  306. public function setColumnNullable(string $tableName, string $columnName, ReflectedColumn $newColumn)
  307. {
  308. $sql = $this->getSetColumnNullableSQL($tableName, $columnName, $newColumn);
  309. return $this->query($sql);
  310. }
  311. public function addColumnPrimaryKey(string $tableName, string $columnName, ReflectedColumn $newColumn)
  312. {
  313. $sql = $this->getSetColumnPkConstraintSQL($tableName, $columnName, $newColumn);
  314. $this->query($sql);
  315. if ($this->canAutoIncrement($newColumn)) {
  316. $sql = $this->getSetColumnPkSequenceSQL($tableName, $columnName, $newColumn);
  317. $this->query($sql);
  318. $sql = $this->getSetColumnPkSequenceStartSQL($tableName, $columnName, $newColumn);
  319. $this->query($sql);
  320. $sql = $this->getSetColumnPkDefaultSQL($tableName, $columnName, $newColumn);
  321. $this->query($sql);
  322. }
  323. return true;
  324. }
  325. public function removeColumnPrimaryKey(string $tableName, string $columnName, ReflectedColumn $newColumn)
  326. {
  327. if ($this->canAutoIncrement($newColumn)) {
  328. $sql = $this->getSetColumnPkDefaultSQL($tableName, $columnName, $newColumn);
  329. $this->query($sql);
  330. $sql = $this->getSetColumnPkSequenceSQL($tableName, $columnName, $newColumn);
  331. $this->query($sql);
  332. }
  333. $sql = $this->getSetColumnPkConstraintSQL($tableName, $columnName, $newColumn);
  334. $this->query($sql);
  335. return true;
  336. }
  337. public function addColumnForeignKey(string $tableName, string $columnName, ReflectedColumn $newColumn)
  338. {
  339. $sql = $this->getAddColumnFkConstraintSQL($tableName, $columnName, $newColumn);
  340. return $this->query($sql);
  341. }
  342. public function removeColumnForeignKey(string $tableName, string $columnName, ReflectedColumn $newColumn)
  343. {
  344. $sql = $this->getRemoveColumnFkConstraintSQL($tableName, $columnName, $newColumn);
  345. return $this->query($sql);
  346. }
  347. public function addTable(ReflectedTable $newTable)
  348. {
  349. $sql = $this->getAddTableSQL($newTable);
  350. return $this->query($sql);
  351. }
  352. public function addColumn(string $tableName, ReflectedColumn $newColumn)
  353. {
  354. $sql = $this->getAddColumnSQL($tableName, $newColumn);
  355. return $this->query($sql);
  356. }
  357. public function removeTable(string $tableName)
  358. {
  359. $sql = $this->getRemoveTableSQL($tableName);
  360. return $this->query($sql);
  361. }
  362. public function removeColumn(string $tableName, string $columnName)
  363. {
  364. $sql = $this->getRemoveColumnSQL($tableName, $columnName);
  365. return $this->query($sql);
  366. }
  367. private function query(string $sql): bool
  368. {
  369. $stmt = $this->pdo->prepare($sql);
  370. //echo "- $sql -- []\n";
  371. return $stmt->execute();
  372. }
  373. }