1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20:
21: App::uses('DataSource', 'Model/Datasource');
22: App::uses('String', 'Utility');
23: App::uses('View', 'View');
24:
25: 26: 27: 28: 29: 30: 31:
32: class DboSource extends DataSource {
33:
34: 35: 36: 37: 38:
39: public $description = "Database Data Source";
40:
41: 42: 43: 44: 45:
46: public $index = array('PRI' => 'primary', 'MUL' => 'index', 'UNI' => 'unique');
47:
48: 49: 50: 51: 52:
53: public $alias = 'AS ';
54:
55: 56: 57: 58: 59: 60: 61:
62: public static $methodCache = array();
63:
64: 65: 66: 67: 68: 69:
70: public $cacheMethods = true;
71:
72: 73: 74: 75: 76: 77: 78:
79: public $useNestedTransactions = false;
80:
81: 82: 83: 84: 85:
86: public $fullDebug = false;
87:
88: 89: 90: 91: 92:
93: public $affected = null;
94:
95: 96: 97: 98: 99:
100: public $numRows = null;
101:
102: 103: 104: 105: 106:
107: public $took = null;
108:
109: 110: 111: 112: 113:
114: protected $_result = null;
115:
116: 117: 118: 119: 120:
121: protected $_queriesCnt = 0;
122:
123: 124: 125: 126: 127:
128: protected $_queriesTime = null;
129:
130: 131: 132: 133: 134:
135: protected $_queriesLog = array();
136:
137: 138: 139: 140: 141: 142: 143:
144: protected $_queriesLogMax = 200;
145:
146: 147: 148: 149: 150:
151: protected $_queryCache = array();
152:
153: 154: 155: 156: 157:
158: protected $_connection = null;
159:
160: 161: 162: 163: 164:
165: public $configKeyName = null;
166:
167: 168: 169: 170: 171:
172: public $startQuote = null;
173:
174: 175: 176: 177: 178:
179: public $endQuote = null;
180:
181: 182: 183: 184: 185:
186: protected $_sqlOps = array('like', 'ilike', 'or', 'not', 'in', 'between', 'regexp', 'similar to');
187:
188: 189: 190: 191: 192:
193: protected $_transactionNesting = 0;
194:
195: 196: 197: 198: 199:
200: protected $_queryDefaults = array(
201: 'conditions' => array(),
202: 'fields' => null,
203: 'table' => null,
204: 'alias' => null,
205: 'order' => null,
206: 'limit' => null,
207: 'joins' => array(),
208: 'group' => null,
209: 'offset' => null
210: );
211:
212: 213: 214: 215: 216:
217: public $virtualFieldSeparator = '__';
218:
219: 220: 221: 222: 223:
224: public $tableParameters = array();
225:
226: 227: 228: 229: 230:
231: public $fieldParameters = array();
232:
233: 234: 235: 236: 237: 238:
239: protected $_methodCacheChange = false;
240:
241: 242: 243: 244: 245: 246: 247:
248: public function __construct($config = null, $autoConnect = true) {
249: if (!isset($config['prefix'])) {
250: $config['prefix'] = '';
251: }
252: parent::__construct($config);
253: $this->fullDebug = Configure::read('debug') > 1;
254: if (!$this->enabled()) {
255: throw new MissingConnectionException(array(
256: 'class' => get_class($this),
257: 'message' => __d('cake_dev', 'Selected driver is not enabled'),
258: 'enabled' => false
259: ));
260: }
261: if ($autoConnect) {
262: $this->connect();
263: }
264: }
265:
266: 267: 268: 269: 270: 271:
272: public function reconnect($config = array()) {
273: $this->disconnect();
274: $this->setConfig($config);
275: $this->_sources = null;
276:
277: return $this->connect();
278: }
279:
280: 281: 282: 283: 284:
285: public function disconnect() {
286: if ($this->_result instanceof PDOStatement) {
287: $this->_result->closeCursor();
288: }
289: unset($this->_connection);
290: $this->connected = false;
291: return true;
292: }
293:
294: 295: 296: 297: 298:
299: public function getConnection() {
300: return $this->_connection;
301: }
302:
303: 304: 305: 306: 307:
308: public function getVersion() {
309: return $this->_connection->getAttribute(PDO::ATTR_SERVER_VERSION);
310: }
311:
312: 313: 314: 315: 316: 317: 318:
319: public function value($data, $column = null) {
320: if (is_array($data) && !empty($data)) {
321: return array_map(
322: array(&$this, 'value'),
323: $data, array_fill(0, count($data), $column)
324: );
325: } elseif (is_object($data) && isset($data->type, $data->value)) {
326: if ($data->type === 'identifier') {
327: return $this->name($data->value);
328: } elseif ($data->type === 'expression') {
329: return $data->value;
330: }
331: } elseif (in_array($data, array('{$__cakeID__$}', '{$__cakeForeignKey__$}'), true)) {
332: return $data;
333: }
334:
335: if ($data === null || (is_array($data) && empty($data))) {
336: return 'NULL';
337: }
338:
339: if (empty($column)) {
340: $column = $this->introspectType($data);
341: }
342:
343: switch ($column) {
344: case 'binary':
345: return $this->_connection->quote($data, PDO::PARAM_LOB);
346: case 'boolean':
347: return $this->_connection->quote($this->boolean($data, true), PDO::PARAM_BOOL);
348: case 'string':
349: case 'text':
350: return $this->_connection->quote($data, PDO::PARAM_STR);
351: default:
352: if ($data === '') {
353: return 'NULL';
354: }
355: if (is_float($data)) {
356: return str_replace(',', '.', strval($data));
357: }
358: if ((is_int($data) || $data === '0') || (
359: is_numeric($data) && strpos($data, ',') === false &&
360: $data[0] != '0' && strpos($data, 'e') === false)
361: ) {
362: return $data;
363: }
364: return $this->_connection->quote($data);
365: }
366: }
367:
368: 369: 370: 371: 372: 373: 374:
375: public function identifier($identifier) {
376: $obj = new stdClass();
377: $obj->type = 'identifier';
378: $obj->value = $identifier;
379: return $obj;
380: }
381:
382: 383: 384: 385: 386: 387: 388:
389: public function expression($expression) {
390: $obj = new stdClass();
391: $obj->type = 'expression';
392: $obj->value = $expression;
393: return $obj;
394: }
395:
396: 397: 398: 399: 400: 401: 402:
403: public function rawQuery($sql, $params = array()) {
404: $this->took = $this->numRows = false;
405: return $this->execute($sql, $params);
406: }
407:
408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421:
422: public function execute($sql, $options = array(), $params = array()) {
423: $options += array('log' => $this->fullDebug);
424:
425: $t = microtime(true);
426: $this->_result = $this->_execute($sql, $params);
427:
428: if ($options['log']) {
429: $this->took = round((microtime(true) - $t) * 1000, 0);
430: $this->numRows = $this->affected = $this->lastAffected();
431: $this->logQuery($sql, $params);
432: }
433:
434: return $this->_result;
435: }
436:
437: 438: 439: 440: 441: 442: 443: 444: 445: 446:
447: protected function _execute($sql, $params = array(), $prepareOptions = array()) {
448: $sql = trim($sql);
449: if (preg_match('/^(?:CREATE|ALTER|DROP)\s+(?:TABLE|INDEX)/i', $sql)) {
450: $statements = array_filter(explode(';', $sql));
451: if (count($statements) > 1) {
452: $result = array_map(array($this, '_execute'), $statements);
453: return array_search(false, $result) === false;
454: }
455: }
456:
457: try {
458: $query = $this->_connection->prepare($sql, $prepareOptions);
459: $query->setFetchMode(PDO::FETCH_LAZY);
460: if (!$query->execute($params)) {
461: $this->_results = $query;
462: $query->closeCursor();
463: return false;
464: }
465: if (!$query->columnCount()) {
466: $query->closeCursor();
467: if (!$query->rowCount()) {
468: return true;
469: }
470: }
471: return $query;
472: } catch (PDOException $e) {
473: if (isset($query->queryString)) {
474: $e->queryString = $query->queryString;
475: } else {
476: $e->queryString = $sql;
477: }
478: throw $e;
479: }
480: }
481:
482: 483: 484: 485: 486: 487:
488: public function lastError(PDOStatement $query = null) {
489: if ($query) {
490: $error = $query->errorInfo();
491: } else {
492: $error = $this->_connection->errorInfo();
493: }
494: if (empty($error[2])) {
495: return null;
496: }
497: return $error[1] . ': ' . $error[2];
498: }
499:
500: 501: 502: 503: 504: 505: 506:
507: public function lastAffected($source = null) {
508: if ($this->hasResult()) {
509: return $this->_result->rowCount();
510: }
511: return 0;
512: }
513:
514: 515: 516: 517: 518: 519: 520:
521: public function lastNumRows($source = null) {
522: return $this->lastAffected();
523: }
524:
525: 526: 527: 528: 529:
530: public function query() {
531: $args = func_get_args();
532: $fields = null;
533: $order = null;
534: $limit = null;
535: $page = null;
536: $recursive = null;
537:
538: if (count($args) === 1) {
539: return $this->fetchAll($args[0]);
540: } elseif (count($args) > 1 && (strpos($args[0], 'findBy') === 0 || strpos($args[0], 'findAllBy') === 0)) {
541: $params = $args[1];
542:
543: if (substr($args[0], 0, 6) === 'findBy') {
544: $all = false;
545: $field = Inflector::underscore(substr($args[0], 6));
546: } else {
547: $all = true;
548: $field = Inflector::underscore(substr($args[0], 9));
549: }
550:
551: $or = (strpos($field, '_or_') !== false);
552: if ($or) {
553: $field = explode('_or_', $field);
554: } else {
555: $field = explode('_and_', $field);
556: }
557: $off = count($field) - 1;
558:
559: if (isset($params[1 + $off])) {
560: $fields = $params[1 + $off];
561: }
562:
563: if (isset($params[2 + $off])) {
564: $order = $params[2 + $off];
565: }
566:
567: if (!array_key_exists(0, $params)) {
568: return false;
569: }
570:
571: $c = 0;
572: $conditions = array();
573:
574: foreach ($field as $f) {
575: $conditions[$args[2]->alias . '.' . $f] = $params[$c++];
576: }
577:
578: if ($or) {
579: $conditions = array('OR' => $conditions);
580: }
581:
582: if ($all) {
583: if (isset($params[3 + $off])) {
584: $limit = $params[3 + $off];
585: }
586:
587: if (isset($params[4 + $off])) {
588: $page = $params[4 + $off];
589: }
590:
591: if (isset($params[5 + $off])) {
592: $recursive = $params[5 + $off];
593: }
594: return $args[2]->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive'));
595: }
596: if (isset($params[3 + $off])) {
597: $recursive = $params[3 + $off];
598: }
599: return $args[2]->find('first', compact('conditions', 'fields', 'order', 'recursive'));
600: }
601: if (isset($args[1]) && $args[1] === true) {
602: return $this->fetchAll($args[0], true);
603: } elseif (isset($args[1]) && !is_array($args[1])) {
604: return $this->fetchAll($args[0], false);
605: } elseif (isset($args[1]) && is_array($args[1])) {
606: if (isset($args[2])) {
607: $cache = $args[2];
608: } else {
609: $cache = true;
610: }
611: return $this->fetchAll($args[0], $args[1], array('cache' => $cache));
612: }
613: }
614:
615: 616: 617: 618: 619: 620:
621: public function fetchRow($sql = null) {
622: if (is_string($sql) && strlen($sql) > 5 && !$this->execute($sql)) {
623: return null;
624: }
625:
626: if ($this->hasResult()) {
627: $this->resultSet($this->_result);
628: $resultRow = $this->fetchResult();
629: if (isset($resultRow[0])) {
630: $this->fetchVirtualField($resultRow);
631: }
632: return $resultRow;
633: }
634: return null;
635: }
636:
637: 638: 639: 640: 641: 642: 643: 644: 645: 646: 647: 648: 649: 650: 651: 652:
653: public function fetchAll($sql, $params = array(), $options = array()) {
654: if (is_string($options)) {
655: $options = array('modelName' => $options);
656: }
657: if (is_bool($params)) {
658: $options['cache'] = $params;
659: $params = array();
660: }
661: $options += array('cache' => true);
662: $cache = $options['cache'];
663: if ($cache && ($cached = $this->getQueryCache($sql, $params)) !== false) {
664: return $cached;
665: }
666: if ($result = $this->execute($sql, array(), $params)) {
667: $out = array();
668:
669: if ($this->hasResult()) {
670: $first = $this->fetchRow();
671: if ($first) {
672: $out[] = $first;
673: }
674: while ($item = $this->fetchResult()) {
675: if (isset($item[0])) {
676: $this->fetchVirtualField($item);
677: }
678: $out[] = $item;
679: }
680: }
681:
682: if (!is_bool($result) && $cache) {
683: $this->_writeQueryCache($sql, $out, $params);
684: }
685:
686: if (empty($out) && is_bool($this->_result)) {
687: return $this->_result;
688: }
689: return $out;
690: }
691: return false;
692: }
693:
694: 695: 696: 697: 698:
699: public function fetchResult() {
700: return false;
701: }
702:
703: 704: 705: 706: 707: 708:
709: public function fetchVirtualField(&$result) {
710: if (isset($result[0]) && is_array($result[0])) {
711: foreach ($result[0] as $field => $value) {
712: if (strpos($field, $this->virtualFieldSeparator) === false) {
713: continue;
714: }
715: list($alias, $virtual) = explode($this->virtualFieldSeparator, $field);
716:
717: if (!ClassRegistry::isKeySet($alias)) {
718: return;
719: }
720: $model = ClassRegistry::getObject($alias);
721: if ($model->isVirtualField($virtual)) {
722: $result[$alias][$virtual] = $value;
723: unset($result[0][$field]);
724: }
725: }
726: if (empty($result[0])) {
727: unset($result[0]);
728: }
729: }
730: }
731:
732: 733: 734: 735: 736: 737: 738:
739: public function field($name, $sql) {
740: $data = $this->fetchRow($sql);
741: if (empty($data[$name])) {
742: return false;
743: }
744: return $data[$name];
745: }
746:
747: 748: 749: 750: 751: 752:
753: public function flushMethodCache() {
754: $this->_methodCacheChange = true;
755: self::$methodCache = array();
756: }
757:
758: 759: 760: 761: 762: 763: 764: 765: 766: 767: 768: 769:
770: public function cacheMethod($method, $key, $value = null) {
771: if ($this->cacheMethods === false) {
772: return $value;
773: }
774: if (empty(self::$methodCache)) {
775: self::$methodCache = Cache::read('method_cache', '_cake_core_');
776: }
777: if ($value === null) {
778: return (isset(self::$methodCache[$method][$key])) ? self::$methodCache[$method][$key] : null;
779: }
780: $this->_methodCacheChange = true;
781: return self::$methodCache[$method][$key] = $value;
782: }
783:
784: 785: 786: 787: 788: 789: 790: 791: 792: 793: 794: 795:
796: public function name($data) {
797: if (is_object($data) && isset($data->type)) {
798: return $data->value;
799: }
800: if ($data === '*') {
801: return '*';
802: }
803: if (is_array($data)) {
804: foreach ($data as $i => $dataItem) {
805: $data[$i] = $this->name($dataItem);
806: }
807: return $data;
808: }
809: $cacheKey = md5($this->startQuote . $data . $this->endQuote);
810: if ($return = $this->cacheMethod(__FUNCTION__, $cacheKey)) {
811: return $return;
812: }
813: $data = trim($data);
814: if (preg_match('/^[\w-]+(?:\.[^ \*]*)*$/', $data)) {
815: if (strpos($data, '.') === false) {
816: return $this->cacheMethod(__FUNCTION__, $cacheKey, $this->startQuote . $data . $this->endQuote);
817: }
818: $items = explode('.', $data);
819: return $this->cacheMethod(__FUNCTION__, $cacheKey,
820: $this->startQuote . implode($this->endQuote . '.' . $this->startQuote, $items) . $this->endQuote
821: );
822: }
823: if (preg_match('/^[\w-]+\.\*$/', $data)) {
824: return $this->cacheMethod(__FUNCTION__, $cacheKey,
825: $this->startQuote . str_replace('.*', $this->endQuote . '.*', $data)
826: );
827: }
828: if (preg_match('/^([\w-]+)\((.*)\)$/', $data, $matches)) {
829: return $this->cacheMethod(__FUNCTION__, $cacheKey,
830: $matches[1] . '(' . $this->name($matches[2]) . ')'
831: );
832: }
833: if (
834: preg_match('/^([\w-]+(\.[\w-]+|\(.*\))*)\s+' . preg_quote($this->alias) . '\s*([\w-]+)$/i', $data, $matches
835: )) {
836: return $this->cacheMethod(
837: __FUNCTION__, $cacheKey,
838: preg_replace(
839: '/\s{2,}/', ' ', $this->name($matches[1]) . ' ' . $this->alias . ' ' . $this->name($matches[3])
840: )
841: );
842: }
843: if (preg_match('/^[\w-_\s]*[\w-_]+/', $data)) {
844: return $this->cacheMethod(__FUNCTION__, $cacheKey, $this->startQuote . $data . $this->endQuote);
845: }
846: return $this->cacheMethod(__FUNCTION__, $cacheKey, $data);
847: }
848:
849: 850: 851: 852: 853:
854: public function isConnected() {
855: return $this->connected;
856: }
857:
858: 859: 860: 861: 862:
863: public function hasResult() {
864: return is_a($this->_result, 'PDOStatement');
865: }
866:
867: 868: 869: 870: 871: 872: 873:
874: public function getLog($sorted = false, $clear = true) {
875: if ($sorted) {
876: $log = sortByKey($this->_queriesLog, 'took', 'desc', SORT_NUMERIC);
877: } else {
878: $log = $this->_queriesLog;
879: }
880: if ($clear) {
881: $this->_queriesLog = array();
882: }
883: return array('log' => $log, 'count' => $this->_queriesCnt, 'time' => $this->_queriesTime);
884: }
885:
886: 887: 888: 889: 890: 891: 892:
893: public function showLog($sorted = false) {
894: $log = $this->getLog($sorted, false);
895: if (empty($log['log'])) {
896: return;
897: }
898: if (PHP_SAPI !== 'cli') {
899: $controller = null;
900: $View = new View($controller, false);
901: $View->set('logs', array($this->configKeyName => $log));
902: echo $View->element('sql_dump', array('_forced_from_dbo_' => true));
903: } else {
904: foreach ($log['log'] as $k => $i) {
905: print (($k + 1) . ". {$i['query']}\n");
906: }
907: }
908: }
909:
910: 911: 912: 913: 914: 915: 916:
917: public function logQuery($sql, $params = array()) {
918: $this->_queriesCnt++;
919: $this->_queriesTime += $this->took;
920: $this->_queriesLog[] = array(
921: 'query' => $sql,
922: 'params' => $params,
923: 'affected' => $this->affected,
924: 'numRows' => $this->numRows,
925: 'took' => $this->took
926: );
927: if (count($this->_queriesLog) > $this->_queriesLogMax) {
928: array_shift($this->_queriesLog);
929: }
930: }
931:
932: 933: 934: 935: 936: 937: 938: 939:
940: public function fullTableName($model, $quote = true, $schema = true) {
941: if (is_object($model)) {
942: $schemaName = $model->schemaName;
943: $table = $model->tablePrefix . $model->table;
944: } elseif (!empty($this->config['prefix']) && strpos($model, $this->config['prefix']) !== 0) {
945: $table = $this->config['prefix'] . strval($model);
946: } else {
947: $table = strval($model);
948: }
949: if ($schema && !isset($schemaName)) {
950: $schemaName = $this->getSchemaName();
951: }
952:
953: if ($quote) {
954: if ($schema && !empty($schemaName)) {
955: if (false == strstr($table, '.')) {
956: return $this->name($schemaName) . '.' . $this->name($table);
957: }
958: }
959: return $this->name($table);
960: }
961: if ($schema && !empty($schemaName)) {
962: if (false == strstr($table, '.')) {
963: return $schemaName . '.' . $table;
964: }
965: }
966: return $table;
967: }
968:
969: 970: 971: 972: 973: 974: 975: 976: 977: 978: 979: 980:
981: public function create(Model $model, $fields = null, $values = null) {
982: $id = null;
983:
984: if (!$fields) {
985: unset($fields, $values);
986: $fields = array_keys($model->data);
987: $values = array_values($model->data);
988: }
989: $count = count($fields);
990:
991: for ($i = 0; $i < $count; $i++) {
992: $valueInsert[] = $this->value($values[$i], $model->getColumnType($fields[$i]));
993: $fieldInsert[] = $this->name($fields[$i]);
994: if ($fields[$i] == $model->primaryKey) {
995: $id = $values[$i];
996: }
997: }
998: $query = array(
999: 'table' => $this->fullTableName($model),
1000: 'fields' => implode(', ', $fieldInsert),
1001: 'values' => implode(', ', $valueInsert)
1002: );
1003:
1004: if ($this->execute($this->renderStatement('create', $query))) {
1005: if (empty($id)) {
1006: $id = $this->lastInsertId($this->fullTableName($model, false, false), $model->primaryKey);
1007: }
1008: $model->setInsertID($id);
1009: $model->id = $id;
1010: return true;
1011: }
1012: $model->onError();
1013: return false;
1014: }
1015:
1016: 1017: 1018: 1019: 1020: 1021: 1022: 1023: 1024: 1025:
1026: public function read(Model $model, $queryData = array(), $recursive = null) {
1027: $queryData = $this->_scrubQueryData($queryData);
1028:
1029: $null = null;
1030: $array = array('callbacks' => $queryData['callbacks']);
1031: $linkedModels = array();
1032: $bypass = false;
1033:
1034: if ($recursive === null && isset($queryData['recursive'])) {
1035: $recursive = $queryData['recursive'];
1036: }
1037:
1038: if ($recursive !== null) {
1039: $_recursive = $model->recursive;
1040: $model->recursive = $recursive;
1041: }
1042:
1043: if (!empty($queryData['fields'])) {
1044: $bypass = true;
1045: $queryData['fields'] = $this->fields($model, null, $queryData['fields']);
1046: } else {
1047: $queryData['fields'] = $this->fields($model);
1048: }
1049:
1050: $_associations = $model->associations();
1051:
1052: if ($model->recursive == -1) {
1053: $_associations = array();
1054: } elseif ($model->recursive === 0) {
1055: unset($_associations[2], $_associations[3]);
1056: }
1057:
1058: foreach ($_associations as $type) {
1059: foreach ($model->{$type} as $assoc => $assocData) {
1060: $linkModel = $model->{$assoc};
1061: $external = isset($assocData['external']);
1062:
1063: $linkModel->getDataSource();
1064: if ($model->useDbConfig === $linkModel->useDbConfig) {
1065: if ($bypass) {
1066: $assocData['fields'] = false;
1067: }
1068: if (true === $this->generateAssociationQuery($model, $linkModel, $type, $assoc, $assocData, $queryData, $external, $null)) {
1069: $linkedModels[$type . '/' . $assoc] = true;
1070: }
1071: }
1072: }
1073: }
1074:
1075: $query = trim($this->generateAssociationQuery($model, null, null, null, null, $queryData, false, $null));
1076:
1077: $resultSet = $this->fetchAll($query, $model->cacheQueries);
1078:
1079: if ($resultSet === false) {
1080: $model->onError();
1081: return false;
1082: }
1083:
1084: $filtered = array();
1085:
1086: if ($queryData['callbacks'] === true || $queryData['callbacks'] === 'after') {
1087: $filtered = $this->_filterResults($resultSet, $model);
1088: }
1089:
1090: if ($model->recursive > -1) {
1091: $joined = array();
1092: if (isset($queryData['joins'][0]['alias'])) {
1093: $joined[$model->alias] = (array)Hash::extract($queryData['joins'], '{n}.alias');
1094: }
1095: foreach ($_associations as $type) {
1096: foreach ($model->{$type} as $assoc => $assocData) {
1097: $linkModel = $model->{$assoc};
1098:
1099: if (!isset($linkedModels[$type . '/' . $assoc])) {
1100: if ($model->useDbConfig === $linkModel->useDbConfig) {
1101: $db = $this;
1102: } else {
1103: $db = ConnectionManager::getDataSource($linkModel->useDbConfig);
1104: }
1105: } elseif ($model->recursive > 1 && ($type === 'belongsTo' || $type === 'hasOne')) {
1106: $db = $this;
1107: }
1108:
1109: if (isset($db) && method_exists($db, 'queryAssociation')) {
1110: $stack = array($assoc);
1111: $stack['_joined'] = $joined;
1112: $db->queryAssociation($model, $linkModel, $type, $assoc, $assocData, $array, true, $resultSet, $model->recursive - 1, $stack);
1113: unset($db);
1114:
1115: if ($type === 'hasMany') {
1116: $filtered[] = $assoc;
1117: }
1118: }
1119: }
1120: }
1121: if ($queryData['callbacks'] === true || $queryData['callbacks'] === 'after') {
1122: $this->_filterResults($resultSet, $model, $filtered);
1123: }
1124: }
1125:
1126: if ($recursive !== null) {
1127: $model->recursive = $_recursive;
1128: }
1129: return $resultSet;
1130: }
1131:
1132: 1133: 1134: 1135: 1136: 1137: 1138: 1139:
1140: protected function _filterResults(&$results, Model $model, $filtered = array()) {
1141: $current = reset($results);
1142: if (!is_array($current)) {
1143: return array();
1144: }
1145: $keys = array_diff(array_keys($current), $filtered, array($model->alias));
1146: $filtering = array();
1147: foreach ($keys as $className) {
1148: if (!isset($model->{$className}) || !is_object($model->{$className})) {
1149: continue;
1150: }
1151: $linkedModel = $model->{$className};
1152: $filtering[] = $className;
1153: foreach ($results as $key => &$result) {
1154: $data = $linkedModel->afterFind(array(array($className => $result[$className])), false);
1155: if (isset($data[0][$className])) {
1156: $result[$className] = $data[0][$className];
1157: } else {
1158: unset($results[$key]);
1159: }
1160: }
1161: }
1162: return $filtering;
1163: }
1164:
1165: 1166: 1167: 1168: 1169: 1170: 1171: 1172: 1173: 1174: 1175: 1176: 1177: 1178: 1179: 1180:
1181: public function queryAssociation(Model $model, &$linkModel, $type, $association, $assocData, &$queryData, $external, &$resultSet, $recursive, $stack) {
1182: if (isset($stack['_joined'])) {
1183: $joined = $stack['_joined'];
1184: unset($stack['_joined']);
1185: }
1186:
1187: if ($query = $this->generateAssociationQuery($model, $linkModel, $type, $association, $assocData, $queryData, $external, $resultSet)) {
1188: if (!is_array($resultSet)) {
1189: throw new CakeException(__d('cake_dev', 'Error in Model %s', get_class($model)));
1190: }
1191: if ($type === 'hasMany' && empty($assocData['limit']) && !empty($assocData['foreignKey'])) {
1192: $ins = $fetch = array();
1193: foreach ($resultSet as &$result) {
1194: if ($in = $this->insertQueryData('{$__cakeID__$}', $result, $association, $assocData, $model, $linkModel, $stack)) {
1195: $ins[] = $in;
1196: }
1197: }
1198:
1199: if (!empty($ins)) {
1200: $ins = array_unique($ins);
1201: $fetch = $this->fetchAssociated($model, $query, $ins);
1202: }
1203:
1204: if (!empty($fetch) && is_array($fetch)) {
1205: if ($recursive > 0) {
1206: foreach ($linkModel->associations() as $type1) {
1207: foreach ($linkModel->{$type1} as $assoc1 => $assocData1) {
1208: $deepModel = $linkModel->{$assoc1};
1209: $tmpStack = $stack;
1210: $tmpStack[] = $assoc1;
1211:
1212: if ($linkModel->useDbConfig === $deepModel->useDbConfig) {
1213: $db = $this;
1214: } else {
1215: $db = ConnectionManager::getDataSource($deepModel->useDbConfig);
1216: }
1217: $db->queryAssociation($linkModel, $deepModel, $type1, $assoc1, $assocData1, $queryData, true, $fetch, $recursive - 1, $tmpStack);
1218: }
1219: }
1220: }
1221: }
1222: if ($queryData['callbacks'] === true || $queryData['callbacks'] === 'after') {
1223: $this->_filterResults($fetch, $model);
1224: }
1225: return $this->_mergeHasMany($resultSet, $fetch, $association, $model, $linkModel);
1226: } elseif ($type === 'hasAndBelongsToMany') {
1227: $ins = $fetch = array();
1228: foreach ($resultSet as &$result) {
1229: if ($in = $this->insertQueryData('{$__cakeID__$}', $result, $association, $assocData, $model, $linkModel, $stack)) {
1230: $ins[] = $in;
1231: }
1232: }
1233: if (!empty($ins)) {
1234: $ins = array_unique($ins);
1235: if (count($ins) > 1) {
1236: $query = str_replace('{$__cakeID__$}', '(' . implode(', ', $ins) . ')', $query);
1237: $query = str_replace('= (', 'IN (', $query);
1238: } else {
1239: $query = str_replace('{$__cakeID__$}', $ins[0], $query);
1240: }
1241: $query = str_replace(' WHERE 1 = 1', '', $query);
1242: }
1243:
1244: $foreignKey = $model->hasAndBelongsToMany[$association]['foreignKey'];
1245: $joinKeys = array($foreignKey, $model->hasAndBelongsToMany[$association]['associationForeignKey']);
1246: list($with, $habtmFields) = $model->joinModel($model->hasAndBelongsToMany[$association]['with'], $joinKeys);
1247: $habtmFieldsCount = count($habtmFields);
1248: $q = $this->insertQueryData($query, null, $association, $assocData, $model, $linkModel, $stack);
1249:
1250: if ($q !== false) {
1251: $fetch = $this->fetchAll($q, $model->cacheQueries);
1252: } else {
1253: $fetch = null;
1254: }
1255: }
1256:
1257: $modelAlias = $model->alias;
1258: $modelPK = $model->primaryKey;
1259: foreach ($resultSet as &$row) {
1260: if ($type !== 'hasAndBelongsToMany') {
1261: $q = $this->insertQueryData($query, $row, $association, $assocData, $model, $linkModel, $stack);
1262: $fetch = null;
1263: if ($q !== false) {
1264: $joinedData = array();
1265: if (($type === 'belongsTo' || $type === 'hasOne') && isset($row[$linkModel->alias], $joined[$model->alias]) && in_array($linkModel->alias, $joined[$model->alias])) {
1266: $joinedData = Hash::filter($row[$linkModel->alias]);
1267: if (!empty($joinedData)) {
1268: $fetch[0] = array($linkModel->alias => $row[$linkModel->alias]);
1269: }
1270: } else {
1271: $fetch = $this->fetchAll($q, $model->cacheQueries);
1272: }
1273: }
1274: }
1275: $selfJoin = $linkModel->name === $model->name;
1276:
1277: if (!empty($fetch) && is_array($fetch)) {
1278: if ($recursive > 0) {
1279: foreach ($linkModel->associations() as $type1) {
1280: foreach ($linkModel->{$type1} as $assoc1 => $assocData1) {
1281: $deepModel = $linkModel->{$assoc1};
1282:
1283: if ($type1 === 'belongsTo' || ($deepModel->alias === $modelAlias && $type === 'belongsTo') || ($deepModel->alias !== $modelAlias)) {
1284: $tmpStack = $stack;
1285: $tmpStack[] = $assoc1;
1286: if ($linkModel->useDbConfig == $deepModel->useDbConfig) {
1287: $db = $this;
1288: } else {
1289: $db = ConnectionManager::getDataSource($deepModel->useDbConfig);
1290: }
1291: $db->queryAssociation($linkModel, $deepModel, $type1, $assoc1, $assocData1, $queryData, true, $fetch, $recursive - 1, $tmpStack);
1292: }
1293: }
1294: }
1295: }
1296: if ($type === 'hasAndBelongsToMany') {
1297: $merge = array();
1298:
1299: foreach ($fetch as $data) {
1300: if (isset($data[$with]) && $data[$with][$foreignKey] === $row[$modelAlias][$modelPK]) {
1301: if ($habtmFieldsCount <= 2) {
1302: unset($data[$with]);
1303: }
1304: $merge[] = $data;
1305: }
1306: }
1307: if (empty($merge) && !isset($row[$association])) {
1308: $row[$association] = $merge;
1309: } else {
1310: $this->_mergeAssociation($row, $merge, $association, $type);
1311: }
1312: } else {
1313: $this->_mergeAssociation($row, $fetch, $association, $type, $selfJoin);
1314: }
1315: if (isset($row[$association])) {
1316: $row[$association] = $linkModel->afterFind($row[$association], false);
1317: }
1318: } else {
1319: $tempArray[0][$association] = false;
1320: $this->_mergeAssociation($row, $tempArray, $association, $type, $selfJoin);
1321: }
1322: }
1323: }
1324: }
1325:
1326: 1327: 1328: 1329: 1330: 1331: 1332: 1333:
1334: public function fetchAssociated(Model $model, $query, $ids) {
1335: $query = str_replace('{$__cakeID__$}', implode(', ', $ids), $query);
1336: if (count($ids) > 1) {
1337: $query = str_replace('= (', 'IN (', $query);
1338: }
1339: return $this->fetchAll($query, $model->cacheQueries);
1340: }
1341:
1342: 1343: 1344: 1345: 1346: 1347: 1348: 1349: 1350: 1351:
1352: protected function _mergeHasMany(&$resultSet, $merge, $association, $model, $linkModel) {
1353: $modelAlias = $model->alias;
1354: $modelPK = $model->primaryKey;
1355: $modelFK = $model->hasMany[$association]['foreignKey'];
1356: foreach ($resultSet as &$result) {
1357: if (!isset($result[$modelAlias])) {
1358: continue;
1359: }
1360: $merged = array();
1361: foreach ($merge as $data) {
1362: if ($result[$modelAlias][$modelPK] === $data[$association][$modelFK]) {
1363: if (count($data) > 1) {
1364: $data = array_merge($data[$association], $data);
1365: unset($data[$association]);
1366: foreach ($data as $key => $name) {
1367: if (is_numeric($key)) {
1368: $data[$association][] = $name;
1369: unset($data[$key]);
1370: }
1371: }
1372: $merged[] = $data;
1373: } else {
1374: $merged[] = $data[$association];
1375: }
1376: }
1377: }
1378: $result = Hash::mergeDiff($result, array($association => $merged));
1379: }
1380: }
1381:
1382: 1383: 1384: 1385: 1386: 1387: 1388: 1389: 1390: 1391:
1392: protected function _mergeAssociation(&$data, &$merge, $association, $type, $selfJoin = false) {
1393: if (isset($merge[0]) && !isset($merge[0][$association])) {
1394: $association = Inflector::pluralize($association);
1395: }
1396:
1397: if ($type === 'belongsTo' || $type === 'hasOne') {
1398: if (isset($merge[$association])) {
1399: $data[$association] = $merge[$association][0];
1400: } else {
1401: if (count($merge[0][$association]) > 1) {
1402: foreach ($merge[0] as $assoc => $data2) {
1403: if ($assoc !== $association) {
1404: $merge[0][$association][$assoc] = $data2;
1405: }
1406: }
1407: }
1408: if (!isset($data[$association])) {
1409: $data[$association] = array();
1410: if ($merge[0][$association]) {
1411: $data[$association] = $merge[0][$association];
1412: }
1413: } else {
1414: if (is_array($merge[0][$association])) {
1415: foreach ($data[$association] as $k => $v) {
1416: if (!is_array($v)) {
1417: $dataAssocTmp[$k] = $v;
1418: }
1419: }
1420:
1421: foreach ($merge[0][$association] as $k => $v) {
1422: if (!is_array($v)) {
1423: $mergeAssocTmp[$k] = $v;
1424: }
1425: }
1426: $dataKeys = array_keys($data);
1427: $mergeKeys = array_keys($merge[0]);
1428:
1429: if ($mergeKeys[0] === $dataKeys[0] || $mergeKeys === $dataKeys) {
1430: $data[$association][$association] = $merge[0][$association];
1431: } else {
1432: $diff = Hash::diff($dataAssocTmp, $mergeAssocTmp);
1433: $data[$association] = array_merge($merge[0][$association], $diff);
1434: }
1435: } elseif ($selfJoin && array_key_exists($association, $merge[0])) {
1436: $data[$association] = array_merge($data[$association], array($association => array()));
1437: }
1438: }
1439: }
1440: } else {
1441: if (isset($merge[0][$association]) && $merge[0][$association] === false) {
1442: if (!isset($data[$association])) {
1443: $data[$association] = array();
1444: }
1445: } else {
1446: foreach ($merge as $row) {
1447: $insert = array();
1448: if (count($row) === 1) {
1449: $insert = $row[$association];
1450: } elseif (isset($row[$association])) {
1451: $insert = array_merge($row[$association], $row);
1452: unset($insert[$association]);
1453: }
1454:
1455: if (empty($data[$association]) || (isset($data[$association]) && !in_array($insert, $data[$association], true))) {
1456: $data[$association][] = $insert;
1457: }
1458: }
1459: }
1460: }
1461: }
1462:
1463: 1464: 1465: 1466: 1467: 1468: 1469: 1470: 1471: 1472: 1473: 1474: 1475:
1476: public function generateAssociationQuery(Model $model, $linkModel, $type, $association, $assocData, &$queryData, $external, &$resultSet) {
1477: $queryData = $this->_scrubQueryData($queryData);
1478: $assocData = $this->_scrubQueryData($assocData);
1479: $modelAlias = $model->alias;
1480:
1481: if (empty($queryData['fields'])) {
1482: $queryData['fields'] = $this->fields($model, $modelAlias);
1483: } elseif (!empty($model->hasMany) && $model->recursive > -1) {
1484: $assocFields = $this->fields($model, $modelAlias, array("{$modelAlias}.{$model->primaryKey}"));
1485: $passedFields = $queryData['fields'];
1486: if (count($passedFields) === 1) {
1487: if (strpos($passedFields[0], $assocFields[0]) === false && !preg_match('/^[a-z]+\(/i', $passedFields[0])) {
1488: $queryData['fields'] = array_merge($passedFields, $assocFields);
1489: } else {
1490: $queryData['fields'] = $passedFields;
1491: }
1492: } else {
1493: $queryData['fields'] = array_merge($passedFields, $assocFields);
1494: }
1495: unset($assocFields, $passedFields);
1496: }
1497:
1498: if ($linkModel === null) {
1499: return $this->buildStatement(
1500: array(
1501: 'fields' => array_unique($queryData['fields']),
1502: 'table' => $this->fullTableName($model),
1503: 'alias' => $modelAlias,
1504: 'limit' => $queryData['limit'],
1505: 'offset' => $queryData['offset'],
1506: 'joins' => $queryData['joins'],
1507: 'conditions' => $queryData['conditions'],
1508: 'order' => $queryData['order'],
1509: 'group' => $queryData['group']
1510: ),
1511: $model
1512: );
1513: }
1514: if ($external && !empty($assocData['finderQuery'])) {
1515: return $assocData['finderQuery'];
1516: }
1517:
1518: $self = $model->name === $linkModel->name;
1519: $fields = array();
1520:
1521: if ($external || (in_array($type, array('hasOne', 'belongsTo')) && $assocData['fields'] !== false)) {
1522: $fields = $this->fields($linkModel, $association, $assocData['fields']);
1523: }
1524: if (empty($assocData['offset']) && !empty($assocData['page'])) {
1525: $assocData['offset'] = ($assocData['page'] - 1) * $assocData['limit'];
1526: }
1527:
1528: switch ($type) {
1529: case 'hasOne':
1530: case 'belongsTo':
1531: $conditions = $this->_mergeConditions(
1532: $assocData['conditions'],
1533: $this->getConstraint($type, $model, $linkModel, $association, array_merge($assocData, compact('external', 'self')))
1534: );
1535:
1536: if (!$self && $external) {
1537: foreach ($conditions as $key => $condition) {
1538: if (is_numeric($key) && strpos($condition, $modelAlias . '.') !== false) {
1539: unset($conditions[$key]);
1540: }
1541: }
1542: }
1543:
1544: if ($external) {
1545: $query = array_merge($assocData, array(
1546: 'conditions' => $conditions,
1547: 'table' => $this->fullTableName($linkModel),
1548: 'fields' => $fields,
1549: 'alias' => $association,
1550: 'group' => null
1551: ));
1552: } else {
1553: $join = array(
1554: 'table' => $linkModel,
1555: 'alias' => $association,
1556: 'type' => isset($assocData['type']) ? $assocData['type'] : 'LEFT',
1557: 'conditions' => trim($this->conditions($conditions, true, false, $model))
1558: );
1559: $queryData['fields'] = array_merge($queryData['fields'], $fields);
1560:
1561: if (!empty($assocData['order'])) {
1562: $queryData['order'][] = $assocData['order'];
1563: }
1564: if (!in_array($join, $queryData['joins'])) {
1565: $queryData['joins'][] = $join;
1566: }
1567: return true;
1568: }
1569: break;
1570: case 'hasMany':
1571: $assocData['fields'] = $this->fields($linkModel, $association, $assocData['fields']);
1572: if (!empty($assocData['foreignKey'])) {
1573: $assocData['fields'] = array_merge($assocData['fields'], $this->fields($linkModel, $association, array("{$association}.{$assocData['foreignKey']}")));
1574: }
1575: $query = array(
1576: 'conditions' => $this->_mergeConditions($this->getConstraint('hasMany', $model, $linkModel, $association, $assocData), $assocData['conditions']),
1577: 'fields' => array_unique($assocData['fields']),
1578: 'table' => $this->fullTableName($linkModel),
1579: 'alias' => $association,
1580: 'order' => $assocData['order'],
1581: 'limit' => $assocData['limit'],
1582: 'offset' => $assocData['offset'],
1583: 'group' => null
1584: );
1585: break;
1586: case 'hasAndBelongsToMany':
1587: $joinFields = array();
1588: $joinAssoc = null;
1589:
1590: if (isset($assocData['with']) && !empty($assocData['with'])) {
1591: $joinKeys = array($assocData['foreignKey'], $assocData['associationForeignKey']);
1592: list($with, $joinFields) = $model->joinModel($assocData['with'], $joinKeys);
1593:
1594: $joinTbl = $model->{$with};
1595: $joinAlias = $joinTbl;
1596:
1597: if (is_array($joinFields) && !empty($joinFields)) {
1598: $joinAssoc = $joinAlias = $model->{$with}->alias;
1599: $joinFields = $this->fields($model->{$with}, $joinAlias, $joinFields);
1600: } else {
1601: $joinFields = array();
1602: }
1603: } else {
1604: $joinTbl = $assocData['joinTable'];
1605: $joinAlias = $this->fullTableName($assocData['joinTable']);
1606: }
1607: $query = array(
1608: 'conditions' => $assocData['conditions'],
1609: 'limit' => $assocData['limit'],
1610: 'offset' => $assocData['offset'],
1611: 'table' => $this->fullTableName($linkModel),
1612: 'alias' => $association,
1613: 'fields' => array_merge($this->fields($linkModel, $association, $assocData['fields']), $joinFields),
1614: 'order' => $assocData['order'],
1615: 'group' => null,
1616: 'joins' => array(array(
1617: 'table' => $joinTbl,
1618: 'alias' => $joinAssoc,
1619: 'conditions' => $this->getConstraint('hasAndBelongsToMany', $model, $linkModel, $joinAlias, $assocData, $association)
1620: ))
1621: );
1622: break;
1623: }
1624: if (isset($query)) {
1625: return $this->buildStatement($query, $model);
1626: }
1627: return null;
1628: }
1629:
1630: 1631: 1632: 1633: 1634: 1635: 1636: 1637: 1638: 1639: 1640:
1641: public function getConstraint($type, $model, $linkModel, $alias, $assoc, $alias2 = null) {
1642: $assoc += array('external' => false, 'self' => false);
1643:
1644: if (empty($assoc['foreignKey'])) {
1645: return array();
1646: }
1647:
1648: switch (true) {
1649: case ($assoc['external'] && $type === 'hasOne'):
1650: return array("{$alias}.{$assoc['foreignKey']}" => '{$__cakeID__$}');
1651: case ($assoc['external'] && $type === 'belongsTo'):
1652: return array("{$alias}.{$linkModel->primaryKey}" => '{$__cakeForeignKey__$}');
1653: case (!$assoc['external'] && $type === 'hasOne'):
1654: return array("{$alias}.{$assoc['foreignKey']}" => $this->identifier("{$model->alias}.{$model->primaryKey}"));
1655: case (!$assoc['external'] && $type === 'belongsTo'):
1656: return array("{$model->alias}.{$assoc['foreignKey']}" => $this->identifier("{$alias}.{$linkModel->primaryKey}"));
1657: case ($type === 'hasMany'):
1658: return array("{$alias}.{$assoc['foreignKey']}" => array('{$__cakeID__$}'));
1659: case ($type === 'hasAndBelongsToMany'):
1660: return array(
1661: array("{$alias}.{$assoc['foreignKey']}" => '{$__cakeID__$}'),
1662: array("{$alias}.{$assoc['associationForeignKey']}" => $this->identifier("{$alias2}.{$linkModel->primaryKey}"))
1663: );
1664: }
1665: return array();
1666: }
1667:
1668: 1669: 1670: 1671: 1672: 1673: 1674: 1675:
1676: public function buildJoinStatement($join) {
1677: $data = array_merge(array(
1678: 'type' => null,
1679: 'alias' => null,
1680: 'table' => 'join_table',
1681: 'conditions' => array()
1682: ), $join);
1683:
1684: if (!empty($data['alias'])) {
1685: $data['alias'] = $this->alias . $this->name($data['alias']);
1686: }
1687: if (!empty($data['conditions'])) {
1688: $data['conditions'] = trim($this->conditions($data['conditions'], true, false));
1689: }
1690: if (!empty($data['table']) && (!is_string($data['table']) || strpos($data['table'], '(') !== 0)) {
1691: $data['table'] = $this->fullTableName($data['table']);
1692: }
1693: return $this->renderJoinStatement($data);
1694: }
1695:
1696: 1697: 1698: 1699: 1700: 1701: 1702: 1703:
1704: public function buildStatement($query, $model) {
1705: $query = array_merge($this->_queryDefaults, $query);
1706: if (!empty($query['joins'])) {
1707: $count = count($query['joins']);
1708: for ($i = 0; $i < $count; $i++) {
1709: if (is_array($query['joins'][$i])) {
1710: $query['joins'][$i] = $this->buildJoinStatement($query['joins'][$i]);
1711: }
1712: }
1713: }
1714: return $this->renderStatement('select', array(
1715: 'conditions' => $this->conditions($query['conditions'], true, true, $model),
1716: 'fields' => implode(', ', $query['fields']),
1717: 'table' => $query['table'],
1718: 'alias' => $this->alias . $this->name($query['alias']),
1719: 'order' => $this->order($query['order'], 'ASC', $model),
1720: 'limit' => $this->limit($query['limit'], $query['offset']),
1721: 'joins' => implode(' ', $query['joins']),
1722: 'group' => $this->group($query['group'], $model)
1723: ));
1724: }
1725:
1726: 1727: 1728: 1729: 1730: 1731:
1732: public function renderJoinStatement($data) {
1733: extract($data);
1734: return trim("{$type} JOIN {$table} {$alias} ON ({$conditions})");
1735: }
1736:
1737: 1738: 1739: 1740: 1741: 1742: 1743:
1744: public function renderStatement($type, $data) {
1745: extract($data);
1746: $aliases = null;
1747:
1748: switch (strtolower($type)) {
1749: case 'select':
1750: return "SELECT {$fields} FROM {$table} {$alias} {$joins} {$conditions} {$group} {$order} {$limit}";
1751: case 'create':
1752: return "INSERT INTO {$table} ({$fields}) VALUES ({$values})";
1753: case 'update':
1754: if (!empty($alias)) {
1755: $aliases = "{$this->alias}{$alias} {$joins} ";
1756: }
1757: return "UPDATE {$table} {$aliases}SET {$fields} {$conditions}";
1758: case 'delete':
1759: if (!empty($alias)) {
1760: $aliases = "{$this->alias}{$alias} {$joins} ";
1761: }
1762: return "DELETE {$alias} FROM {$table} {$aliases}{$conditions}";
1763: case 'schema':
1764: foreach (array('columns', 'indexes', 'tableParameters') as $var) {
1765: if (is_array(${$var})) {
1766: ${$var} = "\t" . implode(",\n\t", array_filter(${$var}));
1767: } else {
1768: ${$var} = '';
1769: }
1770: }
1771: if (trim($indexes) !== '') {
1772: $columns .= ',';
1773: }
1774: return "CREATE TABLE {$table} (\n{$columns}{$indexes}) {$tableParameters};";
1775: case 'alter':
1776: return;
1777: }
1778: }
1779:
1780: 1781: 1782: 1783: 1784: 1785: 1786:
1787: protected function _mergeConditions($query, $assoc) {
1788: if (empty($assoc)) {
1789: return $query;
1790: }
1791:
1792: if (is_array($query)) {
1793: return array_merge((array)$assoc, $query);
1794: }
1795:
1796: if (!empty($query)) {
1797: $query = array($query);
1798: if (is_array($assoc)) {
1799: $query = array_merge($query, $assoc);
1800: } else {
1801: $query[] = $assoc;
1802: }
1803: return $query;
1804: }
1805:
1806: return $assoc;
1807: }
1808:
1809: 1810: 1811: 1812: 1813: 1814: 1815: 1816: 1817: 1818:
1819: public function update(Model $model, $fields = array(), $values = null, $conditions = null) {
1820: if (!$values) {
1821: $combined = $fields;
1822: } else {
1823: $combined = array_combine($fields, $values);
1824: }
1825:
1826: $fields = implode(', ', $this->_prepareUpdateFields($model, $combined, empty($conditions)));
1827:
1828: $alias = $joins = null;
1829: $table = $this->fullTableName($model);
1830: $conditions = $this->_matchRecords($model, $conditions);
1831:
1832: if ($conditions === false) {
1833: return false;
1834: }
1835: $query = compact('table', 'alias', 'joins', 'fields', 'conditions');
1836:
1837: if (!$this->execute($this->renderStatement('update', $query))) {
1838: $model->onError();
1839: return false;
1840: }
1841: return true;
1842: }
1843:
1844: 1845: 1846: 1847: 1848: 1849: 1850: 1851: 1852:
1853: protected function _prepareUpdateFields(Model $model, $fields, $quoteValues = true, $alias = false) {
1854: $quotedAlias = $this->startQuote . $model->alias . $this->endQuote;
1855:
1856: $updates = array();
1857: foreach ($fields as $field => $value) {
1858: if ($alias && strpos($field, '.') === false) {
1859: $quoted = $model->escapeField($field);
1860: } elseif (!$alias && strpos($field, '.') !== false) {
1861: $quoted = $this->name(str_replace($quotedAlias . '.', '', str_replace(
1862: $model->alias . '.', '', $field
1863: )));
1864: } else {
1865: $quoted = $this->name($field);
1866: }
1867:
1868: if ($value === null) {
1869: $updates[] = $quoted . ' = NULL';
1870: continue;
1871: }
1872: $update = $quoted . ' = ';
1873:
1874: if ($quoteValues) {
1875: $update .= $this->value($value, $model->getColumnType($field));
1876: } elseif ($model->getColumnType($field) === 'boolean' && (is_int($value) || is_bool($value))) {
1877: $update .= $this->boolean($value, true);
1878: } elseif (!$alias) {
1879: $update .= str_replace($quotedAlias . '.', '', str_replace(
1880: $model->alias . '.', '', $value
1881: ));
1882: } else {
1883: $update .= $value;
1884: }
1885: $updates[] = $update;
1886: }
1887: return $updates;
1888: }
1889:
1890: 1891: 1892: 1893: 1894: 1895: 1896: 1897:
1898: public function delete(Model $model, $conditions = null) {
1899: $alias = $joins = null;
1900: $table = $this->fullTableName($model);
1901: $conditions = $this->_matchRecords($model, $conditions);
1902:
1903: if ($conditions === false) {
1904: return false;
1905: }
1906:
1907: if ($this->execute($this->renderStatement('delete', compact('alias', 'table', 'joins', 'conditions'))) === false) {
1908: $model->onError();
1909: return false;
1910: }
1911: return true;
1912: }
1913:
1914: 1915: 1916: 1917: 1918: 1919: 1920: 1921:
1922: protected function _matchRecords(Model $model, $conditions = null) {
1923: if ($conditions === true) {
1924: $conditions = $this->conditions(true);
1925: } elseif ($conditions === null) {
1926: $conditions = $this->conditions($this->defaultConditions($model, $conditions, false), true, true, $model);
1927: } else {
1928: $noJoin = true;
1929: foreach ($conditions as $field => $value) {
1930: $originalField = $field;
1931: if (strpos($field, '.') !== false) {
1932: list(, $field) = explode('.', $field);
1933: $field = ltrim($field, $this->startQuote);
1934: $field = rtrim($field, $this->endQuote);
1935: }
1936: if (!$model->hasField($field)) {
1937: $noJoin = false;
1938: break;
1939: }
1940: if ($field !== $originalField) {
1941: $conditions[$field] = $value;
1942: unset($conditions[$originalField]);
1943: }
1944: }
1945: if ($noJoin === true) {
1946: return $this->conditions($conditions);
1947: }
1948: $idList = $model->find('all', array(
1949: 'fields' => "{$model->alias}.{$model->primaryKey}",
1950: 'conditions' => $conditions
1951: ));
1952:
1953: if (empty($idList)) {
1954: return false;
1955: }
1956: $conditions = $this->conditions(array(
1957: $model->primaryKey => Hash::extract($idList, "{n}.{$model->alias}.{$model->primaryKey}")
1958: ));
1959: }
1960: return $conditions;
1961: }
1962:
1963: 1964: 1965: 1966: 1967: 1968:
1969: protected function _getJoins(Model $model) {
1970: $join = array();
1971: $joins = array_merge($model->getAssociated('hasOne'), $model->getAssociated('belongsTo'));
1972:
1973: foreach ($joins as $assoc) {
1974: if (isset($model->{$assoc}) && $model->useDbConfig == $model->{$assoc}->useDbConfig && $model->{$assoc}->getDataSource()) {
1975: $assocData = $model->getAssociated($assoc);
1976: $join[] = $this->buildJoinStatement(array(
1977: 'table' => $model->{$assoc},
1978: 'alias' => $assoc,
1979: 'type' => isset($assocData['type']) ? $assocData['type'] : 'LEFT',
1980: 'conditions' => trim($this->conditions(
1981: $this->_mergeConditions($assocData['conditions'], $this->getConstraint($assocData['association'], $model, $model->{$assoc}, $assoc, $assocData)),
1982: true, false, $model
1983: ))
1984: ));
1985: }
1986: }
1987: return $join;
1988: }
1989:
1990: 1991: 1992: 1993: 1994: 1995: 1996: 1997:
1998: public function calculate(Model $model, $func, $params = array()) {
1999: $params = (array)$params;
2000:
2001: switch (strtolower($func)) {
2002: case 'count':
2003: if (!isset($params[0])) {
2004: $params[0] = '*';
2005: }
2006: if (!isset($params[1])) {
2007: $params[1] = 'count';
2008: }
2009: if (is_object($model) && $model->isVirtualField($params[0])) {
2010: $arg = $this->_quoteFields($model->getVirtualField($params[0]));
2011: } else {
2012: $arg = $this->name($params[0]);
2013: }
2014: return 'COUNT(' . $arg . ') AS ' . $this->name($params[1]);
2015: case 'max':
2016: case 'min':
2017: if (!isset($params[1])) {
2018: $params[1] = $params[0];
2019: }
2020: if (is_object($model) && $model->isVirtualField($params[0])) {
2021: $arg = $this->_quoteFields($model->getVirtualField($params[0]));
2022: } else {
2023: $arg = $this->name($params[0]);
2024: }
2025: return strtoupper($func) . '(' . $arg . ') AS ' . $this->name($params[1]);
2026: }
2027: }
2028:
2029: 2030: 2031: 2032: 2033: 2034: 2035:
2036: public function truncate($table) {
2037: return $this->execute('TRUNCATE TABLE ' . $this->fullTableName($table));
2038: }
2039:
2040: 2041: 2042: 2043: 2044:
2045: public function nestedTransactionSupported() {
2046: return false;
2047: }
2048:
2049: 2050: 2051: 2052: 2053: 2054: 2055:
2056: public function begin() {
2057: if ($this->_transactionStarted) {
2058: if ($this->nestedTransactionSupported()) {
2059: return $this->_beginNested();
2060: }
2061: $this->_transactionNesting++;
2062: return $this->_transactionStarted;
2063: }
2064:
2065: $this->_transactionNesting = 0;
2066: if ($this->fullDebug) {
2067: $this->logQuery('BEGIN');
2068: }
2069: return $this->_transactionStarted = $this->_connection->beginTransaction();
2070: }
2071:
2072: 2073: 2074: 2075: 2076:
2077: protected function _beginNested() {
2078: $query = 'SAVEPOINT LEVEL' . ++$this->_transactionNesting;
2079: if ($this->fullDebug) {
2080: $this->logQuery($query);
2081: }
2082: $this->_connection->exec($query);
2083: return true;
2084: }
2085:
2086: 2087: 2088: 2089: 2090: 2091: 2092:
2093: public function commit() {
2094: if (!$this->_transactionStarted) {
2095: return false;
2096: }
2097:
2098: if ($this->_transactionNesting === 0) {
2099: if ($this->fullDebug) {
2100: $this->logQuery('COMMIT');
2101: }
2102: $this->_transactionStarted = false;
2103: return $this->_connection->commit();
2104: }
2105:
2106: if ($this->nestedTransactionSupported()) {
2107: return $this->_commitNested();
2108: }
2109:
2110: $this->_transactionNesting--;
2111: return true;
2112: }
2113:
2114: 2115: 2116: 2117: 2118:
2119: protected function _commitNested() {
2120: $query = 'RELEASE SAVEPOINT LEVEL' . $this->_transactionNesting--;
2121: if ($this->fullDebug) {
2122: $this->logQuery($query);
2123: }
2124: $this->_connection->exec($query);
2125: return true;
2126: }
2127:
2128: 2129: 2130: 2131: 2132: 2133: 2134:
2135: public function rollback() {
2136: if (!$this->_transactionStarted) {
2137: return false;
2138: }
2139:
2140: if ($this->_transactionNesting === 0) {
2141: if ($this->fullDebug) {
2142: $this->logQuery('ROLLBACK');
2143: }
2144: $this->_transactionStarted = false;
2145: return $this->_connection->rollBack();
2146: }
2147:
2148: if ($this->nestedTransactionSupported()) {
2149: return $this->_rollbackNested();
2150: }
2151:
2152: $this->_transactionNesting--;
2153: return true;
2154: }
2155:
2156: 2157: 2158: 2159: 2160:
2161: protected function _rollbackNested() {
2162: $query = 'ROLLBACK TO SAVEPOINT LEVEL' . $this->_transactionNesting--;
2163: if ($this->fullDebug) {
2164: $this->logQuery($query);
2165: }
2166: $this->_connection->exec($query);
2167: return true;
2168: }
2169:
2170: 2171: 2172: 2173: 2174: 2175:
2176: public function lastInsertId($source = null) {
2177: return $this->_connection->lastInsertId();
2178: }
2179:
2180: 2181: 2182: 2183: 2184: 2185: 2186: 2187: 2188: 2189: 2190: 2191: 2192: 2193:
2194: public function defaultConditions(Model $model, $conditions, $useAlias = true) {
2195: if (!empty($conditions)) {
2196: return $conditions;
2197: }
2198: $exists = $model->exists();
2199: if (!$exists && $conditions !== null) {
2200: return false;
2201: } elseif (!$exists) {
2202: return null;
2203: }
2204: $alias = $model->alias;
2205:
2206: if (!$useAlias) {
2207: $alias = $this->fullTableName($model, false);
2208: }
2209: return array("{$alias}.{$model->primaryKey}" => $model->getID());
2210: }
2211:
2212: 2213: 2214: 2215: 2216: 2217: 2218: 2219:
2220: public function resolveKey(Model $model, $key, $assoc = null) {
2221: if (strpos('.', $key) !== false) {
2222: return $this->name($model->alias) . '.' . $this->name($key);
2223: }
2224: return $key;
2225: }
2226:
2227: 2228: 2229: 2230: 2231: 2232:
2233: protected function _scrubQueryData($data) {
2234: static $base = null;
2235: if ($base === null) {
2236: $base = array_fill_keys(array('conditions', 'fields', 'joins', 'order', 'limit', 'offset', 'group'), array());
2237: $base['callbacks'] = null;
2238: }
2239: return (array)$data + $base;
2240: }
2241:
2242: 2243: 2244: 2245: 2246: 2247: 2248: 2249:
2250: protected function _constructVirtualFields(Model $model, $alias, $fields) {
2251: $virtual = array();
2252: foreach ($fields as $field) {
2253: $virtualField = $this->name($alias . $this->virtualFieldSeparator . $field);
2254: $expression = $this->_quoteFields($model->getVirtualField($field));
2255: $virtual[] = '(' . $expression . ") {$this->alias} {$virtualField}";
2256: }
2257: return $virtual;
2258: }
2259:
2260: 2261: 2262: 2263: 2264: 2265: 2266: 2267: 2268:
2269: public function fields(Model $model, $alias = null, $fields = array(), $quote = true) {
2270: if (empty($alias)) {
2271: $alias = $model->alias;
2272: }
2273: $virtualFields = $model->getVirtualField();
2274: $cacheKey = array(
2275: $alias,
2276: get_class($model),
2277: $model->alias,
2278: $virtualFields,
2279: $fields,
2280: $quote,
2281: ConnectionManager::getSourceName($this),
2282: $model->table
2283: );
2284: $cacheKey = md5(serialize($cacheKey));
2285: if ($return = $this->cacheMethod(__FUNCTION__, $cacheKey)) {
2286: return $return;
2287: }
2288: $allFields = empty($fields);
2289: if ($allFields) {
2290: $fields = array_keys($model->schema());
2291: } elseif (!is_array($fields)) {
2292: $fields = String::tokenize($fields);
2293: }
2294: $fields = array_values(array_filter($fields));
2295: $allFields = $allFields || in_array('*', $fields) || in_array($model->alias . '.*', $fields);
2296:
2297: $virtual = array();
2298: if (!empty($virtualFields)) {
2299: $virtualKeys = array_keys($virtualFields);
2300: foreach ($virtualKeys as $field) {
2301: $virtualKeys[] = $model->alias . '.' . $field;
2302: }
2303: $virtual = ($allFields) ? $virtualKeys : array_intersect($virtualKeys, $fields);
2304: foreach ($virtual as $i => $field) {
2305: if (strpos($field, '.') !== false) {
2306: $virtual[$i] = str_replace($model->alias . '.', '', $field);
2307: }
2308: $fields = array_diff($fields, array($field));
2309: }
2310: $fields = array_values($fields);
2311: }
2312: if (!$quote) {
2313: if (!empty($virtual)) {
2314: $fields = array_merge($fields, $this->_constructVirtualFields($model, $alias, $virtual));
2315: }
2316: return $fields;
2317: }
2318: $count = count($fields);
2319:
2320: if ($count >= 1 && !in_array($fields[0], array('*', 'COUNT(*)'))) {
2321: for ($i = 0; $i < $count; $i++) {
2322: if (is_string($fields[$i]) && in_array($fields[$i], $virtual)) {
2323: unset($fields[$i]);
2324: continue;
2325: }
2326: if (is_object($fields[$i]) && isset($fields[$i]->type) && $fields[$i]->type === 'expression') {
2327: $fields[$i] = $fields[$i]->value;
2328: } elseif (preg_match('/^\(.*\)\s' . $this->alias . '.*/i', $fields[$i])) {
2329: continue;
2330: } elseif (!preg_match('/^.+\\(.*\\)/', $fields[$i])) {
2331: $prepend = '';
2332:
2333: if (strpos($fields[$i], 'DISTINCT') !== false) {
2334: $prepend = 'DISTINCT ';
2335: $fields[$i] = trim(str_replace('DISTINCT', '', $fields[$i]));
2336: }
2337: $dot = strpos($fields[$i], '.');
2338:
2339: if ($dot === false) {
2340: $prefix = !(
2341: strpos($fields[$i], ' ') !== false ||
2342: strpos($fields[$i], '(') !== false
2343: );
2344: $fields[$i] = $this->name(($prefix ? $alias . '.' : '') . $fields[$i]);
2345: } else {
2346: if (strpos($fields[$i], ',') === false) {
2347: $build = explode('.', $fields[$i]);
2348: if (!Hash::numeric($build)) {
2349: $fields[$i] = $this->name(implode('.', $build));
2350: }
2351: }
2352: }
2353: $fields[$i] = $prepend . $fields[$i];
2354: } elseif (preg_match('/\(([\.\w]+)\)/', $fields[$i], $field)) {
2355: if (isset($field[1])) {
2356: if (strpos($field[1], '.') === false) {
2357: $field[1] = $this->name($alias . '.' . $field[1]);
2358: } else {
2359: $field[0] = explode('.', $field[1]);
2360: if (!Hash::numeric($field[0])) {
2361: $field[0] = implode('.', array_map(array(&$this, 'name'), $field[0]));
2362: $fields[$i] = preg_replace('/\(' . $field[1] . '\)/', '(' . $field[0] . ')', $fields[$i], 1);
2363: }
2364: }
2365: }
2366: }
2367: }
2368: }
2369: if (!empty($virtual)) {
2370: $fields = array_merge($fields, $this->_constructVirtualFields($model, $alias, $virtual));
2371: }
2372: return $this->cacheMethod(__FUNCTION__, $cacheKey, array_unique($fields));
2373: }
2374:
2375: 2376: 2377: 2378: 2379: 2380: 2381: 2382: 2383: 2384: 2385: 2386: 2387: 2388: 2389:
2390: public function conditions($conditions, $quoteValues = true, $where = true, $model = null) {
2391: $clause = $out = '';
2392:
2393: if ($where) {
2394: $clause = ' WHERE ';
2395: }
2396:
2397: if (is_array($conditions) && !empty($conditions)) {
2398: $out = $this->conditionKeysToString($conditions, $quoteValues, $model);
2399:
2400: if (empty($out)) {
2401: return $clause . ' 1 = 1';
2402: }
2403: return $clause . implode(' AND ', $out);
2404: }
2405: if (is_bool($conditions)) {
2406: return $clause . (int)$conditions . ' = 1';
2407: }
2408:
2409: if (empty($conditions) || trim($conditions) === '') {
2410: return $clause . '1 = 1';
2411: }
2412: $clauses = '/^WHERE\\x20|^GROUP\\x20BY\\x20|^HAVING\\x20|^ORDER\\x20BY\\x20/i';
2413:
2414: if (preg_match($clauses, $conditions)) {
2415: $clause = '';
2416: }
2417: $conditions = $this->_quoteFields($conditions);
2418: return $clause . $conditions;
2419: }
2420:
2421: 2422: 2423: 2424: 2425: 2426: 2427: 2428:
2429: public function conditionKeysToString($conditions, $quoteValues = true, $model = null) {
2430: $out = array();
2431: $data = $columnType = null;
2432: $bool = array('and', 'or', 'not', 'and not', 'or not', 'xor', '||', '&&');
2433:
2434: foreach ($conditions as $key => $value) {
2435: $join = ' AND ';
2436: $not = null;
2437:
2438: if (is_array($value)) {
2439: $valueInsert = (
2440: !empty($value) &&
2441: (substr_count($key, '?') === count($value) || substr_count($key, ':') === count($value))
2442: );
2443: }
2444:
2445: if (is_numeric($key) && empty($value)) {
2446: continue;
2447: } elseif (is_numeric($key) && is_string($value)) {
2448: $out[] = $this->_quoteFields($value);
2449: } elseif ((is_numeric($key) && is_array($value)) || in_array(strtolower(trim($key)), $bool)) {
2450: if (in_array(strtolower(trim($key)), $bool)) {
2451: $join = ' ' . strtoupper($key) . ' ';
2452: } else {
2453: $key = $join;
2454: }
2455: $value = $this->conditionKeysToString($value, $quoteValues, $model);
2456:
2457: if (strpos($join, 'NOT') !== false) {
2458: if (strtoupper(trim($key)) === 'NOT') {
2459: $key = 'AND ' . trim($key);
2460: }
2461: $not = 'NOT ';
2462: }
2463:
2464: if (empty($value)) {
2465: continue;
2466: }
2467:
2468: if (empty($value[1])) {
2469: if ($not) {
2470: $out[] = $not . '(' . $value[0] . ')';
2471: } else {
2472: $out[] = $value[0];
2473: }
2474: } else {
2475: $out[] = '(' . $not . '(' . implode(') ' . strtoupper($key) . ' (', $value) . '))';
2476: }
2477: } else {
2478: if (is_object($value) && isset($value->type)) {
2479: if ($value->type === 'identifier') {
2480: $data .= $this->name($key) . ' = ' . $this->name($value->value);
2481: } elseif ($value->type === 'expression') {
2482: if (is_numeric($key)) {
2483: $data .= $value->value;
2484: } else {
2485: $data .= $this->name($key) . ' = ' . $value->value;
2486: }
2487: }
2488: } elseif (is_array($value) && !empty($value) && !$valueInsert) {
2489: $keys = array_keys($value);
2490: if ($keys === array_values($keys)) {
2491: $count = count($value);
2492: if ($count === 1 && !preg_match("/\s+NOT$/", $key)) {
2493: $data = $this->_quoteFields($key) . ' = (';
2494: if ($quoteValues) {
2495: if (is_object($model)) {
2496: $columnType = $model->getColumnType($key);
2497: }
2498: $data .= implode(', ', $this->value($value, $columnType));
2499: }
2500: $data .= ')';
2501: } else {
2502: $data = $this->_parseKey($model, $key, $value);
2503: }
2504: } else {
2505: $ret = $this->conditionKeysToString($value, $quoteValues, $model);
2506: if (count($ret) > 1) {
2507: $data = '(' . implode(') AND (', $ret) . ')';
2508: } elseif (isset($ret[0])) {
2509: $data = $ret[0];
2510: }
2511: }
2512: } elseif (is_numeric($key) && !empty($value)) {
2513: $data = $this->_quoteFields($value);
2514: } else {
2515: $data = $this->_parseKey($model, trim($key), $value);
2516: }
2517:
2518: if ($data) {
2519: $out[] = $data;
2520: $data = null;
2521: }
2522: }
2523: }
2524: return $out;
2525: }
2526:
2527: 2528: 2529: 2530: 2531: 2532: 2533: 2534: 2535:
2536: protected function _parseKey($model, $key, $value) {
2537: $operatorMatch = '/^(((' . implode(')|(', $this->_sqlOps);
2538: $operatorMatch .= ')\\x20?)|<[>=]?(?![^>]+>)\\x20?|[>=!]{1,3}(?!<)\\x20?)/is';
2539: $bound = (strpos($key, '?') !== false || (is_array($value) && strpos($key, ':') !== false));
2540:
2541: if (strpos($key, ' ') === false) {
2542: $operator = '=';
2543: } else {
2544: list($key, $operator) = explode(' ', trim($key), 2);
2545:
2546: if (!preg_match($operatorMatch, trim($operator)) && strpos($operator, ' ') !== false) {
2547: $key = $key . ' ' . $operator;
2548: $split = strrpos($key, ' ');
2549: $operator = substr($key, $split);
2550: $key = substr($key, 0, $split);
2551: }
2552: }
2553:
2554: $virtual = false;
2555: if (is_object($model) && $model->isVirtualField($key)) {
2556: $key = $this->_quoteFields($model->getVirtualField($key));
2557: $virtual = true;
2558: }
2559:
2560: $type = is_object($model) ? $model->getColumnType($key) : null;
2561: $null = $value === null || (is_array($value) && empty($value));
2562:
2563: if (strtolower($operator) === 'not') {
2564: $data = $this->conditionKeysToString(
2565: array($operator => array($key => $value)), true, $model
2566: );
2567: return $data[0];
2568: }
2569:
2570: $value = $this->value($value, $type);
2571:
2572: if (!$virtual && $key !== '?') {
2573: $isKey = (
2574: strpos($key, '(') !== false ||
2575: strpos($key, ')') !== false ||
2576: strpos($key, '|') !== false
2577: );
2578: $key = $isKey ? $this->_quoteFields($key) : $this->name($key);
2579: }
2580:
2581: if ($bound) {
2582: return String::insert($key . ' ' . trim($operator), $value);
2583: }
2584:
2585: if (!preg_match($operatorMatch, trim($operator))) {
2586: $operator .= is_array($value) ? ' IN' : ' =';
2587: }
2588: $operator = trim($operator);
2589:
2590: if (is_array($value)) {
2591: $value = implode(', ', $value);
2592:
2593: switch ($operator) {
2594: case '=':
2595: $operator = 'IN';
2596: break;
2597: case '!=':
2598: case '<>':
2599: $operator = 'NOT IN';
2600: break;
2601: }
2602: $value = "({$value})";
2603: } elseif ($null || $value === 'NULL') {
2604: switch ($operator) {
2605: case '=':
2606: $operator = 'IS';
2607: break;
2608: case '!=':
2609: case '<>':
2610: $operator = 'IS NOT';
2611: break;
2612: }
2613: }
2614: if ($virtual) {
2615: return "({$key}) {$operator} {$value}";
2616: }
2617: return "{$key} {$operator} {$value}";
2618: }
2619:
2620: 2621: 2622: 2623: 2624: 2625:
2626: protected function _quoteFields($conditions) {
2627: $start = $end = null;
2628: $original = $conditions;
2629:
2630: if (!empty($this->startQuote)) {
2631: $start = preg_quote($this->startQuote);
2632: }
2633: if (!empty($this->endQuote)) {
2634: $end = preg_quote($this->endQuote);
2635: }
2636: $conditions = str_replace(array($start, $end), '', $conditions);
2637: $conditions = preg_replace_callback(
2638: '/(?:[\'\"][^\'\"\\\]*(?:\\\.[^\'\"\\\]*)*[\'\"])|([a-z0-9_][a-z0-9\\-_]*\\.[a-z0-9_][a-z0-9_\\-]*)/i',
2639: array(&$this, '_quoteMatchedField'),
2640: $conditions
2641: );
2642: if ($conditions !== null) {
2643: return $conditions;
2644: }
2645: return $original;
2646: }
2647:
2648: 2649: 2650: 2651: 2652: 2653:
2654: protected function _quoteMatchedField($match) {
2655: if (is_numeric($match[0])) {
2656: return $match[0];
2657: }
2658: return $this->name($match[0]);
2659: }
2660:
2661: 2662: 2663: 2664: 2665: 2666: 2667:
2668: public function limit($limit, $offset = null) {
2669: if ($limit) {
2670: $rt = ' LIMIT';
2671:
2672: if ($offset) {
2673: $rt .= sprintf(' %u,', $offset);
2674: }
2675:
2676: $rt .= sprintf(' %u', $limit);
2677: return $rt;
2678: }
2679: return null;
2680: }
2681:
2682: 2683: 2684: 2685: 2686: 2687: 2688: 2689:
2690: public function order($keys, $direction = 'ASC', $model = null) {
2691: if (!is_array($keys)) {
2692: $keys = array($keys);
2693: }
2694: $keys = array_filter($keys);
2695: $result = array();
2696: while (!empty($keys)) {
2697: list($key, $dir) = each($keys);
2698: array_shift($keys);
2699:
2700: if (is_numeric($key)) {
2701: $key = $dir;
2702: $dir = $direction;
2703: }
2704:
2705: if (is_string($key) && strpos($key, ',') !== false && !preg_match('/\(.+\,.+\)/', $key)) {
2706: $key = array_map('trim', explode(',', $key));
2707: }
2708: if (is_array($key)) {
2709:
2710: $key = array_reverse($key, true);
2711: foreach ($key as $k => $v) {
2712: if (is_numeric($k)) {
2713: array_unshift($keys, $v);
2714: } else {
2715: $keys = array($k => $v) + $keys;
2716: }
2717: }
2718: continue;
2719: } elseif (is_object($key) && isset($key->type) && $key->type === 'expression') {
2720: $result[] = $key->value;
2721: continue;
2722: }
2723:
2724: if (preg_match('/\\x20(ASC|DESC).*/i', $key, $_dir)) {
2725: $dir = $_dir[0];
2726: $key = preg_replace('/\\x20(ASC|DESC).*/i', '', $key);
2727: }
2728:
2729: $key = trim($key);
2730:
2731: if (is_object($model) && $model->isVirtualField($key)) {
2732: $key = '(' . $this->_quoteFields($model->getVirtualField($key)) . ')';
2733: }
2734: list($alias, $field) = pluginSplit($key);
2735: if (is_object($model) && $alias !== $model->alias && is_object($model->{$alias}) && $model->{$alias}->isVirtualField($key)) {
2736: $key = '(' . $this->_quoteFields($model->{$alias}->getVirtualField($key)) . ')';
2737: }
2738:
2739: if (strpos($key, '.')) {
2740: $key = preg_replace_callback('/([a-zA-Z0-9_-]{1,})\\.([a-zA-Z0-9_-]{1,})/', array(&$this, '_quoteMatchedField'), $key);
2741: }
2742: if (!preg_match('/\s/', $key) && strpos($key, '.') === false) {
2743: $key = $this->name($key);
2744: }
2745: $key .= ' ' . trim($dir);
2746: $result[] = $key;
2747: }
2748: if (!empty($result)) {
2749: return ' ORDER BY ' . implode(', ', $result);
2750: }
2751: return '';
2752: }
2753:
2754: 2755: 2756: 2757: 2758: 2759: 2760:
2761: public function group($group, $model = null) {
2762: if ($group) {
2763: if (!is_array($group)) {
2764: $group = array($group);
2765: }
2766: foreach ($group as $index => $key) {
2767: if (is_object($model) && $model->isVirtualField($key)) {
2768: $group[$index] = '(' . $model->getVirtualField($key) . ')';
2769: }
2770: }
2771: $group = implode(', ', $group);
2772: return ' GROUP BY ' . $this->_quoteFields($group);
2773: }
2774: return null;
2775: }
2776:
2777: 2778: 2779: 2780: 2781:
2782: public function close() {
2783: $this->disconnect();
2784: }
2785:
2786: 2787: 2788: 2789: 2790: 2791: 2792:
2793: public function hasAny(Model $Model, $sql) {
2794: $sql = $this->conditions($sql);
2795: $table = $this->fullTableName($Model);
2796: $alias = $this->alias . $this->name($Model->alias);
2797: $where = $sql ? "{$sql}" : ' WHERE 1 = 1';
2798: $id = $Model->escapeField();
2799:
2800: $out = $this->fetchRow("SELECT COUNT({$id}) {$this->alias}count FROM {$table} {$alias}{$where}");
2801:
2802: if (is_array($out)) {
2803: return $out[0]['count'];
2804: }
2805: return false;
2806: }
2807:
2808: 2809: 2810: 2811: 2812: 2813:
2814: public function length($real) {
2815: if (!preg_match_all('/([\w\s]+)(?:\((\d+)(?:,(\d+))?\))?(\sunsigned)?(\szerofill)?/', $real, $result)) {
2816: $col = str_replace(array(')', 'unsigned'), '', $real);
2817: $limit = null;
2818:
2819: if (strpos($col, '(') !== false) {
2820: list($col, $limit) = explode('(', $col);
2821: }
2822: if ($limit !== null) {
2823: return intval($limit);
2824: }
2825: return null;
2826: }
2827:
2828: $types = array(
2829: 'int' => 1, 'tinyint' => 1, 'smallint' => 1, 'mediumint' => 1, 'integer' => 1, 'bigint' => 1
2830: );
2831:
2832: list($real, $type, $length, $offset, $sign, $zerofill) = $result;
2833: $typeArr = $type;
2834: $type = $type[0];
2835: $length = $length[0];
2836: $offset = $offset[0];
2837:
2838: $isFloat = in_array($type, array('dec', 'decimal', 'float', 'numeric', 'double'));
2839: if ($isFloat && $offset) {
2840: return $length . ',' . $offset;
2841: }
2842:
2843: if (($real[0] == $type) && (count($real) === 1)) {
2844: return null;
2845: }
2846:
2847: if (isset($types[$type])) {
2848: $length += $types[$type];
2849: if (!empty($sign)) {
2850: $length--;
2851: }
2852: } elseif (in_array($type, array('enum', 'set'))) {
2853: $length = 0;
2854: foreach ($typeArr as $key => $enumValue) {
2855: if ($key === 0) {
2856: continue;
2857: }
2858: $tmpLength = strlen($enumValue);
2859: if ($tmpLength > $length) {
2860: $length = $tmpLength;
2861: }
2862: }
2863: }
2864: return intval($length);
2865: }
2866:
2867: 2868: 2869: 2870: 2871: 2872: 2873:
2874: public function boolean($data, $quote = false) {
2875: if ($quote) {
2876: return !empty($data) ? '1' : '0';
2877: }
2878: return !empty($data);
2879: }
2880:
2881: 2882: 2883: 2884: 2885: 2886: 2887: 2888: 2889: 2890:
2891: public function insertMulti($table, $fields, $values) {
2892: $table = $this->fullTableName($table);
2893: $holder = implode(',', array_fill(0, count($fields), '?'));
2894: $fields = implode(', ', array_map(array(&$this, 'name'), $fields));
2895:
2896: $pdoMap = array(
2897: 'integer' => PDO::PARAM_INT,
2898: 'float' => PDO::PARAM_STR,
2899: 'boolean' => PDO::PARAM_BOOL,
2900: 'string' => PDO::PARAM_STR,
2901: 'text' => PDO::PARAM_STR
2902: );
2903: $columnMap = array();
2904:
2905: $sql = "INSERT INTO {$table} ({$fields}) VALUES ({$holder})";
2906: $statement = $this->_connection->prepare($sql);
2907: $this->begin();
2908:
2909: foreach ($values[key($values)] as $key => $val) {
2910: $type = $this->introspectType($val);
2911: $columnMap[$key] = $pdoMap[$type];
2912: }
2913:
2914: foreach ($values as $value) {
2915: $i = 1;
2916: foreach ($value as $col => $val) {
2917: $statement->bindValue($i, $val, $columnMap[$col]);
2918: $i += 1;
2919: }
2920: $statement->execute();
2921: $statement->closeCursor();
2922:
2923: if ($this->fullDebug) {
2924: $this->logQuery($sql, $value);
2925: }
2926: }
2927: return $this->commit();
2928: }
2929:
2930: 2931: 2932: 2933: 2934: 2935: 2936: 2937: 2938: 2939:
2940: public function resetSequence($table, $column) {
2941: }
2942:
2943: 2944: 2945: 2946: 2947: 2948:
2949: public function index($model) {
2950: return array();
2951: }
2952:
2953: 2954: 2955: 2956: 2957: 2958: 2959: 2960:
2961: public function createSchema($schema, $tableName = null) {
2962: if (!is_a($schema, 'CakeSchema')) {
2963: trigger_error(__d('cake_dev', 'Invalid schema object'), E_USER_WARNING);
2964: return null;
2965: }
2966: $out = '';
2967:
2968: foreach ($schema->tables as $curTable => $columns) {
2969: if (!$tableName || $tableName == $curTable) {
2970: $cols = $indexes = $tableParameters = array();
2971: $primary = null;
2972: $table = $this->fullTableName($curTable);
2973:
2974: $primaryCount = 0;
2975: foreach ($columns as $col) {
2976: if (isset($col['key']) && $col['key'] === 'primary') {
2977: $primaryCount++;
2978: }
2979: }
2980:
2981: foreach ($columns as $name => $col) {
2982: if (is_string($col)) {
2983: $col = array('type' => $col);
2984: }
2985: $isPrimary = isset($col['key']) && $col['key'] === 'primary';
2986:
2987: if ($isPrimary && $primaryCount > 1) {
2988: unset($col['key']);
2989: $isPrimary = false;
2990: }
2991: if ($isPrimary) {
2992: $primary = $name;
2993: }
2994: if ($name !== 'indexes' && $name !== 'tableParameters') {
2995: $col['name'] = $name;
2996: if (!isset($col['type'])) {
2997: $col['type'] = 'string';
2998: }
2999: $cols[] = $this->buildColumn($col);
3000: } elseif ($name === 'indexes') {
3001: $indexes = array_merge($indexes, $this->buildIndex($col, $table));
3002: } elseif ($name === 'tableParameters') {
3003: $tableParameters = array_merge($tableParameters, $this->buildTableParameters($col, $table));
3004: }
3005: }
3006: if (!isset($columns['indexes']['PRIMARY']) && !empty($primary)) {
3007: $col = array('PRIMARY' => array('column' => $primary, 'unique' => 1));
3008: $indexes = array_merge($indexes, $this->buildIndex($col, $table));
3009: }
3010: $columns = $cols;
3011: $out .= $this->renderStatement('schema', compact('table', 'columns', 'indexes', 'tableParameters')) . "\n\n";
3012: }
3013: }
3014: return $out;
3015: }
3016:
3017: 3018: 3019: 3020: 3021: 3022: 3023:
3024: public function alterSchema($compare, $table = null) {
3025: return false;
3026: }
3027:
3028: 3029: 3030: 3031: 3032: 3033: 3034: 3035:
3036: public function dropSchema(CakeSchema $schema, $table = null) {
3037: $out = '';
3038:
3039: if ($table && array_key_exists($table, $schema->tables)) {
3040: return $this->_dropTable($table) . "\n";
3041: } elseif ($table) {
3042: return $out;
3043: }
3044:
3045: foreach (array_keys($schema->tables) as $curTable) {
3046: $out .= $this->_dropTable($curTable) . "\n";
3047: }
3048: return $out;
3049: }
3050:
3051: 3052: 3053: 3054: 3055: 3056:
3057: protected function _dropTable($table) {
3058: return 'DROP TABLE ' . $this->fullTableName($table) . ";";
3059: }
3060:
3061: 3062: 3063: 3064: 3065: 3066: 3067:
3068: public function buildColumn($column) {
3069: $name = $type = null;
3070: extract(array_merge(array('null' => true), $column));
3071:
3072: if (empty($name) || empty($type)) {
3073: trigger_error(__d('cake_dev', 'Column name or type not defined in schema'), E_USER_WARNING);
3074: return null;
3075: }
3076:
3077: if (!isset($this->columns[$type])) {
3078: trigger_error(__d('cake_dev', 'Column type %s does not exist', $type), E_USER_WARNING);
3079: return null;
3080: }
3081:
3082: $real = $this->columns[$type];
3083: $out = $this->name($name) . ' ' . $real['name'];
3084:
3085: if (isset($column['length'])) {
3086: $length = $column['length'];
3087: } elseif (isset($column['limit'])) {
3088: $length = $column['limit'];
3089: } elseif (isset($real['length'])) {
3090: $length = $real['length'];
3091: } elseif (isset($real['limit'])) {
3092: $length = $real['limit'];
3093: }
3094: if (isset($length)) {
3095: $out .= '(' . $length . ')';
3096: }
3097:
3098: if (($column['type'] === 'integer' || $column['type'] === 'float') && isset($column['default']) && $column['default'] === '') {
3099: $column['default'] = null;
3100: }
3101: $out = $this->_buildFieldParameters($out, $column, 'beforeDefault');
3102:
3103: if (isset($column['key']) && $column['key'] === 'primary' && ($type === 'integer' || $type === 'biginteger')) {
3104: $out .= ' ' . $this->columns['primary_key']['name'];
3105: } elseif (isset($column['key']) && $column['key'] === 'primary') {
3106: $out .= ' NOT NULL';
3107: } elseif (isset($column['default']) && isset($column['null']) && $column['null'] === false) {
3108: $out .= ' DEFAULT ' . $this->value($column['default'], $type) . ' NOT NULL';
3109: } elseif (isset($column['default'])) {
3110: $out .= ' DEFAULT ' . $this->value($column['default'], $type);
3111: } elseif ($type !== 'timestamp' && !empty($column['null'])) {
3112: $out .= ' DEFAULT NULL';
3113: } elseif ($type === 'timestamp' && !empty($column['null'])) {
3114: $out .= ' NULL';
3115: } elseif (isset($column['null']) && $column['null'] === false) {
3116: $out .= ' NOT NULL';
3117: }
3118: if ($type === 'timestamp' && isset($column['default']) && strtolower($column['default']) === 'current_timestamp') {
3119: $out = str_replace(array("'CURRENT_TIMESTAMP'", "'current_timestamp'"), 'CURRENT_TIMESTAMP', $out);
3120: }
3121: return $this->_buildFieldParameters($out, $column, 'afterDefault');
3122: }
3123:
3124: 3125: 3126: 3127: 3128: 3129: 3130: 3131:
3132: protected function _buildFieldParameters($columnString, $columnData, $position) {
3133: foreach ($this->fieldParameters as $paramName => $value) {
3134: if (isset($columnData[$paramName]) && $value['position'] == $position) {
3135: if (isset($value['options']) && !in_array($columnData[$paramName], $value['options'])) {
3136: continue;
3137: }
3138: $val = $columnData[$paramName];
3139: if ($value['quote']) {
3140: $val = $this->value($val);
3141: }
3142: $columnString .= ' ' . $value['value'] . $value['join'] . $val;
3143: }
3144: }
3145: return $columnString;
3146: }
3147:
3148: 3149: 3150: 3151: 3152: 3153: 3154:
3155: public function buildIndex($indexes, $table = null) {
3156: $join = array();
3157: foreach ($indexes as $name => $value) {
3158: $out = '';
3159: if ($name === 'PRIMARY') {
3160: $out .= 'PRIMARY ';
3161: $name = null;
3162: } else {
3163: if (!empty($value['unique'])) {
3164: $out .= 'UNIQUE ';
3165: }
3166: $name = $this->startQuote . $name . $this->endQuote;
3167: }
3168: if (is_array($value['column'])) {
3169: $out .= 'KEY ' . $name . ' (' . implode(', ', array_map(array(&$this, 'name'), $value['column'])) . ')';
3170: } else {
3171: $out .= 'KEY ' . $name . ' (' . $this->name($value['column']) . ')';
3172: }
3173: $join[] = $out;
3174: }
3175: return $join;
3176: }
3177:
3178: 3179: 3180: 3181: 3182: 3183:
3184: public function readTableParameters($name) {
3185: $parameters = array();
3186: if (method_exists($this, 'listDetailedSources')) {
3187: $currentTableDetails = $this->listDetailedSources($name);
3188: foreach ($this->tableParameters as $paramName => $parameter) {
3189: if (!empty($parameter['column']) && !empty($currentTableDetails[$parameter['column']])) {
3190: $parameters[$paramName] = $currentTableDetails[$parameter['column']];
3191: }
3192: }
3193: }
3194: return $parameters;
3195: }
3196:
3197: 3198: 3199: 3200: 3201: 3202: 3203:
3204: public function buildTableParameters($parameters, $table = null) {
3205: $result = array();
3206: foreach ($parameters as $name => $value) {
3207: if (isset($this->tableParameters[$name])) {
3208: if ($this->tableParameters[$name]['quote']) {
3209: $value = $this->value($value);
3210: }
3211: $result[] = $this->tableParameters[$name]['value'] . $this->tableParameters[$name]['join'] . $value;
3212: }
3213: }
3214: return $result;
3215: }
3216:
3217: 3218: 3219: 3220: 3221: 3222:
3223: public function introspectType($value) {
3224: if (!is_array($value)) {
3225: if (is_bool($value)) {
3226: return 'boolean';
3227: }
3228: if (is_float($value) && floatval($value) === $value) {
3229: return 'float';
3230: }
3231: if (is_int($value) && intval($value) === $value) {
3232: return 'integer';
3233: }
3234: if (is_string($value) && strlen($value) > 255) {
3235: return 'text';
3236: }
3237: return 'string';
3238: }
3239:
3240: $isAllFloat = $isAllInt = true;
3241: $containsFloat = $containsInt = $containsString = false;
3242: foreach ($value as $valElement) {
3243: $valElement = trim($valElement);
3244: if (!is_float($valElement) && !preg_match('/^[\d]+\.[\d]+$/', $valElement)) {
3245: $isAllFloat = false;
3246: } else {
3247: $containsFloat = true;
3248: continue;
3249: }
3250: if (!is_int($valElement) && !preg_match('/^[\d]+$/', $valElement)) {
3251: $isAllInt = false;
3252: } else {
3253: $containsInt = true;
3254: continue;
3255: }
3256: $containsString = true;
3257: }
3258:
3259: if ($isAllFloat) {
3260: return 'float';
3261: }
3262: if ($isAllInt) {
3263: return 'integer';
3264: }
3265:
3266: if ($containsInt && !$containsString) {
3267: return 'integer';
3268: }
3269: return 'string';
3270: }
3271:
3272: 3273: 3274: 3275: 3276: 3277: 3278: 3279:
3280: protected function _writeQueryCache($sql, $data, $params = array()) {
3281: if (preg_match('/^\s*select/i', $sql)) {
3282: $this->_queryCache[$sql][serialize($params)] = $data;
3283: }
3284: }
3285:
3286: 3287: 3288: 3289: 3290: 3291: 3292:
3293: public function getQueryCache($sql, $params = array()) {
3294: if (isset($this->_queryCache[$sql]) && preg_match('/^\s*select/i', $sql)) {
3295: $serialized = serialize($params);
3296: if (isset($this->_queryCache[$sql][$serialized])) {
3297: return $this->_queryCache[$sql][$serialized];
3298: }
3299: }
3300: return false;
3301: }
3302:
3303: 3304: 3305: 3306:
3307: public function __destruct() {
3308: if ($this->_methodCacheChange) {
3309: Cache::write('method_cache', self::$methodCache, '_cake_core_');
3310: }
3311: }
3312:
3313: }
3314: