Pdo.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. <?php
  2. Lib::loadClass('DBG');
  3. Lib::loadClass('Core_TypeFactory'); // Core_TypeFactory::make, Core_iPdoSqlValue::toSqlQuote
  4. class Core_Pdo extends PDO {
  5. protected $_database_name;
  6. protected $_zasob_id;
  7. protected $_type;
  8. protected $_schema;
  9. // public PDO::__construct ( string $dsn [, string $username [, string $password [, array $options ]]] )
  10. public function __construct($dsn, $username, $password, $options = array()) {
  11. $this->_database_name = $options['database'];
  12. $this->_zasob_id = $options['zasob_id'];
  13. $this->_type = $options['type'];
  14. $this->_schema = (!empty($options['schema'])) ? $options['schema'] : null;
  15. unset($options['database']);
  16. unset($options['zasob_id']);
  17. parent::__construct($dsn, $username, $password, $options);
  18. $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  19. $this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  20. $this->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1); // Default 1, fetch returns value types: |> 0 -> int, string, null |> 1 -> string, null
  21. }
  22. public function getDatabaseName() {
  23. return $this->_database_name;
  24. }
  25. public function getZasobId() {
  26. return $this->_zasob_id;
  27. }
  28. public function getType() {
  29. return strtolower($this->_type);
  30. }
  31. public function identifierQuote($identifier) {
  32. return DB::identifierQuote($this->_type, $identifier);
  33. }
  34. public function tableNameQuote($tableName) {
  35. switch (strtolower($this->_type)) {
  36. case 'pgsql': return DB::identifierQuote($this->_type, ($this->_schema) ? "{$this->_schema}.{$tableName}" : $tableName);
  37. case 'mysql': return DB::identifierQuote($this->_type, $tableName);
  38. }
  39. return $tableName;
  40. }
  41. public function getTableStruct($tblName) {// TODO: mved to Core_Storage_*
  42. $sth = $this->prepare("
  43. -- show fields from {$tblName}
  44. select cols.COLUMN_NAME as name
  45. , cols.DATA_TYPE as type
  46. , cols.COLUMN_TYPE as type_mysql
  47. , if('YES' = cols.IS_NULLABLE, 1, 0) as is_nullable
  48. , cols.COLUMN_DEFAULT as default_value
  49. , if(cols.COLUMN_DEFAULT is null, 1, 0) as default_is_null
  50. , cols.CHARACTER_MAXIMUM_LENGTH as max_length
  51. , cols.NUMERIC_PRECISION as num_precision
  52. , cols.NUMERIC_SCALE as num_scale
  53. , cols.CHARACTER_SET_NAME as char_encoding -- latin2
  54. , cols.COLLATION_NAME as char_collation -- latin2_general_ci
  55. , cols.EXTRA as extra
  56. -- , cols.*
  57. from INFORMATION_SCHEMA.COLUMNS cols
  58. where cols.TABLE_SCHEMA = :db_name
  59. and cols.TABLE_NAME = :tbl_name
  60. ");
  61. $sth->bindValue(':db_name', $this->getDatabaseName(), PDO::PARAM_STR);
  62. $sth->bindValue(':tbl_name', $tblName, PDO::PARAM_STR);
  63. $sth->execute();
  64. $structRaw = $sth->fetchAll();
  65. if (empty($structRaw)) throw new Exception("Empty struct for table '{$tblName}'", 404);
  66. foreach ($structRaw as $field) {
  67. $struct[$field['name']] = $field;
  68. }
  69. return $struct;
  70. }
  71. public function assertTableStructXsd($tblName, $expectedStructXsd) {
  72. throw new Exception("Unimplemented - TODO!");
  73. /*
  74. - `decimal(5,2)`:
  75. <xsd:element name="A" type="decimal_5_2"/>
  76. <xsd:simpleType name="decimal_5_2">
  77. <xsd:restriction base="xsd:decimal">
  78. <xsd:totalDigits value="5"/>
  79. <xsd:fractionDigits value="2"/>
  80. </xsd:restriction>
  81. </xsd:simpleType>
  82. */
  83. /* MySQL types:
  84. int tinyint smallint mediumint bigint
  85. decimal
  86. float double real => double
  87. date datetime timestamp time year
  88. char varchar
  89. text tinytext mediumtext longtext
  90. enum
  91. set
  92. bit
  93. boolean => `tinyint(1)` -- 0 or 1
  94. serial => `bigint(20) unsigned` and unique key
  95. binary varbinary
  96. blob tinyblob mediumblob longblob
  97. geometry point linestring polygon multipoint multilinestring multipolygon geometrycollection
  98. */
  99. }
  100. /*
  101. * TODO: update keys:
  102. * TODO: keys name may be different - try to find and connect with given schema?
  103. * TODO: remove old uniq keys?
  104. */
  105. public function assertTableStruct($tblName, $expectedStruct, $params = array()) {
  106. // TODO: make backup for table?
  107. $expectedStruct = $this->_fixExpectedStruct($expectedStruct);
  108. //DBG::_(true, true, "fixedEpectedStruct", $expectedStruct, __CLASS__, __FUNCTION__, __LINE__);
  109. //DBG::_(true, true, "fixedEpectedStruct", $this->showCreateStruct($tblName, $expectedStruct, $params), __CLASS__, __FUNCTION__, __LINE__);
  110. $struct = $this->getTableStruct($tblName);
  111. $expectedFields = array();//array_keys($expectedStruct);
  112. foreach ($expectedStruct as $fldName => $fld) {
  113. if ('UNIQUE KEY' == $fld['type']) continue;
  114. if ('KEY' == $fld['type']) continue;
  115. $expectedFields[] = $fldName;
  116. }
  117. $currentFields = array_keys($struct);
  118. $missingFields = array_diff($expectedFields, $currentFields);
  119. DBG::_(true, true, "struct", $struct, __CLASS__, __FUNCTION__, __LINE__);
  120. DBG::_(true, true, "missingFields", $missingFields, __CLASS__, __FUNCTION__, __LINE__);
  121. foreach ($missingFields as $fldName) {
  122. $fld = $expectedStruct[$fldName];
  123. DBG::_(true, true, "add missing field[{$fldName}]:", $fld, __CLASS__, __FUNCTION__, __LINE__);
  124. $sqlFieldStruct = $this->showTableStructField($fldName, $fld);
  125. if ($sqlFieldStruct) {
  126. $sqlAdd = "alter table {$tblName} add {$sqlFieldStruct}";
  127. DBG::_(true, true, "sqlAdd", $sqlAdd, __CLASS__, __FUNCTION__, __LINE__);
  128. $this->exec($sqlAdd);
  129. } else {
  130. throw new Exception("Unimplemented type '{$fld['type']}': " . json_encode($fld));
  131. }
  132. }
  133. $toUpdateFields = array_intersect($expectedFields, $currentFields);
  134. DBG::_(true, true, "toUpdateFields", $toUpdateFields, __CLASS__, __FUNCTION__, __LINE__);
  135. foreach ($toUpdateFields as $fldName) {
  136. $current = $struct[$fldName];
  137. $expected = $expectedStruct[$fldName];
  138. $needChange = false;
  139. $sqlFieldStruct = $this->showTableStructField($fldName, $expected);
  140. if (!$sqlFieldStruct) throw new Exception("Unimplemented type '{$expected['type']}' for field '{$fldName}': " . json_encode($expected));
  141. $sqlChange = "alter table `{$tblName}` change `{$fldName}` {$sqlFieldStruct}";
  142. DBG::_(true, true, "DBG: sqlChange", $sqlChange, __CLASS__, __FUNCTION__, __LINE__);
  143. //DBG::_(true, true, "TODO: update field[{$fldName}]:", array('expected'=>$expected,'current'=>$current), __CLASS__, __FUNCTION__, __LINE__);
  144. if ($current['type'] != $expected['type']) {
  145. throw new Exception("Unimplemented change field type from '{$current['type']}' to '{$expected['type']}' for field '{$fldName}': " . json_encode($expected));
  146. }
  147. if ($current['is_nullable'] != $expected['is_nullable']) {
  148. $needChange = true;
  149. if ($current['is_nullable'] && !$expected['is_nullable']) {
  150. throw new Exception("Field struct needs change 'is_nullable' to false but this change is not implemented - field '{$fldName}': " . json_encode(array('expected'=>$expected,'current'=>$current)));
  151. }
  152. }
  153. if ($current['max_length'] != $expected['max_length']) {
  154. $needChange = true;
  155. if ($current['max_length'] > $expected['max_length']) {
  156. throw new Exception("Field struct needs decrease 'max_length' but this change is not implemented - field '{$fldName}': " . json_encode(array('expected'=>$expected,'current'=>$current)));
  157. }
  158. }
  159. if ($current['default_value'] !== $expected['default_value']) $needChange = true;
  160. if ($needChange) {
  161. DBG::_(true, true, "EXEC sqlChange ...", $sqlChange, __CLASS__, __FUNCTION__, __LINE__);
  162. $this->exec($sqlChange);
  163. }
  164. }
  165. }
  166. /* assert's that field struct has defined or throw exception if cannot set default value:
  167. ['type'] = 'varchar', 'int', ... (MySQL Types)
  168. ['is_nullable'] = true, false
  169. ['default_value'] = NULL or (string, numeric - based on type)
  170. default_value is not set when default_value == NULL and is_nullable == false
  171. ['default_is_null'] = true, false
  172. ['max_length'] = NULL, [0-...] // MySQL `CHARACTER_MAXIMUM_LENGTH`
  173. ['num_precision'] = NULL, [0-65]
  174. ['num_scale'] = NULL, [0-12]
  175. ['char_encoding'] = 'utf8', 'latin2', ...
  176. ['char_collation'] = 'utf8_general_ci', 'latin2_general_ci', ...
  177. ['extra'] = 'auto_increment', 'on update CURRENT_TIMESTAMP'
  178. ['values'] = null or array for enum and set
  179. TODO: validate - wrong related values. ex: {type: 'int', char_collation: 'latin2'}
  180. TODO: validate - not allowed value. ex: {type: 'xyz'}
  181. */
  182. public function _fixExpectedStruct($expectedStruct) {
  183. $fixedStruct = array();
  184. foreach ($expectedStruct as $fldName => $expected) {
  185. //DBG::_(true, true, "TODO: expected", $expected, __CLASS__, __FUNCTION__, __LINE__);
  186. if (!array_key_exists('type', $expected)) throw new Exception("Undefined type for field '{$fldName}'");
  187. if (!array_key_exists('is_nullable', $expected)) {
  188. $expected['is_nullable'] = false;
  189. if (array_key_exists('default_value', $expected) && null === $expected['default_value']) {
  190. $expected['is_nullable'] = true;
  191. }
  192. }
  193. if (!array_key_exists('default_is_null', $expected)) $expected['default_is_null'] = false;
  194. if (!array_key_exists('default_value', $expected)) {
  195. $expected['default_value'] = null;
  196. // switch ($expected['type']) {
  197. // case 'char':
  198. // case 'varchar': $expected['default_value'] = ($expected['is_nullable'])? null : ''; break;
  199. // case 'tinyint':
  200. // case 'bigint':
  201. // case 'int': $expected['default_value'] = ($expected['is_nullable'])? null : 0; break;
  202. // }
  203. if ($expected['is_nullable'] && null === $expected['default_value']) {
  204. $expected['default_is_null'] = true;
  205. }
  206. }
  207. if (!array_key_exists('max_length', $expected)) {
  208. switch ($expected['type']) {
  209. case 'char':
  210. case 'varchar': $expected['max_length'] = 255; break;
  211. case 'binary': $expected['max_length'] = 255; break;// bainary(0) is possible - why? Cannot store values.
  212. case 'varbinary': $expected['max_length'] = 255; break;
  213. case 'binary':
  214. case 'varbinary':
  215. case 'bit': throw new Exception("Undefined max_length for field '{$fldName}' with type '{$expected['type']}'");
  216. //case 'blob': $expected['max_length'] = 65535; break;// is set by engine
  217. //case 'tinyblob': $expected['max_length'] = 255; break;// is set by engine
  218. //case 'mediumblob': $expected['max_length'] = 16777215; break;// is set by engine
  219. //case 'longblob': $expected['max_length'] = 4294967295; break;// is set by engine
  220. //case 'text': $expected['max_length'] = 65535; break;// is set by engine
  221. //case 'tinytext': $expected['max_length'] = 255; break;// is set by engine
  222. //case 'mediumtext': $expected['max_length'] = 16777215; break;// is set by engine
  223. //case 'longtext': $expected['max_length'] = 4294967295; break;// is set by engine
  224. //case 'enum': $expected['max_length'] = 255; break;// is set by engine
  225. //case 'set': $expected['max_length'] = 255; break;// is set by engine
  226. default: $expected['max_length'] = null;//throw new Exception("Undefined max_length for field '{$fldName}'");
  227. }
  228. }
  229. if (!array_key_exists('num_precision', $expected)) {
  230. switch ($expected['type']) {
  231. case 'int': $expected['num_precision'] = 10; break;// int(11) - +1 in type definition
  232. case 'tinyint': $expected['num_precision'] = 3; break;// int(4)
  233. case 'smallint': $expected['num_precision'] = 5; break;// int(6)
  234. case 'mediumint': $expected['num_precision'] = 7; break;// int(8)
  235. case 'bigint': $expected['num_precision'] = 19; break;// int(20)
  236. case 'decimal': $expected['num_precision'] = 10; break;// decimal(10,0)
  237. default: $expected['num_precision'] = null;
  238. }
  239. //throw new Exception("Undefined num_precision for field '{$fldName}'");
  240. }
  241. if (!array_key_exists('num_scale', $expected)) {
  242. switch ($expected['type']) {
  243. case 'int': $expected['num_scale'] = 0; break;
  244. case 'tinyint': $expected['num_scale'] = 0; break;
  245. case 'smallint': $expected['num_scale'] = 0; break;
  246. case 'mediumint': $expected['num_scale'] = 0; break;
  247. case 'bigint': $expected['num_scale'] = 0; break;
  248. case 'decimal': $expected['num_scale'] = 0; break;// ex.: decimal(3,2); decimal(24,0)
  249. case 'double': $expected['num_scale'] = null; break;// ex.: double(10,4); double; double(17,0);
  250. case 'float': $expected['num_scale'] = null; break;// ex.: float(6,2); float; float(17,0);
  251. default: $expected['num_scale'] = null;
  252. }
  253. //throw new Exception("Undefined num_scale for field '{$fldName}'");
  254. }
  255. if (!array_key_exists('char_encoding', $expected)) {
  256. switch ($expected['type']) {
  257. case 'char':
  258. case 'varchar': $expected['char_encoding'] = 'utf8'; break;
  259. case 'enum':
  260. case 'set': $expected['char_encoding'] = 'utf8'; break;
  261. case 'text':
  262. case 'tinytext':
  263. case 'mediumtext':
  264. case 'longtext': $expected['char_encoding'] = 'utf8'; break;
  265. default: $expected['char_encoding'] = null;
  266. }
  267. //throw new Exception("Undefined char_encoding for field '{$fldName}'");
  268. }
  269. if (!array_key_exists('char_collation', $expected)) {
  270. switch ($expected['type']) {
  271. case 'char':
  272. case 'varchar': $expected['char_collation'] = 'utf8_general_ci'; break;
  273. case 'enum':
  274. case 'set': $expected['char_collation'] = 'utf8_general_ci'; break;
  275. case 'text':
  276. case 'tinytext':
  277. case 'mediumtext':
  278. case 'longtext': $expected['char_collation'] = 'utf8_general_ci'; break;
  279. default: $expected['char_collation'] = null;
  280. }
  281. //throw new Exception("Undefined char_collation for field '{$fldName}'");
  282. }
  283. if (!array_key_exists('extra', $expected)) {
  284. $expected['extra'] = null;//throw new Exception("Undefined extra for field '{$fldName}'");
  285. }
  286. if (!array_key_exists('values', $expected) || !is_array($expected['values']) || empty($expected['values'])) {
  287. switch ($expected['type']) {
  288. case 'enum':
  289. case 'set': throw new Exception("Undefined values for field '{$fldName}' with type '{$expected['type']}'");
  290. default: $expected['values'] = null;
  291. }
  292. }
  293. $fixedStruct[$fldName] = $expected;
  294. }
  295. return $fixedStruct;
  296. }
  297. public function showCreateStruct($tblName, $struct, $params = array()) {
  298. // TODO: check database type, if == MySQL - fix construct
  299. $expectedStruct = $this->_fixExpectedStruct($struct);
  300. $linesSql = array();
  301. $dbgSql = array();
  302. foreach ($expectedStruct as $fldName => $fld) {
  303. $sqlFieldStruct = $this->showTableStructField($fldName, $fld);
  304. if ($sqlFieldStruct) {
  305. $linesSql[] = $sqlFieldStruct;
  306. } else {
  307. $dbgSql[] = "-- Unimplemented type '{$fld['type']}': " . json_encode($fld);
  308. }
  309. }
  310. $linesSql = implode("\n\t\t, ", $linesSql);
  311. $dbgSql = implode("\n\t\t", $dbgSql);
  312. $tblCharEncoding = V::get('char_encoding', 'utf8', $params);
  313. $structSql = <<<EOF_STRUCT_MYSQL
  314. CREATE TABLE IF NOT EXISTS `{$tblName}` (
  315. {$linesSql}
  316. {$dbgSql}
  317. ) ENGINE=MyISAM DEFAULT CHARSET={$tblCharEncoding}
  318. EOF_STRUCT_MYSQL;
  319. return $structSql;
  320. }
  321. public function showTableStructField($fldName, $fld) {
  322. // TODO: check database type, if == MySQL - fix construct
  323. $nullSql = ($fld['is_nullable'])? '' : 'NOT NULL';
  324. $defaultSql = (is_null($fld['default_value']))? 'DEFAULT NULL' : "DEFAULT '{$fld['default_value']}'";
  325. if (is_null($fld['default_value']) && !$fld['is_nullable']) $defaultSql = '';
  326. switch ($fld['type']) {
  327. case 'char':
  328. case 'varchar': return "`{$fldName}` {$fld['type']}({$fld['max_length']}) {$nullSql} {$defaultSql}"; break;
  329. case 'text':
  330. case 'tinytext':
  331. case 'longtext':
  332. case 'mediumtext': return "`{$fldName}` {$fld['type']} {$nullSql}"; break;
  333. case 'time':
  334. case 'timestamp':
  335. case 'year':
  336. case 'date':
  337. case 'datetime': return "`{$fldName}` {$fld['type']} {$nullSql} {$defaultSql}"; break;
  338. // -- Unimplemented type 'int': {"type":"int","is_nullable":false,"default_is_null":false,"default_value":null,"max_length":null,"num_precision":10,"num_scale":0,"char_encoding":null,"char_collation":null,"extra":null}
  339. // -- Unimplemented type 'int': {"type":"int","num_precision":11,"default_value":null,"is_nullable":true,"default_is_null":false,"max_length":null,"num_scale":0,"char_encoding":null,"char_collation":null,"extra":null}
  340. case 'int':
  341. case 'tinyint':
  342. case 'smallint':
  343. case 'mediumint':
  344. case 'bigint':
  345. if ($fld['num_precision'] > 0) {
  346. $typeParamsSql = "(" . ($fld['num_precision'] + 1) . ")";
  347. }
  348. //if ($fld['num_scale']) $typeParamsSql = ",{$fld['num_scale']}";
  349. return "`{$fldName}` {$fld['type']}{$typeParamsSql} {$nullSql} {$defaultSql}";
  350. break;
  351. case 'decimal':
  352. $typeParamsSql = "{$fld['num_precision']},{$fld['num_scale']}";
  353. return "`{$fldName}` {$fld['type']}({$typeParamsSql}) {$nullSql} {$defaultSql}";
  354. break;
  355. case 'float':
  356. case 'double':
  357. case 'real':
  358. return "`{$fldName}` {$fld['type']} {$nullSql} {$defaultSql}";
  359. break;
  360. case 'enum':
  361. case 'set':
  362. $typeParamsSql = "'" . implode("','", $fld['values']) . "'";
  363. return "`{$fldName}` {$fld['type']}({$typeParamsSql}) {$nullSql} {$defaultSql}";
  364. break;
  365. case 'bit':
  366. case 'binary':
  367. case 'varbinary':
  368. return "`{$fldName}` {$fld['type']}({$fld['max_length']}) {$nullSql} {$defaultSql}";
  369. break;
  370. case 'boolean':
  371. case 'serial':
  372. case 'blob':
  373. case 'tinyblob':
  374. case 'mediumblob':
  375. case 'longblob':
  376. return "`{$fldName}` {$fld['type']} {$nullSql} {$defaultSql}";
  377. break;
  378. case 'geometry':
  379. case 'point':
  380. case 'linestring':
  381. case 'polygon':
  382. case 'multipoint':
  383. case 'multilinestring':
  384. case 'multipolygon':
  385. case 'geometrycollection':
  386. return "`{$fldName}` {$fld['type']} {$nullSql} {$defaultSql}";
  387. break;
  388. case 'UNIQUE KEY':
  389. $keyFieldsSql = "`" . implode("`,`", $fld['key_fields']) . "`";
  390. return "UNIQUE KEY `{$fldName}` ({$keyFieldsSql})";
  391. break;
  392. case 'KEY':
  393. $keyFieldsSql = "`" . implode("`,`", $fld['key_fields']) . "`";
  394. return "KEY `{$fldName}` ({$keyFieldsSql})";
  395. break;
  396. case 'PRIMARY KEY':
  397. $keyFieldsSql = "`" . implode("`,`", $fld['key_fields']) . "`";
  398. return "PRIMARY KEY ({$keyFieldsSql})";
  399. break;
  400. }
  401. return null;
  402. }
  403. public function fetchValue($sql, $values = []) { // for sql like `select count() from ...`
  404. $sth = $this->prepare($sql);
  405. if (!empty($values)) {
  406. $this->bindValues($sth, $values);
  407. DBG::log($this->getRawSql($sth), 'sql');
  408. } else {
  409. DBG::log($sql, 'sql');
  410. }
  411. $sth->execute();
  412. return $sth->fetchColumn();
  413. }
  414. public function fetchValuesList($sql, $values = []) { // for sql like `select ID from ...` @returns array of ID
  415. return array_map(function ($row) {
  416. return reset($row);
  417. }, $this->fetchAll($sql, $values));
  418. }
  419. public function fetchValuesListByKey($sql, $key, $values = []) { // for sql like `select ID from ...` @returns array of ID
  420. return array_map(function ($row) {
  421. return reset($row);
  422. }, $this->fetchAllByKey($sql, $key, $values));
  423. }
  424. public function fetchKeyValueList($sql, $keyField, $valueField, $values = []) { // for sql like `select key, val from ...` @returns assoc array key => val
  425. return array_map(function ($row) use ($valueField) {
  426. return V::get($valueField, '', $row);
  427. }, $this->fetchAllByKey($sql, $keyField, $values));
  428. }
  429. public function fetchFirst($sql, $values = []) { // fetch only first row
  430. $sth = $this->prepare($sql);
  431. if (!empty($values)) {
  432. $this->bindValues($sth, $values);
  433. DBG::log($this->getRawSql($sth), 'sql');
  434. } else {
  435. DBG::log($sql, 'sql');
  436. }
  437. $sth->execute();
  438. return $sth->fetch();
  439. }
  440. public function fetchFirstNoLog($sql, $values = []) { // fetch only first row - used in User::getID() required in DBG
  441. $sth = $this->prepare($sql);
  442. if (!empty($values)) {
  443. $this->bindValues($sth, $values);
  444. }
  445. $sth->execute();
  446. return $sth->fetch();
  447. }
  448. public function fetchAll($sql, $values = []) {
  449. $sth = $this->prepare($sql);
  450. if (!empty($values)) {
  451. $this->bindValues($sth, $values);
  452. DBG::log($this->getRawSql($sth), 'sql');
  453. } else {
  454. DBG::log($sql, 'sql');
  455. }
  456. $sth->execute();
  457. return $sth->fetchAll();
  458. }
  459. public function fetchAllByKey($sql, $key = 'ID', $values = []) {
  460. $rowsByKey = array();
  461. $sth = $this->prepare($sql);
  462. if (!empty($values)) {
  463. $this->bindValues($sth, $values);
  464. DBG::log($this->getRawSql($sth), 'sql');
  465. } else {
  466. DBG::log($sql, 'sql');
  467. }
  468. $sth->execute();
  469. $rows = $sth->fetchAll();
  470. foreach ($rows as $row) {
  471. $keyRow = V::get($key, null, $row);
  472. $rowsByKey[$keyRow] = $row;
  473. }
  474. return $rowsByKey;
  475. }
  476. public function bindValues($sth, $values) {
  477. foreach ($values as $name => $value) {
  478. $val = $value;
  479. $type = PDO::PARAM_STR;
  480. if (is_array($value)) {
  481. $val = $value[0];
  482. if (count($value) > 1) {
  483. $type = $value[1];
  484. }
  485. }
  486. $sth->bindValue($name, $val, $type);
  487. if (!isset($sth->bindedValues)) $sth->bindedValues = array();
  488. $sth->bindedValues[$name] = array($val, $type);
  489. }
  490. }
  491. public function getRawSql($sth, $values = array()) {
  492. $sql = $sth->queryString;
  493. $params = array();
  494. if (!empty($sth->bindedValues)) {
  495. foreach ($sth->bindedValues as $name => $value) {
  496. $params[$name] = array($value[0], $value[1]);
  497. }
  498. }
  499. foreach ($values as $name => $value) {
  500. $val = $value;
  501. $type = PDO::PARAM_STR;
  502. if (is_array($value)) {
  503. $val = $value[0];
  504. if (count($value) > 1) {
  505. $type = $value[1];
  506. }
  507. }
  508. $params[$name] = array($val, $type);
  509. }
  510. if (!empty($params)) {
  511. foreach ($params as $name => $val) {
  512. $outValue = $val[0];
  513. if (PDO::PARAM_STR == $val[1]) $outValue = "'{$outValue}'";
  514. $sql = str_replace((':' === $name[0] ? $name : ":{$name}"), $outValue, $sql);
  515. }
  516. }
  517. return $sql;
  518. }
  519. public function insert($tableName, $item, $sqlSchema = []) {// @returns int last inserted id
  520. if (empty($tableName)) throw new Exception("Missing table name");
  521. if (!is_array($item)) throw new Exception("Missing item");
  522. $sqlListFields = [];
  523. $sqlListValues = [];
  524. foreach ($item as $field => $val) {
  525. $sqlListFields[] = $this->identifierQuote($field);
  526. $sqlListValues[] = $this->convertValueToSqlSafe($val, V::get($field, null, $sqlSchema));
  527. }
  528. $sqlTableName = $this->tableNameQuote($tableName);
  529. $sql = "
  530. insert into {$sqlTableName} (" . implode(", ", $sqlListFields) . ")
  531. values (" . implode(", ", $sqlListValues) . ")
  532. ";
  533. $this->execSql($sql);
  534. return $this->lastInsertId();
  535. }
  536. public function insertIgnore($tableName, $item, $sqlSchema = []) {// @returns int last inserted id
  537. if (empty($tableName)) throw new Exception("Missing table name");
  538. if (!is_array($item)) throw new Exception("Missing item");
  539. $sqlListFields = [];
  540. $sqlListValues = [];
  541. foreach ($item as $field => $val) {
  542. $sqlListFields[] = $this->identifierQuote($field);
  543. $sqlListValues[] = $this->convertValueToSqlSafe($val, V::get($field, null, $sqlSchema));
  544. }
  545. $sqlTableName = $this->tableNameQuote($tableName);
  546. $sql = "
  547. insert ignore into {$sqlTableName} (" . implode(", ", $sqlListFields) . ")
  548. values (" . implode(", ", $sqlListValues) . ")
  549. ";
  550. $this->execSql($sql);
  551. return $this->lastInsertId();
  552. }
  553. public function update($tableName, $primaryKeyName, $primaryKey, $item, $sqlSchema = []) {// @returns int affected rows
  554. if (empty($tableName)) throw new Exception("Missing table name");
  555. if (empty($primaryKeyName)) throw new Exception("Missing primaryKey name");
  556. if (empty($primaryKey)) throw new Exception("Missing primaryKey");
  557. if (empty($item) || !is_array($item)) throw new Exception("Missing item");
  558. $sqlUpdateSet = [];
  559. foreach ($item as $field => $val) {
  560. $sqlFieldName = $this->identifierQuote($field);
  561. $sqlValue = $this->convertValueToSqlSafe($val, V::get($field, null, $sqlSchema));
  562. $sqlUpdateSet[] = "{$sqlFieldName} = {$sqlValue}";
  563. }
  564. $sqlTableName = $this->tableNameQuote($tableName);
  565. $sqlPkName = $this->identifierQuote($primaryKeyName);
  566. $sqlPkValue = $this->quote($primaryKey, PDO::PARAM_STR);
  567. $sql = "
  568. update {$sqlTableName}
  569. set " . implode("\n , ", $sqlUpdateSet) . "
  570. where {$sqlPkName} = {$sqlPkValue}
  571. ";
  572. return $this->execSql($sql);
  573. }
  574. // fieldName => fieldValue, // will update if duplicate key (sql: `on duplicate key update`)
  575. // '@insert' => [
  576. // 'A_RECORD_CREATE_AUTHOR' => User::getLogin(),
  577. // 'A_RECORD_CREATE_DATE' => 'NOW()',
  578. // ],
  579. // '@update' => [
  580. // 'A_RECORD_UPDATE_AUTHOR' => User::getLogin(),
  581. // 'A_RECORD_UPDATE_DATE' => 'NOW()',
  582. // ]
  583. public function insertOrUpdate($tableName, $item, $sqlSchema = []) {
  584. if (empty($tableName)) throw new Exception("Missing table name");
  585. if (empty($item) || !is_array($item)) throw new Exception("Missing item");
  586. $sqlListFields = [];
  587. $sqlListValues = [];
  588. $sqlUpdateSet = [];
  589. foreach ($item as $field => $val) {
  590. if ('@insert' == $field) continue;
  591. if ('@update' == $field) continue;
  592. $sqlFieldName = $this->identifierQuote($field);
  593. $sqlValue = $this->convertValueToSqlSafe($val, V::get($field, null, $sqlSchema));
  594. $sqlListFields[] = $sqlFieldName;
  595. $sqlListValues[] = $sqlValue;
  596. $sqlUpdateSet[] = "{$sqlFieldName} = {$sqlValue}";
  597. }
  598. if (!empty($item['@insert'])) {
  599. foreach ($item['@insert'] as $field => $val) {
  600. $sqlFieldName = $this->identifierQuote($field);
  601. $sqlValue = $this->convertValueToSqlSafe($val, V::get($field, null, $sqlSchema));
  602. $sqlListFields[] = $sqlFieldName;
  603. $sqlListValues[] = $sqlValue;
  604. }
  605. }
  606. if (!empty($item['@update'])) {
  607. foreach ($item['@update'] as $field => $val) {
  608. $sqlFieldName = $this->identifierQuote($field);
  609. $sqlValue = $this->convertValueToSqlSafe($val, V::get($field, null, $sqlSchema));
  610. $sqlUpdateSet[] = "{$sqlFieldName} = {$sqlValue}";
  611. }
  612. }
  613. $sqlTableName = $this->tableNameQuote($tableName);
  614. $sql = "
  615. insert into {$sqlTableName} (" . implode(", ", $sqlListFields) . ")
  616. values (" . implode(", ", $sqlListValues) . ")
  617. ";
  618. if (!empty($sqlUpdateSet)) $sql .= " on duplicate key update " . implode(", ", $sqlUpdateSet);
  619. $affected = $this->execSql($sql);
  620. return true; // return $affected; // $this->lastInsertId();
  621. }
  622. public function convertValueToSqlSafe($value, $xsdType = null) {
  623. if (is_object($value)) {
  624. if ($value instanceof Core_iPdoSqlValue) return $value->toSqlQuote($this); // TODO: use P5Type to convert to sql value
  625. throw new Exception("DB Error: convertValueToSqlSafe value not implementing iPdoSqlValue '" . get_class($value) . "'");
  626. }
  627. if ('NOW()' === $value) return 'NOW()';
  628. if (NULL === $value) return 'NULL';
  629. if ('GeomFromText' == substr($value, 0, strlen('GeomFromText'))) return $value; // TODO: convert to Core_iPdoSqlValue, security
  630. if ('FROM_UNIXTIME' == substr($value, 0, strlen('FROM_UNIXTIME'))) return $value; // TODO: convert to Core_iPdoSqlValue, security
  631. return $this->quote($value, PDO::PARAM_STR); // TODO: use $sqlSchema if set
  632. }
  633. public function execSql($sql, $values = []) {
  634. try {
  635. if (empty($values)) {
  636. DBG::log($sql, 'sql');
  637. $retAffected = $this->exec($sql);
  638. } else {
  639. $sth = $this->prepare($sql);
  640. if (!empty($values)) {
  641. $this->bindValues($sth, $values);
  642. DBG::log($this->getRawSql($sth), 'sql');
  643. } else {
  644. DBG::log($sql, 'sql');
  645. }
  646. $sth->execute();
  647. $retAffected = $sth->rowCount();
  648. }
  649. } catch (Exception $e) {
  650. DBG::log($e);
  651. $dbType = $this->getType();
  652. $duplicateRegexp = "/^SQLSTATE\[23000\]\: Integrity constraint violation: 1062 Duplicate entry '([0-9]+)' for key '(.*)'/";
  653. if ('mysql' == $dbType && preg_match_all($duplicateRegexp, $e->getMessage(), $matches) > 0) {
  654. DBG::log(['matches'=>$matches,'msg'=>$e->getMessage(),'regex'=>$duplicateRegexp], 'array', '$matches duplicate test');
  655. throw new MysqlDuplicateEntryException("Duplicate entry '{$matches[1][0]}'", 1062, null, $matches[1][0], $matches[2][0]);
  656. } else {
  657. throw $e;
  658. }
  659. }
  660. return $retAffected;
  661. }
  662. public function getBlob($tableName, $fieldName, $pkField, $primaryKey) {
  663. if (empty($tableName)) throw new Exception("Missing tableName in PDO::getBlob");
  664. if (empty($fieldName)) throw new Exception("Missing fieldName in PDO::getBlob");
  665. if (empty($pkField)) throw new Exception("Missing pkField in PDO::getBlob");
  666. if (empty($primaryKey)) throw new Exception("Missing primaryKey in PDO::getBlob");
  667. $dbType = $this->getType();
  668. switch ($dbType) {
  669. case 'mysql': {
  670. $sql = "
  671. select `{$fieldName}`
  672. from `{$tableName}`
  673. where `{$pkField}` = :pk
  674. limit 1
  675. ";
  676. $sth = $this->prepare($sql);
  677. $sth->bindValue(':pk', $primaryKey, PDO::PARAM_STR);
  678. $sth->execute();
  679. $sth->bindColumn(1, $content, PDO::PARAM_LOB);
  680. $sth->fetch();
  681. return $content;
  682. } break;
  683. default: throw new Exception("Not implemented getBlob for database type '{$dbType}'");
  684. }
  685. }
  686. public function queryNotBuffered($query) {
  687. if ($bufferedQuery = $this->getAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY)) DB::getPDO()->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
  688. $return = parent::query($query);
  689. if ($bufferedQuery) DB::getPDO()->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
  690. return $return;
  691. }
  692. public function tryHandleException($handler, $methodName, $args) { // try again on exception
  693. try {
  694. return call_user_func_array([ $this, $methodName ], $args);
  695. } catch (Exception $e) {
  696. DBG::log("DBG:PDO->tryHandleException Exception trying to fix using handler ...");
  697. DBG::log($e);
  698. $handler($e);
  699. return call_user_func_array([ $this, $methodName ], $args);
  700. }
  701. }
  702. }
  703. class MysqlDuplicateEntryException extends DatabaseDuplicateEntryException {}
  704. class DatabaseDuplicateEntryException extends Exception {
  705. public function __construct($message, $code = 0, Exception $previous = null, $sqlPrimaryKey = '', $sqlKeyName = '') {
  706. $this->keyName = $sqlKeyName;
  707. $this->primaryKey = $sqlPrimaryKey;
  708. parent::__construct($message, $code, $previous);
  709. }
  710. }