Skip to content

Commit 72aab10

Browse files
committed
docs: sqlbuilder examples
1 parent 81139f5 commit 72aab10

3 files changed

Lines changed: 193 additions & 0 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
use Gt\Database\Connection\Settings;
3+
use Gt\Database\Database;
4+
5+
chdir(dirname(__DIR__));
6+
require "vendor/autoload.php";
7+
require __DIR__ . "/bootstrap.php";
8+
9+
[$workspace, $queryPath, $databasePath] = createExampleWorkspace("08-sqlbuilder-query-collections");
10+
11+
try {
12+
$classCode = <<<PHP
13+
use Gt\SqlBuilder\InsertBuilder;
14+
use Gt\SqlBuilder\SelectBuilder;
15+
16+
class Product {
17+
public function insert():InsertBuilder {
18+
return (new InsertBuilder())
19+
->into("product")
20+
->columns("name", "price")
21+
->values(":name", ":price");
22+
}
23+
24+
public function getByMinPrice():SelectBuilder {
25+
return (new SelectBuilder())
26+
->select("id", "name", "price")
27+
->from("product")
28+
->where("price >= :minPrice")
29+
->orderBy("price desc", "id");
30+
}
31+
}
32+
PHP;
33+
34+
writePhpQueryCollection(
35+
$queryPath,
36+
"Product",
37+
"Demo\\Query",
38+
$classCode
39+
);
40+
41+
$settings = new Settings(
42+
$queryPath,
43+
Settings::DRIVER_SQLITE,
44+
$databasePath
45+
);
46+
$db = new Database($settings);
47+
48+
$db->executeSql(implode("\n", [
49+
"create table product(",
50+
"\tid integer primary key autoincrement,",
51+
"\tname text not null,",
52+
"\tprice decimal(10,2) not null",
53+
")",
54+
]));
55+
56+
$productQueries = $db->queryCollection("Product");
57+
$productQueries->setAppNamespace("Demo\\Query");
58+
59+
$productQueries->insert("insert", [
60+
"name" => "Keyboard",
61+
"price" => 59.99,
62+
]);
63+
$productQueries->insert("insert", [
64+
"name" => "Mouse",
65+
"price" => 19.99,
66+
]);
67+
$productQueries->insert("insert", [
68+
"name" => "Monitor",
69+
"price" => 249.99,
70+
]);
71+
72+
$rows = $productQueries->fetchAll("getByMinPrice", [
73+
"minPrice" => 50,
74+
]);
75+
foreach($rows as $row) {
76+
printf("%d %-10s %.2f\n", $row->id, $row->name, $row->getFloat("price"));
77+
}
78+
}
79+
finally {
80+
removeDirectory($workspace);
81+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
use Gt\Database\Connection\Settings;
3+
use Gt\Database\Database;
4+
5+
chdir(dirname(__DIR__));
6+
require "vendor/autoload.php";
7+
require __DIR__ . "/bootstrap.php";
8+
9+
[$workspace, $queryPath, $databasePath] = createExampleWorkspace("09-sqlbuilder-overrides-and-reports");
10+
11+
try {
12+
$classCode = <<<PHP
13+
use Gt\SqlBuilder\SelectBuilder;
14+
15+
class Report {
16+
public function categoryTotals():SelectBuilder {
17+
return (new SelectBuilder())
18+
->select(
19+
"category.name as categoryName",
20+
"count(product.id) as productCount",
21+
"round(sum(product.price), 2) as totalValue"
22+
)
23+
->from("product")
24+
->innerJoin("category on category.id = product.categoryId")
25+
->where(
26+
"product.price >= :minPrice",
27+
"product.name != :excludedName"
28+
)
29+
->groupBy("category.name")
30+
->having("count(product.id) >= 1")
31+
->orderBy("totalValue desc", "category.name");
32+
}
33+
}
34+
PHP;
35+
36+
writePhpQueryCollection(
37+
$queryPath,
38+
"Report",
39+
"Demo\\Query",
40+
$classCode
41+
);
42+
writeSqlQuery($queryPath, "Report", "getByCode", implode("\n", [
43+
"select product.code, product.name, category.name as categoryName, product.price",
44+
"from product",
45+
"inner join category on category.id = product.categoryId",
46+
"where product.code = :code",
47+
"limit 1",
48+
]));
49+
50+
$settings = new Settings(
51+
$queryPath,
52+
Settings::DRIVER_SQLITE,
53+
$databasePath
54+
);
55+
$db = new Database($settings);
56+
57+
$db->executeSql(implode("\n", [
58+
"create table category(",
59+
"\tid integer primary key autoincrement,",
60+
"\tname text not null",
61+
")",
62+
]));
63+
$db->executeSql(implode("\n", [
64+
"create table product(",
65+
"\tid integer primary key autoincrement,",
66+
"\tcategoryId integer not null,",
67+
"\tcode text not null,",
68+
"\tname text not null,",
69+
"\tprice decimal(10,2) not null,",
70+
"\tforeign key(categoryId) references category(id)",
71+
")",
72+
]));
73+
$db->executeSql("insert into category(name) values('Peripherals'), ('Displays')");
74+
$db->executeSql(implode("\n", [
75+
"insert into product(categoryId, code, name, price) values",
76+
"(1, 'KEY-01', 'Keyboard', 59.99),",
77+
"(1, 'MOU-01', 'Mouse', 19.99),",
78+
"(2, 'MON-01', 'Monitor', 249.99),",
79+
"(2, 'MON-02', 'Portable Display', 179.99)",
80+
]));
81+
82+
$reportQueries = $db->queryCollection("Report");
83+
$reportQueries->setAppNamespace("Demo\\Query");
84+
85+
$product = $reportQueries->fetch("getByCode", [
86+
"code" => "MON-01",
87+
]);
88+
printf(
89+
"Override query: %s in %s costs %.2f\n",
90+
$product->name,
91+
$product->categoryName,
92+
$product->getFloat("price")
93+
);
94+
95+
$totals = $reportQueries->fetchAll("categoryTotals", [
96+
"minPrice" => 20,
97+
"excludedName" => "Discontinued",
98+
]);
99+
foreach($totals as $row) {
100+
printf(
101+
"Category: %-12s Count: %d Total: %.2f\n",
102+
$row->categoryName,
103+
$row->getInt("productCount"),
104+
$row->getFloat("totalValue")
105+
);
106+
}
107+
}
108+
finally {
109+
removeDirectory($workspace);
110+
}

example/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ Each example:
2121
- `05-database-migrations.php`: running SQL migrations with `Migrator`
2222
- `06-multiple-connections.php`: named connections and switching context
2323
- `07-php-query-collections.php`: PHP-backed query collections with custom namespace
24+
- `08-sqlbuilder-query-collections.php`: simple `phpgt/sqlbuilder` query collection methods
25+
- `09-sqlbuilder-overrides-and-reports.php`: class-backed reports plus SQL overrides

0 commit comments

Comments
 (0)