91 private $newJoins = array();
119 $this->parameters =
null;
136 private function getTokens1($string, &$array)
138 $tok = strtok($string,
",");
158 private function getTokens($string, &$array)
173 if ( is_array($string) )
175 $string = implode(
',', $string);
178 $source = str_split($string .
',');
183 for ($i = 0; $i < $l; $i++)
220 private function getJoin()
223 $MIOLO->Uses(
'database/' . $this->db->system .
'/msqljoin.class');
224 $className =
"{$this->db->system}SqlJoin";
225 $join =
new $className();
226 $join->_sqlJoin($this);
257 $this->getTokens($string, $this->columns);
275 $this->columns =
null;
300 $this->getTokens($string, $this->tables);
302 $subSelect = strstr(strtoupper($string),
'SELECT ');
303 if ( $this->considerarTabelasSubSelects && $subSelect && empty($this->tables) ) {
304 $this->tables[$string] = $string;
312 $this->tables =
null;
328 $this->getTokens($string, $this->groupBy);
335 $this->groupBy =
null;
351 $this->getTokens($string, $this->orderBy);
363 $this->orderBy =
null;
376 $this->
setWhere($column .
' = ?', array($value));
383 $this->
setWhere($column .
' <> ?', array($value));
390 $this->
setWhere($column .
' BETWEEN ? AND ?', array($value1, $value2));
397 $this->
setWhere($column .
' > ?', array($value));
404 $this->
setWhere($column .
' >= ?', array($value));
411 $this->
setWhere($column .
' < ?', array($value));
418 $this->
setWhere($column .
' <= ?', array($value));
425 $this->
setWhere($column .
' NOT ILIKE ?', array($value));
432 $this->
setWhere($column .
' NOT LIKE ?', array($value));
439 $this->
setWhere($column .
' LIKE ?', array($value));
446 $this->
setWhere(
'UNACCENT(' . $column .
') LIKE UNACCENT(?)', array($value));
453 $this->
setWhere($column .
' ILIKE ?', array($value));
460 $this->
setWhere(
'UNACCENT(' . $column .
') ILIKE UNACCENT(?)', array($value));
491 $this->where .= (($this->where !=
'') && ($string !=
'') ?
" and " :
"") . $string;
522 $this->where .= (($this->where !=
'') && ($string !=
'') ?
" and " :
"") . $string;
547 $this->where .= (($this->where !=
'') && ($string !=
'') ?
" or " :
"") . $string;
596 $this->offsetSQL =
null;
612 $this->having .= (($this->having !=
'') && ($string !=
'') ?
" and " :
"") . $string;
628 $this->having .= (($this->having !=
'') && ($string !=
'') ?
" and " :
"") . $string;
644 $this->having .= (($this->having !=
'') && ($string !=
'') ?
" or " :
"") . $string;
661 function setJoin($table1, $table2, $cond, $type =
'INNER')
663 $this->join[] = array
687 $this->SetJoin($table1, $table2, $cond,
'LEFT');
705 $this->SetJoin($table1, $table2, $cond,
'RIGHT');
755 $pos = strpos($sqlText,
'?');
759 $prepared .= $sqlText;
766 $prepared .= substr($sqlText, 0, $pos);
771 if ( substr($value, 0, 1) ==
':' )
773 $prepared .= substr($value, 1);
775 else if ( $value instanceof
MSQLExpr )
777 $prepared .= $value->getValue();
779 else if ( is_array($value) )
789 $prepared .= is_null( $p = $value ) ?
'NULL' :
"'" . str_replace(
'\\',
'\\\\', str_replace(
"'",
"''", $p)) .
"'";
793 $sqlText = substr($sqlText, $pos + 1);
797 $MIOLO->Assert($i == count(
$parameters),
"SQL PREPARE: Parâmetros inconsistentes! SQL: $sqlText");
798 $this->command = $prepared;
813 $msql->setTables($tableName);
815 foreach (
$where as $column => $value )
817 $msql->addEqualCondition($column, $value);
820 return $msql->delete();
834 $msql->setColumns( array_keys($values) );
835 $msql->setParameters( array_values($values) );
836 $msql->setTables($tableName);
838 return $msql->insert();
853 $msql->setColumns( array_keys($values) );
854 $msql->setParameters( array_values($values) );
855 $msql->setTables($tableName);
857 foreach (
$where as $key => $val )
859 $msql->addEqualCondition($key, $val);
862 return $msql->update();
879 $sqlText =
'INSERT INTO ' . implode($this->tables,
',') .
' ( ' . implode($this->columns,
',') .
' ) VALUES ( ';
881 for ($i = 0; $i < count($this->columns); $i++)
884 $sqlText .= implode($par,
',') .
' )';
885 $this->command = $sqlText;
890 $this->
prepare($this->parameters);
905 $sqlText =
'INSERT INTO ' . implode($this->tables,
',') .
' ( ' . implode($this->columns,
',') .
' ) ';
907 $this->command = $sqlText;
923 $sqlText =
'DELETE FROM ' . implode($this->tables,
',');
924 $MIOLO->Assert($this->where !=
'',
"SQL DELETE: Condição não informada!");
926 $this->command = $sqlText;
931 $this->Prepare($this->parameters);
949 $sqlText =
'UPDATE ' . implode($this->tables,
',') .
' SET ';
951 foreach ($this->columns as $c)
954 $sqlText .= implode($par,
',');
955 $MIOLO->Assert($this->where !=
'',
"SQL UPDATE: Condição não informada!");
957 $this->command = $sqlText;
962 $this->Prepare($this->parameters);
977 if ($this->join != NULL)
981 $sqlText =
'SELECT ' . ($this->distinct ?
'DISTINCT ' :
'') . implode($this->columns,
',');
983 if ($this->tables !=
'')
985 $sqlText .=
' FROM ' . implode($this->tables,
',') .
' ' . $this->getNewJoinsSQL();
988 if ($this->where !=
'')
993 if ($this->groupBy !=
'')
995 $sqlText .=
' GROUP BY ' . implode($this->groupBy,
',');
998 if ($this->having !=
'')
1003 if ($this->orderBy !=
'')
1005 $sqlText .=
' ORDER BY ' . implode($this->orderBy,
',');
1008 if ($this->limit !=
'')
1013 if ($this->offsetSQL !=
'')
1018 $this->command = $sqlText;
1023 $this->Prepare($this->parameters);
1032 $new = clone($this);
1033 $new instanceof
MSQL;
1039 ->setColumns(
'COUNT(*)');
1041 return $new->select();
1053 $this->columns =
'';
1056 $this->groupBy =
'';
1058 $this->orderBy =
'';
1059 $this->parameters =
null;
1060 $this->command =
'';
1072 $numargs = func_num_args();
1110 $this->parameters[] = $value;
1122 $numargs = func_num_args();
1126 $this->range = func_get_arg(0);
1128 elseif ($numargs == 2)
1130 $page = func_get_arg(0);
1131 $rows = func_get_arg(1);
1151 $this->range->offset = $offset;
1152 $this->range->rows = $rows;
1172 $split = preg_split(
'/(\(|\))/i', $source, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
1174 foreach ($split as $key=>$s)
1181 if ($parenteses == 0 )
1183 if ( preg_match(
'/' . preg_quote($target) .
'/', $s) )
1185 $pos += mb_strpos($s, $target);
1197 $pos += mb_strlen($s);
1200 return ($fim ? $pos : -1);
1216 if (mb_substr($cmd, 0, mb_strlen($clause)) != $clause)
1219 $cmd = mb_substr($cmd, mb_strlen($clause));
1220 $n = count($delimiters);
1224 while (($pos < 0) && ($i < $n))
1225 $pos = $this->FindStr($delimiters[$i++], $cmd);
1229 $r = mb_substr($cmd, 0, $pos);
1230 $cmd = mb_substr($cmd, $pos);
1247 $this->command = $sqltext;
1249 $sqltext = trim($sqltext) .
" #";
1250 $sqltext = preg_replace(
"/(?i)select /",
"select ", $sqltext);
1251 $sqltext = preg_replace(
"/(?i) from /",
" from ", $sqltext);
1252 $sqltext = preg_replace(
"/(?i) where /",
" where ", $sqltext);
1253 $sqltext = preg_replace(
"/(?i) order by /",
" order by ", $sqltext);
1254 $sqltext = preg_replace(
"/(?i) group by /",
" group by ", $sqltext);
1255 $sqltext = preg_replace(
"/(?i) having /",
" having ", $sqltext);
1256 $this->SetColumns($this->ParseSqlCommand($sqltext,
"select", array(
"from")));
1258 if ($this->FindStr(
'JOIN', $sqltext) < 0)
1260 $this->SetTables($this->ParseSqlCommand($sqltext,
"from", array(
"where",
"group by",
"order by",
"#")));
1264 $this->join = $this->ParseSqlCommand($sqltext,
"from", array(
"where",
"group by",
"order by",
"#"));
1267 $this->SetWhere($this->ParseSqlCommand($sqltext,
"where", array(
"group by",
"order by",
"#")));
1268 $this->SetGroupBy($this->ParseSqlCommand($sqltext,
"group by", array(
"having",
"order by",
"#")));
1269 $this->SetHaving($this->ParseSqlCommand($sqltext,
"having", array(
"order by",
"#")));
1270 $this->SetOrderBy($this->ParseSqlCommand($sqltext,
"order by", array(
"#")));
1285 $this->addNewJoin(
$join);
1300 $this->addNewJoin(
$join);
1311 return $this->newJoins;
1314 private function setNewJoins(array $newJoins)
1316 $this->newJoins = $newJoins;
1321 $this->newJoins[] =
$join;
1327 private function getNewJoinsSQL()
1348 foreach ( $values as $value )
1356 $out[] =
"'" . str_replace(
"'",
"''", $value) .
"'";
1360 return '(' . implode(
', ', $out) .
')';
1373 $this->value = $value;
1378 return $this->value;
1383 $this->value = $value;
1400 return $this->table;
1405 $this->table = $table;
1417 $this->type = $type;
1424 return $this->condition;
1429 $this->condition = $condition;
setWhereAnd($string, array $parameters=null)
addIlikeCondition($column, $value)
prepare($parameters=null)
setColumnsOverride(array $columns)
addSmallerCondition($column, $value)
setWhere($string, array $parameters=null)
setLeftJoin($table1, $table2, $cond)
addInnerJoin($table, $cond)
createFrom($sqltext, $params=array())
addEqualCondition($column, $value)
addNotIlikeCondition($column, $value)
addNotLikeCondition($column, $value)
addSmallerEqualCondition($column, $value)
update($parameters=null, array $values=null)
addGreaterEqualCondition($column, $value)
static deleteTable($tableName, array $where)
addWhereIn($column, array $values)
addLikeConditionUnaccent($column, $value)
setRightJoin($table1, $table2, $cond)
__construct($columns='', $tables='', $where='', $orderBy='', $groupBy='', $having='')
static insertTable($tableName, array $values)
addIlikeConditionUnaccent($column, $value)
setJoin($table1, $table2, $cond, $type='INNER')
static updateTable($tableName, array $values, array $where)
setOffset($offset, $rows)
$considerarTabelasSubSelects
convertArrayToIn(array $values=null)
setWhereOr($string, array $parameters=null)
setColumns($string, $distinct=false)
addWhereNotIn($column, array $values)
addGreaterCondition($column, $value)
findStr($target, $source)
addBetweenCondition($column, $value1, $value2)
parseSqlCommand(&$cmd, $clause, $delimiters)
addLeftJoin($table, $cond)
addLikeCondition($column, $value)
addNotEqualCondition($column, $value)