Cheetah
ChWsbUpgradeDb.php
Go to the documentation of this file.
1 <?php
2 
8 define( 'CH_UPGRADE_DB_FULL_VISUAL_PROCESSING', true );
9 define( 'CH_UPGRADE_DB_FULL_DEBUG_MODE', true );
10 
12 {
14  protected $link;
15  protected static $instance;
16  protected $oCurrentStmt;
18 
19 
20  function __construct()
21  {
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;
29 
30  if (!$this->link) {
31  $this->connect();
32  }
33  }
34 
38  protected function connect()
39  {
40  $sSocketOrHost = ($this->socket) ? "unix_socket={$this->socket}" : "host={$this->host};port={$this->port}";
41 
42  $this->link = new PDO(
43  "mysql:{$sSocketOrHost};dbname={$this->dbname};charset=utf8",
44  $this->user,
45  $this->password,
46  [
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,
52  ]
53  );
54  }
55 
59  protected function disconnect()
60  {
61  $this->link = null;
62  }
63 
72  public function getRow($sQuery, $aBindings = [], $iFetchStyle = PDO::FETCH_ASSOC)
73  {
74  if ($iFetchStyle != PDO::FETCH_ASSOC && $iFetchStyle != PDO::FETCH_NUM && $iFetchStyle != PDO::FETCH_BOTH) {
75  $iFetchStyle = PDO::FETCH_ASSOC;
76  }
77 
78  $oStmt = $this->res($sQuery, $aBindings);
79  if (!$oStmt)
80  return false;
81 
82  return $oStmt->fetch($iFetchStyle);
83  }
84 
92  public function getColumn($sQuery, $aBindings = [])
93  {
94  $oStmt = $this->res($sQuery, $aBindings);
95  if (!$oStmt)
96  return false;
97 
98  $aResultRows = [];
99  while ($row = $oStmt->fetchColumn()) {
100  $aResultRows[] = $row;
101  }
102 
103  return $aResultRows;
104  }
105 
114  public function getOne($sQuery, $aBindings = [], $iIndex = 0)
115  {
116  $oStmt = $this->res($sQuery, $aBindings);
117  if (!$oStmt)
118  return false;
119 
120  $result = $oStmt->fetch(PDO::FETCH_BOTH);
121  if ($result) {
122  return $result[$iIndex];
123  }
124 
125  return null;
126  }
127 
137  public function getFirstRow($sQuery, $aBindings = [], $iFetchStyle = PDO::FETCH_ASSOC)
138  {
139  if ($iFetchStyle != PDO::FETCH_ASSOC && $iFetchStyle != PDO::FETCH_NUM) {
140  $this->iCurrentFetchStyle = PDO::FETCH_ASSOC;
141  } else {
142  $this->iCurrentFetchStyle = $iFetchStyle;
143  }
144 
145  $oStmt = $this->res($sQuery, $aBindings);
146  if (!$oStmt)
147  return false;
148 
149  $this->oCurrentStmt = $oStmt;
150 
151  $result = $this->oCurrentStmt->fetch($this->iCurrentFetchStyle);
152  if ($result) {
153  return $result;
154  }
155 
156  return [];
157  }
158 
164  public function getNextRow()
165  {
166  $aResult = [];
167 
168  if (!$this->oCurrentStmt) {
169  return $aResult;
170  }
171 
172  $aResult = $this->oCurrentStmt->fetch($this->iCurrentFetchStyle);
173 
174  if ($aResult === false) {
175  $this->oCurrentStmt = null;
176  $this->iCurrentFetchStyle = PDO::FETCH_ASSOC;
177 
178  return [];
179  }
180 
181  return $aResult;
182  }
183 
191  public function getNumRows($oStmt = null)
192  {
193  return $this->getAffectedRows($oStmt);
194  }
195 
202  public function getAffectedRows($oStmt = null)
203  {
204  if ($oStmt) {
205  return $oStmt->rowCount();
206  }
207 
208  if ($this->oCurrentStmt) {
209  return $this->oCurrentStmt->rowCount();
210  }
211 
212  return 0;
213  }
214 
222  public function query($sQuery, $aBindings = [])
223  {
224  $oStmt = $this->res($sQuery, $aBindings);
225  if (!$oStmt)
226  return false;
227 
228  return $oStmt->rowCount();
229  }
230 
239  public function res($sQuery, $aBindings = [], $bReplaying = false)
240  {
241  if (strlen(trim($sQuery)) < 1) {
242  throw new InvalidArgumentException('Please provide a valid sql query');
243  }
244 
245  if ($this->link === null) {
246  $this->connect();
247  }
248 
249  if ($aBindings) {
250  $oStmt = $this->link->prepare($sQuery);
251  $oStmt->execute($aBindings);
252  } else {
253  $oStmt = $this->link->query($sQuery);
254  }
255 
256  return $oStmt;
257  }
258 
267  public function getAll($sQuery, $aBindings = [], $iFetchType = PDO::FETCH_ASSOC)
268  {
269  if ($iFetchType != PDO::FETCH_ASSOC && $iFetchType != PDO::FETCH_NUM && $iFetchType != PDO::FETCH_BOTH) {
270  $iFetchType = PDO::FETCH_ASSOC;
271  }
272 
273  $oStmt = $this->res($sQuery, $aBindings);
274  if (!$oStmt)
275  return false;
276 
277  return $oStmt->fetchAll($iFetchType);
278  }
279 
288  public function fillArray($oStmt, $iFetchType = PDO::FETCH_ASSOC)
289  {
290  if ($iFetchType != PDO::FETCH_ASSOC && $iFetchType != PDO::FETCH_NUM && $iFetchType != PDO::FETCH_BOTH) {
291  $iFetchType = PDO::FETCH_ASSOC;
292  }
293 
294  $aResult = [];
295  while ($row = $oStmt->fetch($iFetchType)) {
296  $aResult[] = $row;
297  }
298 
299  return $aResult;
300  }
301 
311  public function getAllWithKey($sQuery, $sFieldKey, $aBindings = [], $iFetchType = PDO::FETCH_ASSOC)
312  {
313  $oStmt = $this->res($sQuery, $aBindings);
314  if (!$oStmt)
315  return false;
316 
317  $aResult = [];
318  if ($oStmt) {
319  while ($row = $oStmt->fetch($iFetchType)) {
320  $aResult[$row[$sFieldKey]] = $row;
321  }
322 
323  $oStmt = null;
324  }
325 
326  return $aResult;
327  }
328 
338  public function getPairs($sQuery, $sFieldKey, $sFieldValue, $aBindings = [])
339  {
340  $oStmt = $this->res($sQuery, $aBindings);
341  if (!$oStmt)
342  return false;
343 
344  $aResult = [];
345  if ($oStmt) {
346  while ($row = $oStmt->fetch()) {
347  $aResult[$row[$sFieldKey]] = $row[$sFieldValue];
348  }
349 
350  $oStmt = null;
351  }
352 
353  return $aResult;
354  }
355 
361  public function lastId()
362  {
363  return $this->link->lastInsertId();
364  }
365 
371  public function listTables()
372  {
373  $aResult = [];
374 
375  $oStmt = $this->link->query("SHOW TABLES FROM `{$this->dbname}`");
376  if (!$oStmt)
377  return false;
378 
379  while ($row = $oStmt->fetch(PDO::FETCH_NUM)) {
380  $aResult[] = $row[0];
381  }
382 
383  return $aResult;
384  }
385 
386  public function getFields($sTable)
387  {
388  $oFieldsStmt = $this->link->query("SHOW COLUMNS FROM `{$sTable}`");
389  if (!$oFieldsStmt)
390  return false;
391 
392  $aResult = ['original' => [], 'uppercase' => []];
393  while ($row = $oFieldsStmt->fetch()) {
394  $sName = $row['Field'];
395  $aResult['original'][] = $sName;
396  $aResult['uppercase'][] = strtoupper($sName);
397  }
398 
399  return $aResult;
400  }
401 
402  public function isFieldExists($sTable, $sFieldName)
403  {
404  $aFields = $this->getFields($sTable);
405 
406  return in_array(strtoupper($sFieldName), $aFields['uppercase']);
407  }
408 
409  public function fetchField($mixedQuery, $iField, $aBindings = [])
410  {
411  if(is_string($mixedQuery))
412  $mixedQuery = $this->res($mixedQuery, $aBindings);
413 
414  return $mixedQuery->getColumnMeta($iField);
415  }
416 
417  public function isIndexExists($sTable, $sIndexName)
418  {
419  $bIndex = false;
420 
421  $aIndexes = $this->getAll("SHOW INDEXES FROM `" . $sTable . "`");
422  if (!$aIndexes)
423  return null;
424 
425  foreach($aIndexes as $aIndex)
426  if($aIndex['Key_name'] == $sIndexName) {
427  $bIndex = true;
428  break;
429  }
430 
431  return $bIndex;
432  }
433 
440  public function escape($sText, $bReal = false)
441  {
442  $pdoEscapted = $this->link->quote($sText);
443 
444  if ($bReal) {
445  return $pdoEscapted;
446  }
447 
448  // don't need the actual quotes pdo adds, so it
449  // behaves kinda like mysql_real_escape_string
450  // p.s. things we do for legacy code
451  return trim($pdoEscapted, "'");
452  }
453 
454  function executeSQL($sPath, $aReplace = array (), $isBreakOnError = true)
455  {
456  if(!file_exists($sPath) || !($rHandler = fopen($sPath, "r")))
457  return array ('query' => "fopen($sPath, 'r')", 'error' => 'file not found or permission denied');
458 
459  $sQuery = "";
460  $sDelimiter = ';';
461  $aResult = array();
462  while(!feof($rHandler)) {
463  $sStr = trim(fgets($rHandler));
464 
465  if(empty($sStr) || $sStr[0] == "" || $sStr[0] == "#" || ($sStr[0] == "-" && $sStr[1] == "-"))
466  continue;
467 
468  //--- Change delimiter ---//
469  if(strpos($sStr, "DELIMITER //") !== false || strpos($sStr, "DELIMITER ;") !== false) {
470  $sDelimiter = trim(str_replace('DELIMITER', '', $sStr));
471  continue;
472  }
473 
474  $sQuery .= $sStr;
475 
476  //--- Check for multiline query ---//
477  if(substr($sStr, -strlen($sDelimiter)) != $sDelimiter)
478  continue;
479 
480  //--- Execute query ---//
481  if ($aReplace)
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);
486  if(!$rResult) {
487  $aErrInfo = $this->link->errorInfo();
488  $aResult[] = array('query' => $sQuery, 'error' => $aErrInfo[2]);
489  if ($isBreakOnError)
490  break;
491  }
492 
493  $sQuery = "";
494  }
495  fclose($rHandler);
496 
497  return empty($aResult) ? true : $aResult;
498  }
499 }
ChWsbUpgradeDb\$oCurrentStmt
$oCurrentStmt
Definition: ChWsbUpgradeDb.php:16
ChWsbUpgradeDb\disconnect
disconnect()
Definition: ChWsbUpgradeDb.php:59
ChWsbUpgradeDb\$instance
static $instance
Definition: ChWsbUpgradeDb.php:15
ChWsbUpgradeDb\getFirstRow
getFirstRow($sQuery, $aBindings=[], $iFetchStyle=PDO::FETCH_ASSOC)
Definition: ChWsbUpgradeDb.php:137
true
if(!defined("TRUE_VAL")) define("TRUE_VAL" true
Definition: constants.inc.php:8
ChWsbUpgradeDb\listTables
listTables()
Definition: ChWsbUpgradeDb.php:371
ChWsbUpgradeDb\$dbname
$dbname
Definition: ChWsbUpgradeDb.php:13
ChWsbUpgradeDb\getOne
getOne($sQuery, $aBindings=[], $iIndex=0)
Definition: ChWsbUpgradeDb.php:114
ChWsbUpgradeDb\lastId
lastId()
Definition: ChWsbUpgradeDb.php:361
$aResult
$aResult
Definition: index.php:19
ChWsbUpgradeDb\isIndexExists
isIndexExists($sTable, $sIndexName)
Definition: ChWsbUpgradeDb.php:417
php
ChWsbUpgradeDb
Definition: ChWsbUpgradeDb.php:12
ChWsbUpgradeDb\getNextRow
getNextRow()
Definition: ChWsbUpgradeDb.php:164
ChWsbUpgradeDb\getFields
getFields($sTable)
Definition: ChWsbUpgradeDb.php:386
ChWsbUpgradeDb\$link
$link
Definition: ChWsbUpgradeDb.php:14
ChWsbUpgradeDb\getNumRows
getNumRows($oStmt=null)
Definition: ChWsbUpgradeDb.php:191
ChWsbUpgradeDb\$user
$user
Definition: ChWsbUpgradeDb.php:13
$iIndex
$iIndex
Definition: bottom_menu_compose.php:142
$aFields
$aFields
Definition: preValues.php:19
ChWsbUpgradeDb\$port
$port
Definition: ChWsbUpgradeDb.php:13
ChWsbUpgradeDb\getRow
getRow($sQuery, $aBindings=[], $iFetchStyle=PDO::FETCH_ASSOC)
Definition: ChWsbUpgradeDb.php:72
ChWsbUpgradeDb\getPairs
getPairs($sQuery, $sFieldKey, $sFieldValue, $aBindings=[])
Definition: ChWsbUpgradeDb.php:338
ChWsbUpgradeDb\executeSQL
executeSQL($sPath, $aReplace=array(), $isBreakOnError=true)
Definition: ChWsbUpgradeDb.php:454
ChWsbUpgradeDb\fetchField
fetchField($mixedQuery, $iField, $aBindings=[])
Definition: ChWsbUpgradeDb.php:409
ChWsbUpgradeDb\getAffectedRows
getAffectedRows($oStmt=null)
Definition: ChWsbUpgradeDb.php:202
ChWsbUpgradeDb\$iCurrentFetchStyle
$iCurrentFetchStyle
Definition: ChWsbUpgradeDb.php:17
ChWsbUpgradeDb\escape
escape($sText, $bReal=false)
Definition: ChWsbUpgradeDb.php:440
ChWsbUpgradeDb\getAllWithKey
getAllWithKey($sQuery, $sFieldKey, $aBindings=[], $iFetchType=PDO::FETCH_ASSOC)
Definition: ChWsbUpgradeDb.php:311
ChWsbUpgradeDb\connect
connect()
Definition: ChWsbUpgradeDb.php:38
ChWsbUpgradeDb\__construct
__construct()
Definition: ChWsbUpgradeDb.php:20
ChWsbUpgradeDb\$password
$password
Definition: ChWsbUpgradeDb.php:13
ChWsbUpgradeDb\res
res($sQuery, $aBindings=[], $bReplaying=false)
Definition: ChWsbUpgradeDb.php:239
ChWsbUpgradeDb\isFieldExists
isFieldExists($sTable, $sFieldName)
Definition: ChWsbUpgradeDb.php:402
ChWsbUpgradeDb\fillArray
fillArray($oStmt, $iFetchType=PDO::FETCH_ASSOC)
Definition: ChWsbUpgradeDb.php:288
ChWsbUpgradeDb\$host
$host
Definition: ChWsbUpgradeDb.php:13
ChWsbUpgradeDb\query
query($sQuery, $aBindings=[])
Definition: ChWsbUpgradeDb.php:222
empty
Attr AllowedRel this is empty
Definition: Attr.AllowedRel.txt:7
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
ChWsbUpgradeDb\$socket
$socket
Definition: ChWsbUpgradeDb.php:13
$sName
$sName
Definition: ChWsbAdminTools.php:853
ChWsbUpgradeDb\getColumn
getColumn($sQuery, $aBindings=[])
Definition: ChWsbUpgradeDb.php:92
ChWsbUpgradeDb\getAll
getAll($sQuery, $aBindings=[], $iFetchType=PDO::FETCH_ASSOC)
Definition: ChWsbUpgradeDb.php:267