@@ -53,22 +53,27 @@ public function tableExists(string $table)
5353 switch ($ this ->config ['dbtype ' ]) {
5454 case 'sqlite ' :
5555 $ schema = $ this ->select ('sqlite_master ' )->where (['type ' => 'table ' , 'name ' => $ table ])->all ();
56+
5657 break ;
5758
5859 case 'mysql ' :
5960 $ schema = $ this ->select ('INFORMATION_SCHEMA.TABLES ' )->where (['TABLE_SCHEMA ' => $ this ->config ['dbname ' ], 'TABLE_NAME ' => $ table ])->all ();
61+
6062 break ;
6163
6264 case 'pgsql ' :
6365 $ schema = $ this ->select ('pg_catalog.pg_tables ' )->where (['schemaname ' => 'public ' , 'tablename ' => $ table ])->all ();
66+
6467 break ;
6568
6669 case 'sqlsrv ' :
6770 $ schema = $ this ->select ('information_schema.tables ' )->where (['table_schema ' => 'dbo ' , 'table_name ' => $ table ])->all ();
71+
6872 break ;
6973
7074 default :
7175 $ schema = $ this ->select ('INFORMATION_SCHEMA.SCHEMATA ' )->where ('SCHEMA_NAME ' , $ table )->all ();
76+
7277 break ;
7378 }
7479
@@ -132,7 +137,7 @@ public function dropTable(string $table)
132137 public function backup (string $ dbName , string $ destination , bool $ withDifferential = false )
133138 {
134139 return $ this ->query ("BACKUP DATABASE $ dbName TO DISK = ' $ destination' " .
135- $ withDifferential ? 'WITH DIFFERENTIAL ' : '' )->execute ();
140+ ( $ withDifferential ? 'WITH DIFFERENTIAL ' : '' ) )->execute ();
136141 }
137142
138143 /**
@@ -355,6 +360,8 @@ public function orWhere($condition, $comparator = null, $value = null): self
355360 return $ this ;
356361 }
357362
363+ /**
364+ * Add a JSON where clause to the query
358365 *
359366 * @param string $column The JSON column
360367 * @param string $jsonKey The key within the JSON structure
@@ -363,9 +370,8 @@ public function orWhere($condition, $comparator = null, $value = null): self
363370 */
364371 public function whereJson (string $ column , string $ jsonKey , $ value , string $ comparator = '= ' ): self
365372 {
366- // Check if the value is an integer, and cast the JSON value to an integer
373+ // cast the extracted value to a number when comparing against one
367374 $ jsonExpression = is_int ($ value )
368- # just incase: ? "CAST(JSON_EXTRACT($column, '$.$jsonKey') AS UNSIGNED)";
369375 ? "JSON_EXTRACT( $ column, '$. $ jsonKey') + 0 "
370376 : "JSON_EXTRACT( $ column, '$. $ jsonKey') " ;
371377
@@ -442,95 +448,9 @@ public function orWhereJsonContains(string $column, $value, ?string $jsonKey = n
442448 /**
443449 * Fetch current query with all related data
444450 *
445- * @param string $column The JSON column
446- * @param string $jsonKey The key within the JSON structure
447- * @param mixed $value The value to compare against
448- * @param string $comparator The comparison operator (default '=')
449- */
450- public function whereJson (string $ column , string $ jsonKey , $ value , string $ comparator = '= ' ): self
451- {
452- // Check if the value is an integer, and cast the JSON value to an integer
453- $ jsonExpression = is_int ($ value )
454- ? "JSON_EXTRACT( $ column, '$. $ jsonKey') + 0 " # just incase: ? "CAST(JSON_EXTRACT($column, '$.$jsonKey') AS UNSIGNED)";
455- : "JSON_EXTRACT( $ column, '$. $ jsonKey') " ;
456-
457- $ this ->query = Builder::where ($ this ->query , $ jsonExpression , $ value , $ comparator );
458- $ this ->bind (...(Builder::$ bindings ));
459-
460- return $ this ;
461- }
462-
463- /**
464- * Add a JSON where clause with OR comparator to the query
465- *
466- * @param string $column The JSON column
467- * @param string $jsonKey The key within the JSON structure
468- * @param mixed $value The value to compare against
469- * @param string $comparator The comparison operator (default '=')
470- */
471- public function orWhereJson (string $ column , string $ jsonKey , $ value , string $ comparator = '= ' ): self
472- {
473- $ jsonExpression = is_int ($ value )
474- ? "JSON_EXTRACT( $ column, '$. $ jsonKey') + 0 "
475- : "JSON_EXTRACT( $ column, '$. $ jsonKey') " ;
476-
477- $ this ->query = Builder::where ($ this ->query , $ jsonExpression , $ value , $ comparator , 'OR ' );
478- $ this ->bind (...(Builder::$ bindings ));
479-
480- return $ this ;
481- }
482-
483- /**
484- * Add a JSON contains clause to the query
485- *
486- * @param string $column The JSON column
487- * @param mixed $value The value to check for
488- * @param string|null $jsonKey The key within the JSON structure (optional)
489- */
490- public function whereJsonContains (string $ column , $ value , ?string $ jsonKey = null ): self
491- {
492- $ jsonValue = json_encode ($ value , JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
493-
494- $ jsonExpression = $ jsonKey
495- ? "JSON_CONTAINS( $ column, ?, '$. $ jsonKey') "
496- : "JSON_CONTAINS( $ column, ?) " ;
497-
498-
499- Builder::$ bindings [] = $ jsonValue ;
500- $ this ->query = Builder::where ($ this ->query , $ jsonExpression , 1 , '= ' );
501- $ this ->bind (...(Builder::$ bindings ));
502-
503- return $ this ;
504- }
505-
506- /**
507- * Add a JSON contains clause with OR comparator to the query
508- *
509- * @param string $column The JSON column
510- * @param mixed $value The value to check for
511- * @param string|null $jsonKey The key within the JSON structure (optional)
512- */
513- public function orWhereJsonContains (string $ column , $ value , ?string $ jsonKey = null ): self
514- {
515- $ jsonValue = json_encode ($ value , JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
516-
517- $ jsonExpression = $ jsonKey
518- ? "JSON_CONTAINS( $ column, ?, '$. $ jsonKey') "
519- : "JSON_CONTAINS( $ column, ?) " ;
520-
521- Builder::$ bindings [] = $ jsonValue ;
522- $ this ->query = Builder::where ($ this ->query , $ jsonExpression , 1 , '= ' , 'OR ' );
523- $ this ->bind (...(Builder::$ bindings ));
524-
525- return $ this ;
526- }
527-
528- /**
529- * Fetch current query with all related data
530- *
531451 * @param string $table The table to join
532452 * @param string $foreignKey The foreign key to use
533- *
453+ *
534454 * @return self
535455 */
536456 public function with (string $ table , ?string $ foreignKey = null )
@@ -539,7 +459,7 @@ public function with(string $table, ?string $foreignKey = null)
539459
540460 $ this ->eager [] = [
541461 'table ' => $ table ,
542- 'foreignKey ' => $ foreignKey
462+ 'foreignKey ' => $ foreignKey,
543463 ];
544464
545465 return $ this ;
@@ -605,8 +525,9 @@ public function search(string $row, string $value, ?array $hidden = []): ?array
605525 */
606526 public function beginTransaction (): self
607527 {
608- $ this ->query ('START TRANSACTION ' );
609- $ this ->execute ();
528+ // PDO's own transaction handling works across mysql, sqlite,
529+ // postgres and sqlsrv — raw START TRANSACTION is mysql-only
530+ $ this ->connection ($ this ->currentConnection )->beginTransaction ();
610531
611532 return $ this ;
612533 }
@@ -618,8 +539,7 @@ public function beginTransaction(): self
618539 */
619540 public function commit (): self
620541 {
621- $ this ->query ('COMMIT ' );
622- $ this ->execute ();
542+ $ this ->connection ($ this ->currentConnection )->commit ();
623543
624544 return $ this ;
625545 }
@@ -631,8 +551,7 @@ public function commit(): self
631551 */
632552 public function rollback (): self
633553 {
634- $ this ->query ('ROLLBACK ' );
635- $ this ->execute ();
554+ $ this ->connection ($ this ->currentConnection )->rollBack ();
636555
637556 return $ this ;
638557 }
@@ -654,7 +573,7 @@ public function transaction($callback): bool
654573 return true ;
655574 } catch (\Exception $ e ) {
656575 $ this ->rollback ();
657- $ this ->errorsArray = $ e ;
576+ $ this ->errors [ ' transaction ' ] = $ e-> getMessage () ;
658577
659578 return false ;
660579 }
0 commit comments