Pdo.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. <?php
  2. class Core_Pdo extends PDO {
  3. protected $_database_name;
  4. protected $_zasob_id;
  5. // public PDO::__construct ( string $dsn [, string $username [, string $password [, array $options ]]] )
  6. public function __construct($dsn, $username, $password, $options = array()) {
  7. $this->_database_name = $options['database'];
  8. $this->_zasob_id = $options['zasob_id'];
  9. unset($options['database']);
  10. unset($options['zasob_id']);
  11. parent::__construct($dsn, $username, $password, $options);
  12. $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  13. $this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  14. }
  15. public function getDatabaseName() {
  16. return $this->_database_name;
  17. }
  18. public function getZasobId() {
  19. return $this->_zasob_id;
  20. }
  21. public function getTableStruct($tblName) {
  22. $sth = $this->prepare("
  23. -- show fields from {$tblName}
  24. select cols.COLUMN_NAME as name
  25. , cols.DATA_TYPE as type
  26. , cols.COLUMN_TYPE as type_mysql
  27. , if('YES' = cols.IS_NULLABLE, 1, 0) as is_nullable
  28. , cols.COLUMN_DEFAULT as default_value
  29. , if(cols.COLUMN_DEFAULT is null, 1, 0) as default_is_null
  30. , cols.CHARACTER_MAXIMUM_LENGTH as max_length
  31. , cols.NUMERIC_PRECISION as num_precision
  32. , cols.NUMERIC_SCALE as num_scale
  33. , cols.CHARACTER_SET_NAME as char_encoding -- latin2
  34. , cols.COLLATION_NAME as char_collation -- latin2_general_ci
  35. , cols.EXTRA as extra
  36. -- , cols.*
  37. from INFORMATION_SCHEMA.COLUMNS cols
  38. where cols.TABLE_SCHEMA = :db_name
  39. and cols.TABLE_NAME = :tbl_name
  40. ");
  41. $sth->bindValue(':db_name', $this->getDatabaseName(), PDO::PARAM_STR);
  42. $sth->bindValue(':tbl_name', $tblName, PDO::PARAM_STR);
  43. $sth->execute();
  44. $structRaw = $sth->fetchAll();
  45. if (empty($structRaw)) throw new Exception("Empty struct for table '{$tblName}'");
  46. foreach ($structRaw as $field) {
  47. $struct[$field['name']] = $field;
  48. }
  49. return $struct;
  50. }
  51. public function assertTableStructXsd($tblName, $expectedStructXsd) {
  52. throw new Exception("Unimplemented - TODO!");
  53. /*
  54. - `decimal(5,2)`:
  55. <xsd:element name="A" type="decimal_5_2"/>
  56. <xsd:simpleType name="decimal_5_2">
  57. <xsd:restriction base="xsd:decimal">
  58. <xsd:totalDigits value="5"/>
  59. <xsd:fractionDigits value="2"/>
  60. </xsd:restriction>
  61. </xsd:simpleType>
  62. */
  63. /* MySQL types:
  64. int tinyint smallint mediumint bigint
  65. decimal
  66. float double real => double
  67. date datetime timestamp time year
  68. char varchar
  69. text tinytext mediumtext longtext
  70. enum
  71. set
  72. bit
  73. boolean => `tinyint(1)` -- 0 or 1
  74. serial => `bigint(20) unsigned` and unique key
  75. binary varbinary
  76. blob tinyblob mediumblob longblob
  77. geometry point linestring polygon multipoint multilinestring multipolygon geometrycollection
  78. */
  79. }
  80. /*
  81. * TODO: keys name may be different - try to find and connect with given schema?
  82. * TODO: remove old uniq keys?
  83. */
  84. public function assertTableStruct($tblName, $expectedStruct, $params = array()) {
  85. // $expectedStruct['t1'] = array('type'=>'varchar', 'max_length'=>300, 'default_value'=>null)
  86. $expectedStruct = $this->_fixExpectedStruct($expectedStruct);
  87. //DBG::_(true, true, "fixedEpectedStruct", $expectedStruct, __CLASS__, __FUNCTION__, __LINE__);
  88. //DBG::_(true, true, "fixedEpectedStruct", $this->showCreateStructMysql($tblName, $expectedStruct, $params), __CLASS__, __FUNCTION__, __LINE__);
  89. $struct = $this->getTableStruct($tblName);
  90. $expectedFields = array_keys($expectedStruct);
  91. $currentFields = array_keys($struct);
  92. $missingFields = array_diff($expectedFields, $currentFields);
  93. DBG::_(true, true, "struct", $struct, __CLASS__, __FUNCTION__, __LINE__);
  94. DBG::_(true, true, "missingFields", $missingFields, __CLASS__, __FUNCTION__, __LINE__);
  95. foreach ($missingFields as $fldName => $expected) {
  96. DBG::_(true, true, "TODO: add missing field[{$fldName}]:", $expected, __CLASS__, __FUNCTION__, __LINE__);
  97. $sqlType = array();
  98. $sqlType['type'] = $expected['type'];
  99. $sqlType['null'] = ($expected['is_nullable'])? '' : 'NOT NULL';
  100. $sqlType['default'] = ($expected['is_nullable'])? '' : 'NOT NULL';
  101. $sqlAdd = "alter table CRM_NOTIFY add {$fldName} {$sqlType['type']} {$sqlType['null']} {$sqlType['default']}";
  102. DBG::_(true, true, "TODO: sqlChange", $sqlAdd, __CLASS__, __FUNCTION__, __LINE__);
  103. }
  104. $toUpdateFields = array_intersect($expectedFields, $currentFields);
  105. DBG::_(true, true, "toUpdateFields", $toUpdateFields, __CLASS__, __FUNCTION__, __LINE__);
  106. foreach ($toUpdateFields as $fldName) {
  107. $current = $struct[$fldName];
  108. $expected = $expectedStruct[$fldName];
  109. $needChange = false;
  110. $toChange = array();
  111. $toChange['type'] = $current['type'];
  112. $toChange['null'] = ($current['is_nullable'])? '' : 'NOT NULL';
  113. // TODO: cannot determine when default is NULL
  114. $toChange['default'] = '';
  115. if (!empty($current['default_value'])) $toChange['default'] = "DEFAULT '{$current['default_value']}'";
  116. if (is_null($current['default_value'])) $toChange['default'] = "DEFAULT NULL";
  117. DBG::_(true, true, "TODO: update field[{$fldName}]:", array('expected'=>$expected,'current'=>$current), __CLASS__, __FUNCTION__, __LINE__);
  118. if ($current['type'] != $expected['type']) {
  119. throw new Exception("Unimplemented change field type from {$current['type']} to {$expected['type']}");
  120. }
  121. if ($current['is_nullable'] != $expected['is_nullable']) {
  122. if (!$current['is_nullable']) {
  123. $toChange['null'] = '';
  124. }
  125. }
  126. $sqlChange = "alter table CRM_NOTIFY change {$fldName} {$fldName} {$toChange['type']} {$toChange['null']} {$toChange['default']}";
  127. if ($needChange) {
  128. DBG::_(true, true, "TODO: sqlChange", $sqlChange, __CLASS__, __FUNCTION__, __LINE__);
  129. }
  130. }
  131. if(0)foreach ($struct as $col) {
  132. $fldName = $col['Field'];
  133. if (!array_key_exists($fldName, $expectedStruct)) continue;// skip not defined cols
  134. $expected = $expectedStruct[$fldName];
  135. $toChange = array();
  136. $toChange['need_change'] = false;
  137. $toChange['type'] = $col['Type'];
  138. $toChange['null'] = ('YES' == $col['Null'])? '' : 'NOT NULL';
  139. // TODO: cannot determine when default is NULL
  140. $toChange['default'] = (!is_null($col['Default']))? "DEFAULT {$col['Default']}" : '';
  141. $expectedNullable = (array_key_exists('nullable', $expected) && $expected['nullable']);
  142. $colNullable = ('YES' == $col['Null']);
  143. if ($expectedNullable != $colNullable) {
  144. $toChange['null'] = ($expectedNullable)? '' : 'NOT NULL';
  145. $toChange['need_change'] = true;
  146. }
  147. // examples:
  148. // 1 - NOT NULL
  149. // 2 - NOT NULL DEFAULT ''
  150. // x 3 - NOT NULL DEFAULT NULL
  151. // x 4 - NULL
  152. // 5 - NULL DEFAULT ''
  153. // 6 - NULL DEFAULT NULL
  154. // 1 == 2, 4 -> 6
  155. $expectedHasDefault = array_key_exists('default', $expected);
  156. $expectedDefaultValue = (array_key_exists('default', $expected))? $expected['default'] : "''";
  157. $colDefault = $col['Default'];
  158. if ($expectedDefault != $colDefault) {
  159. $toChange['default'] = 'DEFAULT ' . ((is_null($expectedDefault))? 'NULL' : $expectedDefault);
  160. $toChange['need_change'] = true;
  161. }
  162. // ALTER TABLE `CRM_NOTIFY` CHANGE `who` `who` VARCHAR(20) NOT NULL
  163. // ALTER TABLE `CRM_NOTIFY` CHANGE `who` `who` VARCHAR(20) NULL
  164. // ALTER TABLE `CRM_NOTIFY` CHANGE `who` `who` VARCHAR(20) NULL DEFAULT NULL
  165. $sqlChange = "alter table CRM_NOTIFY change {$fldName} {$fldName} {$toChange['type']} {$toChange['null']} {$toChange['default']}";
  166. if ($toChange['need_change']) {
  167. DBG::_(true, true, "TODO: sqlChange", $sqlChange, __CLASS__, __FUNCTION__, __LINE__);
  168. }
  169. }
  170. }
  171. /* assert's that field struct has defined or throw exception if cannot set default value:
  172. ['type'] = 'int', 'varchar', ... (MySQL Types)
  173. ['is_nullable'] = true, false
  174. ['default_value'] = NULL or (string, numeric - based on type)
  175. default_value is not set when default_value == NULL and is_nullable == false
  176. ['default_is_null'] = true, false
  177. ['max_length'] = NULL, [0-...] // MySQL `CHARACTER_MAXIMUM_LENGTH`
  178. ['num_precision'] = NULL, [0-65]
  179. ['num_scale'] = NULL, [0-12]
  180. ['char_encoding'] = 'utf8', 'latin2', ...
  181. ['char_collation'] = 'utf8_general_ci', 'latin2_general_ci', ...
  182. ['extra'] = 'auto_increment', 'on update CURRENT_TIMESTAMP'
  183. ['values'] = null or array for enum and set
  184. TODO: validate - wrong related values. ex: {type: 'int', char_collation: 'latin2'}
  185. TODO: validate - not allowed value. ex: {type: 'xyz'}
  186. */
  187. public function _fixExpectedStruct($expectedStruct) {
  188. $fixedStruct = array();
  189. foreach ($expectedStruct as $fldName => $expected) {
  190. //DBG::_(true, true, "TODO: expected", $expected, __CLASS__, __FUNCTION__, __LINE__);
  191. if (!array_key_exists('type', $expected)) throw new Exception("Undefined type for field '{$fldName}'");
  192. if (!array_key_exists('is_nullable', $expected)) {
  193. $expected['is_nullable'] = false;
  194. if (array_key_exists('default_value', $expected) && null === $expected['default_value']) {
  195. $expected['is_nullable'] = true;
  196. }
  197. }
  198. if (!array_key_exists('default_is_null', $expected)) $expected['default_is_null'] = false;
  199. if (!array_key_exists('default_value', $expected)) {
  200. $expected['default_value'] = null;
  201. // switch ($expected['type']) {
  202. // case 'char':
  203. // case 'varchar': $expected['default_value'] = ($expected['is_nullable'])? null : ''; break;
  204. // case 'tinyint':
  205. // case 'bigint':
  206. // case 'int': $expected['default_value'] = ($expected['is_nullable'])? null : 0; break;
  207. // }
  208. if ($expected['is_nullable'] && null === $expected['default_value']) {
  209. $expected['default_is_null'] = true;
  210. }
  211. }
  212. if (!array_key_exists('max_length', $expected)) {
  213. switch ($expected['type']) {
  214. case 'char':
  215. case 'varchar': $expected['max_length'] = 255; break;
  216. case 'binary': $expected['max_length'] = 255; break;// bainary(0) is possible - why? Cannot store values.
  217. case 'varbinary': $expected['max_length'] = 255; break;
  218. case 'binary':
  219. case 'varbinary':
  220. case 'bit': throw new Exception("Undefined max_length for field '{$fldName}' with type '{$expected['type']}'");
  221. //case 'blob': $expected['max_length'] = 65535; break;// is set by engine
  222. //case 'tinyblob': $expected['max_length'] = 255; break;// is set by engine
  223. //case 'mediumblob': $expected['max_length'] = 16777215; break;// is set by engine
  224. //case 'longblob': $expected['max_length'] = 4294967295; break;// is set by engine
  225. //case 'text': $expected['max_length'] = 65535; break;// is set by engine
  226. //case 'tinytext': $expected['max_length'] = 255; break;// is set by engine
  227. //case 'mediumtext': $expected['max_length'] = 16777215; break;// is set by engine
  228. //case 'longtext': $expected['max_length'] = 4294967295; break;// is set by engine
  229. //case 'enum': $expected['max_length'] = 255; break;// is set by engine
  230. //case 'set': $expected['max_length'] = 255; break;// is set by engine
  231. default: $expected['max_length'] = null;//throw new Exception("Undefined max_length for field '{$fldName}'");
  232. }
  233. }
  234. if (!array_key_exists('num_precision', $expected)) {
  235. switch ($expected['type']) {
  236. case 'int': $expected['num_precision'] = 10; break;// int(11) - +1 in type definition
  237. case 'tinyint': $expected['num_precision'] = 3; break;// int(4)
  238. case 'smallint': $expected['num_precision'] = 5; break;// int(6)
  239. case 'mediumint': $expected['num_precision'] = 7; break;// int(8)
  240. case 'bigint': $expected['num_precision'] = 19; break;// int(20)
  241. case 'decimal': $expected['num_precision'] = 10; break;// decimal(10,0)
  242. default: $expected['num_precision'] = null;
  243. }
  244. //throw new Exception("Undefined num_precision for field '{$fldName}'");
  245. }
  246. if (!array_key_exists('num_scale', $expected)) {
  247. switch ($expected['type']) {
  248. case 'int': $expected['num_scale'] = 0; break;
  249. case 'tinyint': $expected['num_scale'] = 0; break;
  250. case 'smallint': $expected['num_scale'] = 0; break;
  251. case 'mediumint': $expected['num_scale'] = 0; break;
  252. case 'bigint': $expected['num_scale'] = 0; break;
  253. case 'decimal': $expected['num_scale'] = 0; break;// ex.: decimal(3,2); decimal(24,0)
  254. case 'double': $expected['num_scale'] = null; break;// ex.: double(10,4); double; double(17,0);
  255. case 'float': $expected['num_scale'] = null; break;// ex.: float(6,2); float; float(17,0);
  256. default: $expected['num_scale'] = null;
  257. }
  258. //throw new Exception("Undefined num_scale for field '{$fldName}'");
  259. }
  260. if (!array_key_exists('char_encoding', $expected)) {
  261. switch ($expected['type']) {
  262. case 'char':
  263. case 'varchar': $expected['char_encoding'] = 'utf8'; break;
  264. case 'enum':
  265. case 'set': $expected['char_encoding'] = 'utf8'; break;
  266. case 'text':
  267. case 'tinytext':
  268. case 'mediumtext':
  269. case 'longtext': $expected['char_encoding'] = 'utf8'; break;
  270. default: $expected['char_encoding'] = null;
  271. }
  272. //throw new Exception("Undefined char_encoding for field '{$fldName}'");
  273. }
  274. if (!array_key_exists('char_collation', $expected)) {
  275. switch ($expected['type']) {
  276. case 'char':
  277. case 'varchar': $expected['char_collation'] = 'utf8_general_ci'; break;
  278. case 'enum':
  279. case 'set': $expected['char_collation'] = 'utf8_general_ci'; break;
  280. case 'text':
  281. case 'tinytext':
  282. case 'mediumtext':
  283. case 'longtext': $expected['char_collation'] = 'utf8_general_ci'; break;
  284. default: $expected['char_collation'] = null;
  285. }
  286. //throw new Exception("Undefined char_collation for field '{$fldName}'");
  287. }
  288. if (!array_key_exists('extra', $expected)) {
  289. $expected['extra'] = null;//throw new Exception("Undefined extra for field '{$fldName}'");
  290. }
  291. if (!array_key_exists('values', $expected) || !is_array($expected['values']) || empty($expected['values'])) {
  292. switch ($expected['type']) {
  293. case 'enum':
  294. case 'set': throw new Exception("Undefined values for field '{$fldName}' with type '{$expected['type']}'");
  295. default: $expected['values'] = null;
  296. }
  297. }
  298. $fixedStruct[$fldName] = $expected;
  299. }
  300. return $fixedStruct;
  301. }
  302. public function showCreateStructMysql($tblName, $struct, $params = array()) {
  303. $expectedStruct = $this->_fixExpectedStruct($struct);
  304. $linesSql = array();
  305. $dbgSql = array();
  306. foreach ($expectedStruct as $fldName => $fld) {
  307. $nullSql = ($fld['is_nullable'])? '' : 'NOT NULL';
  308. $defaultSql = (is_null($fld['default_value']))? 'DEFAULT NULL' : "DEFAULT '{$fld['default_value']}'";
  309. if (is_null($fld['default_value']) && !$fld['is_nullable']) $defaultSql = '';
  310. switch ($fld['type']) {
  311. case 'char':
  312. case 'varchar': $linesSql[] = "`{$fldName}` {$fld['type']}({$fld['max_length']}) {$nullSql} {$defaultSql}"; break;
  313. case 'text':
  314. case 'tinytext':
  315. case 'longtext':
  316. case 'mediumtext': $linesSql[] = "`{$fldName}` {$fld['type']} {$nullSql}"; break;
  317. case 'time':
  318. case 'timestamp':
  319. case 'year':
  320. case 'date':
  321. case 'datetime': $linesSql[] = "`{$fldName}` {$fld['type']} {$nullSql} {$defaultSql}"; break;
  322. // -- 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}
  323. // -- 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}
  324. case 'int':
  325. case 'tinyint':
  326. case 'smallint':
  327. case 'mediumint':
  328. case 'bigint':
  329. if ($fld['num_precision'] > 0) {
  330. $typeParamsSql = "(" . ($fld['num_precision'] + 1) . ")";
  331. }
  332. //if ($fld['num_scale']) $typeParamsSql = ",{$fld['num_scale']}";
  333. $linesSql[] = "`{$fldName}` {$fld['type']}{$typeParamsSql} {$nullSql} {$defaultSql}";
  334. break;
  335. case 'decimal':
  336. $typeParamsSql = "{$fld['num_precision']},{$fld['num_scale']}";
  337. $linesSql[] = "`{$fldName}` {$fld['type']}({$typeParamsSql}) {$nullSql} {$defaultSql}";
  338. break;
  339. case 'float':
  340. case 'double':
  341. case 'real':
  342. $linesSql[] = "`{$fldName}` {$fld['type']} {$nullSql} {$defaultSql}";
  343. break;
  344. case 'enum':
  345. case 'set':
  346. $typeParamsSql = "'" . implode("','", $fld['values']) . "'";
  347. $linesSql[] = "`{$fldName}` {$fld['type']}({$typeParamsSql}) {$nullSql} {$defaultSql}";
  348. break;
  349. case 'bit':
  350. case 'binary':
  351. case 'varbinary':
  352. $linesSql[] = "`{$fldName}` {$fld['type']}({$fld['max_length']}) {$nullSql} {$defaultSql}";
  353. break;
  354. case 'boolean':
  355. case 'serial':
  356. case 'blob':
  357. case 'tinyblob':
  358. case 'mediumblob':
  359. case 'longblob':
  360. $linesSql[] = "`{$fldName}` {$fld['type']} {$nullSql} {$defaultSql}";
  361. break;
  362. case 'geometry':
  363. case 'point':
  364. case 'linestring':
  365. case 'polygon':
  366. case 'multipoint':
  367. case 'multilinestring':
  368. case 'multipolygon':
  369. case 'geometrycollection':
  370. $linesSql[] = "`{$fldName}` {$fld['type']} {$nullSql} {$defaultSql}";
  371. break;
  372. case 'UNIQUE KEY':
  373. $keyFieldsSql = "`" . implode("`,`", $fld['key_fields']) . "`";
  374. $linesSql[] = "UNIQUE KEY `{$fldName}` ({$keyFieldsSql})";
  375. break;
  376. case 'KEY':
  377. $keyFieldsSql = "`" . implode("`,`", $fld['key_fields']) . "`";
  378. $linesSql[] = "KEY `{$fldName}` ({$keyFieldsSql})";
  379. break;
  380. // $expectedStruct['uniq_key_1'] = array('type'=>'UNIQUE KEY', 'key_fields'=>array('who','when','what'));// UNIQUE KEY `uniq_key_1` (`who`,`when`,`what`)
  381. // $expectedStruct['key_who'] = array('type'=>'KEY', 'key_fields'=>array('who'));// KEY `key_who` (`who`)
  382. default: $dbgSql[] = "-- Unimplemented type '{$fld['type']}': " . json_encode($fld);
  383. }
  384. }
  385. $linesSql = implode("\n\t\t, ", $linesSql);
  386. $dbgSql = implode("\n\t\t", $dbgSql);
  387. $tblCharEncoding = V::get('char_encoding', 'utf8', $params);
  388. $structSql = <<<EOF_STRUCT_MYSQL
  389. CREATE TABLE IF NOT EXISTS `{$tblName}` (
  390. {$linesSql}
  391. {$dbgSql}
  392. ) ENGINE=MyISAM DEFAULT CHARSET={$tblCharEncoding}
  393. EOF_STRUCT_MYSQL;
  394. return $structSql;
  395. }
  396. public function fetchAll($sql) {
  397. $sth = $this->prepare($sql);
  398. $sth->execute();
  399. return $sth->fetchAll();
  400. }
  401. public function bindValues($sth, $values) {
  402. foreach ($values as $name => $value) {
  403. $val = $value;
  404. $type = PDO::PARAM_STR;
  405. if (is_array($value)) {
  406. $val = $value[0];
  407. if (count($value) > 1) {
  408. $type = $value[1];
  409. }
  410. }
  411. $sth->bindValue($name, $val, $type);
  412. if (!isset($sth->bindedValues)) $sth->bindedValues = array();
  413. $sth->bindedValues[$name] = array($val, $type);
  414. }
  415. }
  416. public function getRawSql($sth, $values = array()) {
  417. $sql = $sth->queryString;
  418. $params = array();
  419. if (!empty($sth->bindedValues)) {
  420. foreach ($sth->bindedValues as $name => $value) {
  421. $params[$name] = array($value[0], $value[1]);
  422. }
  423. }
  424. foreach ($values as $name => $value) {
  425. $val = $value;
  426. $type = PDO::PARAM_STR;
  427. if (is_array($value)) {
  428. $val = $value[0];
  429. if (count($value) > 1) {
  430. $type = $value[1];
  431. }
  432. }
  433. $params[$name] = array($val, $type);
  434. }
  435. if (!empty($params)) {
  436. foreach ($params as $name => $val) {
  437. $outValue = $val[0];
  438. if (PDO::PARAM_STR == $val[1]) $outValue = "'{$outValue}'";
  439. $sql = str_replace(":{$name}", $outValue, $sql);
  440. }
  441. }
  442. return $sql;
  443. }
  444. }