Skip to content

Commit 59456ac

Browse files
update
1 parent c8ab142 commit 59456ac

File tree

5 files changed

+60
-18
lines changed

5 files changed

+60
-18
lines changed

src/Connectors/Connector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ public function table(string $table)
118118
/**
119119
* Check if table exists
120120
*
121-
* @param string|null $table
121+
* @param mixed $table
122122
*
123123
* @return bool
124124
*/
125-
public function tableExists($table)
125+
public function tableExists(...$table)
126126
{
127127
return $this->table('')->tableExists($table);
128128
}

src/Schema/Builder.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,23 +1268,37 @@ public function orHavingRaw($sql, array $bindings = [])
12681268
/**
12691269
* Check if table exists
12701270
*
1271-
* @param string $table
1271+
* @param mixed $table
12721272
*
12731273
* @return bool
12741274
*/
1275-
public function tableExists($table)
1275+
public function tableExists(...$table)
12761276
{
1277+
$tables = Forge::flattenValue($table);
1278+
12771279
try{
12781280
$pdo = $this->connection->pdo;
12791281

12801282
// check if Database connection has been established
1281-
if(!$this->isPDO($pdo)){
1283+
if(!$this->isPDO($pdo) || empty($tables)){
12821284
return false;
12831285
}
12841286

1285-
$pdo->query("
1286-
select exists (select 1 from `{$table}` limit 1) as 'exists'
1287-
")->execute();
1287+
// Get the database driver (mysql, pgsql, sqlite)
1288+
$driver = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
1289+
1290+
if ($driver === 'sqlite') {
1291+
$selectQueries = array_map(fn($table) => "(SELECT 1 FROM \"{$table}\" LIMIT 1)", $tables);
1292+
$sql = "SELECT COALESCE(" . implode(", ", $selectQueries) . ", 0) AS \"exists\"";
1293+
} elseif($driver === 'mysql') {
1294+
$caseQueries = array_map(fn($table) => "WHEN EXISTS (SELECT 1 FROM `{$table}` LIMIT 1) THEN 1", $tables);
1295+
$sql = "SELECT CASE " . implode(" ", $caseQueries) . " ELSE 0 END AS `exists`";
1296+
} else{
1297+
$caseQueries = array_map(fn($table) => "WHEN EXISTS (SELECT 1 FROM \"{$table}\" LIMIT 1) THEN 1", $tables);
1298+
$sql = "SELECT CASE " . implode(" ", $caseQueries) . " ELSE 0 END AS \"exists\"";
1299+
}
1300+
1301+
$pdo->query($sql)->execute();
12881302

12891303
$this->close();
12901304

src/Schema/Traits/BuilderTrait.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,9 @@ protected function decrementEach(array $columns, array $extra = [])
10321032
*/
10331033
protected function insertBuilder(array $values, $ignore = false)
10341034
{
1035+
$createdAT = 'created_at';
1036+
$updatedAT = 'updated_at';
1037+
10351038
// Since every insert gets treated like a batch insert, we will make sure the
10361039
// bindings are structured in a way that is convenient when building these
10371040
// inserts statements by verifying these elements are actually an array.
@@ -1040,12 +1043,21 @@ protected function insertBuilder(array $values, $ignore = false)
10401043
}
10411044

10421045
// check if timestamps is available in table
1043-
// if yes then add time stamdps to columns
1046+
// if yes then add time stamps to columns
10441047
// with this helper, developers dont need to worry adding them
10451048
$stampData = $this->timeStampData();
10461049

10471050
if (! is_array(reset($values))) {
1048-
$values = [array_merge($values, $stampData)];
1051+
1052+
if(!isset($values[$createdAT]) && $this->isTimeStampsReady){
1053+
$values[$createdAT] = $stampData[$createdAT];
1054+
}
1055+
1056+
if(!isset($values[$updatedAT]) && $this->isTimeStampsReady){
1057+
$values[$updatedAT] = $stampData[$updatedAT];
1058+
}
1059+
1060+
$values = [$values];
10491061
}
10501062

10511063
// Here, we will sort the insert keys for every record so that each insert is
@@ -1055,7 +1067,15 @@ protected function insertBuilder(array $values, $ignore = false)
10551067
foreach ($values as $key => $value) {
10561068
ksort($value);
10571069

1058-
$values[$key] = array_merge($values, $stampData);
1070+
if(!isset($value[$createdAT]) && $this->isTimeStampsReady){
1071+
$value[$createdAT] = $stampData[$createdAT];
1072+
}
1073+
1074+
if(!isset($value[$updatedAT]) && $this->isTimeStampsReady){
1075+
$value[$updatedAT] = $stampData[$updatedAT];
1076+
}
1077+
1078+
$values[$key] = $value;
10591079
}
10601080
}
10611081

@@ -1276,11 +1296,11 @@ protected function isTimeStampsReady()
12761296

12771297
// get results data
12781298
$result = $stmt->fetchAll(PDO::FETCH_COLUMN);
1279-
1280-
if(is_array($result) && count($result) === 2){
1281-
return true;
1282-
}
1283-
return false;
1299+
1300+
// if columns exists in the table
1301+
$this->isTimeStampsReady = is_array($result) && count($result) === 2;
1302+
1303+
return $this->isTimeStampsReady;
12841304
} catch (\PDOException $th) {
12851305
return false;
12861306
}
@@ -1334,6 +1354,7 @@ protected function fetchAll(int $mode = PDO::FETCH_ASSOC)
13341354
*/
13351355
protected function close($table = true)
13361356
{
1357+
$this->isTimeStampsReady = false;
13371358
$this->bindings = [
13381359
'select' => [],
13391360
'from' => [],

src/Schema/Traits/MySqlProperties.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ trait MySqlProperties{
7070
*/
7171
public $distinct = false;
7272

73+
/**
74+
* Indicates if the timestamps is available in the database table.
75+
*
76+
* @var bool
77+
*/
78+
public $isTimeStampsReady = false;
79+
7380
/**
7481
* @var string
7582
*/

src/Traits/DBSetupTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ public static function table($table)
2828
/**
2929
* Check if table exists
3030
*
31-
* @param string $table
31+
* @param mixed $table
3232
*
3333
* @return bool
3434
*/
35-
public static function tableExists($table)
35+
public static function tableExists(...$table)
3636
{
3737
return (new Connector)->table('')->tableExists($table);
3838
}

0 commit comments

Comments
 (0)