8 define(
'CH_UPGRADE_DB_FULL_VISUAL_PROCESSING',
true );
9 define(
'CH_UPGRADE_DB_FULL_DEBUG_MODE',
true );
22 $this->host = DATABASE_HOST;
23 $this->port = DATABASE_PORT;
24 $this->socket = DATABASE_SOCK;
25 $this->dbname = DATABASE_NAME;
26 $this->user = DATABASE_USER;
27 $this->password = DATABASE_PASS;
28 $this->iCurrentFetchStyle = PDO::FETCH_ASSOC;
40 $sSocketOrHost = (
$this->socket) ?
"unix_socket={$this->socket}" :
"host={$this->host};port={$this->port}";
42 $this->link =
new PDO(
43 "mysql:{$sSocketOrHost};dbname={$this->dbname};charset=utf8",
47 PDO::MYSQL_ATTR_INIT_COMMAND =>
'SET sql_mode=""',
48 PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT,
49 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
50 PDO::ATTR_EMULATE_PREPARES =>
false,
51 PDO::ATTR_PERSISTENT => defined(
'DATABASE_PERSISTENT') && DATABASE_PERSISTENT ?
true :
false,
72 public function getRow($sQuery, $aBindings = [], $iFetchStyle = PDO::FETCH_ASSOC)
74 if ($iFetchStyle != PDO::FETCH_ASSOC && $iFetchStyle != PDO::FETCH_NUM && $iFetchStyle != PDO::FETCH_BOTH) {
75 $iFetchStyle = PDO::FETCH_ASSOC;
78 $oStmt = $this->
res($sQuery, $aBindings);
82 return $oStmt->fetch($iFetchStyle);
94 $oStmt = $this->
res($sQuery, $aBindings);
99 while ($row = $oStmt->fetchColumn()) {
100 $aResultRows[] = $row;
116 $oStmt = $this->
res($sQuery, $aBindings);
120 $result = $oStmt->fetch(PDO::FETCH_BOTH);
137 public function getFirstRow($sQuery, $aBindings = [], $iFetchStyle = PDO::FETCH_ASSOC)
139 if ($iFetchStyle != PDO::FETCH_ASSOC && $iFetchStyle != PDO::FETCH_NUM) {
140 $this->iCurrentFetchStyle = PDO::FETCH_ASSOC;
142 $this->iCurrentFetchStyle = $iFetchStyle;
145 $oStmt = $this->
res($sQuery, $aBindings);
149 $this->oCurrentStmt = $oStmt;
151 $result = $this->oCurrentStmt->fetch($this->iCurrentFetchStyle);
168 if (!$this->oCurrentStmt) {
172 $aResult = $this->oCurrentStmt->fetch($this->iCurrentFetchStyle);
175 $this->oCurrentStmt =
null;
176 $this->iCurrentFetchStyle = PDO::FETCH_ASSOC;
205 return $oStmt->rowCount();
208 if ($this->oCurrentStmt) {
209 return $this->oCurrentStmt->rowCount();
222 public function query($sQuery, $aBindings = [])
224 $oStmt = $this->
res($sQuery, $aBindings);
228 return $oStmt->rowCount();
239 public function res($sQuery, $aBindings = [], $bReplaying =
false)
241 if (strlen(trim($sQuery)) < 1) {
242 throw new InvalidArgumentException(
'Please provide a valid sql query');
245 if ($this->link ===
null) {
250 $oStmt = $this->link->prepare($sQuery);
251 $oStmt->execute($aBindings);
253 $oStmt = $this->link->query($sQuery);
267 public function getAll($sQuery, $aBindings = [], $iFetchType = PDO::FETCH_ASSOC)
269 if ($iFetchType != PDO::FETCH_ASSOC && $iFetchType != PDO::FETCH_NUM && $iFetchType != PDO::FETCH_BOTH) {
270 $iFetchType = PDO::FETCH_ASSOC;
273 $oStmt = $this->
res($sQuery, $aBindings);
277 return $oStmt->fetchAll($iFetchType);
288 public function fillArray($oStmt, $iFetchType = PDO::FETCH_ASSOC)
290 if ($iFetchType != PDO::FETCH_ASSOC && $iFetchType != PDO::FETCH_NUM && $iFetchType != PDO::FETCH_BOTH) {
291 $iFetchType = PDO::FETCH_ASSOC;
295 while ($row = $oStmt->fetch($iFetchType)) {
311 public function getAllWithKey($sQuery, $sFieldKey, $aBindings = [], $iFetchType = PDO::FETCH_ASSOC)
313 $oStmt = $this->
res($sQuery, $aBindings);
319 while ($row = $oStmt->fetch($iFetchType)) {
338 public function getPairs($sQuery, $sFieldKey, $sFieldValue, $aBindings = [])
340 $oStmt = $this->
res($sQuery, $aBindings);
346 while ($row = $oStmt->fetch()) {
347 $aResult[$row[$sFieldKey]] = $row[$sFieldValue];
363 return $this->link->lastInsertId();
375 $oStmt = $this->link->query(
"SHOW TABLES FROM `{$this->dbname}`");
379 while ($row = $oStmt->fetch(PDO::FETCH_NUM)) {
388 $oFieldsStmt = $this->link->query(
"SHOW COLUMNS FROM `{$sTable}`");
392 $aResult = [
'original' => [],
'uppercase' => []];
393 while ($row = $oFieldsStmt->fetch()) {
406 return in_array(strtoupper($sFieldName),
$aFields[
'uppercase']);
409 public function fetchField($mixedQuery, $iField, $aBindings = [])
411 if(is_string($mixedQuery))
412 $mixedQuery = $this->
res($mixedQuery, $aBindings);
414 return $mixedQuery->getColumnMeta($iField);
421 $aIndexes = $this->
getAll(
"SHOW INDEXES FROM `" . $sTable .
"`");
425 foreach($aIndexes
as $aIndex)
426 if($aIndex[
'Key_name'] == $sIndexName) {
440 public function escape($sText, $bReal =
false)
442 $pdoEscapted = $this->link->quote($sText);
451 return trim($pdoEscapted,
"'");
454 function executeSQL($sPath, $aReplace = array (), $isBreakOnError =
true)
456 if(!file_exists($sPath) || !($rHandler = fopen($sPath,
"r")))
457 return array (
'query' =>
"fopen($sPath, 'r')",
'error' =>
'file not found or permission denied');
462 while(!feof($rHandler)) {
463 $sStr = trim(fgets($rHandler));
465 if(
empty($sStr) || $sStr[0] ==
"" || $sStr[0] ==
"#" || ($sStr[0] ==
"-" && $sStr[1] ==
"-"))
469 if(strpos($sStr,
"DELIMITER //") !==
false || strpos($sStr,
"DELIMITER ;") !==
false) {
470 $sDelimiter = trim(str_replace(
'DELIMITER',
'', $sStr));
477 if(substr($sStr, -strlen($sDelimiter)) != $sDelimiter)
482 $sQuery = str_replace($aReplace[
'from'], $aReplace[
'to'], $sQuery);
483 if($sDelimiter !=
';')
484 $sQuery = str_replace($sDelimiter,
"", $sQuery);
485 $rResult = $this->
res(trim($sQuery),
false);
487 $aErrInfo = $this->link->errorInfo();
488 $aResult[] = array(
'query' => $sQuery,
'error' => $aErrInfo[2]);