|
| 1 | +<?php |
| 2 | + |
| 3 | +chdir(dirname(__DIR__)); |
| 4 | +require "vendor/autoload.php"; |
| 5 | +require __DIR__ . "/bootstrap.php"; |
| 6 | + |
| 7 | +[$workspace, $queryPath, $databasePath] = createExampleWorkspace("04-dynamic-bindings"); |
| 8 | + |
| 9 | +try { |
| 10 | + $db = createExampleDatabase($queryPath, $databasePath); |
| 11 | + $db->executeSql(implode("\n", [ |
| 12 | + "create table purchase(", |
| 13 | + "\tid integer primary key,", |
| 14 | + "\tcustomerId text not null,", |
| 15 | + "\tproductId integer not null,", |
| 16 | + "\tcreatedAt text not null", |
| 17 | + ")", |
| 18 | + ])); |
| 19 | + |
| 20 | + writeSqlQuery($queryPath, "purchase", "bulkInsert", implode("\n", [ |
| 21 | + "insert into purchase(id, customerId, productId, createdAt)", |
| 22 | + "values (?, ?, ?, ?)", |
| 23 | + ])); |
| 24 | + writeSqlQuery($queryPath, "purchase", "findIn", implode("\n", [ |
| 25 | + "select id, customerId from purchase", |
| 26 | + "where id in (:__dynamicIn)", |
| 27 | + "order by :orderBy", |
| 28 | + "limit :limit offset :offset", |
| 29 | + ])); |
| 30 | + writeSqlQuery($queryPath, "purchase", "findByPairs", implode("\n", [ |
| 31 | + "select id, customerId, productId from purchase", |
| 32 | + "where :__dynamicOr", |
| 33 | + "order by id", |
| 34 | + ])); |
| 35 | + |
| 36 | + $db->insert("purchase/bulkInsert", 1, "cust_1", 100, "2026-01-01"); |
| 37 | + $db->insert("purchase/bulkInsert", 2, "cust_2", 101, "2026-01-02"); |
| 38 | + $db->insert("purchase/bulkInsert", 3, "cust_2", 102, "2026-01-03"); |
| 39 | + $db->insert("purchase/bulkInsert", 4, "cust_3", 103, "2026-01-04"); |
| 40 | + |
| 41 | + $dynamicInRows = $db->fetchAll("purchase/findIn", [ |
| 42 | + "__dynamicIn" => [1, 3, 4], |
| 43 | + "orderBy" => "id desc", |
| 44 | + "limit" => 10, |
| 45 | + "offset" => 0, |
| 46 | + ]); |
| 47 | + echo "Dynamic IN results:\n"; |
| 48 | + foreach($dynamicInRows as $row) { |
| 49 | + echo "{$row->id} {$row->customerId}\n"; |
| 50 | + } |
| 51 | + |
| 52 | + $dynamicOrRows = $db->fetchAll("purchase/findByPairs", [ |
| 53 | + "__dynamicOr" => [ |
| 54 | + ["customerId" => "cust_2", "productId" => 101], |
| 55 | + ["customerId" => "cust_3", "productId" => 103], |
| 56 | + ], |
| 57 | + ]); |
| 58 | + echo "Dynamic OR results:\n"; |
| 59 | + foreach($dynamicOrRows as $row) { |
| 60 | + echo "{$row->id} {$row->customerId} {$row->productId}\n"; |
| 61 | + } |
| 62 | +} |
| 63 | +finally { |
| 64 | + removeDirectory($workspace); |
| 65 | +} |
0 commit comments