Skip to content

Commit e0abe5b

Browse files
committed
Merge pull request #250 from avbdr/master
new functions
2 parents a109005 + fc46ca6 commit e0abe5b

File tree

2 files changed

+49
-22
lines changed

2 files changed

+49
-22
lines changed

MysqliDb.php

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -385,31 +385,27 @@ public function getValue($tableName, $column)
385385
}
386386

387387
/**
388+
* Insert method to add new row
388389
*
389390
* @param <string $tableName The name of the table.
390391
* @param array $insertData Data containing information for inserting into the DB.
391392
*
392393
* @return boolean Boolean indicating whether the insert query was completed succesfully.
393394
*/
394-
public function insert($tableName, $insertData)
395-
{
396-
if ($this->isSubQuery)
397-
return;
398-
399-
$this->_query = "INSERT INTO " .self::$prefix . $tableName;
400-
$stmt = $this->_buildQuery(null, $insertData);
401-
$stmt->execute();
402-
$this->_stmtError = $stmt->error;
403-
$this->reset();
404-
$this->count = $stmt->affected_rows;
405-
406-
if ($stmt->affected_rows < 1)
407-
return false;
408-
409-
if ($stmt->insert_id > 0)
410-
return $stmt->insert_id;
395+
public function insert ($tableName, $insertData) {
396+
return $this->_buildInsert ($tableName, $insertData, 'INSERT');
397+
}
411398

412-
return true;
399+
/**
400+
* Replace method to add new row
401+
*
402+
* @param <string $tableName The name of the table.
403+
* @param array $insertData Data containing information for inserting into the DB.
404+
*
405+
* @return boolean Boolean indicating whether the insert query was completed succesfully.
406+
*/
407+
public function replace ($tableName, $insertData) {
408+
return $this->_buildInsert ($tableName, $insertData, 'REPLACE');
413409
}
414410

415411
/**
@@ -697,6 +693,35 @@ protected function _buildPair ($operator, $value) {
697693
return " " . $operator . " (" . $subQuery['query'] . ") " . $subQuery['alias'];
698694
}
699695

696+
/**
697+
* Internal function to build and execute INSERT/REPLACE calls
698+
*
699+
* @param <string $tableName The name of the table.
700+
* @param array $insertData Data containing information for inserting into the DB.
701+
*
702+
* @return boolean Boolean indicating whether the insert query was completed succesfully.
703+
*/
704+
private function _buildInsert ($tableName, $insertData, $operation)
705+
{
706+
if ($this->isSubQuery)
707+
return;
708+
709+
$this->_query = $operation . " " . implode (' ', $this->_queryOptions) ." INTO " .self::$prefix . $tableName;
710+
$stmt = $this->_buildQuery (null, $insertData);
711+
$stmt->execute();
712+
$this->_stmtError = $stmt->error;
713+
$this->reset();
714+
$this->count = $stmt->affected_rows;
715+
716+
if ($stmt->affected_rows < 1)
717+
return false;
718+
719+
if ($stmt->insert_id > 0)
720+
return $stmt->insert_id;
721+
722+
return true;
723+
}
724+
700725
/**
701726
* Abstraction method that will compile the WHERE statement,
702727
* any passed update data, and the desired rows.

readme.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,11 @@ if ($id)
107107
echo 'user was created. Id=' . $id;
108108
else
109109
echo 'insert failed: ' . $db->getLastError();
110-
111110
```
112111

112+
### Replace Query
113+
replace() method implements same API as insert();
114+
113115
### Update Query
114116
```php
115117
$data = Array (
@@ -294,17 +296,17 @@ echo "Showing {$count} from {$db->totalCount}";
294296
```
295297

296298
### Query Keywords
297-
To add LOW PRIORITY | DELAYED | HIGH PRIORITY | IGNORE and the rest of mysql keywords to INSERT , SELECT , UPDATE, DELETE query:
299+
To add LOW PRIORITY | DELAYED | HIGH PRIORITY | IGNORE and the rest of the mysql keywords to INSERT (), REPLACE (), GET (), UPDATE (), DELETE() method:
298300
```php
299301
$db->setQueryOption('LOW_PRIORITY');
300-
$db->insert($table,$param);
302+
$db->insert ($table, $param);
301303
// GIVES: INSERT LOW_PRIORITY INTO table ...
302304
```
303305

304306
Also you can use an array of keywords:
305307
```php
306308
$db->setQueryOption(Array('LOW_PRIORITY', 'IGNORE'));
307-
$db->insert($table,$param);
309+
$db->insert ($table,$param);
308310
// GIVES: INSERT LOW_PRIORITY IGNORE INTO table ...
309311
```
310312

0 commit comments

Comments
 (0)