Core\Db::inList
Core\Db::inList(array<mixed> $values): Core\Db\InListCore\Db::inList( array<mixed> $values, ): Core\Db\InList
Marks an array as a list of values, so one placeholder in a statement stands for all of them.
A placeholder normally binds one value. where id in ? with an array of three ids is not what a database expects: it wants where id in (?, ?, ?). Most programs build that text themselves, counting the values and gluing that many question marks together. Core\Db::inList does it for you. You write ? once, and the marker tells Novis how wide the list is.
The marker is an ordinary value. You can put it in a variable and hand it on, and you bind it in the $params array like any other value. It works the same inside in and inside not in.
An empty list throws a LogicError. Inside in an empty list matches nothing, and inside not in it matches everything, so there is no answer that is right in both places. Test the list before you mark it, and take the other branch when it is empty.
Good to know: the SQL text never changes. Your program does not build it from a count, so a value from a visitor cannot reach the statement as text.
Parameters
$valuesarray<mixed>The values to bind, one placeholder each, in the order of the array. Keys are ignored: the values fill positions inside one placeholder and have no names.
Return value
Returns Core\Db\InList. A Core\Db\InList to bind to a single placeholder. It has no methods, and you can use it only as a bound parameter. Two lists of different lengths with the same SQL are two entries in the statement cache. The expanded statement has a different number of placeholders.
Errors
LogicError$valuesis empty. An empty list means "match nothing" insideINand "match everything" insideNOT IN. Check for an empty list in your code instead.
Examples
Find the rows a list names
<?nvs// Shows how to read several rows with one list of ids. `Core\Db::inList` marks an// array as a list of values. The single `?` in the SQL text then stands for all// of them.
var $db = Core\Db::open({ driver: Core\Db\Driver::Sqlite, path: ':memory:' });$db->execute('create table city (id integer primary key, name text not null)', []);$db->executeMany('insert into city (id, name) values (?, ?)', [ [1, 'Berlin'], [2, 'Hamburg'], [3, 'Bremen'], [4, 'Cologne'],]);
array<int> $wanted = [2, 4];
// This prints `Hamburg` and `Cologne`.foreach ($db->query('select name from city where id in ? order by id', [Core\Db::inList($wanted)]) as Core\Db\Row $row) { echo $row->string('name') ?? '', "\n";}
$db->close();HamburgCologneLeave out the values a list names
<?nvs// Shows how to skip a list of values. `not in ?` keeps every row whose value is// not in the list. The same marker works for both `in` and `not in`.
var $db = Core\Db::open({ driver: Core\Db\Driver::Sqlite, path: ':memory:' });$db->execute('create table task (id integer primary key, state text not null)', []);$db->executeMany('insert into task (id, state) values (?, ?)', [ [1, 'open'], [2, 'done'], [3, 'cancelled'], [4, 'open'],]);
// The states a board does not show.array<string> $hidden = ['done', 'cancelled'];
// This prints `1 open` and `4 open`.foreach ($db->query('select id, state from task where state not in ? order by id', [Core\Db::inList($hidden)]) as Core\Db\Row $row) { echo $row->int('id') ?? 0, ' ', $row->string('state') ?? '', "\n";}
// An empty list throws a `LogicError`. Inside `in` it would match nothing, and// inside `not in` it would match everything. Test the list before you mark it.array<string> $none = [];if (Core\Arr::isEmpty($none)) { echo "nothing to leave out\n";}
$db->close();1 open4 opennothing to leave outOne query instead of one per row
<?nvs// Shows how a page reads the rows it still needs in one statement. It collects// the ids while it walks the orders, then asks for all the customers at once.// One query for the whole page is much faster than one query per order.
var $db = Core\Db::open({ driver: Core\Db\Driver::Sqlite, path: ':memory:' });$db->execute('create table customer (id integer primary key, name text not null)', []);$db->execute('create table orders (id integer primary key, customer_id integer not null)', []);$db->executeMany('insert into customer (id, name) values (?, ?)', [ [1, 'Ada'], [2, 'Grace'], [3, 'Alan'],]);$db->executeMany('insert into orders (id, customer_id) values (?, ?)', [ [10, 1], [11, 3], [12, 1],]);
// Two of the three orders belong to the same customer, so the list has a// repeated id. `Core\Arr::unique` leaves one of each.array<int> $ids = [];foreach ($db->query('select customer_id from orders order by id', []) as Core\Db\Row $row) { $ids[] = $row->int('customer_id') ?? 0;}$ids = Core\Arr::unique($ids);
// This prints `Ada` and `Alan`.foreach ($db->query('select name from customer where id in ? order by id', [Core\Db::inList($ids)]) as Core\Db\Row $row) { echo $row->string('name') ?? '', "\n";}
$db->close();AdaAlan