11<?php
22
3+ declare (strict_types=1 );
34
45namespace PhpClickHouseLaravel ;
56
6-
77use ClickHouseDB \Client ;
8+ use ClickHouseDB \Statement ;
9+ use Exception ;
810use Illuminate \Database \Eloquent \Concerns \HasAttributes ;
911use Illuminate \Support \Facades \DB ;
1012use Illuminate \Support \Str ;
1315
1416class BaseModel
1517{
16-
1718 use HasAttributes;
1819
1920 /**
@@ -50,7 +51,7 @@ class BaseModel
5051 *
5152 * @return string
5253 */
53- public function getTable ()
54+ public function getTable (): string
5455 {
5556 return $ this ->table ?? Str::snake (Str::pluralStudly (class_basename ($ this )));
5657 }
@@ -60,7 +61,7 @@ public function getTable()
6061 *
6162 * @return string
6263 */
63- public function getTableForInserts ()
64+ public function getTableForInserts (): string
6465 {
6566 return $ this ->tableForInserts ?? $ this ->getTable ();
6667 }
@@ -69,15 +70,15 @@ public function getTableForInserts()
6970 * Use this field for OPTIMIZE TABLE OR ALTER TABLE (also DELETE) queries
7071 * @return string
7172 */
72- public function getTableSources ()
73+ public function getTableSources (): string
7374 {
7475 return $ this ->tableSources ?? $ this ->getTable ();
7576 }
7677
7778 /**
7879 * @return Client
7980 */
80- public static function getClient ()
81+ public static function getClient (): Client
8182 {
8283 return DB ::connection ('clickhouse ' )->getClient ();
8384 }
@@ -87,23 +88,25 @@ public static function getClient()
8788 * @param array $attributes
8889 * @return static
8990 */
90- public static function make ($ attributes = [])
91+ public static function make (array $ attributes = [])
9192 {
9293 $ model = new static ;
9394 $ model ->fill ($ attributes );
95+
9496 return $ model ;
9597 }
9698
9799 /**
98100 * Save a new model and return the instance.
99101 * @param array $attributes
100102 * @return static
101- * @throws \ Exception
103+ * @throws Exception
102104 */
103- public static function create ($ attributes = [])
105+ public static function create (array $ attributes = [])
104106 {
105107 $ model = static ::make ($ attributes );
106108 $ model ->save ();
109+
107110 return $ model ;
108111 }
109112
@@ -112,36 +115,38 @@ public static function create($attributes = [])
112115 * @param array $attributes
113116 * @return $this
114117 */
115- public function fill (array $ attributes )
118+ public function fill (array $ attributes ): self
116119 {
117120 foreach ($ attributes as $ key => $ value ) {
118121 $ this ->setAttribute ($ key , $ value );
119122 }
123+
120124 return $ this ;
121125 }
122126
123127 /**
124128 * Save the model to the database.
125129 * @param array $options
126130 * @return bool
127- * @throws \ Exception
131+ * @throws Exception
128132 */
129- public function save (array $ options = [])
133+ public function save (array $ options = []): bool
130134 {
131135 if ($ this ->exists ) {
132- throw new \ Exception ("Clickhouse does not allow update rows " );
136+ throw new Exception ("Clickhouse does not allow update rows " );
133137 }
134138 $ this ->exists = !static ::insertAssoc ([$ this ->getAttributes ()])->isError ();
139+
135140 return $ this ->exists ;
136141 }
137142
138143 /**
139144 * Bulk insert into Clickhouse database
140145 * @param array[] $rows
141- * @return \ClickHouseDB\ Statement
146+ * @return Statement
142147 * @deprecated use insertBulk
143148 */
144- public static function insert ($ rows )
149+ public static function insert (array $ rows ): Statement
145150 {
146151 return static ::getClient ()->insert ((new static )->getTableForInserts (), $ rows );
147152 }
@@ -150,10 +155,10 @@ public static function insert($rows)
150155 * Bulk insert into Clickhouse database
151156 * @param array[] $rows
152157 * @param array $columns
153- * @return \ClickHouseDB\ Statement
158+ * @return Statement
154159 * @example MyModel::insertBulk([['model 1', 1], ['model 2', 2]], ['model_name', 'some_param']);
155160 */
156- public static function insertBulk ($ rows , $ columns = [])
161+ public static function insertBulk (array $ rows , array $ columns = []): Statement
157162 {
158163 return static ::getClient ()->insert ((new static )->getTableForInserts (), $ rows , $ columns );
159164 }
@@ -162,21 +167,23 @@ public static function insertBulk($rows, $columns = [])
162167 * Prepare each row by calling static::prepareFromRequest to bulk insert into database
163168 * @param array[] $rows
164169 * @param array $columns
165- * @return \ClickHouseDB\ Statement
170+ * @return Statement
166171 */
167- public static function prepareAndInsert ($ rows , $ columns = [])
172+ public static function prepareAndInsert (array $ rows , array $ columns = []): Statement
168173 {
169174 $ 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 );
171178 }
172179
173180 /**
174181 * Bulk insert rows as associative array into Clickhouse database
175182 * @param array[] $rows
176- * @return \ClickHouseDB\ Statement
183+ * @return Statement
177184 * @example MyModel::insertAssoc([['model_name' => 'model 1', 'some_param' => 1], ['model_name' => 'model 2', 'some_param' => 2]]);
178185 */
179- public static function insertAssoc ($ rows )
186+ public static function insertAssoc (array $ rows ): Statement
180187 {
181188 $ rows = array_values ($ rows );
182189 if (isset ($ rows [0 ]) && isset ($ rows [1 ])) {
@@ -191,9 +198,9 @@ public static function insertAssoc($rows)
191198 /**
192199 * Prepare each row by calling static::prepareAssocFromRequest to bulk insert into database
193200 * @param array[] $rows
194- * @return \ClickHouseDB\ Statement
201+ * @return Statement
195202 */
196- public static function prepareAndInsertAssoc ($ rows )
203+ public static function prepareAndInsertAssoc (array $ rows ): Statement
197204 {
198205 $ rows = array_map ('static::prepareAssocFromRequest ' , $ rows );
199206 return static ::insertAssoc ($ rows );
@@ -206,7 +213,7 @@ public static function prepareAndInsertAssoc($rows)
206213 * @param array $columns
207214 * @return array
208215 */
209- public static function prepareFromRequest ($ row , $ columns = [])
216+ public static function prepareFromRequest (array $ row , array $ columns = []): array
210217 {
211218 return $ row ;
212219 }
@@ -217,7 +224,7 @@ public static function prepareFromRequest($row, $columns = [])
217224 * @param array $row
218225 * @return array
219226 */
220- public static function prepareAssocFromRequest ($ row )
227+ public static function prepareAssocFromRequest (array $ row ): array
221228 {
222229 return $ row ;
223230 }
@@ -226,7 +233,7 @@ public static function prepareAssocFromRequest($row)
226233 * @param array $select optional = ['*']
227234 * @return Builder
228235 */
229- public static function select ($ select = ['* ' ])
236+ public static function select (array $ select = ['* ' ]): Builder
230237 {
231238 return (new Builder )->select ($ select )->from ((new static )->getTable ());
232239 }
@@ -235,7 +242,7 @@ public static function select($select = ['*'])
235242 * Necessary stub for HasAttributes trait
236243 * @return array
237244 */
238- public function getCasts ()
245+ public function getCasts (): array
239246 {
240247 return $ this ->casts ;
241248 }
@@ -244,7 +251,7 @@ public function getCasts()
244251 * Necessary stub for HasAttributes trait
245252 * @return bool
246253 */
247- public function usesTimestamps ()
254+ public function usesTimestamps (): bool
248255 {
249256 return false ;
250257 }
@@ -265,7 +272,7 @@ public function getRelationValue($key)
265272 * @param string $key
266273 * @return mixed
267274 */
268- public function __get ($ key )
275+ public function __get (string $ key )
269276 {
270277 return $ this ->getAttribute ($ key );
271278 }
@@ -277,7 +284,7 @@ public function __get($key)
277284 * @param mixed $value
278285 * @return void
279286 */
280- public function __set ($ key , $ value )
287+ public function __set (string $ key , $ value ): void
281288 {
282289 $ this ->setAttribute ($ key , $ value );
283290 }
@@ -287,9 +294,9 @@ public function __set($key, $value)
287294 * @source https://clickhouse.tech/docs/ru/sql-reference/statements/optimize/
288295 * @param bool $final
289296 * @param string|null $partition
290- * @return \ClickHouseDB\ Statement
297+ * @return Statement
291298 */
292- public static function optimize ($ final = false , $ partition = null )
299+ public static function optimize (bool $ final = false , ? string $ partition = null ): Statement
293300 {
294301 $ sql = "OPTIMIZE TABLE " . (new static )->getTableSources ();
295302 if ($ partition ) {
@@ -298,6 +305,7 @@ public static function optimize($final = false, $partition = null)
298305 if ($ final ) {
299306 $ sql .= " FINAL " ;
300307 }
308+
301309 return static ::getClient ()->write ($ sql );
302310 }
303311
@@ -320,20 +328,21 @@ public static function where($column, $operator = null, $value = null, string $c
320328 } else {
321329 $ builder ->where ($ column , $ operator , $ value , $ concatOperator );
322330 }
331+
323332 return $ builder ;
324333 }
325334
326335 /**
327336 * @param string $expression
328337 * @return Builder
329338 */
330- public static function whereRaw (string $ expression )
339+ public static function whereRaw (string $ expression ): Builder
331340 {
332341 $ static = new static ;
342+
333343 return (new Builder )->select (['* ' ])
334344 ->from ($ static ->getTable ())
335345 ->setSourcesTable ($ static ->getTableSources ())
336346 ->whereRaw ($ expression );
337347 }
338-
339348}
0 commit comments