Repository navigation
Expand file tree
/
Copy pathquery_builder_sqlite.php
More file actions
192 lines (165 loc) · 5.95 KB
/
Copy pathquery_builder_sqlite.php
File metadata and controls
192 lines (165 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/*
* This file is part of the Artemeon Core - Web Application Framework.
*
* (c) Artemeon <www.artemeon.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
/**
* Runnable demo of Connection::createQueryBuilder() backed by an in-memory SQLite database.
*
* Usage (from the repo root):
*
* php examples/query_builder_sqlite.php
*
* Uses SQLite so it runs without a database server; the same calls work on
* MySQL/MariaDB and PostgreSQL connections.
*/
use Artemeon\Database\Connection;
use Artemeon\Database\ConnectionParameters;
use Artemeon\Database\DriverFactory;
use Artemeon\Database\Schema\DataType;
use Doctrine\DBAL\ArrayParameterType;
require __DIR__ . '/../vendor/autoload.php';
$params = new ConnectionParameters(
host: 'localhost',
username: '',
password: '',
database: ':memory:',
port: null,
driver: 'sqlite3',
);
$connection = new Connection($params, new DriverFactory());
// --- Schema + seed -----------------------------------------------------------
$table = 'demo_books';
$tableReviews = 'demo_reviews';
$connection->createTable(
$table,
[
'id' => [DataType::CHAR20, false],
'title' => [DataType::CHAR100, false],
'author' => [DataType::CHAR100, false],
'year' => [DataType::INT, true],
],
['id'],
);
$connection->createTable(
$tableReviews,
[
'id' => [DataType::CHAR20, false],
'book_id' => [DataType::CHAR20, false],
'stars' => [DataType::INT, true],
'comment' => [DataType::CHAR100, true],
],
['id'],
);
$books = [
['id' => 'b-1', 'title' => 'The Pragmatic Programmer', 'author' => 'Andy Hunt', 'year' => 1999],
['id' => 'b-2', 'title' => 'Refactoring', 'author' => 'Martin Fowler', 'year' => 1999],
['id' => 'b-3', 'title' => 'Domain-Driven Design', 'author' => 'Eric Evans', 'year' => 2003],
['id' => 'b-4', 'title' => 'Clean Code', 'author' => 'Robert Martin', 'year' => 2008],
['id' => 'b-5', 'title' => 'Designing Data-Intensive Applications', 'author' => 'Martin Kleppmann', 'year' => 2017],
];
foreach ($books as $book) {
$connection->insert($table, $book);
}
$reviews = [
['id' => 'r-1', 'book_id' => 'b-1', 'stars' => 5, 'comment' => 'Essential read'],
['id' => 'r-2', 'book_id' => 'b-1', 'stars' => 4, 'comment' => 'Very practical'],
['id' => 'r-3', 'book_id' => 'b-3', 'stars' => 5, 'comment' => 'Changed how I think'],
['id' => 'r-4', 'book_id' => 'b-4', 'stars' => 3, 'comment' => 'Good but dated'],
['id' => 'r-5', 'book_id' => 'b-5', 'stars' => 5, 'comment' => 'Comprehensive'],
];
foreach ($reviews as $review) {
$connection->insert($tableReviews, $review);
}
// --- Build queries via the Doctrine DBAL QueryBuilder ------------------------
echo "1) SELECT with WHERE + ORDER BY (positional parameter)\n";
echo "------------------------------------------------------\n";
$rows = $connection->createQueryBuilder()
->select('title', 'author', 'year')
->from($table)
->where('year >= ?')
->orderBy('year', 'ASC')
->setParameter(0, 2000)
->executeQuery()
->fetchAllAssociative();
foreach ($rows as $row) {
printf(" %d %-45s %s\n", $row['year'], $row['title'], $row['author']);
}
echo "\n2) SELECT with named parameter (DBAL rewrites to positional)\n";
echo "------------------------------------------------------------\n";
$row = $connection->createQueryBuilder()
->select('title', 'author')
->from($table)
->where('id = :id')
->setParameter('id', 'b-3')
->executeQuery()
->fetchAssociative();
printf(" Book b-3 → %s by %s\n", $row['title'], $row['author']);
echo "\n3) UPDATE returning the affected row count\n";
echo "-----------------------------------------\n";
$affected = $connection->createQueryBuilder()
->update($table)
->set('author', '?')
->where('id = ?')
->setParameter(0, 'Andy Hunt and Dave Thomas')
->setParameter(1, 'b-1')
->executeStatement();
printf(" Updated %d row(s).\n", $affected);
$confirmed = $connection->fetchOne(
'SELECT author FROM ' . $table . ' WHERE id = ?',
['b-1'],
);
printf(" New author for b-1: %s\n", $confirmed);
echo "\n4) Aggregate via QueryBuilder\n";
echo "-----------------------------\n";
$count = $connection->createQueryBuilder()
->select('COUNT(*)')
->from($table)
->executeQuery()
->fetchOne();
printf(" Total books in table: %d\n", $count);
echo "\n5) Complex WHERE with AND, OR, and IN (array parameter)\n";
echo "-------------------------------------------------------\n";
// Books published before 2000 OR after 2010, but only from a specific id set
$qb = $connection->createQueryBuilder();
$rows = $qb
->select('title', 'author', 'year')
->from($table)
->where(
$qb->expr()->and(
$qb->expr()->or(
$qb->expr()->lt('year', ':cutoff_low'),
$qb->expr()->gt('year', ':cutoff_high'),
),
$qb->expr()->in('id', ':ids'),
),
)
->setParameter('cutoff_low', 2000)
->setParameter('cutoff_high', 2010)
->setParameter('ids', ['b-1', 'b-3', 'b-5'], ArrayParameterType::STRING)
->orderBy('year', 'ASC')
->executeQuery()
->fetchAllAssociative();
foreach ($rows as $row) {
printf(" %d %-45s %s\n", $row['year'], $row['title'], $row['author']);
}
echo "\n6) JOIN books with reviews, filter by minimum rating\n";
echo "------------------------------------------------------\n";
$qb = $connection->createQueryBuilder();
$rows = $qb
->select('b.title', 'b.author', 'r.stars', 'r.comment')
->from($table, 'b')
->innerJoin('b', $tableReviews, 'r', 'r.book_id = b.id')
->where($qb->expr()->gte('r.stars', ':min_stars'))
->setParameter('min_stars', 5)
->orderBy('b.year', 'ASC')
->executeQuery()
->fetchAllAssociative();
foreach ($rows as $row) {
printf(" %-45s %d★ %s\n", $row['title'], $row['stars'], $row['comment']);
}