Tutorial Extension  1.0.0
SellerDeck Extensions - Tutorial Extension
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
CDataBase Class Reference

Inherited by CDataBaseAccess, and CDataBaseSqlServer.

Public Member Functions

 __construct ($aConnectionDetails, $sDatabaseID)
 
 GetDatabaseID ()
 
 GetDataBases ()
 
 GetDBUser ()
 
 GetDBAuthType ()
 
 Expression ($sExpression)
 
 QuoteValue ($vData)
 
 QuoteTable ($vTableName)
 
 QuoteColumn ($vData)
 
 QuoteCompundColumn ($vUnquotedElement)
 
 GetDB ()
 
 GetLastQuery ()
 
 SQL ($sQuery, $nReturnType=\PDO::FETCH_ASSOC)
 
 Insert ($sTable, $aValues, $bAutoID=true, $sIDColumn=null)
 
 Update ($sTable, $aValues, $aWhere=[[1, '', '']])
 
 Select ($sTable, $vColumns="*", $aWhere=[], $aOthers=[], $nReturnType=\PDO::FETCH_ASSOC)
 
 CompileWhere ($aWhere=[], $aBoolOp=BOOP_OP_AND)
 
 CompileJoinOn ($aJoinOn=[])
 

Protected Member Functions

 Quote ($sData, $sQuoteBegin, $sQuoteEnd)
 
 Sanitize ($sData)
 
 QuoteArray ($aUnquotedArray, $bValue=true)
 
 QuoteUpdateArray ($aUnquotedArray)
 
 QuoteValueArray ($aUnquotedArray)
 
 QuoteColumnArray ($aUnquotedArray)
 
 QuoteValueList ($aUnquotedArray, $sSeparator=", ")
 
 QuoteColumnList ($aUnquotedArray, $sSeparator=", ")
 
 BasicInsert ($sTable, $aValues)
 
 CompileSelectSql ($aQuery=[])
 
 CompileWhereElement ($aValue=[])
 
 IsElementFormat ($aValue)
 
 CompileLimit ($vLimit)
 
 CompileJoin ($aJoins)
 
 ConcatenateJoins ($aJoinDirections, $aJoinTables, $aJoinOns)
 
 CompileGroupBy ($aGroupByList)
 
 CompileOrderBy ($aOrderByList)
 
 BoolValue ($bData)
 

Protected Attributes

 $m_oDB = null
 
 $m_sDataBaseID = DB_CATALOG
 
 $m_sDBAuthType = "DB"
 
 $m_sDBUser = "dbo"
 
 $m_aDataBases = []
 
 $m_sColumnQuoteBegin = '`'
 
 $m_sColumnQuoteEnd = '`'
 
 $m_sValueQuoteBegin = "'"
 
 $m_sValueQuoteEnd = "'"
 
 $m_sNullValue = "NULL"
 
 $m_sLastQuery = ""
 

Detailed Description

Definition at line 14 of file CDataBase.php.

Constructor & Destructor Documentation

__construct (   $aConnectionDetails,
  $sDatabaseID 
)

__construct - Object constructor method

public

Parameters
array$aConnectionDetails
Returns
void

Definition at line 79 of file CDataBase.php.

80  {
81  $sDsn = arr_get($aConnectionDetails, 'sDsn', '');
82  $this->m_sDBUser = arr_get($aConnectionDetails, 'sUser', '');
83  $sPassword = arr_get($aConnectionDetails, 'sPassword', '');
84  $this->m_sDataBaseID = arr_get($aConnectionDetails, 'sDatabaseID', '');
85  $this->m_sDBAuthType = arr_get($aConnectionDetails, 'sDBAuthType', '');
86  $this->m_aDataBases = arr_get($aConnectionDetails, 'aDataBases', []);
87  $this->m_oDB = new \PDO($sDsn, $this->m_sDBUser, $sPassword);
88  \SDExtension\Helper\CLogger::get(LOG_CHANNEL_NAME)->addInfo("Connected to Database: $sDatabaseID");
89  }
static get($sChannel="default", $sLogRoot="")
Definition: CLogger.php:90

Member Function Documentation

BasicInsert (   $sTable,
  $aValues 
)
protected

BasicInsert - Generates and runs Basic INSERT expression

protected

Parameters
string$sTableTable name
array$aValuesArray of (DB Column => Value) pairs
Returns
null

Definition at line 444 of file CDataBase.php.

445  {
446  $sTableQuoted = $this->QuoteTable($sTable);
447  $sColumnList = $this->QuoteColumnList(array_keys($aValues));
448  $sValueList = $this->QuoteValueList(array_values($aValues));
449  $sQuery = "INSERT INTO $sTableQuoted (" . $sColumnList . ") VALUES (" . $sValueList . ");";
450  $nResult = $this->m_oDB->exec($sQuery);
451  \SDExtension\Helper\CLogger::get(LOG_CHANNEL_NAME)->addDebug(__FUNCTION__ . " : $sQuery");
452  return null;
453  }
static get($sChannel="default", $sLogRoot="")
Definition: CLogger.php:90
QuoteValueList($aUnquotedArray, $sSeparator=", ")
Definition: CDataBase.php:375
QuoteColumnList($aUnquotedArray, $sSeparator=", ")
Definition: CDataBase.php:389
QuoteTable($vTableName)
Definition: CDataBase.php:220
BoolValue (   $bData)
protected

BoolValue - Converts Boolean to appropriate value depending on driver

protected

Parameters
bool$bDataBoolean value
Returns
bool

Definition at line 917 of file CDataBase.php.

918  {
919  return $bData;
920  }
CompileGroupBy (   $aGroupByList)
protected

CompileGroupBy - Compiles GROUP BY expression

protected

Parameters
array$aGroupByListArray of GROUP BYs
Returns
string GROUP BY expression

Definition at line 880 of file CDataBase.php.

881  {
882  $aGroupBy = [];
883  foreach ($aGroupByList as $nKey => $aGroupByEntry)
884  {
885  $aGroupBy[] = $this->QuoteCompundColumn(arr_get($aGroupByEntry, 0, "")) . "." .
886  $this->QuoteCompundColumn(arr_get($aGroupByEntry, 1, ""));
887  }
888  return implode(", ", $aGroupBy);
889  }
QuoteCompundColumn($vUnquotedElement)
Definition: CDataBase.php:282
CompileJoin (   $aJoins)
protected

CompileJoin - Compiles JOIN expression

protected

Parameters
array$aJoinsArray of JOINs
Returns
string JOIN expression

Definition at line 828 of file CDataBase.php.

829  {
830  $aJoinDirections = [];
831  $aJoinTables = [];
832  $aJoinOns = [];
833  foreach ($aJoins as $aJoin)
834  {
835  $sJoinTable = "";
836  switch (strtolower(arr_get($aJoin, 0, "inner")))
837  {
838  case "right":
839  $aJoinDirections[] = "RIGHT JOIN";
840  break;
841  case "left":
842  $aJoinDirections[] = "LEFT JOIN";
843  break;
844  default:
845  $aJoinDirections[] = "INNER JOIN";
846  break;
847  }
848  $aJoinTables[] = $this->QuoteTable(arr_get($aJoin, 1, ""));
849  $aJoinOns[] = arr_get($aJoin, 2, []);
850  }
851  return $this->ConcatenateJoins($aJoinDirections, $aJoinTables, $aJoinOns);
852  }
ConcatenateJoins($aJoinDirections, $aJoinTables, $aJoinOns)
Definition: CDataBase.php:863
QuoteTable($vTableName)
Definition: CDataBase.php:220
CompileJoinOn (   $aJoinOn = [])

CompileJoinOn - Generates JOIN ... ON expression

Todo:
"OR" is missing from WHERE

protected

Parameters
array$aJoinOnArray of conditions
Returns
string WHERE statement

Definition at line 778 of file CDataBase.php.

779  {
780  $aJoinOnList = [];
781  foreach ($aJoinOn as $sKey => $aValue)
782  {
783  if (is_array($aValue))
784  {
785  if (count($aValue) == 3)
786  {
787  $aJoinOnList[] = $this->QuoteCompundColumn($aValue[0]) .
788  " $aValue[1] " .
789  $this->QuoteCompundColumn($aValue[2]);
790  }
791  elseif (count($aValue) == 2)
792  {
793  $aJoinOnList[] = $this->QuoteCompundColumn($aValue[0]) .
794  " = " .
795  $this->QuoteCompundColumn($aValue[1]);
796  }
797  }
798  }
799  return implode(' AND ', $aJoinOnList);
800  }
QuoteCompundColumn($vUnquotedElement)
Definition: CDataBase.php:282
CompileLimit (   $vLimit)
protected

CompileLimit - Compiles LIMIT expression

protected

Parameters
array | string$vLimitArray or String of LIMIT(s)
Returns
string LIMIT expression

Definition at line 809 of file CDataBase.php.

810  {
811  if (is_array($vLimit))
812  {
813  return implode(", ", $vLimit);
814  }
815  else
816  {
817  return $vLimit;
818  }
819  }
CompileOrderBy (   $aOrderByList)
protected

CompileOrderBy - Compiles ORDER BY expression

protected

Parameters
array$aOrderByListArray of ORDER BYs
Returns
string ORDER BY expression

Definition at line 898 of file CDataBase.php.

899  {
900  $aOrderBy = [];
901  foreach ($aOrderByList as $nKey => $aOrderByEntry)
902  {
903  $aOrderBy[] = $this->QuoteCompundColumn(arr_get($aOrderByEntry, 0, ""))
904  . " "
905  . strtoupper(arr_get($aOrderByEntry, 1, "ASC"));
906  }
907  return implode(", ", $aOrderBy);
908  }
QuoteCompundColumn($vUnquotedElement)
Definition: CDataBase.php:282
CompileSelectSql (   $aQuery = [])
protected

CompileSelectSql - Compiles SQL expression

protected

Parameters
array$aWhereArray of conditions
Returns
string WHERE statement

Definition at line 630 of file CDataBase.php.

631  {
632  $sSelectSQL = "";
633  if (!empty($aQuery["select"]))
634  {
635  $sSelectSQL .= "SELECT $aQuery[select] ";
636  }
637  else
638  {
639  $sSelectSQL .= "SELECT * ";
640  }
641  if (!empty($aQuery["from"]))
642  {
643  $sSelectSQL .= "FROM $aQuery[from] ";
644  }
645  if (!empty($aQuery["join"]))
646  {
647  $sSelectSQL .= " $aQuery[join] ";
648  }
649  if (!empty($aQuery["where"]))
650  {
651  $sSelectSQL .= "WHERE $aQuery[where] ";
652  }
653  if (!empty($aQuery["group_by"]))
654  {
655  $sSelectSQL .= "GROUP BY $aQuery[group_by] ";
656  }
657  if (!empty($aQuery["order_by"]))
658  {
659  $sSelectSQL .= "ORDER BY $aQuery[order_by] ";
660  }
661  if (!empty($aQuery["limit"]))
662  {
663  $sSelectSQL .= "LIMIT $aQuery[limit] ";
664  }
665  return $sSelectSQL;
666  }
CompileWhere (   $aWhere = [],
  $aBoolOp = BOOP_OP_AND 
)

CompileWhere - Generates WHERE expression

Used in SELECT and UPDATE

Todo:
"OR" is missing from WHERE

protected

Parameters
array$aWhereArray of conditions
Returns
string WHERE statement

Definition at line 742 of file CDataBase.php.

743  {
744  $aWhereList = [];
745  if (is_array($aWhere))
746  {
747  foreach ($aWhere as $vKey => $aValue)
748  {
749  if (BOOP_OP_AND === $vKey || BOOP_OP_OR === $vKey)
750  {
751  $aWhereList[] = $this->CompileWhere($aValue, $vKey);
752  }
753  else
754  {
755  if ($this->IsElementFormat($aValue))
756  {
757  $aWhereList[] = $this->CompileWhereElement($aValue);
758  }
759  else
760  {
761  $aWhereList[] = "(" . $this->CompileWhere($aValue, $aBoolOp) . ")";
762  }
763  }
764  }
765  }
766  return implode(" $aBoolOp ", $aWhereList);
767  }
CompileWhereElement($aValue=[])
Definition: CDataBase.php:675
CompileWhere($aWhere=[], $aBoolOp=BOOP_OP_AND)
Definition: CDataBase.php:742
CompileWhereElement (   $aValue = [])
protected

CompileWhereElement - Compiles a single WHERE element

protected

Parameters
array$aValueArray of a single condition
Returns
string WHERE statement

Definition at line 675 of file CDataBase.php.

676  {
677  if (count($aValue) == 3)
678  {
679  return $this->QuoteColumn($aValue[0]) .
680  " $aValue[1] " .
681  $this->QuoteValue($aValue[2]);
682  }
683  elseif (count($aValue) == 2)
684  {
685  $sWhere = $this->QuoteColumn($aValue[0]);
686  if (starts_with($aValue[1], SQL_EXPRESSION_PREFIX))
687  {
688  return $sWhere .
689  " " .
690  remove_prefix($aValue[1], SQL_EXPRESSION_PREFIX);
691  }
692  elseif (starts_with($aValue[1], SQL_TYPE_STRING))
693  {
694  return $sWhere .
695  " LIKE " .
696  $this->QuoteValue($aValue[1]);
697  }
698  else
699  {
700  return $sWhere .
701  " = " .
702  $this->QuoteValue($aValue[1]);
703  }
704  }
705  else
706  {
707  return $aValue[0];
708  }
709  }
ConcatenateJoins (   $aJoinDirections,
  $aJoinTables,
  $aJoinOns 
)
protected

ConcatenateJoins - Concatenates JOIN expression

protected

Parameters
array$aJoinDirectionsArray of JOIN Directions
array$aJoinTablesArray of JOIN Tables
array$aJoinOnsArray of JOIN ONs
Returns
string JOIN expression

Definition at line 863 of file CDataBase.php.

864  {
865  $sJoins = "";
866  foreach ($aJoinTables as $nKey => $sJoinTable)
867  {
868  $sJoins .= arr_get($aJoinDirections, $nKey, "INNER JOIN") . " $sJoinTable ON (" . $this->CompileJoinOn(arr_get($aJoinOns, $nKey, "")) . ") ";
869  }
870  return $sJoins;
871  }
CompileJoinOn($aJoinOn=[])
Definition: CDataBase.php:778
Expression (   $sExpression)

Expression - Mark SQL Expression to act as CDataBase Expression (ie. not to be surounded by quotes)

public

Parameters
string$sExpressionSQL Expression to be marked as CDataBase expression
Returns
string Marked as expression

Definition at line 142 of file CDataBase.php.

143  {
144  return SQL_EXPRESSION_PREFIX . $sExpression;
145  }
GetDatabaseID ( )

GetDatabaseID - Database ID

public

Returns
string Database ID

Definition at line 97 of file CDataBase.php.

98  {
99  return $this->m_sDataBaseID;
100  }
GetDataBases ( )

GetDataBases - DataBases array

public

Returns
array DataBase

Definition at line 108 of file CDataBase.php.

109  {
110  return $this->m_aDataBases;
111  }
GetDB ( )

GetDB - DB

public

Returns
object DB

Definition at line 406 of file CDataBase.php.

407  {
408  return $this->m_oDB;
409  }
GetDBAuthType ( )

GetDBAuthType - DataBase Authentication Type

public

Returns
string DataBase Authentication Type

Definition at line 130 of file CDataBase.php.

131  {
132  return $this->m_sDBAuthType;
133  }
GetDBUser ( )

GetDBUser - DataBase User

public

Returns
string DataBase User

Definition at line 119 of file CDataBase.php.

120  {
121  return $this->m_sDBUser;
122  }
GetLastQuery ( )

GetLastQuery - Returns last query SQL

public

Returns
string Quoted array

Definition at line 417 of file CDataBase.php.

418  {
419  return $this->m_sLastQuery;
420  }
Insert (   $sTable,
  $aValues,
  $bAutoID = true,
  $sIDColumn = null 
)

Insert - Generates and runs INSERT expression

Todo:
"OR" is missing from WHERE

public

Parameters
string$sTableTable name
array$aValuesArray of (DB Column => Value) pairs
bool$bAutoIDHas Auto ID?
string$sIDColumnID Column
Returns
null | mixed Last inserted ID

Definition at line 467 of file CDataBase.php.

468  {
469  //
470  // Default INSERT, without ID column
471  //
472  if ($sIDColumn == null || $bAutoID)
473  {
474  return $this->BasicInsert($sTable, $aValues);
475  }
476  $sQuery = "";
477  $sTableQuoted = $this->QuoteTable($sTable);
478  $bInserted = false;
479  $nCounter = 0;
480  $nLastInsertID = 0;
481  //
482  // Repeated INSERT
483  //
484  while (!$bInserted && $nCounter < MAX_RETRY_BY_INSERT)
485  {
486  $sIDColumnQuoted = $this->QuoteColumn($sIDColumn);
487  $sMaxID = "MaxIDOf$sTable";
488  $oMaxIDResult = $this->Select($sTable, [SQL_EXPRESSION_PREFIX . "MAX($sIDColumnQuoted) AS $sMaxID"]);
489  //
490  // Can't read Table
491  //
492  if ($oMaxIDResult === false)
493  {
494  \SDExtension\Helper\CLogger::get(LOG_CHANNEL_NAME)->addWarning("Can't read $sTable when getting Last ID!");
495  return null;
496  }
497  $aMaxIDResult = $oMaxIDResult->fetch(\PDO::FETCH_ASSOC);
498  $nMaxID = $aMaxIDResult[$sMaxID];
499  //
500  // First record
501  //
502  if (empty($nMaxID))
503  {
504  $nMaxID = 1;
505  }
506  //
507  // Not numeric value: can't be increased!
508  //
509  elseif (!is_numeric($nMaxID))
510  {
511  \SDExtension\Helper\CLogger::get(LOG_CHANNEL_NAME)->addWarning("Table ID [$sTable.$sIDColumn] is not numeric [$nMaxID]!");
512  return null;
513  }
514  //
515  // Attepmting INSERT
516  //
517  $nNewID = $nMaxID + INCREASE_ID_BY_INSERT;
518  $aValues[$sIDColumn] = $nNewID;
519  $sColumnList = $this->QuoteColumnList(array_keys($aValues));
520  $sValueList = $this->QuoteValueList(array_values($aValues));
521  $sQuery = "INSERT INTO $sTableQuoted (" . $sColumnList . ") VALUES (" . $sValueList . ");";
522  $nResult = $this->m_oDB->exec($sQuery);
523  //
524  // Unsuccessful #1 invalid query?
525  //
526  if ($nResult === false)
527  {
528  \SDExtension\Helper\CLogger::get(LOG_CHANNEL_NAME)->addWarning("Can't INSERT into table [$sTable] with ID [$sIDColumn] value $nNewID!");
529  }
530  //
531  // Unsuccessful #2 zero records inserted.
532  //
533  elseif ($nResult <= 0)
534  {
535  \SDExtension\Helper\CLogger::get(LOG_CHANNEL_NAME)->addWarning("0 records inserted.");
536  }
537  //
538  // Insertion successful
539  //
540  else
541  {
542  $nLastInsertID = $nNewID;
543  $bInserted = true;
544  }
545  $nCounter++;
546  }
547  $this->m_sLastQuery = $sQuery;
548  \SDExtension\Helper\CLogger::get(LOG_CHANNEL_NAME)->addDebug(__FUNCTION__ . " : " . $sQuery);
549  //
550  // Final insertion unsuccessful
551  //
552  if (!$bInserted)
553  {
554  \SDExtension\Helper\CLogger::get(LOG_CHANNEL_NAME)->addWarning("Can't INSERT into table [$sTable] with ID [$sIDColumn] value $nNewID!");
555  return null;
556  }
557  return $nLastInsertID;
558  }
static get($sChannel="default", $sLogRoot="")
Definition: CLogger.php:90
Select($sTable, $vColumns="*", $aWhere=[], $aOthers=[], $nReturnType=\PDO::FETCH_ASSOC)
Definition: CDataBase.php:594
QuoteValueList($aUnquotedArray, $sSeparator=", ")
Definition: CDataBase.php:375
BasicInsert($sTable, $aValues)
Definition: CDataBase.php:444
QuoteColumnList($aUnquotedArray, $sSeparator=", ")
Definition: CDataBase.php:389
QuoteTable($vTableName)
Definition: CDataBase.php:220
IsElementFormat (   $aValue)
protected

IsElementFormat - Checks if value is a well formatted WHERE element

protected

Parameters
array$aValueValue
Returns
bool

Definition at line 718 of file CDataBase.php.

719  {
720  if (is_array($aValue) && isset($aValue[0]))
721  {
722  if (is_array($aValue[0]) && isset($aValue[0][0]))
723  {
724  return !is_array($aValue[0][0]);
725  }
726  return true;
727  }
728  return false;
729  }
Quote (   $sData,
  $sQuoteBegin,
  $sQuoteEnd 
)
protected

Quote - Surrounds string with given prefix and suffix (quote signs)

protected

Parameters
string$sDataData to quote
string$sQuoteBeginBegin quote sign
string$sQuoteBeginEnd quote sign
Returns
string Quoted string

Definition at line 156 of file CDataBase.php.

157  {
158  return $sQuoteBegin . $sData . $sQuoteEnd;
159  }
QuoteArray (   $aUnquotedArray,
  $bValue = true 
)
protected

QuoteArray - Quotes array elements

protected

Parameters
array$aUnquotedArrayArray to quote
bool$bValue
Returns
array Quoted array

Definition at line 307 of file CDataBase.php.

308  {
309  $aQuotedArray = [];
310  foreach ($aUnquotedArray as $sData)
311  {
312  if ($bValue)
313  {
314  $aQuotedArray[] = $this->QuoteValue($sData);
315  }
316  else
317  {
318  $aQuotedArray[] = $this->QuoteColumn($sData);
319  }
320  }
321  return $aQuotedArray;
322  }
QuoteColumn (   $vData)

QuoteColumn - Quotes DB Column string

public

Parameters
string | array$vDataData to quote
Returns
string Quoted string

Definition at line 244 of file CDataBase.php.

245  {
246  $sTableName = "";
247  if (is_array($vData))
248  {
249  $sTableName = array_shift($vData);
250  $vData = array_shift($vData);
251  if (empty($vData))
252  {
253  $vData = $sTableName;
254  }
255  else
256  {
257  $sTableName = $this->Quote($sTableName, $this->m_sColumnQuoteBegin, $this->m_sColumnQuoteEnd) . ".";
258  }
259  }
260  if (starts_with($vData, SQL_EXPRESSION_PREFIX))
261  {
262  return remove_prefix($vData, SQL_EXPRESSION_PREFIX);
263  }
264  if (starts_with($vData, SQL_DATA_PREFIX))
265  {
266  return $this->QuoteValue(remove_prefix($vData, SQL_DATA_PREFIX));
267  }
268  if ($vData == "*")
269  {
270  return $vData;
271  }
272  return $sTableName . $this->Quote($vData, $this->m_sColumnQuoteBegin, $this->m_sColumnQuoteEnd);
273  }
Quote($sData, $sQuoteBegin, $sQuoteEnd)
Definition: CDataBase.php:156
QuoteColumnArray (   $aUnquotedArray)
protected

QuoteColumnArray - Quotes array elements, with DB Column quotes

protected

Parameters
array$aUnquotedArrayArray to quote
Returns
array Quoted array

Definition at line 362 of file CDataBase.php.

363  {
364  return $this->QuoteArray($aUnquotedArray, false);
365  }
QuoteArray($aUnquotedArray, $bValue=true)
Definition: CDataBase.php:307
QuoteColumnList (   $aUnquotedArray,
  $sSeparator = ", " 
)
protected

QuoteColumnList - Generates string of quoted DB Columns used in INSERT expressions

protected

Parameters
array$aUnquotedArrayArray to quote
string$sSeparatorSeparator
Returns
array Quoted array

Definition at line 389 of file CDataBase.php.

390  {
391  $aProcessedUnquotedArray = array();
392  foreach ($aUnquotedArray as $vUnquotedElement)
393  {
394  $aProcessedUnquotedArray[] = $this->QuoteCompundColumn($vUnquotedElement);
395  }
396  $sList = implode($sSeparator, $aProcessedUnquotedArray);
397  return $sList;
398  }
QuoteCompundColumn($vUnquotedElement)
Definition: CDataBase.php:282
QuoteCompundColumn (   $vUnquotedElement)

QuoteCompundColumn - Quotes DB Column string or array

public

Parameters
string | array$vUnquotedElementData to quote
Returns
string Quoted string

Definition at line 282 of file CDataBase.php.

283  {
284  if (is_array($vUnquotedElement))
285  {
286  $sAlias = "";
287  if (count($vUnquotedElement) > 2)
288  {
289  $sAlias = " AS " . $this->QuoteColumn(array_pop($vUnquotedElement));
290  }
291  return implode('.', array_map(array($this, 'QuoteColumn'), $vUnquotedElement)) . $sAlias;
292  }
293  else
294  {
295  return $this->QuoteColumn($vUnquotedElement);
296  }
297  }
QuoteTable (   $vTableName)

QuoteTable - Quotes Table name

public

Parameters
string | array$vTableNameTable name
Returns
string Quoted Table name

Definition at line 220 of file CDataBase.php.

221  {
222  if (is_array($vTableName))
223  {
224  $sTableName = array_shift($vTableName);
225  $sTableAlias = array_shift($vTableName);
226  return
227  $this->Quote($sTableName, $this->m_sColumnQuoteBegin, $this->m_sColumnQuoteEnd) .
228  (!empty($sTableAlias) ? " AS " .
229  $this->Quote($sTableAlias, $this->m_sColumnQuoteBegin, $this->m_sColumnQuoteEnd) : "");
230  }
231  else
232  {
233  return $this->Quote($vTableName, $this->m_sColumnQuoteBegin, $this->m_sColumnQuoteEnd);
234  }
235  }
Quote($sData, $sQuoteBegin, $sQuoteEnd)
Definition: CDataBase.php:156
QuoteUpdateArray (   $aUnquotedArray)
protected

QuoteUpdateArray - Generates array of quoted parameters used in UPDATE expressions

protected

Parameters
array$aUnquotedArrayArray to quote
Returns
string Quoted string

Definition at line 331 of file CDataBase.php.

332  {
333  $aQuotedArray = array();
334  foreach ($aUnquotedArray as $sKey => $sData)
335  {
336  $aQuotedArray[] = $this->QuoteColumn($sKey) .
337  " = " .
338  $this->QuoteValue($sData);
339  }
340  return implode(", ", $aQuotedArray);
341  }
QuoteValue (   $vData)

QuoteValue - Quotes Value string

public

Parameters
mixed$vDataData to quote
Returns
string Quoted string

Definition at line 180 of file CDataBase.php.

181  {
182  if (starts_with($vData, SQL_COLUMN))
183  {
184  $vData = remove_prefix($vData, SQL_COLUMN);
185  return $this->Quote($vData, $this->m_sColumnQuoteBegin, $this->m_sColumnQuoteEnd);
186  }
187  if (starts_with($vData, SQL_TYPE_STRING))
188  {
189  $vData = remove_prefix($vData, SQL_TYPE_STRING);
190  $vData = $this->Sanitize($vData);
191  return $this->Quote($vData, $this->m_sValueQuoteBegin, $this->m_sValueQuoteEnd);
192  }
193  if (starts_with($vData, SQL_EXPRESSION_PREFIX))
194  {
195  return remove_prefix($vData, SQL_EXPRESSION_PREFIX);
196  }
197  if (is_bool($vData))
198  {
199  return $this->BoolValue($vData);
200  }
201  if (is_numeric($vData))
202  {
203  return $vData;
204  }
205  if ($vData == null)
206  {
207  return $this->m_sNullValue;
208  }
209  $vData = $this->Sanitize($vData);
210  return $this->Quote($vData, $this->m_sValueQuoteBegin, $this->m_sValueQuoteEnd);
211  }
Quote($sData, $sQuoteBegin, $sQuoteEnd)
Definition: CDataBase.php:156
QuoteValueArray (   $aUnquotedArray)
protected

QuoteValueArray - Quotes array elements, with Value quotes

protected

Parameters
array$aUnquotedArrayArray to quote
Returns
array Quoted array

Definition at line 350 of file CDataBase.php.

351  {
352  return $this->QuoteArray($aUnquotedArray, true);
353  }
QuoteArray($aUnquotedArray, $bValue=true)
Definition: CDataBase.php:307
QuoteValueList (   $aUnquotedArray,
  $sSeparator = ", " 
)
protected

QuoteValueList - Generates string of quoted Values used in INSERT expressions

protected

Parameters
array$aUnquotedArrayArray to quote
string$sSeparatorSeparator
Returns
array Quoted array

Definition at line 375 of file CDataBase.php.

376  {
377  $sList = implode($sSeparator, $this->QuoteValueArray($aUnquotedArray));
378  return $sList;
379  }
QuoteValueArray($aUnquotedArray)
Definition: CDataBase.php:350
Sanitize (   $sData)
protected

Sanitize - Sanitizes string

protected

Parameters
string$sDataData to sanitize
Returns
string Sanitized string

Definition at line 168 of file CDataBase.php.

169  {
170  return addslashes($sData);
171  }
Select (   $sTable,
  $vColumns = "*",
  $aWhere = [],
  $aOthers = [],
  $nReturnType = \PDO::FETCH_ASSOC 
)

Select - Generates and runs SELECT expression

Todo:
"OR" is missing from WHERE

public

Parameters
string$sTableTable name
string | array$vColumnsColumns to select
array$aWhereArray of conditions
array$aOthersOther parameters
int$nReturnTypeReturn Type (associative array by default)
Returns
array|object Query result

Definition at line 594 of file CDataBase.php.

595  {
596  $aQuery = [];
597  if (is_array($vColumns))
598  {
599  $aQuery["select"] = $this->QuoteColumnList($vColumns);
600  }
601  else
602  {
603  $aQuery["select"] = $vColumns;
604  }
605  $aQuery["from"] = $this->QuoteTable($sTable);
606  if (!empty($aWhere))
607  {
608  //
609  // TODO: "OR" is missing...
610  //
611  $aQuery["where"] = $this->CompileWhere($aWhere);
612  }
613  $aQuery["join"] = $this->CompileJoin(arr_get($aOthers, "join", []));
614  $aQuery["group_by"] = $this->CompileGroupBy(arr_get($aOthers, "group_by", []));
615  $aQuery["order_by"] = $this->CompileOrderBy(arr_get($aOthers, "order_by", []));
616  $aQuery["limit"] = $this->CompileLimit(arr_get($aOthers, "limit", []));
617  $sQuery = $this->CompileSelectSql($aQuery);
618  $this->m_sLastQuery = $sQuery;
619  \SDExtension\Helper\CLogger::get(LOG_CHANNEL_NAME)->addDebug(__FUNCTION__ . " : " . $sQuery);
620  return $this->m_oDB->query($sQuery, $nReturnType);
621  }
static get($sChannel="default", $sLogRoot="")
Definition: CLogger.php:90
CompileOrderBy($aOrderByList)
Definition: CDataBase.php:898
CompileSelectSql($aQuery=[])
Definition: CDataBase.php:630
CompileGroupBy($aGroupByList)
Definition: CDataBase.php:880
QuoteColumnList($aUnquotedArray, $sSeparator=", ")
Definition: CDataBase.php:389
CompileWhere($aWhere=[], $aBoolOp=BOOP_OP_AND)
Definition: CDataBase.php:742
QuoteTable($vTableName)
Definition: CDataBase.php:220
SQL (   $sQuery,
  $nReturnType = \PDO::FETCH_ASSOC 
)

SQL query

public

Parameters
string$sQueryTable name
int$nReturnTypeReturn Type (associative array by default)
Returns
array|object Query result

Definition at line 430 of file CDataBase.php.

431  {
432  \SDExtension\Helper\CLogger::get(LOG_CHANNEL_NAME)->addDebug(__FUNCTION__ . " : " . $sQuery);
433  return $this->m_oDB->query($sQuery, $nReturnType);
434  }
static get($sChannel="default", $sLogRoot="")
Definition: CLogger.php:90
Update (   $sTable,
  $aValues,
  $aWhere = [[1,
''  ,
'']]   
)

Update - Generates and runs UPDATE expression

Todo:
"OR" is missing from WHERE

public

Parameters
string$sTableTable name
array$aValuesArray of (DB Column => Value) pairs
array$aWhereArray of conditions
Returns
int Number of rows that were modified or deleted

Definition at line 571 of file CDataBase.php.

572  {
573  $sSetList = $this->QuoteUpdateArray($aValues);
574  $sTable = $this->QuoteTable($sTable);
575  $sQuery = "UPDATE $sTable SET " . $sSetList . " WHERE " . $this->CompileWhere($aWhere) . ";";
576  $this->m_sLastQuery = $sQuery;
577  \SDExtension\Helper\CLogger::get(LOG_CHANNEL_NAME)->addDebug(__FUNCTION__ . " : " . $sQuery);
578  return $this->m_oDB->exec($sQuery);
579  }
static get($sChannel="default", $sLogRoot="")
Definition: CLogger.php:90
CompileWhere($aWhere=[], $aBoolOp=BOOP_OP_AND)
Definition: CDataBase.php:742
QuoteUpdateArray($aUnquotedArray)
Definition: CDataBase.php:331
QuoteTable($vTableName)
Definition: CDataBase.php:220

The documentation for this class was generated from the following file: