@@ -17,6 +17,7 @@ class Query {
1717 private int |null $ offset = null ;
1818 private array |null $ orderBy = null ;
1919 private bool $ temporaryQuery = false ;
20+ private bool $ withDeleted = false ;
2021 private ModelInformation $ modelInformation ;
2122
2223 /**
@@ -49,7 +50,12 @@ public function where(string $field, mixed $var2, mixed $var3 = null): Query {
4950 $ operator = '= ' ;
5051 } else
5152 $ value = $ var3 ;
52- return $ this ->whereRaw ('` ' .$ this ->modelInformation ->getFieldName ($ field ).'` ' , $ operator , '? ' , [$ value ]);
53+ return $ this ->whereRaw ('` ' . $ this ->modelInformation ->getFieldName ($ field ) . '` ' , $ operator , '? ' , [$ value ]);
54+ }
55+
56+ public function whereId ($ id ): Query {
57+ $ this ->whereRaw ("` " . $ this ->modelInformation ->getIdentifier () . "` " , "= " , "? " , [$ id ]);
58+ return $ this ;
5359 }
5460
5561 public function whereDay (string $ field , mixed $ val ): Query {
@@ -96,17 +102,17 @@ public function whereColumns(string $field1Table, string $field1Name, string $op
96102 $ this ->queries [] = [
97103 'type ' => 'AND ' ,
98104 'query ' =>
99- UloleORM::getTableName ($ field1Table ) . '.` ' . UloleORM::getModelInformation ($ field1Table )->getFieldName ($ field1Name ) . '` '
105+ UloleORM::getTableName ($ field1Table ) . '.` ' . UloleORM::getModelInformation ($ field1Table )->getFieldName ($ field1Name ) . '` '
100106 . $ operator . ' '
101- . UloleORM::getTableName ($ field2Table ) . '.` ' . UloleORM::getModelInformation ($ field2Table )->getFieldName ($ field2Name ). '` ' ];
107+ . UloleORM::getTableName ($ field2Table ) . '.` ' . UloleORM::getModelInformation ($ field2Table )->getFieldName ($ field2Name ) . '` ' ];
102108 return $ this ;
103109 }
104110
105111 /**
106112 * @return $this
107113 */
108114 public function orWhere (string $ var1 , mixed $ var2 , mixed $ var3 = null ): Query {
109- return $ this ->or (fn ($ q ) => $ q ->where ($ var1 , $ var2 , $ var3 ));
115+ return $ this ->or (fn ($ q ) => $ q ->where ($ var1 , $ var2 , $ var3 ));
110116 }
111117
112118 /**
@@ -190,7 +196,7 @@ public function whereExists(string $table, callable $callable): Query {
190196 * @throws Null
191197 */
192198 public function in (string $ field , callable |array $ var , string |null $ table = null ): Query {
193- $ query = ['type ' => 'WHERE_IN ' , "column " => '` ' . $ this ->modelInformation ->getFieldName ($ field ). '` ' , "not " => false ];
199+ $ query = ['type ' => 'WHERE_IN ' , "column " => '` ' . $ this ->modelInformation ->getFieldName ($ field ) . '` ' , "not " => false ];
194200 if (is_array ($ var )) {
195201 $ query ["query " ] = implode (", " , array_map (fn () => "? " , $ var ));
196202 $ query ["vars " ] = $ var ;
@@ -206,6 +212,7 @@ public function in(string $field, callable|array $var, string|null $table = null
206212
207213 return $ this ;
208214 }
215+
209216 public function notIn (string $ field , callable |array $ var , string |null $ table = null ): Query {
210217 $ this ->in ($ field , $ var , $ table );
211218 $ this ->queries [count ($ this ->queries ) - 1 ]["not " ] = true ;
@@ -240,14 +247,15 @@ public function first(): mixed {
240247 if ($ this ->limit === null )
241248 $ this ->limit (1 );
242249
243- return $ this ->get ()[0 ] ?? null ;
250+ return $ this ->all ()[0 ] ?? null ;
244251 }
245252
246253 /**
247254 * @return T[]
248255 */
249256 public function all (): array {
250257 $ vars = [];
258+
251259 $ query = $ this ->buildQuery ();
252260 $ vars = array_merge ($ vars , $ query ->vars );
253261
@@ -283,7 +291,7 @@ private function selectNumber(string $f): int|float {
283291 $ vars = [];
284292 $ query = $ this ->buildQuery ();
285293 $ vars = array_merge ($ vars , $ query ->vars );
286- $ statement = $ this ->run ('SELECT ' . $ f . ' as num FROM ' . UloleORM::getTableName ($ this ->model ) . $ query ->query . '; ' , $ vars );
294+ $ statement = $ this ->run ('SELECT ' . $ f . ' as num FROM ' . UloleORM::getTableName ($ this ->model ) . $ query ->query . '; ' , $ vars );
287295 $ statement ->setFetchMode (PDO ::FETCH_NUM );
288296 $ result = $ statement ->fetch ();
289297 if ($ result === false )
@@ -297,39 +305,49 @@ public function count(): int {
297305 }
298306
299307 public function sum (string $ field ): float |int {
300- return $ this ->selectNumber ("SUM(` " . $ this ->modelInformation ->getFieldName ($ field ). "`) " );
308+ return $ this ->selectNumber ("SUM(` " . $ this ->modelInformation ->getFieldName ($ field ) . "`) " );
301309 }
302310
303311 public function sub (string $ field ): float |int {
304- return $ this ->selectNumber ("SUM(-` " . $ this ->modelInformation ->getFieldName ($ field ). "`) " );
312+ return $ this ->selectNumber ("SUM(-` " . $ this ->modelInformation ->getFieldName ($ field ) . "`) " );
305313 }
306314
307315 public function avg (string $ field ): float |int {
308- return $ this ->selectNumber ("AVG(` " . $ this ->modelInformation ->getFieldName ($ field ). "`) " );
316+ return $ this ->selectNumber ("AVG(` " . $ this ->modelInformation ->getFieldName ($ field ) . "`) " );
309317 }
310318
311319 public function min (string $ field ): float |int {
312- return $ this ->selectNumber ("MIN(` " . $ this ->modelInformation ->getFieldName ($ field ). "`) " );
320+ return $ this ->selectNumber ("MIN(` " . $ this ->modelInformation ->getFieldName ($ field ) . "`) " );
313321 }
314322
315323 public function max (string $ field ): float |int {
316- return $ this ->selectNumber ("MAX(` " . $ this ->modelInformation ->getFieldName ($ field ). "`) " );
324+ return $ this ->selectNumber ("MAX(` " . $ this ->modelInformation ->getFieldName ($ field ) . "`) " );
317325 }
318326
319327 public function update (): bool {
320328 $ vars = [];
329+
330+ $ updatedAt = $ this ->modelInformation ->getUpdatedAt ();
331+ if ($ updatedAt !== null ) {
332+ $ this ->set ($ updatedAt , date ("Y-m-d H:i:s " ));
333+ }
334+
321335 $ query = $ this ->buildQuery ();
322336 $ vars = array_merge ($ vars , $ query ->vars );
323337
324- return $ this ->run ('UPDATE ` ' . UloleORM::getTableName ($ this ->model ) . '` ' . $ query ->query . '; ' , $ vars , true );
338+ return $ this ->run ('UPDATE ` ' . UloleORM::getTableName ($ this ->model ) . '` ' . $ query ->query . '; ' , $ vars , true ) !== false ;
325339 }
326340
327341 public function delete (): bool {
328342 $ vars = [];
329343 $ query = $ this ->buildQuery ();
330344 $ vars = array_merge ($ vars , $ query ->vars );
331345
332- return $ this ->run ('DELETE FROM ` ' . UloleORM::getTableName ($ this ->model ) . '` ' . $ query ->query . '; ' , $ vars , true );
346+ $ deletedAt = $ this ->modelInformation ->getDeletedAt ();
347+ if ($ deletedAt !== null )
348+ $ this ->set ($ this ->modelInformation ->getFieldName ($ deletedAt ), date ("Y-m-d H:i:s " ))->update ();
349+
350+ return $ this ->run ('DELETE FROM ` ' . UloleORM::getTableName ($ this ->model ) . '` ' . $ query ->query . '; ' , $ vars , true ) !== false ;
333351 }
334352
335353 public function run (string $ query , array $ vars = [], bool $ returnResult = false ): PDOStatement |bool |null {
@@ -345,13 +363,17 @@ public function run(string $query, array $vars = [], bool $returnResult = false)
345363 }
346364
347365 protected function buildQuery (): object {
366+ $ deletedAt = $ this ->modelInformation ->getDeletedAt ();
367+ if ($ deletedAt !== null && !$ this ->withDeleted )
368+ $ this ->isNull ($ this ->modelInformation ->getFieldName ($ deletedAt ));
369+
348370 $ out = (object )["query " => '' , 'vars ' => []];
349371
350372 $ usedWhere = $ this ->temporaryQuery ;
351373 $ usedSet = false ;
352374 $ useCondition = false ;
353375
354- foreach (array_filter ($ this ->queries , fn ($ q ) => $ q ["type " ] == "SET " ) as $ query ) {
376+ foreach (array_filter ($ this ->queries , fn ($ q ) => $ q ["type " ] == "SET " ) as $ query ) {
355377 if (!$ usedSet ) {
356378 $ out ->query .= " SET " ;
357379 $ usedSet = true ;
@@ -390,10 +412,10 @@ protected function buildQuery(): object {
390412 $ out ->query .= $ where ->query ;
391413 $ out ->query .= ') ' ;
392414 }
393- } else if ($ query ["type " ] == 'WHERE_EXISTS ' || $ query ["type " ] == 'WHERE_NOT_EXISTS ' ) {
394- $ out ->query .= " WHERE " . ($ query ["type " ] == 'WHERE_NOT_EXISTS ' ? "NOT " : "" ) . "EXISTS ( " . $ query ["query " ]. ") " ;
395- } else if ($ query ["type " ] == 'WHERE_IN ' ) {
396- $ out ->query .= " WHERE " . $ query ["column " ] . ($ query ["not " ] ? " NOT " : ' ' ). " IN ( " . $ query ["query " ]. ") " ;
415+ } else if ($ query ["type " ] == 'WHERE_EXISTS ' || $ query ["type " ] == 'WHERE_NOT_EXISTS ' ) {
416+ $ out ->query .= " WHERE " . ($ query ["type " ] == 'WHERE_NOT_EXISTS ' ? "NOT " : "" ) . "EXISTS ( " . $ query ["query " ] . ") " ;
417+ } else if ($ query ["type " ] == 'WHERE_IN ' ) {
418+ $ out ->query .= " WHERE " . $ query ["column " ] . ($ query ["not " ] ? " NOT " : ' ' ) . " IN ( " . $ query ["query " ] . ") " ;
397419 $ out ->vars = $ query ["vars " ];
398420 }
399421 }
@@ -411,6 +433,11 @@ public function getQueries(): array {
411433 return $ this ->queries ;
412434 }
413435
436+ public function withDeleted ($ withDeleted = true ): Query {
437+ $ this ->withDeleted = $ withDeleted ;
438+ return $ this ;
439+ }
440+
414441 /**
415442 * @return $this
416443 */
0 commit comments