Commit fc46ca63 authored by Alexander Butenko's avatar Alexander Butenko

Added replace() function

parent 260c78ff
......@@ -385,31 +385,27 @@ class MysqliDb
}
/**
* Insert method to add new row
*
* @param <string $tableName The name of the table.
* @param array $insertData Data containing information for inserting into the DB.
*
* @return boolean Boolean indicating whether the insert query was completed succesfully.
*/
public function insert($tableName, $insertData)
{
if ($this->isSubQuery)
return;
$this->_query = "INSERT " . implode(' ', $this->_queryOptions) ." INTO " .self::$prefix . $tableName;
$stmt = $this->_buildQuery(null, $insertData);
$stmt->execute();
$this->_stmtError = $stmt->error;
$this->reset();
$this->count = $stmt->affected_rows;
if ($stmt->affected_rows < 1)
return false;
if ($stmt->insert_id > 0)
return $stmt->insert_id;
public function insert ($tableName, $insertData) {
return $this->_buildInsert ($tableName, $insertData, 'INSERT');
}
return true;
/**
* Replace method to add new row
*
* @param <string $tableName The name of the table.
* @param array $insertData Data containing information for inserting into the DB.
*
* @return boolean Boolean indicating whether the insert query was completed succesfully.
*/
public function replace ($tableName, $insertData) {
return $this->_buildInsert ($tableName, $insertData, 'REPLACE');
}
/**
......@@ -697,6 +693,35 @@ class MysqliDb
return " " . $operator . " (" . $subQuery['query'] . ") " . $subQuery['alias'];
}
/**
* Internal function to build and execute INSERT/REPLACE calls
*
* @param <string $tableName The name of the table.
* @param array $insertData Data containing information for inserting into the DB.
*
* @return boolean Boolean indicating whether the insert query was completed succesfully.
*/
private function _buildInsert ($tableName, $insertData, $operation)
{
if ($this->isSubQuery)
return;
$this->_query = $operation . " " . implode (' ', $this->_queryOptions) ." INTO " .self::$prefix . $tableName;
$stmt = $this->_buildQuery (null, $insertData);
$stmt->execute();
$this->_stmtError = $stmt->error;
$this->reset();
$this->count = $stmt->affected_rows;
if ($stmt->affected_rows < 1)
return false;
if ($stmt->insert_id > 0)
return $stmt->insert_id;
return true;
}
/**
* Abstraction method that will compile the WHERE statement,
* any passed update data, and the desired rows.
......
......@@ -107,9 +107,11 @@ if ($id)
echo 'user was created. Id=' . $id;
else
echo 'insert failed: ' . $db->getLastError();
```
### Replace Query
replace() method implements same API as insert();
### Update Query
```php
$data = Array (
......@@ -294,17 +296,17 @@ echo "Showing {$count} from {$db->totalCount}";
```
### Query Keywords
To add LOW PRIORITY | DELAYED | HIGH PRIORITY | IGNORE and the rest of mysql keywords to INSERT , SELECT , UPDATE, DELETE query:
To add LOW PRIORITY | DELAYED | HIGH PRIORITY | IGNORE and the rest of the mysql keywords to INSERT (), REPLACE (), GET (), UPDATE (), DELETE() method:
```php
$db->setQueryOption('LOW_PRIORITY');
$db->insert($table,$param);
$db->insert ($table, $param);
// GIVES: INSERT LOW_PRIORITY INTO table ...
```
Also you can use an array of keywords:
```php
$db->setQueryOption(Array('LOW_PRIORITY', 'IGNORE'));
$db->insert($table,$param);
$db->insert ($table,$param);
// GIVES: INSERT LOW_PRIORITY IGNORE INTO table ...
```
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment