|
|
|
|
@@ -80,6 +80,9 @@ class Db extends PhpObject
|
|
|
|
|
|
|
|
|
|
//Timezone
|
|
|
|
|
$this->setQuery("SET time_zone='".date_default_timezone_get()."'");
|
|
|
|
|
|
|
|
|
|
//Returned Types
|
|
|
|
|
$this->oConnection->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
|
@@ -480,6 +483,28 @@ class Db extends PhpObject
|
|
|
|
|
return $iTableId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Unlike insertUpdateRow, this function depends on the table defined UNIQUE KEY
|
|
|
|
|
* and not on the custom-defined $asKeys table
|
|
|
|
|
*/
|
|
|
|
|
public function upsertRow($sTableName, $asData, $asUpdateFields)
|
|
|
|
|
{
|
|
|
|
|
$this->cleanSql($sTableName);
|
|
|
|
|
$this->cleanSql($asData);
|
|
|
|
|
$this->cleanSql($asUpdateFields);
|
|
|
|
|
|
|
|
|
|
$asQuotedData = $this->addQuotes($asData);
|
|
|
|
|
$asUpdateData = array_intersect_key($asData, array_flip($asUpdateFields));
|
|
|
|
|
$asQuotesdUpdateData = $this->addQuotes($asUpdateData);
|
|
|
|
|
|
|
|
|
|
$sQuery =
|
|
|
|
|
"INSERT INTO ".$sTableName." (`".implode("`, `", array_keys($asQuotedData))."`) ".
|
|
|
|
|
"VALUES (".implode(", ", $asQuotedData).") ".
|
|
|
|
|
"ON DUPLICATE KEY UPDATE ".$this->implodeAll($asQuotesdUpdateData, " = ", ", ");
|
|
|
|
|
|
|
|
|
|
return $this->setQuery($sQuery)?$this->getLastId():0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function selectInsert($sTableName, $asData, $asKeys=array())
|
|
|
|
|
{
|
|
|
|
|
return $this->insertUpdateRow($sTableName, $asData, $asKeys, false);
|
|
|
|
|
@@ -561,7 +586,8 @@ class Db extends PhpObject
|
|
|
|
|
{
|
|
|
|
|
if(is_array($asConstraints))
|
|
|
|
|
{
|
|
|
|
|
if(array_key_exists('constOpe', $asInfo) && array_key_exists($sField, $asInfo['constOpe']) && $asInfo['constOpe'][$sField]=='BETWEEN') {
|
|
|
|
|
$sConstOpe = $asInfo['constOpe'][$sField] ?? '';
|
|
|
|
|
if($sConstOpe == 'BETWEEN') {
|
|
|
|
|
//Between
|
|
|
|
|
$asSelection[$sField] = $asConstraints['from'].' AND '.$asConstraints['to'];
|
|
|
|
|
$asInfo['constOpe'][$sField] = " BETWEEN ";
|
|
|
|
|
@@ -569,7 +595,7 @@ class Db extends PhpObject
|
|
|
|
|
else {
|
|
|
|
|
//Multiple values (IN)
|
|
|
|
|
$asSelection[$sField] = "(".implode(', ', $asConstraints).")";
|
|
|
|
|
$asInfo['constOpe'][$sField] = " IN ";
|
|
|
|
|
$asInfo['constOpe'][$sField] = ($sConstOpe=='NOT IN')?" NOT IN ":" IN ";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
elseif(!array_key_exists('constOpe', $asInfo) || !array_key_exists($sField, $asInfo['constOpe'])) $asInfo['constOpe'][$sField] = " = ";
|
|
|
|
|
@@ -629,12 +655,12 @@ class Db extends PhpObject
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function selectRow($sTableName, $asConstraints=array(), $sColumnName='*')
|
|
|
|
|
public function selectRow($sTableName, $asConstraints=array(), $sColumnName='*', $asConstOpe=[])
|
|
|
|
|
{
|
|
|
|
|
//Table ID directly
|
|
|
|
|
if(!is_array($asConstraints)) $asConstraints = array($this->getId($sTableName)=>$asConstraints);
|
|
|
|
|
|
|
|
|
|
$asRows = $this->selectRows(array('select'=>$sColumnName, 'from'=>$sTableName, 'constraint'=>$asConstraints));
|
|
|
|
|
$asRows = $this->selectRows(array('select'=>$sColumnName, 'from'=>$sTableName, 'constraint'=>$asConstraints, 'constOpe'=>$asConstOpe));
|
|
|
|
|
$iCountNb = count($asRows);
|
|
|
|
|
switch($iCountNb)
|
|
|
|
|
{
|
|
|
|
|
@@ -665,13 +691,13 @@ class Db extends PhpObject
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function selectValue($sTableName, $sColumnName, $oConstraints=array())
|
|
|
|
|
public function selectValue($sTableName, $sColumnName, $oConstraints=array(), $asConstOpe=[])
|
|
|
|
|
{
|
|
|
|
|
if(!is_array($oConstraints))
|
|
|
|
|
{
|
|
|
|
|
$oConstraints = array($this->getId($sTableName)=>$oConstraints);
|
|
|
|
|
}
|
|
|
|
|
$oResult = $this->selectRow($sTableName, $oConstraints, $sColumnName);
|
|
|
|
|
$oResult = $this->selectRow($sTableName, $oConstraints, $sColumnName, $asConstOpe);
|
|
|
|
|
return empty($oResult)?false:$oResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|