1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Adi |
5
|
|
|
* Date: 2/24/2019 |
6
|
|
|
* Time: 6:54 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Qpdb\QueryBuilder\Abstracts; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Qpdb\PdoWrapper\PdoWrapperService; |
13
|
|
|
use Qpdb\QueryBuilder\Dependencies\QueryException; |
14
|
|
|
use Qpdb\QueryBuilder\QueryBuild; |
15
|
|
|
|
16
|
|
|
abstract class AbstractTableDao |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $table; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string|array |
26
|
|
|
*/ |
27
|
|
|
protected $primary; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $orderField; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* AbstractTableCrud constructor. |
37
|
|
|
*/ |
38
|
|
|
public function __construct() |
39
|
|
|
{ |
40
|
|
|
$this->table = $this->getTableName(); |
41
|
|
|
$this->primary = (array)$this->getPrimaryKey(); |
42
|
|
|
$this->orderField = $this->getOrderField(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
abstract protected function getTableName(); |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string|array |
53
|
|
|
*/ |
54
|
|
|
abstract protected function getPrimaryKey(); |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
abstract protected function getOrderField(); |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param $id |
64
|
|
|
* @param array $fields |
65
|
|
|
* @return array|bool |
66
|
|
|
* @throws QueryException |
67
|
|
|
*/ |
68
|
|
|
public function getRowById( $id, array $fields = [] ) |
69
|
|
|
{ |
70
|
|
|
$conditions = $this->getPrimaryKeyConditions( $id ); |
71
|
|
|
$result = QueryBuild::select( $this->table )->fields( $fields ); |
72
|
|
|
foreach ( $conditions as $field => $value ) |
73
|
|
|
$result->whereEqual( $field, $value ); |
74
|
|
|
|
75
|
|
|
return $result->first()->execute(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $id |
81
|
|
|
* @return array|int|null |
82
|
|
|
* @throws QueryException |
83
|
|
|
*/ |
84
|
|
|
public function deleteRowById( $id ) |
85
|
|
|
{ |
86
|
|
|
$conditions = $this->getPrimaryKeyConditions( $id ); |
87
|
|
|
$result = QueryBuild::delete( $this->table ); |
88
|
|
|
foreach ( $conditions as $field => $value ) |
89
|
|
|
$result->whereEqual( $field, $value ); |
90
|
|
|
|
91
|
|
|
return $result->execute(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $id |
96
|
|
|
* @param array $arrayUpdater |
97
|
|
|
* @return array|int|null |
98
|
|
|
* @throws \Qpdb\QueryBuilder\Dependencies\QueryException |
99
|
|
|
*/ |
100
|
|
|
public function updateRowById( $id, array $arrayUpdater ) |
101
|
|
|
{ |
102
|
|
|
$conditions = $this->getPrimaryKeyConditions( $id ); |
103
|
|
|
$result = QueryBuild::update( $this->table ); |
104
|
|
|
foreach ( $conditions as $field => $value ) |
105
|
|
|
$result->whereEqual( $field, $value ); |
106
|
|
|
$result->setFieldsByArray( $arrayUpdater ); |
107
|
|
|
|
108
|
|
|
return $result->execute(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param array $arrayValues |
114
|
|
|
* @return bool|mixed|\PDOStatement |
115
|
|
|
* @throws QueryException |
116
|
|
|
*/ |
117
|
|
|
public function insertRow( array $arrayValues ) |
118
|
|
|
{ |
119
|
|
|
return QueryBuild::insert( $this->table )->setFieldsByArray( $arrayValues )->execute(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param array $updates_ord |
124
|
|
|
* @return bool|\PDOStatement |
125
|
|
|
* @throws QueryException |
126
|
|
|
*/ |
127
|
|
|
public function saveOrder( $updates_ord = array() ) |
128
|
|
|
{ |
129
|
|
|
if ( empty( $this->orderField ) ) |
130
|
|
|
throw new QueryException( 'Order field is not defined' ); |
131
|
|
|
|
132
|
|
|
$query = /** @lang text */ |
133
|
|
|
"UPDATE `{$this->table}` SET `{$this->orderField}` = CASE `{$this->primary[0]}` \r\n"; |
134
|
|
|
foreach ( $updates_ord as $position => $id ) { |
135
|
|
|
$pos = $position + 1; |
136
|
|
|
$query .= " WHEN '$id' THEN '$pos' \r\n"; |
137
|
|
|
} |
138
|
|
|
$query .= "ELSE `{$this->orderField}` END"; |
139
|
|
|
|
140
|
|
|
return PdoWrapperService::getInstance()->query($query, []); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param mixed |
146
|
|
|
* @return array |
147
|
|
|
* @throws QueryException |
148
|
|
|
*/ |
149
|
|
|
protected function getPrimaryKeyConditions( $id ) |
150
|
|
|
{ |
151
|
|
|
if ( !is_array( $id ) ) |
152
|
|
|
$id = [ $id ]; |
153
|
|
|
|
154
|
|
|
if ( count( $this->primary ) !== count( $id ) ) |
|
|
|
|
155
|
|
|
throw new QueryException( 'Invalid primary key', QueryException::QUERY_CRUD_INVALID_PRIMARY ); |
156
|
|
|
|
157
|
|
|
$conditions = []; |
158
|
|
|
|
159
|
|
|
foreach ( $this->primary as $index => $key ) |
160
|
|
|
$conditions[ $key ] = $id[ $index ]; |
161
|
|
|
|
162
|
|
|
return $conditions; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
} |