Commit 11fd698
committed
feat: add Db::inc()/Db::dec() support to onDuplicate() + migrate all examples to QueryBuilder API
MAJOR ENHANCEMENT: Complete migration to QueryBuilder API + UPSERT improvements
Features Added:
1. Db::inc() and Db::dec() now work in onDuplicate() for all dialects
2. PostgreSQL UPSERT with Db::raw() now properly handles column references
3. All examples migrated from rawQuery to QueryBuilder API
4. CI now tests all examples on all available dialects
API Changes (backwards compatible):
- buildUpsertClause() signature updated across all dialects:
public function buildUpsertClause(
array $updateColumns,
string $defaultConflictTarget = 'id',
string $tableName = '' // NEW: for PostgreSQL ambiguous column resolution
): string
Examples Refactored (13 files):
✅ Replaced rawQuery SELECT → find()->select()->where()->get()
✅ Replaced rawQuery INSERT → find()->insert()
✅ Replaced rawQuery UPDATE → find()->update()
✅ Replaced rawQuery DELETE → find()->delete()
✅ Replaced rawQueryValue COUNT → find()->select([Db::count()])->getValue()
✅ Kept rawQuery ONLY for DDL: CREATE TABLE, ALTER TABLE, DROP TABLE
Dialect Fixes:
MySQL:
- Added Db::inc()/Db::dec() handling in buildUpsertClause()
- Generates: `age` = `age` + 5
PostgreSQL:
- Added Db::inc()/Db::dec() handling with table qualification
- Generates: "age" = "user_stats"."age" + 5
- Fixed Db::raw() to auto-qualify column references
- Resolves "ambiguous column" errors in UPSERT
SQLite:
- Added Db::inc()/Db::dec() handling in buildUpsertClause()
- Generates: "age" = "age" + 5
QueryBuilder Improvements:
- executeInsert() now catches PDOException for tables without auto-increment
- Passes table name to buildUpsertClause() for PostgreSQL support
Files Changed:
Examples (13):
- 01-basic/04-insert-update.php (concat via Db::raw)
- 02-intermediate/03-pagination.php (COUNT via QueryBuilder)
- 03-advanced/02-bulk-operations.php (DELETE via QueryBuilder)
- 03-advanced/03-upsert.php (fixed PRIMARY KEY, Db::inc() usage)
- 04-json/01-json-basics.php (JSONB for PostgreSQL)
- 04-json/02-json-queries.php (JSONB for PostgreSQL)
- 05-helpers/01-string-helpers.php (string concat via Db::raw)
- 05-helpers/02-math-helpers.php (UPDATE via QueryBuilder)
- 05-helpers/04-null-helpers.php (INSERT/SELECT via QueryBuilder)
- 06-real-world/01-blog-system.php (JSONB, SELECT COUNT via QueryBuilder)
- 06-real-world/02-user-auth.php (hasPermission via QueryBuilder)
- 06-real-world/03-search-filters.php (all SELECTs via QueryBuilder)
- 06-real-world/04-multi-tenant.php (SELECT COUNT via QueryBuilder)
Source (5):
- src/dialects/DialectInterface.php
- src/dialects/MySQLDialect.php
- src/dialects/PostgreSQLDialect.php
- src/dialects/SqliteDialect.php
- src/query/QueryBuilder.php
Tests (3):
- tests/PdoDbMySQLTest.php (added testUpsertWithIncHelper)
- tests/PdoDbPostgreSQLTest.php (added testUpsertWithIncHelper)
- tests/PdoDbSqliteTest.php (added testUpsertWithIncHelper)
Scripts (1):
- scripts/test-examples.sh (fixed PostgreSQL port detection)
CI (1):
- .github/workflows/tests.yml (added example testing for all dialects)
Testing Results:
✅ All 343 PHPUnit tests pass (1544 assertions)
✅ All 21 examples work on SQLite (21/21)
✅ All 20 examples work on MySQL (20/20)
✅ All 20 examples work on PostgreSQL (20/20)
✅ PHPStan level 8 passes with no errors
✅ No warnings or errors in any example
Breaking Changes: NONE (API is backwards compatible)1 parent 48563c6 commit 11fd698
File tree
23 files changed
+329
-87
lines changed- .github/workflows
- examples
- 01-basic
- 02-intermediate
- 03-advanced
- 04-json
- 05-helpers
- 06-real-world
- scripts
- src
- dialects
- query
- tests
23 files changed
+329
-87
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
55 | 74 | | |
56 | 75 | | |
57 | 76 | | |
| |||
95 | 114 | | |
96 | 115 | | |
97 | 116 | | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
98 | 131 | | |
99 | 132 | | |
100 | 133 | | |
| |||
117 | 150 | | |
118 | 151 | | |
119 | 152 | | |
| 153 | + | |
| 154 | + | |
120 | 155 | | |
121 | 156 | | |
122 | 157 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
111 | | - | |
| 111 | + | |
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
| 63 | + | |
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
82 | | - | |
| 82 | + | |
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | | - | |
| 44 | + | |
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
66 | | - | |
| 66 | + | |
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
91 | | - | |
| 91 | + | |
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
| 99 | + | |
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
| |||
139 | 139 | | |
140 | 140 | | |
141 | 141 | | |
142 | | - | |
| 142 | + | |
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
| |||
148 | 148 | | |
149 | 149 | | |
150 | 150 | | |
151 | | - | |
| 151 | + | |
152 | 152 | | |
153 | 153 | | |
154 | 154 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
88 | | - | |
| 88 | + | |
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
| |||
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
126 | | - | |
127 | | - | |
| 126 | + | |
| 127 | + | |
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | | - | |
24 | | - | |
| 24 | + | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | | - | |
24 | | - | |
| 24 | + | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
131 | | - | |
| 131 | + | |
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
89 | | - | |
90 | | - | |
| 89 | + | |
| 90 | + | |
91 | 91 | | |
92 | 92 | | |
93 | 93 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
88 | 88 | | |
89 | 89 | | |
90 | 90 | | |
91 | | - | |
| 91 | + | |
92 | 92 | | |
93 | 93 | | |
94 | 94 | | |
| |||
139 | 139 | | |
140 | 140 | | |
141 | 141 | | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
151 | 152 | | |
152 | 153 | | |
153 | 154 | | |
| |||
0 commit comments