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
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TypeConverter.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace Tqdev\PhpCrudApi\Database;
  3. class TypeConverter
  4. {
  5. private $driver;
  6. public function __construct(string $driver)
  7. {
  8. $this->driver = $driver;
  9. }
  10. private $fromJdbc = [
  11. 'mysql' => [
  12. 'clob' => 'longtext',
  13. 'boolean' => 'bit',
  14. 'blob' => 'longblob',
  15. 'timestamp' => 'datetime',
  16. ],
  17. 'pgsql' => [
  18. 'clob' => 'text',
  19. 'blob' => 'bytea',
  20. ],
  21. 'sqlsrv' => [
  22. 'boolean' => 'bit',
  23. 'varchar' => 'nvarchar',
  24. 'clob' => 'ntext',
  25. 'blob' => 'image',
  26. ],
  27. ];
  28. private $toJdbc = [
  29. 'simplified' => [
  30. 'char' => 'varchar',
  31. 'longvarchar' => 'clob',
  32. 'nchar' => 'varchar',
  33. 'nvarchar' => 'varchar',
  34. 'longnvarchar' => 'clob',
  35. 'binary' => 'varbinary',
  36. 'longvarbinary' => 'blob',
  37. 'tinyint' => 'integer',
  38. 'smallint' => 'integer',
  39. 'real' => 'float',
  40. 'numeric' => 'decimal',
  41. 'time_with_timezone' => 'time',
  42. 'timestamp_with_timezone' => 'timestamp',
  43. ],
  44. 'mysql' => [
  45. 'bit' => 'boolean',
  46. 'tinyblob' => 'blob',
  47. 'mediumblob' => 'blob',
  48. 'longblob' => 'blob',
  49. 'tinytext' => 'clob',
  50. 'mediumtext' => 'clob',
  51. 'longtext' => 'clob',
  52. 'text' => 'clob',
  53. 'mediumint' => 'integer',
  54. 'int' => 'integer',
  55. 'polygon' => 'geometry',
  56. 'point' => 'geometry',
  57. 'datetime' => 'timestamp',
  58. 'year' => 'integer',
  59. 'enum' => 'varchar',
  60. 'json' => 'clob',
  61. ],
  62. 'pgsql' => [
  63. 'bigserial' => 'bigint',
  64. 'bit varying' => 'bit',
  65. 'box' => 'geometry',
  66. 'bytea' => 'blob',
  67. 'bpchar' => 'char',
  68. 'character varying' => 'varchar',
  69. 'character' => 'char',
  70. 'cidr' => 'varchar',
  71. 'circle' => 'geometry',
  72. 'double precision' => 'double',
  73. 'inet' => 'integer',
  74. //'interval [ fields ]'
  75. 'json' => 'clob',
  76. 'jsonb' => 'clob',
  77. 'line' => 'geometry',
  78. 'lseg' => 'geometry',
  79. 'macaddr' => 'varchar',
  80. 'money' => 'decimal',
  81. 'path' => 'geometry',
  82. 'point' => 'geometry',
  83. 'polygon' => 'geometry',
  84. 'real' => 'float',
  85. 'serial' => 'integer',
  86. 'text' => 'clob',
  87. 'time without time zone' => 'time',
  88. 'time with time zone' => 'time_with_timezone',
  89. 'timestamp without time zone' => 'timestamp',
  90. 'timestamp with time zone' => 'timestamp_with_timezone',
  91. //'tsquery'=
  92. //'tsvector'
  93. //'txid_snapshot'
  94. 'uuid' => 'char',
  95. 'xml' => 'clob',
  96. ],
  97. // source: https://docs.microsoft.com/en-us/sql/connect/jdbc/using-basic-data-types?view=sql-server-2017
  98. 'sqlsrv' => [
  99. 'varbinary(0)' => 'blob',
  100. 'bit' => 'boolean',
  101. 'datetime' => 'timestamp',
  102. 'datetime2' => 'timestamp',
  103. 'float' => 'double',
  104. 'image' => 'blob',
  105. 'int' => 'integer',
  106. 'money' => 'decimal',
  107. 'ntext' => 'clob',
  108. 'smalldatetime' => 'timestamp',
  109. 'smallmoney' => 'decimal',
  110. 'text' => 'clob',
  111. 'timestamp' => 'binary',
  112. 'udt' => 'varbinary',
  113. 'uniqueidentifier' => 'char',
  114. 'xml' => 'clob',
  115. ],
  116. ];
  117. // source: https://docs.oracle.com/javase/9/docs/api/java/sql/Types.html
  118. private $valid = [
  119. //'array' => true,
  120. 'bigint' => true,
  121. 'binary' => true,
  122. 'bit' => true,
  123. 'blob' => true,
  124. 'boolean' => true,
  125. 'char' => true,
  126. 'clob' => true,
  127. //'datalink' => true,
  128. 'date' => true,
  129. 'decimal' => true,
  130. 'distinct' => true,
  131. 'double' => true,
  132. 'float' => true,
  133. 'integer' => true,
  134. //'java_object' => true,
  135. 'longnvarchar' => true,
  136. 'longvarbinary' => true,
  137. 'longvarchar' => true,
  138. 'nchar' => true,
  139. 'nclob' => true,
  140. //'null' => true,
  141. 'numeric' => true,
  142. 'nvarchar' => true,
  143. //'other' => true,
  144. 'real' => true,
  145. //'ref' => true,
  146. //'ref_cursor' => true,
  147. //'rowid' => true,
  148. 'smallint' => true,
  149. //'sqlxml' => true,
  150. //'struct' => true,
  151. 'time' => true,
  152. 'time_with_timezone' => true,
  153. 'timestamp' => true,
  154. 'timestamp_with_timezone' => true,
  155. 'tinyint' => true,
  156. 'varbinary' => true,
  157. 'varchar' => true,
  158. // extra:
  159. 'geometry' => true,
  160. ];
  161. public function toJdbc(string $type, int $size): string
  162. {
  163. $jdbcType = strtolower($type);
  164. if (isset($this->toJdbc[$this->driver]["$jdbcType($size)"])) {
  165. $jdbcType = $this->toJdbc[$this->driver]["$jdbcType($size)"];
  166. }
  167. if (isset($this->toJdbc[$this->driver][$jdbcType])) {
  168. $jdbcType = $this->toJdbc[$this->driver][$jdbcType];
  169. }
  170. if (isset($this->toJdbc['simplified'][$jdbcType])) {
  171. $jdbcType = $this->toJdbc['simplified'][$jdbcType];
  172. }
  173. if (!isset($this->valid[$jdbcType])) {
  174. throw new \Exception("Unsupported type '$jdbcType' for driver '$this->driver'");
  175. }
  176. return $jdbcType;
  177. }
  178. public function fromJdbc(string $type): string
  179. {
  180. $jdbcType = strtolower($type);
  181. if (isset($this->fromJdbc[$this->driver][$jdbcType])) {
  182. $jdbcType = $this->fromJdbc[$this->driver][$jdbcType];
  183. }
  184. return $jdbcType;
  185. }
  186. }