Aggregate.php
9 months ago
CRUD.php
1 year ago
FromClause.php
4 years ago
GroupByStatement.php
4 years ago
HavingClause.php
3 years ago
InsertInto.php
1 year ago
JoinClause.php
4 years ago
LimitStatement.php
4 years ago
MetaQuery.php
3 years ago
OffsetStatement.php
4 years ago
OrderByStatement.php
1 year ago
SelectStatement.php
4 years ago
TablePrefix.php
4 years ago
UnionOperator.php
4 years ago
WhereClause.php
3 years ago
CRUD.php
155 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\QueryBuilder\Concerns; |
| 4 | |
| 5 | use Give\Framework\Database\DB; |
| 6 | use Give\Vendors\StellarWP\Arrays\Arr; |
| 7 | |
| 8 | /** |
| 9 | * @since 2.19.0 |
| 10 | */ |
| 11 | trait CRUD |
| 12 | { |
| 13 | /** |
| 14 | * @see https://developer.wordpress.org/reference/classes/wpdb/insert/ |
| 15 | * |
| 16 | * @since 4.0.0 Add support for inserting multiple rows at once |
| 17 | * @since 2.19.0 |
| 18 | * |
| 19 | * @param array|string $format |
| 20 | * |
| 21 | * @param array $data |
| 22 | * @return false|int |
| 23 | * |
| 24 | */ |
| 25 | public function insert($data, $format = null) |
| 26 | { |
| 27 | if (Arr::is_list($data)) { |
| 28 | return DB::query( |
| 29 | $this->getInsertIntoSQL($data, $format) |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | return DB::insert( |
| 34 | $this->getTable(), |
| 35 | $data, |
| 36 | $format |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @see https://developer.wordpress.org/reference/classes/wpdb/update/ |
| 42 | * |
| 43 | * @since 2.19.0 |
| 44 | * |
| 45 | * @param null $format |
| 46 | * |
| 47 | * @param array $data |
| 48 | * @return false|int |
| 49 | * |
| 50 | */ |
| 51 | public function update($data, $format = null) |
| 52 | { |
| 53 | return DB::update( |
| 54 | $this->getTable(), |
| 55 | $data, |
| 56 | $this->getWhere(), |
| 57 | $format, |
| 58 | null |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @since 2.19.0 |
| 64 | * |
| 65 | * @return false|int |
| 66 | * |
| 67 | * @see https://developer.wordpress.org/reference/classes/wpdb/delete/ |
| 68 | */ |
| 69 | public function delete() |
| 70 | { |
| 71 | return DB::delete( |
| 72 | $this->getTable(), |
| 73 | $this->getWhere(), |
| 74 | null |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @since 4.3.0 |
| 80 | */ |
| 81 | public function truncate() |
| 82 | { |
| 83 | DB::query('TRUNCATE TABLE ' . $this->getTable()); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get results |
| 88 | * |
| 89 | * @since 2.19.0 |
| 90 | * |
| 91 | * @param string ARRAY_A|ARRAY_N|OBJECT|OBJECT_K $output |
| 92 | * |
| 93 | * @return array|object|null |
| 94 | */ |
| 95 | public function getAll($output = OBJECT) |
| 96 | { |
| 97 | return DB::get_results($this->getSQL(), $output); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Get row |
| 102 | * |
| 103 | * @since 2.19.0 |
| 104 | * |
| 105 | * @param string ARRAY_A|ARRAY_N|OBJECT|OBJECT_K $output |
| 106 | * |
| 107 | * @return array|object|null |
| 108 | */ |
| 109 | public function get($output = OBJECT) |
| 110 | { |
| 111 | return DB::get_row($this->getSQL(), $output); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get a single column's value from the first result of a query. |
| 116 | * |
| 117 | * @since 2.24.0 |
| 118 | * |
| 119 | * @param string $column |
| 120 | * |
| 121 | * @return mixed |
| 122 | */ |
| 123 | public function value(string $column) |
| 124 | { |
| 125 | $result = (array) $this->select($column)->get(); |
| 126 | return count($result) > 0 ? $result[$column] : null; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @since 2.19.0 |
| 131 | * |
| 132 | * @return string |
| 133 | */ |
| 134 | private function getTable() |
| 135 | { |
| 136 | return $this->froms[0]->table; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @since 2.19.0 |
| 141 | * |
| 142 | * @return array[] |
| 143 | */ |
| 144 | private function getWhere() |
| 145 | { |
| 146 | $wheres = []; |
| 147 | |
| 148 | foreach ($this->wheres as $where) { |
| 149 | $wheres[$where->column] = $where->value; |
| 150 | } |
| 151 | |
| 152 | return $wheres; |
| 153 | } |
| 154 | } |
| 155 |