anewt-developers team mailing list archive
-
anewt-developers team
-
Mailing list archive
-
Message #00169
[Branch ~uws/anewt/anewt.uws] Rev 1737: [autorecord] Simplify _db_find_by_id() implementation
------------------------------------------------------------
revno: 1737
committer: Wouter Bolsterlee <uws@xxxxxxxxx>
branch nick: anewt.uws
timestamp: Tue 2010-01-05 20:44:37 +0100
message:
[autorecord] Simplify _db_find_by_id() implementation
...and as a by-effect non-integer primary keys should work
as well now.
modified:
autorecord/autorecord.lib.php
--
lp:anewt
https://code.launchpad.net/~uws/anewt/anewt.uws
Your team Anewt developers is subscribed to branch lp:anewt.
To unsubscribe from this branch go to https://code.launchpad.net/~uws/anewt/anewt.uws/+edit-subscription.
=== modified file 'autorecord/autorecord.lib.php'
--- autorecord/autorecord.lib.php 2009-08-02 16:32:09 +0000
+++ autorecord/autorecord.lib.php 2010-01-05 19:44:37 +0000
@@ -703,48 +703,28 @@
*/
final protected static function _db_find_by_id($class, $just_one_result, $values, $connection)
{
- assert('is_string($class)');
- assert('is_bool($just_one_result)');
- assert('is_numeric_array($values)');
- assert('$connection instanceof AnewtDatabaseConnection');
+ assert('is_numeric_array($values) && $values;');
+ $columns = call_user_func(array($class, 'db_columns'));
$primary_key_column = call_user_func(array($class, 'db_primary_key_column'));
- $sql_select = AnewtAutoRecord::_db_sql_select($class, null, $connection);
- $sql_from = AnewtAutoRecord::_db_sql_from($class, null, $connection);
- $sql_order_by = AnewtAutoRecord::_db_sql_order_by($class, null, $connection);
-
- $sql_order_by_full = is_null($sql_order_by) ? '' : sprintf('ORDER BY %s', $sql_order_by);
-
-
- /* Single item requested: return an instance or null */
-
- if ($just_one_result)
+ $primary_key_column_type = $columns[$primary_key_column];
+
+ if (count($values) == 1)
{
- $row = $connection->prepare_execute_fetch_one(
- 'SELECT ?raw? FROM ?raw? WHERE ?column? = ?int?;',
- $sql_select, $sql_from, $primary_key_column, $values[0]);
-
- if (!$row)
- return null;
-
- return AnewtAutoRecord::_db_object_from_array($class, $row);
+ /* Simple lookup by column */
+ return AnewtAutoRecord::_db_find_by_column(
+ $class, $just_one_result, $primary_key_column, $values[0], $connection);
}
-
-
- /* Possibly multiple results: return an array with zero or more instances */
-
else
{
- /* Return early if there are no id values at all */
- if (!$values)
- return array();
-
- $sql_ids = $connection->create_sql_template(join(', ', array_fill(0, count($values), '?int?')))->fillv($values);
- $rows = $connection->prepare_execute_fetch_all(
- 'SELECT ?raw? FROM ?raw? WHERE ?column? IN (?raw?) ?raw?;',
- $sql_select, $sql_from, $primary_key_column, $sql_ids, $sql_order_by_full);
-
- return AnewtAutoRecord::_db_objects_from_arrays($class, $rows);
+ /* Lookup using IN() query */
+ $where = sprintf('?column? IN (?%s[]?)', $primary_key_column_type);
+ return AnewtAutoRecord::_db_find_by_sql(
+ $class,
+ $just_one_result,
+ array('where' => $where),
+ array($primary_key_column, $values),
+ $connection);
}
}