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