-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCRUD.php
More file actions
179 lines (165 loc) · 4.13 KB
/
CRUD.php
File metadata and controls
179 lines (165 loc) · 4.13 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
<?php
namespace StellarWP\DB\QueryBuilder\Concerns;
use StellarWP\DB\DB;
/**
* @since 1.0.0
*/
trait CRUD {
/**
* @see https://developer.wordpress.org/reference/classes/wpdb/insert/
*
* @since 1.0.0
*
* @param array|string $format
*
* @param array $data
* @return false|int
*
*/
public function insert( $data, $format = null ) {
return DB::insert(
$this->getTable(),
$data,
$format
);
}
/**
* @see https://developer.wordpress.org/reference/classes/wpdb/update/
*
* @since 1.0.0
*
* @param array $data
* @param array|string|null $format
*
* @return false|int
*
*/
public function update( $data, $format = null ) {
return DB::update(
$this->getTable(),
$data,
$this->getWhere(),
$format,
null
);
}
/**
* Upsert allows for inserting or updating a row depending on whether it already exists.
*
* @since 1.0.8
*
* @param array<string, string|int|float|bool|null> $data The data to insert or update.
* @param array $match The columns to match on.
* @param string|array|null $format Array of formats to be mapped to each value in $data. If string, the format will be used for all values in $data.
*
* @return false|int Number of rows updated/inserted, false on error.
*/
public function upsert( $data, $match = [], $format = null ) {
// Build the where clause(s).
foreach ( $match as $column ) {
$this->where( $column, $data[ $column ] );
}
// If the row exists, update it.
if ( $this->get() ) {
return $this->update( $data, $format );
}
// Otherwise, insert it.
return $this->insert( $data, $format );
}
/**
* Delete rows from the database.
*
* Unlike WordPress's $wpdb->delete() method, this implementation generates and executes
* a DELETE SQL statement directly, which allows for advanced features like ORDER BY and LIMIT.
*
* Supports:
* - WHERE clauses (including whereLike, whereIn, whereBetween, etc.)
* - ORDER BY for controlling which rows are deleted first
* - LIMIT to restrict the number of rows deleted
* - Complex WHERE conditions (AND, OR, nested queries)
*
* Usage examples:
* ```php
* // Simple delete with WHERE
* DB::table('posts')->where('post_status', 'draft')->delete();
*
* // Delete with LIMIT (delete only 10 rows)
* DB::table('posts')->where('post_type', 'temp')->limit(10)->delete();
*
* // Delete oldest posts first using ORDER BY and LIMIT
* DB::table('posts')
* ->where('post_status', 'trash')
* ->orderBy('post_date', 'ASC')
* ->limit(100)
* ->delete();
*
* // Delete with LIKE pattern
* DB::table('posts')->whereLike('post_title', 'Draft:%')->delete();
*
* // Delete with multiple conditions
* DB::table('posts')
* ->where('post_type', 'page')
* ->where('post_status', 'auto-draft')
* ->whereBetween('ID', 1, 1000)
* ->delete();
* ```
*
* Restrictions:
* - Table aliases in the FROM clause may not be supported on older database versions
* (MySQL < 8.0.24, MariaDB < 11.6). Avoid using table aliases with delete().
* - JOINs are not supported in DELETE statements with this implementation
*
* @since 1.0.0
*
* @return false|int Number of rows deleted, or false on error.
*
* @see QueryBuilder::deleteSQL() for the SQL generation logic
*/
public function delete() {
return DB::query( $this->deleteSQL() );
}
/**
* Get results
*
* @since 1.0.0
*
* @param string $output ARRAY_A|ARRAY_N|OBJECT|OBJECT_K
*
* @return array|object|null
*/
public function getAll( $output = OBJECT ) {
return DB::get_results( $this->getSQL(), $output );
}
/**
* Get row
*
* @since 1.0.0
*
* @param string $output ARRAY_A|ARRAY_N|OBJECT|OBJECT_K
*
* @return array|object|null
*/
public function get( $output = OBJECT ) {
return DB::get_row( $this->getSQL(), $output );
}
/**
* @since 1.0.0
*
* @return string
*/
private function getTable() {
return $this->froms[0]->table;
}
/**
* @since 1.0.0
*
* @return array[]
*/
private function getWhere() {
$wheres = [];
foreach ( $this->wheres as $where ) {
$wheres[ $where->column ] = $where->value;
}
return $wheres;
}
}