|
| 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 | +} |
0 commit comments