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.

TypeConverter.php 5.7KB

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