-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy path06-sql-formatter.php
More file actions
142 lines (121 loc) · 4.02 KB
/
06-sql-formatter.php
File metadata and controls
142 lines (121 loc) · 4.02 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
<?php
/**
* Example: SQL Formatter / Pretty Printer
*
* Demonstrates SQL formatting for human-readable output during debugging.
*/
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../helpers.php';
$db = createExampleDb();
$driver = getCurrentDriver($db);
echo "=== SQL Formatter Example (on {$driver}) ===\n\n";
// Setup: Create sample tables using fluent API (cross-dialect)
$schema = $db->schema();
recreateTable($db, 'users', [
'id' => $schema->integer()->notNull(),
'name' => $schema->string(100),
'email' => $schema->string(100),
'status' => $schema->string(20),
'created_at' => $schema->datetime()->defaultExpression('CURRENT_TIMESTAMP'),
], ['primaryKey' => ['id']]);
recreateTable($db, 'orders', [
'id' => $schema->integer()->notNull(),
'user_id' => $schema->integer(),
'total' => $schema->decimal(10, 2),
'status' => $schema->string(20),
], ['primaryKey' => ['id']]);
echo "✓ Tables created\n\n";
// 1. Basic query - unformatted (default)
echo "1. Unformatted SQL (default):\n";
echo " " . str_repeat('-', 70) . "\n";
$sqlData = $db->find()
->from('users')
->where('status', 'active')
->toSQL(false);
echo " " . $sqlData['sql'] . "\n";
echo " " . str_repeat('-', 70) . "\n\n";
// 2. Basic query - formatted
echo "2. Formatted SQL:\n";
echo " " . str_repeat('-', 70) . "\n";
$sqlData = $db->find()
->from('users')
->where('status', 'active')
->orderBy('name')
->toSQL(true);
$formattedLines = explode("\n", $sqlData['sql']);
foreach ($formattedLines as $line) {
echo " " . $line . "\n";
}
echo " " . str_repeat('-', 70) . "\n\n";
// 3. Complex query with JOINs
echo "3. Complex query with JOINs (formatted):\n";
echo " " . str_repeat('-', 70) . "\n";
$sqlData = $db->find()
->from('users')
->select(['users.name', 'users.email', 'COUNT(orders.id) as order_count'])
->join('orders', 'users.id = orders.user_id')
->where('users.status', 'active')
->andWhere('orders.total', 100, '>')
->groupBy('users.id', 'users.name', 'users.email')
->having('COUNT(orders.id)', 1, '>')
->orderBy('order_count', 'DESC')
->limit(10)
->toSQL(true);
$formattedLines = explode("\n", $sqlData['sql']);
foreach ($formattedLines as $line) {
echo " " . $line . "\n";
}
echo " " . str_repeat('-', 70) . "\n\n";
// 4. Query with multiple WHERE conditions (AND/OR)
echo "4. Query with multiple WHERE conditions (formatted):\n";
echo " " . str_repeat('-', 70) . "\n";
$sqlData = $db->find()
->from('users')
->where('status', 'active')
->andWhere('created_at', date('Y-m-d', strtotime('-30 days')), '>=')
->orWhere('email', '[email protected]')
->orderBy('name')
->toSQL(true);
$formattedLines = explode("\n", $sqlData['sql']);
foreach ($formattedLines as $line) {
echo " " . $line . "\n";
}
echo " " . str_repeat('-', 70) . "\n\n";
// 5. Subquery (formatted)
echo "5. Query with subquery (formatted):\n";
echo " " . str_repeat('-', 70) . "\n";
$sqlData = $db->find()
->from('users')
->whereIn('id', function ($q) {
$q->from('orders')
->select('user_id')
->where('total', 500, '>')
->groupBy('user_id');
})
->toSQL(true);
$formattedLines = explode("\n", $sqlData['sql']);
foreach ($formattedLines as $line) {
echo " " . $line . "\n";
}
echo " " . str_repeat('-', 70) . "\n\n";
// 6. CTE (Common Table Expression) query (formatted)
echo "6. Query with CTE (formatted):\n";
echo " " . str_repeat('-', 70) . "\n";
$sqlData = $db->find()
->with('active_users', function ($q) {
$q->from('users')
->where('status', 'active');
})
->from('active_users')
->select(['name', 'email'])
->orderBy('name')
->toSQL(true);
$formattedLines = explode("\n", $sqlData['sql']);
foreach ($formattedLines as $line) {
echo " " . $line . "\n";
}
echo " " . str_repeat('-', 70) . "\n\n";
echo "=== SQL Formatter Example Complete ===\n";
// Clean up
$schema->dropTableIfExists('users');
$schema->dropTableIfExists('orders');