diff options
-rw-r--r-- | README | 2 | ||||
-rw-r--r-- | it_dbi.class | 5 | ||||
-rwxr-xr-x | tests/it_dbi.t | 14 |
3 files changed, 11 insertions, 10 deletions
@@ -88,10 +88,10 @@ Functions: $t = new Tablename($query) -- return a dbi object, executes optional select $t->select($query) -- read first result of (encoded) query into t. $t->iterate() -- advance to next result - $t->store($fields) -- Update a record (efficiently) or create it if missing $t->update($fields) -- update selected record from key=>value pairs $t->insert($fields) -- insert a new record from key=>value pairs $t->replace($fields) -- replace a new record from key=>value pairs + $t->smart_replace($fields) -- create a record or update efficiently if exists $t->delete($query) -- delete current record or those found by query $t->query($sqlquery) -- execute a raw SQL query on db connection $t = Tablename::get($id) -- return valid object loaded with record $id or null diff --git a/it_dbi.class b/it_dbi.class index 89b641f..1bb36e0 100644 --- a/it_dbi.class +++ b/it_dbi.class @@ -684,10 +684,11 @@ function replace($tags = array()) /** - * Update a record (efficiently) or create it if missing + * Create a record like replace(), but optimize if similar or identical record already exists + * MUST GIVE ALL FIELDS INCLUDING ID * @param $tags key => value pairs to set */ -function store($tags = array()) +function smart_replace($tags = array()) { return $tags[$this->_p['keyfield']] && $this->read($tags[$this->_p['keyfield']]) ? $this->update($tags) : $this->replace($tags); } diff --git a/tests/it_dbi.t b/tests/it_dbi.t index e988fa8..946f7a8 100755 --- a/tests/it_dbi.t +++ b/tests/it_dbi.t @@ -237,37 +237,37 @@ foreach ($record as $dummy_rec) is($count, 2, "Iterator reused"); $GLOBALS['debug_sqllog'] = true; -@$record->store(array('ID' => 5, 'x' => 6)); +@$record->smart_replace(array('ID' => 5, 'x' => 6)); like( $record->_sqllog[1]['query'], "REPLACE", - "store => REPLACE for new entries" + "smart_replace => REPLACE for new entries" ); $record->clear(); @$record->read(5); is( $record->x, 6, - "saving with store" + "saving with smart_replace" ); $record->_sqllog = array(); -@$record->store(array('ID' => 5, 'x' => 7)); +@$record->smart_replace(array('ID' => 5, 'x' => 7)); like( $record->_sqllog[1]['query'], "UPDATE", - "store => UPDATE for existing entries" + "smart_replace => UPDATE for existing entries" ); $record->clear(); @$record->read(5); is( $record->x, 7, - "updating with store" + "updating with smart_replace" ); $record->_sqllog = array(); -@$record->store(array('ID' => 5, 'x' => 7)); +@$record->smart_replace(array('ID' => 5, 'x' => 7)); is( $record->_sqllog[1]['query'], null, # Only SELECT, no UPDATE |