8 define(
'CH_WSB_TABLE_PROFILES',
'`Profiles`');
10 require_once(CH_DIRECTORY_PATH_CLASSES .
'ChWsbParams.php');
54 $this->host = DATABASE_HOST;
55 $this->port = DATABASE_PORT;
56 $this->socket = DATABASE_SOCK;
57 $this->dbname = DATABASE_NAME;
58 $this->user = DATABASE_USER;
59 $this->password = DATABASE_PASS;
60 $this->iCurrentFetchStyle = PDO::FETCH_ASSOC;
74 $this->oParams =
$GLOBALS[
'ch_db_param'];
84 if (!isset(self::$instance)) {
85 self::$instance =
new static();
96 $sSocketOrHost = (
$this->socket) ?
"unix_socket={$this->socket}" :
"host={$this->host};port={$this->port}";
99 $this->link =
new PDO(
100 "mysql:{$sSocketOrHost};dbname={$this->dbname};charset=utf8",
104 PDO::MYSQL_ATTR_INIT_COMMAND =>
'SET sql_mode=""',
105 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
106 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
107 PDO::ATTR_EMULATE_PREPARES =>
false,
108 PDO::ATTR_PERSISTENT => defined(
'DATABASE_PERSISTENT') && DATABASE_PERSISTENT ?
true :
false,
112 catch (PDOException
$o) {
113 throw new Exception (
"DB connect failed: " .
$o->getMessage());
132 $oTimeZone =
new DateTimeZone($sTimezone);
133 $oDate =
new DateTime(
'now', $oTimeZone);
134 $this->link->query(
'SET time_zone = "' . $oDate->format(
'P') .
'"');
150 public function res($sQuery, $aBindings = [], $bReplaying =
false)
152 if (strlen(trim($sQuery)) < 1) {
153 throw new InvalidArgumentException(
'Please provide a valid sql query');
156 if ($this->link ===
null) {
160 if (isset(
$GLOBALS[
'ch_profiler'])) {
161 $GLOBALS[
'ch_profiler']->beginQuery($sQuery);
166 $oStmt = $this->link->prepare($sQuery);
167 $oStmt->execute($aBindings);
169 $oStmt = $this->link->query($sQuery);
171 }
catch (PDOException $e) {
174 if (!$bReplaying && (stripos($e->getMessage(),
'gone away') !==
false)) {
181 return $this->
res($sQuery, $aBindings,
true);
191 if (isset(
$GLOBALS[
'ch_profiler'])) {
192 $GLOBALS[
'ch_profiler']->endQuery($oStmt);
206 public function getAll($sQuery, $aBindings = [], $iFetchType = PDO::FETCH_ASSOC)
208 if ($iFetchType != PDO::FETCH_ASSOC && $iFetchType != PDO::FETCH_NUM && $iFetchType != PDO::FETCH_BOTH) {
209 $iFetchType = PDO::FETCH_ASSOC;
212 $oStmt = $this->
res($sQuery, $aBindings);
214 return $oStmt->fetchAll($iFetchType);
225 public function getRow($sQuery, $aBindings = [], $iFetchStyle = PDO::FETCH_ASSOC)
227 if ($iFetchStyle != PDO::FETCH_ASSOC && $iFetchStyle != PDO::FETCH_NUM && $iFetchStyle != PDO::FETCH_BOTH) {
228 $iFetchStyle = PDO::FETCH_ASSOC;
231 $oStmt = $this->
res($sQuery, $aBindings);
233 return $oStmt->fetch($iFetchStyle);
245 $oStmt = $this->
res($sQuery, $aBindings);
248 while ($row = $oStmt->fetchColumn()) {
249 $aResultRows[] = $row;
265 $oStmt = $this->
res($sQuery, $aBindings);
267 $result = $oStmt->fetch(PDO::FETCH_BOTH);
284 public function getFirstRow($sQuery, $aBindings = [], $iFetchStyle = PDO::FETCH_ASSOC)
286 if ($iFetchStyle != PDO::FETCH_ASSOC && $iFetchStyle != PDO::FETCH_NUM) {
287 $this->iCurrentFetchStyle = PDO::FETCH_ASSOC;
289 $this->iCurrentFetchStyle = $iFetchStyle;
292 $oStmt = $this->
res($sQuery, $aBindings);
294 $this->oCurrentStmt = $oStmt;
296 $result = $this->oCurrentStmt->fetch($this->iCurrentFetchStyle);
313 if (!$this->oCurrentStmt) {
317 $aResult = $this->oCurrentStmt->fetch($this->iCurrentFetchStyle);
320 $this->oCurrentStmt =
null;
321 $this->iCurrentFetchStyle = PDO::FETCH_ASSOC;
338 public function getAllWithKey($sQuery, $sFieldKey, $aBindings = [], $iFetchType = PDO::FETCH_ASSOC)
340 $oStmt = $this->
res($sQuery, $aBindings);
344 while ($row = $oStmt->fetch($iFetchType)) {
363 public function getPairs($sQuery, $sFieldKey, $sFieldValue, $aBindings = [])
365 $oStmt = $this->
res($sQuery, $aBindings);
369 while ($row = $oStmt->fetch()) {
370 $aResult[$row[$sFieldKey]] = $row[$sFieldValue];
386 public function query($sQuery, $aBindings = [])
388 return $this->
res($sQuery, $aBindings)->rowCount();
412 return $oStmt->rowCount();
415 if ($this->oCurrentStmt) {
416 return $this->oCurrentStmt->rowCount();
430 public function fillArray($oStmt, $iFetchType = PDO::FETCH_ASSOC)
432 if ($iFetchType != PDO::FETCH_ASSOC && $iFetchType != PDO::FETCH_NUM && $iFetchType != PDO::FETCH_BOTH) {
433 $iFetchType = PDO::FETCH_ASSOC;
437 while ($row = $oStmt->fetch($iFetchType)) {
451 return $this->link->lastInsertId();
456 return $this->oParams->get(
$sName, $bCache);
461 $this->oParams->set(
$sName, $sValue);
475 $oStmt = $this->link->query(
"SHOW TABLES FROM `{$this->dbname}`");
476 while ($row = $oStmt->fetch(PDO::FETCH_NUM)) {
485 $oFieldsStmt = $this->link->query(
"SHOW COLUMNS FROM `{$sTable}`");
487 $aResult = [
'original' => [],
'uppercase' => []];
488 while ($row = $oFieldsStmt->fetch()) {
501 return in_array(strtoupper($sFieldName),
$aFields[
'uppercase']);
504 public function fetchField($mixedQuery, $iField, $aBindings = [])
506 if(is_string($mixedQuery))
507 $mixedQuery = $this->
res($mixedQuery, $aBindings);
509 return $mixedQuery->getColumnMeta($iField);
514 if ($this->oDbCacheObject !=
null) {
517 $sEngine =
getParam(
'sys_db_cache_engine');
518 $this->oDbCacheObject =
ch_instance(
'ChWsbCache' . $sEngine);
519 if (!$this->oDbCacheObject->isAvailable()) {
520 $this->oDbCacheObject =
ch_instance(
'ChWsbCacheFile');
536 $aArgs = func_get_args();
540 if (!
getParam(
'sys_db_cache_enable')) {
541 return call_user_func_array(array($this, $sFunc), $aArgs);
548 $mixedRet =
$oCache->getData($sKey);
550 if ($mixedRet !==
null) {
556 $mixedRet = call_user_func_array(array($this, $sFunc),
559 $oCache->setData($sKey, $mixedRet);
571 return $oCache->delData($sKey);
580 $aArgs = func_get_args();
585 $GLOBALS[
'gl_db_cache'][
$sName] = call_user_func_array(array($this, $sFunc), $aArgs);
612 foreach($a
as $k => $v)
613 $s .=
"`{$k}` = " . $this->
escape($v) . $sDiv;
615 return trim(
$s, $sDiv);
624 public function escape($sText, $bReal =
true)
626 $pdoEscapted = $this->link->quote($sText);
635 return 0 === mb_strpos($pdoEscapted,
"'") ? mb_substr($pdoEscapted, 1, -1) : $pdoEscapted;
652 if (is_array($mixed)) {
654 foreach ($mixed
as $v)
655 $s .= (is_numeric($v) ? $v : $this->
escape($v)) .
',';
657 return substr(
$s, 0, -1);
662 return is_numeric($mixed) ? $mixed : ($mixed ? $this->
escape($mixed) :
'NULL');
672 if (is_array($mixed)) {
673 foreach ($mixed
as $k => $v) {
674 $mixed[$k] = $this->
getOne(
"SELECT '$v'");
679 return $this->
getOne(
"SELECT '$mixed'");