27 #require_once 'Zend/Db/Adapter/Abstract.php'; 32 #require_once 'Zend/Db/Expr.php'; 90 (([\'"]).*?[^\\\]\2) # $1 : Skip single & double quoted expressions 91 |( # $3 : Match comments 92 (?:\#|--).*?$ # - Single line comments 93 | # - Multi line (nested) comments 94 /\* # . comment open marker 95 (?: [^/*] # . non comment-marker characters 96 |/(?!\*) # . ! not a comment open 97 |\*(?!/) # . ! not a comment close 98 |(?R) # . recursive case 99 )* # . repeat eventually 100 \*\/ # . comment close marker 101 )\s* # Trim after comments 102 |(?<=;)\s+ # Trim after semi-colon 127 self::DISTINCT =>
false,
128 self::COLUMNS => array(),
129 self::UNION => array(),
130 self::FROM => array(),
131 self::WHERE => array(),
132 self::GROUP => array(),
133 self::HAVING => array(),
134 self::ORDER => array(),
135 self::LIMIT_COUNT =>
null,
136 self::LIMIT_OFFSET =>
null,
137 self::FOR_UPDATE =>
false 208 $this->_bind = $bind;
264 public function columns($cols =
'*', $correlationName =
null)
266 if ($correlationName ===
null && count($this->_parts[self::FROM])) {
267 $correlationNameKeys = array_keys($this->_parts[self::FROM]);
268 $correlationName = current($correlationNameKeys);
271 if (!array_key_exists($correlationName, $this->_parts[self::FROM])) {
275 #require_once 'Zend/Db/Select/Exception.php'; 304 #require_once 'Zend/Db/Select/Exception.php'; 306 "union() only accepts an array of Zend_Db_Select instances of sql query strings." 310 if (!in_array(
$type, self::$_unionTypes)) {
311 #require_once 'Zend/Db/Select/Exception.php'; 528 if (!is_array($spec)) {
529 $spec = array($spec);
532 foreach ($spec as $val) {
534 $noComments = preg_replace(self::REGEX_SQL_COMMENTS,
'$1', (
string) $val);
535 if (preg_match(self::REGEX_COLUMN_EXPR_GROUP, $noComments)) {
559 $cond = $this->_adapter->quoteInto($cond,
$value,
$type);
562 if ($this->_parts[self::HAVING]) {
563 $this->_parts[
self::HAVING][] = self::SQL_AND .
" ($cond)";
586 $cond = $this->_adapter->quoteInto($cond,
$value,
$type);
589 if ($this->_parts[self::HAVING]) {
590 $this->_parts[
self::HAVING][] = self::SQL_OR .
" ($cond)";
606 if (!is_array($spec)) {
607 $spec = array($spec);
611 foreach ($spec as $val) {
613 $expr = $val->__toString();
623 if (preg_match(
'/(.*\W)(' . self::SQL_ASC .
'|' . self::SQL_DESC .
')\b/si', $val, $matches)) {
624 $val = trim($matches[1]);
625 $direction = $matches[2];
628 $noComments = preg_replace(self::REGEX_SQL_COMMENTS,
'$1', (
string) $val);
629 if (preg_match(self::REGEX_COLUMN_EXPR_ORDER, $noComments)) {
632 $this->_parts[
self::ORDER][] = array($val, $direction);
663 $rowCount = ($rowCount > 0) ? $rowCount : 1;
690 $part = strtolower($part);
691 if (!array_key_exists($part, $this->_parts)) {
692 #require_once 'Zend/Db/Select/Exception.php'; 695 return $this->_parts[$part];
705 public function query($fetchMode =
null, $bind = array())
711 $stmt = $this->_adapter->query($this);
712 if ($fetchMode ==
null) {
713 $fetchMode = $this->_adapter->getFetchMode();
715 $stmt->setFetchMode($fetchMode);
727 foreach (array_keys(self::$_partsInit) as $part) {
728 $method =
'_render' . ucfirst($part);
729 if (method_exists($this,
$method)) {
746 }
elseif (array_key_exists($part, self::$_partsInit)) {
747 $this->_parts[$part] = self::$_partsInit[$part];
781 if (!in_array(
$type, self::$_joinTypes) &&
$type != self::FROM) {
785 #require_once 'Zend/Db/Select/Exception.php'; 789 if (count($this->_parts[self::UNION])) {
790 #require_once 'Zend/Db/Select/Exception.php'; 798 foreach (
$name as $_correlationName => $_tableName) {
799 if (is_string($_correlationName)) {
802 $correlationName = $_correlationName;
806 $correlationName = $this->_uniqueCorrelation(
$tableName);
812 $correlationName = $this->_uniqueCorrelation(
't');
813 }
elseif (preg_match(
'/^(.+)\s+AS\s+(.+)$/i',
$name, $m)) {
815 $correlationName = $m[2];
818 $correlationName = $this->_uniqueCorrelation(
$tableName);
826 $lastFromCorrelationName =
null;
827 if (!empty($correlationName)) {
828 if (array_key_exists($correlationName, $this->_parts[self::FROM])) {
832 #require_once 'Zend/Db/Select/Exception.php'; 836 if (
$type == self::FROM) {
841 while ($tmpFromParts) {
842 $currentCorrelationName = key($tmpFromParts);
843 if ($tmpFromParts[$currentCorrelationName][
'joinType'] != self::FROM) {
846 $lastFromCorrelationName = $currentCorrelationName;
847 $this->_parts[
self::FROM][$currentCorrelationName] = array_shift($tmpFromParts);
850 $tmpFromParts = array();
852 $this->_parts[
self::FROM][$correlationName] = array(
856 'joinCondition' => $cond
858 while ($tmpFromParts) {
859 $currentCorrelationName = key($tmpFromParts);
860 $this->_parts[
self::FROM][$currentCorrelationName] = array_shift($tmpFromParts);
865 if (
$type == self::FROM && $lastFromCorrelationName ==
null) {
866 $lastFromCorrelationName =
true;
868 $this->
_tableCols($correlationName, $cols, $lastFromCorrelationName);
900 if (empty($this->_parts[self::FROM])) {
901 #require_once 'Zend/Db/Select/Exception.php'; 905 $join = $this->_adapter->quoteIdentifier(key($this->_parts[self::FROM]),
true);
906 $from = $this->_adapter->quoteIdentifier($this->_uniqueCorrelation(
$name),
true);
909 foreach ((array)$cond as $fieldName) {
910 $cond1 = $from .
'.' . $fieldName;
911 $cond2 = $join .
'.' . $fieldName;
912 $joinCond[] = $cond1 .
' = ' . $cond2;
914 $cond = implode(
' '.self::SQL_AND.
' ', $joinCond);
925 private function _uniqueCorrelation(
$name)
927 if (is_array(
$name)) {
929 $c = is_string($k) ? $k : end(
$name);
932 $dot = strrpos(
$name,
'.');
933 $c = ($dot ===
false) ?
$name : substr(
$name, $dot+1);
935 for (
$i = 2; array_key_exists($c, $this->_parts[self::FROM]); ++
$i) {
936 $c =
$name .
'_' . (string)
$i;
950 protected function _tableCols($correlationName, $cols, $afterCorrelationName =
null)
952 if (!is_array($cols)) {
953 $cols = array($cols);
956 if ($correlationName ==
null) {
957 $correlationName =
'';
960 $columnValues = array();
962 foreach (array_filter($cols) as
$alias => $col) {
963 $currentCorrelationName = $correlationName;
964 if (is_string($col)) {
966 $col = trim(str_replace(
"\n",
' ', $col));
967 if (preg_match(
'/^(.+)\s+' . self::SQL_AS .
'\s+(.+)$/i', $col, $m)) {
972 if (preg_match(self::REGEX_COLUMN_EXPR, $col)) {
974 }
elseif (preg_match(
'/(.+)\.(.+)/', $col, $m)) {
975 $currentCorrelationName = $m[1];
979 $columnValues[] = array($currentCorrelationName, $col, is_string(
$alias) ?
$alias :
null);
985 if ($afterCorrelationName ===
true || is_string($afterCorrelationName)) {
989 $tmpColumns = array();
993 if (is_string($afterCorrelationName)) {
994 while ($tmpColumns) {
995 $this->_parts[
self::COLUMNS][] = $currentColumn = array_shift($tmpColumns);
996 if ($currentColumn[0] == $afterCorrelationName) {
1003 foreach ($columnValues as $columnValue) {
1004 array_push($this->_parts[self::COLUMNS], $columnValue);
1008 while ($tmpColumns) {
1009 array_push($this->_parts[self::COLUMNS], array_shift($tmpColumns));
1025 if (count($this->_parts[self::UNION])) {
1026 #require_once 'Zend/Db/Select/Exception.php'; 1031 $condition = $this->_adapter->quoteInto($condition,
$value,
$type);
1035 if ($this->_parts[self::WHERE]) {
1036 if ($bool ===
true) {
1037 $cond = self::SQL_AND .
' ';
1039 $cond = self::SQL_OR .
' ';
1043 return $cond .
"($condition)";
1065 return $this->_adapter->quoteIdentifier(
$schema,
true) .
'.';
1077 return $this->_adapter->quoteTableAs(
$tableName, $correlationName,
true);
1088 if ($this->_parts[self::DISTINCT]) {
1103 if (!count($this->_parts[self::COLUMNS])) {
1108 foreach ($this->_parts[self::COLUMNS] as $columnEntry) {
1109 list($correlationName, $column,
$alias) = $columnEntry;
1111 $columns[] = $this->_adapter->quoteColumnAs($column,
$alias,
true);
1113 if ($column == self::SQL_WILDCARD) {
1117 if (empty($correlationName)) {
1118 $columns[] = $this->_adapter->quoteColumnAs($column,
$alias,
true);
1120 $columns[] = $this->_adapter->quoteColumnAs(array($correlationName, $column),
$alias,
true);
1125 return $sql .
' ' . implode(
', ',
$columns);
1140 if (empty($this->_parts[self::FROM])) {
1146 foreach ($this->_parts[self::FROM] as $correlationName =>
$table) {
1152 if (! empty($from)) {
1153 $tmp .=
' ' . strtoupper($joinType) .
' ';
1160 if (!empty($from) && ! empty(
$table[
'joinCondition'])) {
1161 $tmp .=
' ' . self::SQL_ON .
' ' .
$table[
'joinCondition'];
1169 if (!empty($from)) {
1170 $sql .=
' ' . self::SQL_FROM .
' ' . implode(
"\n", $from);
1184 if ($this->_parts[self::UNION]) {
1185 $parts = count($this->_parts[self::UNION]);
1186 foreach ($this->_parts[self::UNION] as $cnt => $union) {
1192 if ($cnt < $parts - 1) {
1193 $sql .=
' ' .
$type .
' ';
1209 if ($this->_parts[self::FROM] && $this->_parts[self::WHERE]) {
1210 $sql .=
' ' . self::SQL_WHERE .
' ' . implode(
' ', $this->_parts[self::WHERE]);
1224 if ($this->_parts[self::FROM] && $this->_parts[self::GROUP]) {
1226 foreach ($this->_parts[self::GROUP] as $term) {
1227 $group[] = $this->_adapter->quoteIdentifier($term,
true);
1229 $sql .=
' ' . self::SQL_GROUP_BY .
' ' . implode(
",\n\t",
$group);
1243 if ($this->_parts[self::FROM] && $this->_parts[self::HAVING]) {
1244 $sql .=
' ' . self::SQL_HAVING .
' ' . implode(
' ', $this->_parts[self::HAVING]);
1258 if ($this->_parts[self::ORDER]) {
1260 foreach ($this->_parts[self::ORDER] as $term) {
1261 if (is_array($term)) {
1262 if(is_numeric($term[0]) && strval(intval($term[0])) == $term[0]) {
1263 $order[] = (int)trim($term[0]) .
' ' . $term[1];
1265 $order[] = $this->_adapter->quoteIdentifier($term[0],
true) .
' ' . $term[1];
1267 }
elseif (is_numeric($term) && strval(intval($term)) == $term) {
1268 $order[] = (int)trim($term);
1270 $order[] = $this->_adapter->quoteIdentifier($term,
true);
1273 $sql .=
' ' . self::SQL_ORDER_BY .
' ' . implode(
', ',
$order);
1290 if (!empty($this->_parts[self::LIMIT_OFFSET])) {
1291 $offset = (int) $this->_parts[self::LIMIT_OFFSET];
1295 if (!empty($this->_parts[self::LIMIT_COUNT])) {
1296 $count = (int) $this->_parts[self::LIMIT_COUNT];
1303 $sql = trim($this->_adapter->limit($sql,
$count, $offset));
1317 if ($this->_parts[self::FOR_UPDATE]) {
1343 if (preg_match(
'/^join([a-zA-Z]*?)Using$/',
$method, $matches)) {
1344 $type = strtolower($matches[1]);
1347 if (!in_array(
$type, self::$_joinTypes)) {
1348 #require_once 'Zend/Db/Select/Exception.php'; 1351 if (in_array(
$type, array(self::CROSS_JOIN, self::NATURAL_JOIN))) {
1352 #require_once 'Zend/Db/Select/Exception.php'; 1358 array_unshift($args,
$type);
1359 return call_user_func_array(array($this,
'_joinUsing'), $args);
1362 #require_once 'Zend/Db/Select/Exception.php'; 1375 }
catch (Exception $e) {
1376 trigger_error($e->getMessage(), E_USER_WARNING);
1379 return (
string)$sql;
orHaving($cond, $value=null, $type=null)
joinRight($name, $cond, $cols=self::SQL_WILDCARD, $schema=null)
where($cond, $value=null, $type=null)
elseif(isset( $params[ 'redirect_parent']))
orWhere($cond, $value=null, $type=null)
joinLeft($name, $cond, $cols=self::SQL_WILDCARD, $schema=null)
_getQuotedSchema($schema=null)
joinCross($name, $cols=self::SQL_WILDCARD, $schema=null)
__construct(Zend_Db_Adapter_Abstract $adapter)
join($name, $cond, $cols=self::SQL_WILDCARD, $schema=null)
_getQuotedTable($tableName, $correlationName=null)
joinInner($name, $cond, $cols=self::SQL_WILDCARD, $schema=null)
const REGEX_COLUMN_EXPR_ORDER
joinNatural($name, $cols=self::SQL_WILDCARD, $schema=null)
from($name, $cols=' *', $schema=null)
columns($cols=' *', $correlationName=null)
query($fetchMode=null, $bind=array())
limitPage($page, $rowCount)
having($cond, $value=null, $type=null)
limit($count=null, $offset=null)
joinFull($name, $cond, $cols=self::SQL_WILDCARD, $schema=null)
__call($method, array $args)
_tableCols($correlationName, $cols, $afterCorrelationName=null)
_joinUsing($type, $name, $cond, $cols=' *', $schema=null)
_where($condition, $value=null, $type=null, $bool=true)
_join($type, $name, $cond, $cols, $schema=null)
const REGEX_COLUMN_EXPR_GROUP
if(!isset($_GET['name'])) $name