119 return $this->password;
129 return $this->database;
143 $this->queryExecutingEnabled =
false;
156 $this->queryExecutingEnabled =
true;
167 return $this->queryExecutingEnabled;
180 $dump = $this->disabledQueryExecutingDump;
181 $this->disabledQueryExecutingDump =
null;
200 if ($this->sqlHelper ==
null)
205 return $this->sqlHelper;
236 parent::disconnect();
246 return (($this->options & self::DEFERRED) !== 0);
255 return (($this->options & self::PERSISTENT) !== 0);
308 list($sql, $binds, $offset, $limit) = self::parseQueryFunctionArgs(func_get_args());
312 $sql = $this->
getSqlHelper()->getTopSql($sql, $limit, $offset);
315 $trackerQuery =
null;
317 if ($this->queryExecutingEnabled)
319 $connection = Main\Application::getInstance()->getConnectionPool()->getSlaveConnection($sql);
320 if($connection ===
null)
327 $trackerQuery = $this->sqlTracker->getNewTrackerQuery();
328 $trackerQuery->setNode($connection->getNodeId());
331 $result = $connection->queryInternal($sql, $binds, $trackerQuery);
335 if ($this->disabledQueryExecutingDump ===
null)
337 $this->disabledQueryExecutingDump = array();
340 $this->disabledQueryExecutingDump[] = $sql;
359 $result = $this->
query($sql, $binds, 0, 1);
361 if ($row = $result->fetch())
363 return array_shift($row);
380 $this->
query($sql, $binds);
401 $numArgs = count($args);
413 elseif ($numArgs == 2)
415 if (is_array($args[1]))
416 list($sql, $binds) = $args;
418 list($sql, $limit) = $args;
420 elseif ($numArgs == 3)
422 if (is_array($args[1]))
423 list($sql, $binds, $limit) = $args;
425 list($sql, $offset, $limit) = $args;
429 list($sql, $binds, $offset, $limit) = $args;
432 return array($sql, $binds, $offset, $limit);
447 public function add($tableName, array $data, $identity =
"ID")
449 $insert = $this->
getSqlHelper()->prepareInsert($tableName, $data);
452 "INSERT INTO ".$this->getSqlHelper()->quote($tableName).
"(".$insert[0].
") ".
453 "VALUES (".$insert[1].
")";
468 public function addMulti($tableName, $rows, $identity =
"ID")
474 foreach ($rows as $data)
476 $insert = $this->
getSqlHelper()->prepareInsert($tableName, $data,
true);
477 $inserts[] = $insert;
480 foreach ($insert[0] as $column)
482 $uniqueColumns[$column] =
true;
489 foreach ($inserts as $insert)
492 $columns = array_flip($insert[0]);
493 $values = $insert[1];
497 foreach (array_keys($uniqueColumns) as $column)
499 if (array_key_exists($column, $columns))
502 $finalValues[] = $values[$columns[$column]];
507 $finalValues[] =
'DEFAULT';
511 $sqlValues[] =
'('.join(
', ', $finalValues).
')';
514 $sql =
"INSERT INTO {$this->getSqlHelper()->quote($tableName)} (".join(
', ', array_keys($uniqueColumns)).
") ".
515 "VALUES ".join(
', ', $sqlValues);
547 $result[] = $ex->getMessage();
566 $delimiter = $this->
getSqlHelper()->getQueryDelimiter();
568 $sqlBatch = trim($sqlBatch);
575 if (preg_match(
"%^(.*?)(['\"`#]|--|\\$\\$|".$delimiter.
")%is", $sqlBatch, $match))
578 if ($match[2] ==
"\"" || $match[2] ==
"'" || $match[2] ==
"`")
580 $sqlBatch = mb_substr($sqlBatch, mb_strlen($match[0]));
585 $sqlBatch = mb_substr($sqlBatch, mb_strlen($stringMatch[0]));
586 $sql .= $stringMatch[0];
596 elseif ($match[2] ==
"#" || $match[2] ==
"--")
599 $sqlBatch = mb_substr($sqlBatch, mb_strlen($match[1]));
602 $p = mb_strpos($sqlBatch,
"\n");
605 $p1 = mb_strpos($sqlBatch,
"\r");
612 $sqlBatch = mb_substr($sqlBatch, $p);
616 $sqlBatch = mb_substr($sqlBatch, $p1);
621 $sqlBatch = mb_substr($sqlBatch, $p);
625 elseif ($match[2] ==
'$$')
628 $sqlBatch = mb_substr($sqlBatch, mb_strlen($match[0]));
632 $p = mb_strpos($sqlBatch,
'$$');
640 $sql .= mb_substr($sqlBatch, 0, $p + 2);
641 $sqlBatch = mb_substr($sqlBatch, $p + 2);
648 $sqlBatch = mb_substr($sqlBatch, mb_strlen($match[0]));
651 if (preg_match(
"%^[\n\r\t ]%", $sqlBatch))
656 $statements[] = str_replace(
"\r\n",
"\n", $sql);
661 elseif (!empty($sqlBatch))
673 while (!empty($sqlBatch));
675 $sql = trim($sql,
" \t\n\r");
678 $statements[] = str_replace(
"\r\n",
"\n", $sql);
726 abstract public function getIndexName($tableName, array $columns, $strict =
false);
748 abstract public function createTable($tableName, $fields, $primary = array(), $autoincrement = array());
762 if (!is_array($columnNames))
764 $columnNames = array($columnNames);
767 foreach ($columnNames as &$columnName)
769 $columnName = $this->
getSqlHelper()->quote($columnName);
772 $sql =
'ALTER TABLE '.$this->getSqlHelper()->quote($tableName).
' ADD PRIMARY KEY('.join(
', ', $columnNames).
')';
774 return $this->
query($sql);
790 if (!is_array($columnNames))
792 $columnNames = array($columnNames);
797 foreach ($columnNames as &$columnName)
803 $sql =
'CREATE INDEX '.$sqlHelper->quote($indexName).
' ON '.
$sqlHelper->quote($tableName).
' ('.join(
', ', $columnNames).
')';
805 return $this->
query($sql);
821 return ($tableFields[$columnName] ??
null);
909 public function lock($name, $timeout = 0)
937 if ($this->sqlTracker ==
null)
940 $this->sqlTracker->reset();
942 $this->trackSql =
true;
943 return $this->sqlTracker;
953 $this->trackSql =
false;
964 return $this->sqlTracker;
1020 $this->tableColumnsCache = array();
1041 return $this->nodeId;
1046 if(isset($this->configuration[
"include_after_connected"]) && $this->configuration[
"include_after_connected"] <>
'')
1048 include($this->configuration[
"include_after_connected"]);
1061 if(isset($this->utf8mb4[
"global"]) && $this->utf8mb4[
"global"] ===
true)
1066 if($table !==
null && isset($this->utf8mb4[
"tables"][$table]) && $this->utf8mb4[
"tables"][$table] ===
true)
1071 if($table !==
null && $column !==
null && isset($this->utf8mb4[
"tables"][$table][$column]) && $this->utf8mb4[
"tables"][$table][$column] ===
true)
1079 protected static function findIndex(array $indexes, array $columns, $strict)
1081 $columnsList = implode(
",", $columns);
1083 foreach ($indexes as $indexName => $indexColumns)
1085 ksort($indexColumns);
1086 $indexColumnList = implode(
",", $indexColumns);
1089 if ($indexColumnList === $columnsList)
1096 if (str_starts_with($indexColumnList, $columnsList))
setTracker(Diag\SqlTracker $sqlTracker=null)
isTableExists($tableName)
static findIndex(array $indexes, array $columns, $strict)
getTableField($tableName, $columnName)
static parseQueryFunctionArgs($args)
isUtf8mb4($table=null, $column=null)
getIndexName($tableName, array $columns, $strict=false)
getTableFields($tableName)
createResult($result, Diag\SqlTrackerQuery $trackerQuery=null)
createIndex($tableName, $indexName, $columnNames)
createPrimaryIndex($tableName, $columnNames)
executeSqlBatch($sqlBatch, $stopOnError=false)
startTracker($reset=false)
queryInternal($sql, array $binds=null, Diag\SqlTrackerQuery $trackerQuery=null)
renameTable($currentName, $newName)
dropColumn($tableName, $columnName)
isIndexExists($tableName, array $columns)
$disabledQueryExecutingDump
isQueryExecutingEnabled()
addMulti($tableName, $rows, $identity="ID")
getDisabledQueryExecutingDump()
add($tableName, array $data, $identity="ID")
queryScalar($sql, array $binds=null)
queryExecute($sql, array $binds=null)
createTable($tableName, $fields, $primary=array(), $autoincrement=array())
truncateTable($tableName)
__construct(array $configuration)