From abfc7d5e3419e6f317a6366b7e4c273b0465ed2e Mon Sep 17 00:00:00 2001 From: Christian Schneider Date: Thu, 26 Jun 2008 15:08:57 +0000 Subject: Replaced deprecated mysql_list_tables by SHOW TABLES Constructor of generated classes now take varargs too and added tests for it --- it_dbi.class | 27 +++++++++++++++------------ tests/it_dbi.t | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/it_dbi.class b/it_dbi.class index 396ff84..709ddee 100644 --- a/it_dbi.class +++ b/it_dbi.class @@ -109,13 +109,13 @@ function createclasses($p = array()) $p += $dbi->_p; $p['dbid'] = "{$p['user']}@{$p['server']}:{$p['db']}"; - $dbi->_connect($p); - if (!($tables = mysql_list_tables($p['db'], $dbi->_link))) - $dbi->_fatal("createclasses(): can't list on tables \"{$p['db']}\""); - - for ($i = 0; $i < mysql_num_rows($tables); $i++) - it_dbi::createclass(array('table' => mysql_tablename($tables, $i)) + $p); + for ($res = $dbi->query('SHOW TABLES'); $row = mysql_fetch_row($res);) + { + # Either create class in autoloader or manually just below + if (!class_exists($row[0])) + it_dbi::createclass(array('table' => $row[0]) + $p); + } } @@ -139,7 +139,7 @@ function createclass($p) $dbi->_tables[$dbid][$row[0]] = true; } - if ($dbi->_tables[$dbid][$p['table']]) # Do not generate classes for non-existant tables + if ($p['forcecreate'] || $dbi->_tables[$dbid][$p['table']]) # Do not generate classes for non-existant tables (can be overridden by forcecreate => true, used in tests/it_dbi.t) { $classname = $p['classprefix'] . $p['table']; @@ -147,12 +147,15 @@ function createclass($p) { $code = "class $classname extends it_dbi { - function $classname(\$query = null, \$p = array()) + function $classname(/* $query ... */) { - if (\$p) - it::error(array('title' => 'Deprecated use of \$p parameter for $classname', 'body' => var_export(\$p, true), 'backtraceskip' => 1)); - \$p += " . var_export($p, true) . "; - \$this->it_dbi(\$p, \$query); + \$args = func_get_args(); + \$query = array_shift(\$args); # Preserve type (scalar/array) in single parameter case + + foreach (\$args as \$arg) + \$query = array_merge((array)\$query, (array)\$arg); + + \$this->it_dbi(" . var_export($p, true) . ", \$query); } }"; diff --git a/tests/it_dbi.t b/tests/it_dbi.t index 63c7413..6e14657 100755 --- a/tests/it_dbi.t +++ b/tests/it_dbi.t @@ -51,6 +51,43 @@ is( "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, @@ -58,7 +95,6 @@ is( "constructor with multiple select parameters" ); - $record->select(array('x' => 64738)); is( array($record->_key, $record->x, $record->foo), -- cgit v1.2.3