1
1
<?php
2
2
3
+ declare (strict_types=1 );
3
4
4
5
namespace PhpClickHouseLaravel ;
5
6
6
-
7
7
use ClickHouseDB \Client ;
8
+ use ClickHouseDB \Statement ;
9
+ use Exception ;
8
10
use Illuminate \Database \Eloquent \Concerns \HasAttributes ;
9
11
use Illuminate \Support \Facades \DB ;
10
12
use Illuminate \Support \Str ;
13
15
14
16
class BaseModel
15
17
{
16
-
17
18
use HasAttributes;
18
19
19
20
/**
@@ -50,7 +51,7 @@ class BaseModel
50
51
*
51
52
* @return string
52
53
*/
53
- public function getTable ()
54
+ public function getTable (): string
54
55
{
55
56
return $ this ->table ?? Str::snake (Str::pluralStudly (class_basename ($ this )));
56
57
}
@@ -60,7 +61,7 @@ public function getTable()
60
61
*
61
62
* @return string
62
63
*/
63
- public function getTableForInserts ()
64
+ public function getTableForInserts (): string
64
65
{
65
66
return $ this ->tableForInserts ?? $ this ->getTable ();
66
67
}
@@ -69,15 +70,15 @@ public function getTableForInserts()
69
70
* Use this field for OPTIMIZE TABLE OR ALTER TABLE (also DELETE) queries
70
71
* @return string
71
72
*/
72
- public function getTableSources ()
73
+ public function getTableSources (): string
73
74
{
74
75
return $ this ->tableSources ?? $ this ->getTable ();
75
76
}
76
77
77
78
/**
78
79
* @return Client
79
80
*/
80
- public static function getClient ()
81
+ public static function getClient (): Client
81
82
{
82
83
return DB ::connection ('clickhouse ' )->getClient ();
83
84
}
@@ -87,23 +88,25 @@ public static function getClient()
87
88
* @param array $attributes
88
89
* @return static
89
90
*/
90
- public static function make ($ attributes = [])
91
+ public static function make (array $ attributes = [])
91
92
{
92
93
$ model = new static ;
93
94
$ model ->fill ($ attributes );
95
+
94
96
return $ model ;
95
97
}
96
98
97
99
/**
98
100
* Save a new model and return the instance.
99
101
* @param array $attributes
100
102
* @return static
101
- * @throws \ Exception
103
+ * @throws Exception
102
104
*/
103
- public static function create ($ attributes = [])
105
+ public static function create (array $ attributes = [])
104
106
{
105
107
$ model = static ::make ($ attributes );
106
108
$ model ->save ();
109
+
107
110
return $ model ;
108
111
}
109
112
@@ -112,36 +115,38 @@ public static function create($attributes = [])
112
115
* @param array $attributes
113
116
* @return $this
114
117
*/
115
- public function fill (array $ attributes )
118
+ public function fill (array $ attributes ): self
116
119
{
117
120
foreach ($ attributes as $ key => $ value ) {
118
121
$ this ->setAttribute ($ key , $ value );
119
122
}
123
+
120
124
return $ this ;
121
125
}
122
126
123
127
/**
124
128
* Save the model to the database.
125
129
* @param array $options
126
130
* @return bool
127
- * @throws \ Exception
131
+ * @throws Exception
128
132
*/
129
- public function save (array $ options = [])
133
+ public function save (array $ options = []): bool
130
134
{
131
135
if ($ this ->exists ) {
132
- throw new \ Exception ("Clickhouse does not allow update rows " );
136
+ throw new Exception ("Clickhouse does not allow update rows " );
133
137
}
134
138
$ this ->exists = !static ::insertAssoc ([$ this ->getAttributes ()])->isError ();
139
+
135
140
return $ this ->exists ;
136
141
}
137
142
138
143
/**
139
144
* Bulk insert into Clickhouse database
140
145
* @param array[] $rows
141
- * @return \ClickHouseDB\ Statement
146
+ * @return Statement
142
147
* @deprecated use insertBulk
143
148
*/
144
- public static function insert ($ rows )
149
+ public static function insert (array $ rows ): Statement
145
150
{
146
151
return static ::getClient ()->insert ((new static )->getTableForInserts (), $ rows );
147
152
}
@@ -150,10 +155,10 @@ public static function insert($rows)
150
155
* Bulk insert into Clickhouse database
151
156
* @param array[] $rows
152
157
* @param array $columns
153
- * @return \ClickHouseDB\ Statement
158
+ * @return Statement
154
159
* @example MyModel::insertBulk([['model 1', 1], ['model 2', 2]], ['model_name', 'some_param']);
155
160
*/
156
- public static function insertBulk ($ rows , $ columns = [])
161
+ public static function insertBulk (array $ rows , array $ columns = []): Statement
157
162
{
158
163
return static ::getClient ()->insert ((new static )->getTableForInserts (), $ rows , $ columns );
159
164
}
@@ -162,21 +167,23 @@ public static function insertBulk($rows, $columns = [])
162
167
* Prepare each row by calling static::prepareFromRequest to bulk insert into database
163
168
* @param array[] $rows
164
169
* @param array $columns
165
- * @return \ClickHouseDB\ Statement
170
+ * @return Statement
166
171
*/
167
- public static function prepareAndInsert ($ rows , $ columns = [])
172
+ public static function prepareAndInsert (array $ rows , array $ columns = []): Statement
168
173
{
169
174
$ rows = array_map ('static::prepareFromRequest ' , $ rows , $ columns );
170
- return static ::getClient ()->insert ((new static )->getTableForInserts (), $ rows , $ columns );
175
+
176
+ return static ::getClient ()
177
+ ->insert ((new static )->getTableForInserts (), $ rows , $ columns );
171
178
}
172
179
173
180
/**
174
181
* Bulk insert rows as associative array into Clickhouse database
175
182
* @param array[] $rows
176
- * @return \ClickHouseDB\ Statement
183
+ * @return Statement
177
184
* @example MyModel::insertAssoc([['model_name' => 'model 1', 'some_param' => 1], ['model_name' => 'model 2', 'some_param' => 2]]);
178
185
*/
179
- public static function insertAssoc ($ rows )
186
+ public static function insertAssoc (array $ rows ): Statement
180
187
{
181
188
$ rows = array_values ($ rows );
182
189
if (isset ($ rows [0 ]) && isset ($ rows [1 ])) {
@@ -191,9 +198,9 @@ public static function insertAssoc($rows)
191
198
/**
192
199
* Prepare each row by calling static::prepareAssocFromRequest to bulk insert into database
193
200
* @param array[] $rows
194
- * @return \ClickHouseDB\ Statement
201
+ * @return Statement
195
202
*/
196
- public static function prepareAndInsertAssoc ($ rows )
203
+ public static function prepareAndInsertAssoc (array $ rows ): Statement
197
204
{
198
205
$ rows = array_map ('static::prepareAssocFromRequest ' , $ rows );
199
206
return static ::insertAssoc ($ rows );
@@ -206,7 +213,7 @@ public static function prepareAndInsertAssoc($rows)
206
213
* @param array $columns
207
214
* @return array
208
215
*/
209
- public static function prepareFromRequest ($ row , $ columns = [])
216
+ public static function prepareFromRequest (array $ row , array $ columns = []): array
210
217
{
211
218
return $ row ;
212
219
}
@@ -217,7 +224,7 @@ public static function prepareFromRequest($row, $columns = [])
217
224
* @param array $row
218
225
* @return array
219
226
*/
220
- public static function prepareAssocFromRequest ($ row )
227
+ public static function prepareAssocFromRequest (array $ row ): array
221
228
{
222
229
return $ row ;
223
230
}
@@ -226,7 +233,7 @@ public static function prepareAssocFromRequest($row)
226
233
* @param array $select optional = ['*']
227
234
* @return Builder
228
235
*/
229
- public static function select ($ select = ['* ' ])
236
+ public static function select (array $ select = ['* ' ]): Builder
230
237
{
231
238
return (new Builder )->select ($ select )->from ((new static )->getTable ());
232
239
}
@@ -235,7 +242,7 @@ public static function select($select = ['*'])
235
242
* Necessary stub for HasAttributes trait
236
243
* @return array
237
244
*/
238
- public function getCasts ()
245
+ public function getCasts (): array
239
246
{
240
247
return $ this ->casts ;
241
248
}
@@ -244,7 +251,7 @@ public function getCasts()
244
251
* Necessary stub for HasAttributes trait
245
252
* @return bool
246
253
*/
247
- public function usesTimestamps ()
254
+ public function usesTimestamps (): bool
248
255
{
249
256
return false ;
250
257
}
@@ -265,7 +272,7 @@ public function getRelationValue($key)
265
272
* @param string $key
266
273
* @return mixed
267
274
*/
268
- public function __get ($ key )
275
+ public function __get (string $ key )
269
276
{
270
277
return $ this ->getAttribute ($ key );
271
278
}
@@ -277,7 +284,7 @@ public function __get($key)
277
284
* @param mixed $value
278
285
* @return void
279
286
*/
280
- public function __set ($ key , $ value )
287
+ public function __set (string $ key , $ value ): void
281
288
{
282
289
$ this ->setAttribute ($ key , $ value );
283
290
}
@@ -287,9 +294,9 @@ public function __set($key, $value)
287
294
* @source https://clickhouse.tech/docs/ru/sql-reference/statements/optimize/
288
295
* @param bool $final
289
296
* @param string|null $partition
290
- * @return \ClickHouseDB\ Statement
297
+ * @return Statement
291
298
*/
292
- public static function optimize ($ final = false , $ partition = null )
299
+ public static function optimize (bool $ final = false , ? string $ partition = null ): Statement
293
300
{
294
301
$ sql = "OPTIMIZE TABLE " . (new static )->getTableSources ();
295
302
if ($ partition ) {
@@ -298,6 +305,7 @@ public static function optimize($final = false, $partition = null)
298
305
if ($ final ) {
299
306
$ sql .= " FINAL " ;
300
307
}
308
+
301
309
return static ::getClient ()->write ($ sql );
302
310
}
303
311
@@ -320,20 +328,21 @@ public static function where($column, $operator = null, $value = null, string $c
320
328
} else {
321
329
$ builder ->where ($ column , $ operator , $ value , $ concatOperator );
322
330
}
331
+
323
332
return $ builder ;
324
333
}
325
334
326
335
/**
327
336
* @param string $expression
328
337
* @return Builder
329
338
*/
330
- public static function whereRaw (string $ expression )
339
+ public static function whereRaw (string $ expression ): Builder
331
340
{
332
341
$ static = new static ;
342
+
333
343
return (new Builder )->select (['* ' ])
334
344
->from ($ static ->getTable ())
335
345
->setSourcesTable ($ static ->getTableSources ())
336
346
->whereRaw ($ expression );
337
347
}
338
-
339
348
}
0 commit comments