|
|
@@ -415,14 +415,12 @@ EOF_STRUCT_MYSQL;
|
|
|
|
|
|
// for sql like `select count() from ...`
|
|
|
public function fetchValue($sql) {
|
|
|
- DBG::_('DBG_SQL', '>3', "sql", $sql, __CLASS__, __FUNCTION__, __LINE__);
|
|
|
DBG::log($sql, 'sql');
|
|
|
$sth = $this->query($sql);
|
|
|
return $sth->fetchColumn();
|
|
|
}
|
|
|
|
|
|
public function fetchFirst($sql) {// fetch only first row
|
|
|
- DBG::_('DBG_SQL', '>3', "sql", $sql, __CLASS__, __FUNCTION__, __LINE__);
|
|
|
DBG::log($sql, 'sql');
|
|
|
$sth = $this->prepare($sql);
|
|
|
$sth->execute();
|
|
|
@@ -430,7 +428,6 @@ EOF_STRUCT_MYSQL;
|
|
|
}
|
|
|
|
|
|
public function fetchAll($sql) {
|
|
|
- DBG::_('DBG_SQL', '>3', "sql", $sql, __CLASS__, __FUNCTION__, __LINE__);
|
|
|
DBG::log($sql, 'sql');
|
|
|
$sth = $this->prepare($sql);
|
|
|
$sth->execute();
|
|
|
@@ -438,7 +435,6 @@ EOF_STRUCT_MYSQL;
|
|
|
}
|
|
|
|
|
|
public function fetchAllByKey($sql, $key = 'ID') {
|
|
|
- DBG::_('DBG_SQL', '>3', "sql", $sql, __CLASS__, __FUNCTION__, __LINE__);
|
|
|
DBG::log($sql, 'sql');
|
|
|
$rowsByKey = array();
|
|
|
$sth = $this->prepare($sql);
|
|
|
@@ -498,53 +494,90 @@ EOF_STRUCT_MYSQL;
|
|
|
}
|
|
|
|
|
|
public function insert($tableName, $item, $sqlSchema = []) {// @returns int last inserted id
|
|
|
+ if (empty($tableName)) throw new Exception("Missing table name");
|
|
|
+ if (empty($item) || !is_array($item)) throw new Exception("Missing item");
|
|
|
$sqlFields = [];
|
|
|
$sqlValues = [];
|
|
|
foreach ($item as $field => $val) {
|
|
|
- if ('NOW()' === $val) {
|
|
|
- $sqlVal = 'NOW()';
|
|
|
- } else {
|
|
|
- $sqlVal = $this->quote($val, PDO::PARAM_STR);// TODO: use $sqlSchema if set
|
|
|
- }
|
|
|
$sqlFields[] = "`{$field}`";
|
|
|
- $sqlValues[] = $sqlVal;
|
|
|
+ $sqlValues[] = $this->convertValueToSqlSafe($val, V::get($field, null, $sqlSchema));
|
|
|
}
|
|
|
$sql = "
|
|
|
insert into `{$tableName}` (" . implode(", ", $sqlFields) . ")
|
|
|
values (" . implode(", ", $sqlValues) . ")
|
|
|
";
|
|
|
- DBG::_('DBG_SQL', '>3', "sql", $sql, __CLASS__, __FUNCTION__, __LINE__);
|
|
|
- DBG::log($sql, 'sql');
|
|
|
- $this->exec($sql);
|
|
|
+ $this->execSql($sql);
|
|
|
return $this->lastInsertId();
|
|
|
}
|
|
|
|
|
|
public function update($tableName, $primaryKeyName, $primaryKey, $item, $sqlSchema = []) {// @returns int affected rows
|
|
|
+ if (empty($tableName)) throw new Exception("Missing table name");
|
|
|
if (empty($primaryKeyName)) throw new Exception("Missing primaryKey name");
|
|
|
if (empty($primaryKey)) throw new Exception("Missing primaryKey");
|
|
|
if (empty($item) || !is_array($item)) throw new Exception("Missing item");
|
|
|
$sqlPrimaryKey = $this->quote($primaryKey, PDO::PARAM_STR);
|
|
|
$sqlUpdateSet = [];
|
|
|
foreach ($item as $field => $val) {
|
|
|
- if ('NOW()' === $val) {
|
|
|
- $sqlVal = 'NOW()';
|
|
|
- } else {
|
|
|
- $sqlVal = $this->quote($val, PDO::PARAM_STR);// TODO: use $sqlSchema if set
|
|
|
- }
|
|
|
- $sqlUpdateSet[] = "`{$field}` = {$sqlVal}";
|
|
|
+ $sqlUpdateSet[] = "`{$field}` = " . $this->convertValueToSqlSafe($val, V::get($field, null, $sqlSchema));
|
|
|
}
|
|
|
$sql = "
|
|
|
update `{$tableName}`
|
|
|
set " . implode("\n , ", $sqlUpdateSet) . "
|
|
|
where `{$primaryKeyName}` = {$sqlPrimaryKey}
|
|
|
";
|
|
|
- DBG::_('DBG_SQL', '>3', "sql", $sql, __CLASS__, __FUNCTION__, __LINE__);
|
|
|
- DBG::log($sql, 'sql');
|
|
|
- return $this->exec($sql);
|
|
|
+ return $this->execSql($sql);
|
|
|
+ }
|
|
|
+
|
|
|
+ // '@insert' => [
|
|
|
+ // 'A_RECORD_CREATE_AUTHOR' => User::getLogin(),
|
|
|
+ // 'A_RECORD_CREATE_DATE' => 'NOW()',
|
|
|
+ // ],
|
|
|
+ // '@update' => [
|
|
|
+ // 'A_RECORD_UPDATE_AUTHOR' => User::getLogin(),
|
|
|
+ // 'A_RECORD_UPDATE_DATE' => 'NOW()',
|
|
|
+ // ]
|
|
|
+ public function insertOrUpdate($tableName, $uniqField, $uniqValue, $item, $sqlSchema = []) {
|
|
|
+ if (empty($tableName)) throw new Exception("Missing table name");
|
|
|
+ if (empty($uniqField)) throw new Exception("Missing uniq field name");
|
|
|
+ if (empty($uniqValue)) throw new Exception("Missing uniq field value");
|
|
|
+ if (empty($item) || !is_array($item)) throw new Exception("Missing item");
|
|
|
+ $sqlFields = [];
|
|
|
+ $sqlValues = [];
|
|
|
+ $sqlUpdateSet = [];
|
|
|
+ foreach ($item as $field => $val) {
|
|
|
+ if ('@insert' == $field) continue;
|
|
|
+ if ('@update' == $field) continue;
|
|
|
+ $sqlVal = $this->convertValueToSqlSafe($val, V::get($field, null, $sqlSchema));
|
|
|
+ $sqlFields[] = "`{$field}`";
|
|
|
+ $sqlValues[] = $sqlVal;
|
|
|
+ if ($field !== $uniqField) $sqlUpdateSet[] = "`{$field}` = {$sqlVal}";
|
|
|
+ }
|
|
|
+ if (!empty($item['@insert'])) {
|
|
|
+ foreach ($item['@insert'] as $field => $val) {
|
|
|
+ $sqlFields[] = "`{$field}`";
|
|
|
+ $sqlValues[] = $this->convertValueToSqlSafe($val, V::get($field, null, $sqlSchema));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!empty($item['@update'])) {
|
|
|
+ foreach ($item['@update'] as $field => $val) {
|
|
|
+ $sqlUpdateSet[] = "`{$field}` = " . $this->convertValueToSqlSafe($val, V::get($field, null, $sqlSchema));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $sql = "
|
|
|
+ insert into `{$tableName}` (" . implode(", ", $sqlFields) . ")
|
|
|
+ values (" . implode(", ", $sqlValues) . ")
|
|
|
+ ";
|
|
|
+ if (!empty($sqlUpdateSet)) $sql .= " on duplicate key update " . implode(", ", $sqlUpdateSet);
|
|
|
+ $affected = $this->execSql($sql);
|
|
|
+ return true; // return $affected; // $this->lastInsertId();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function convertValueToSqlSafe($value, $xsdType = null) {
|
|
|
+ if ('NOW()' === $value) return 'NOW()';
|
|
|
+ else return $this->quote($value, PDO::PARAM_STR);// TODO: use $sqlSchema if set
|
|
|
}
|
|
|
|
|
|
public function execSql($sql) {
|
|
|
- DBG::_('DBG_SQL', '>3', "sql", $sql, __CLASS__, __FUNCTION__, __LINE__);
|
|
|
DBG::log($sql, 'sql');
|
|
|
return $this->exec($sql);
|
|
|
}
|