|
|
@@ -419,6 +419,13 @@ EOF_STRUCT_MYSQL;
|
|
|
return $sth->fetchColumn();
|
|
|
}
|
|
|
|
|
|
+ public function fetchFirst($sql) {// fetch only first row
|
|
|
+ DBG::_('DBG_SQL', '>3', "sql", $sql, __CLASS__, __FUNCTION__, __LINE__);
|
|
|
+ $sth = $this->prepare($sql);
|
|
|
+ $sth->execute();
|
|
|
+ return $sth->fetch();
|
|
|
+ }
|
|
|
+
|
|
|
public function fetchAll($sql) {
|
|
|
DBG::_('DBG_SQL', '>3', "sql", $sql, __CLASS__, __FUNCTION__, __LINE__);
|
|
|
$sth = $this->prepare($sql);
|
|
|
@@ -485,4 +492,41 @@ EOF_STRUCT_MYSQL;
|
|
|
return $sql;
|
|
|
}
|
|
|
|
|
|
+ public function insert($tableName, $item, $sqlSchema = []) {// @returns int last inserted id
|
|
|
+ $sqlFields = [];
|
|
|
+ $sqlValues = [];
|
|
|
+ foreach ($item as $field => $val) {
|
|
|
+ $sqlVal = $this->quote($val, PDO::PARAM_STR);// TODO: use $sqlSchema if set
|
|
|
+ $sqlFields[] = "`{$field}`";
|
|
|
+ $sqlValues[] = $sqlVal;
|
|
|
+ }
|
|
|
+ $sql = "
|
|
|
+ insert into `{$tableName}` (" . implode(", ", $sqlFields) . ")
|
|
|
+ values (" . implode(", ", $sqlValues) . ")
|
|
|
+ ";
|
|
|
+ echo $sql . "\n";
|
|
|
+ DBG::_('DBG_SQL', '>3', "sql", $sql, __CLASS__, __FUNCTION__, __LINE__);
|
|
|
+ $this->exec($sql);
|
|
|
+ return $this->lastInsertId();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function update($tableName, $primaryKeyName, $primaryKey, $item, $sqlSchema = []) {// @returns int affected rows
|
|
|
+ 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) {
|
|
|
+ $sqlVal = $this->quote($val, PDO::PARAM_STR);// TODO: use $sqlSchema if set
|
|
|
+ $sqlUpdateSet[] = "`{$field}` = {$sqlVal}";
|
|
|
+ }
|
|
|
+ $sql = "
|
|
|
+ update `{$tableName}`
|
|
|
+ set " . implode("\n , ", $sqlUpdateSet) . "
|
|
|
+ where `{$primaryKeyName}` = {$sqlPrimaryKey}
|
|
|
+ ";
|
|
|
+ DBG::_('DBG_SQL', '>3', "sql", $sql, __CLASS__, __FUNCTION__, __LINE__);
|
|
|
+ return $this->exec($sql);
|
|
|
+ }
|
|
|
+
|
|
|
}
|