#!/www/server/bin/php -qC "lib_search_ch", 'safety' => 0); $dbi = new it_dbi($db); $dbi->query('create temporary table it_dbi_test ( ID int not null auto_increment, x int, foo varchar(42), primary key(ID) );'); $record = new it_dbi($db + array('table' => "it_dbi_test")); $record->insert(array('x' => 42, 'foo' => null)); $record->insert(array('foo' => "bar")); $record->insert(array('x' => 64738, 'foo' => "q'uux")); is( $record->ID, 3, "auto_increment" ); $record->read(1); is( array($record->_key, $record->x, $record->foo), array(1, 42, null), "read" ); is( $record->select(), 3, "select without parameters select all", ); is( $record->select(array('foo <>' => ""), "LIMIT 1"), 1, "select with multiple parameters (LIMIT part)" ); is( $record->foo, "bar", "select with multiple parameters (foo part)" ); it_dbi::createclass(array('table' => "it_dbi_test", 'forcecreate' => true)); $record = new it_dbi_test; is( $record->x, null, "constructor of created class without argument" ); $record = new it_dbi_test(2); is( $record->ID, 2, "constructor of created class with id parameter" ); $record = new it_dbi_test(array('x >' => 0), "ORDER BY x DESC"); is( $record->x, 64738, "constructor of created class with multiple select parameters" ); $record = new it_dbi_test(array('foo' => 'bar')); is( $record->ID, 2, "constructor of created class with single array parameter" ); $record = new it_dbi($db + array('table' => "it_dbi_test")); is( $record->x, null, "constructor without parameters" ); $record = new it_dbi($db + array('table' => "it_dbi_test"), array('x >' => 0), "ORDER BY x DESC"); is( $record->x, 64738, "constructor with multiple select parameters" ); $record->select(array('x' => 64738)); is( array($record->_key, $record->x, $record->foo), array(3, 64738, "q'uux"), "select" ); $record->update(array('x' => 17)); is( array($record->_key, $record->x, $record->foo), array(3, 17, "q'uux"), "update" ); $record->update(array('-x' => 'RAND() * 10')); isnt( array($record->_key, $record->x, $record->foo), array(3, 17, "q'uux"), "update" ); $rand = $record->x; is ( $record->_set(array('x' => $rand, 'foo' => "bar")), "SET foo='bar'", 'update: _set optimization' ); $record->update(array('foo' => "bar")); $record->select(array('foo' => "bar")); $record->iterate(); is( array($record->_key, $record->x, $record->foo), array(2, null, "bar"), "iterate record 2" ); $record->update(array('foo' => "qux")); $record->iterate(); is( array($record->_key, $record->x, $record->foo), array(3, $rand, "bar"), "iterate record 3" ); $record->update(array('foo' => "quux")); $record->read(2); is( array($record->_key, isset($record->x), $record->foo), array(2, false, "qux"), "iterate update record 2" ); $record->read(3); is( array($record->_key, $record->x, $record->foo), array(3, $rand, "quux"), "iterate update record 3" ); is( gettype($record->_key) . "/" . gettype($record->x) . "/" . gettype($record->_data['x']) . "/" . gettype($record->foo), "integer/integer/integer/string", "automatic type detection" ); $count = 0; foreach (new it_dbi_test as $id => $record) { $count++; is($record->_key, $id, "Iterator id $id"); } is($count, 3, "Iterator without select"); $count = 0; foreach (new it_dbi_test('foo <>' => "") as $id => $record) { $count++; is($record->_key, $id, "Iterator id $id"); } is($count, 2, "Iterator with select"); $count = 0; foreach ($record as $rec) $count++; is($count, 2, "Iterator reused");