Skip to content

Commit

Permalink
add new method to check if table exists
Browse files Browse the repository at this point in the history
  • Loading branch information
BMTmohammedtaha committed Sep 27, 2023
1 parent fbf31e0 commit 21609ec
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/Build/InfoQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ public function __construct(protected array $attributes, protected Syntax $synta
*/
public function dbName(): string
{
return (string) match ($this->syntax->getDriver()) {
return (string) match ($this->syntax->getDriver()) {
Driver::MySQL => $this->syntax->getCommand('select', 1) . $this->syntax->getCommand('database') . "()",
Driver::PostgreSQL => $this->syntax->getCommand('select', 1) . 'current_database()',
Driver::SQLite => $this->syntax->getCommand('pragma', 1) . 'database_list',
};


}

/**
Expand All @@ -55,7 +53,7 @@ public function listDatabase(): string
Driver::SQLite => '',
};

if(empty($query)){
if (empty($query)) {
throw new \Exception("Error Processing Query, driver '{$this->syntax->getDriver()}' doesn't has statement for list database tables");
}
return $query;
Expand All @@ -75,7 +73,7 @@ public function listTables(): string
};
}

/**
/**
* Get the SQL query to list columns of a table.
*
* @return string The SQL query to list columns.
Expand All @@ -94,7 +92,27 @@ public function listCols(): string
};
}

/**
public function tableExists(): string
{
return (string) match ($this->syntax->getDriver()) {
Driver::MySQL => sprintf(
"%s %s %s '%s'",
$this->syntax->getCommand('show', 1),
$this->syntax->getCommand('tables', 1),
$this->syntax->getCommand('like', 1),
$this->getAttribute('table_name')
),
Driver::PostgreSQL => sprintf(
'%s %s (%s)',
$this->syntax->getCommand('select', 1),
$this->syntax->getCommand('exists', 1),
(new Select('information_schema.tables'))->columns([1])->where(['table_name' => $this->getAttribute('table_name')]),
),
Driver::SQLite => (new Select('sqlite_master'))->columns(['name'])->where(['type' => 'table', 'name' => $this->getAttribute('table_name')])
};
}

/**
* Get the SQL query to retrieve the schema of a table.
*
* @return string The SQL query to retrieve table schema.
Expand Down Expand Up @@ -141,6 +159,7 @@ public function build(): string
'list_cols' => $this->listCols(),
'table_schema' => $this->tableSchema(),
'table_indexes' => $this->tableIndexes(),
'table_exists' => $this->tableExists()
};
}

Expand Down
7 changes: 7 additions & 0 deletions src/Operations/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ public function listColumns(string $table_name): self
return $this;
}

public function tableExists(string $table_name): self
{
$this->setAttribute('table_name', $table_name);
$this->setAttribute('info', 'table_exists');
return $this;
}

/**
* Specify that the INFO query should retrieve the schema of a table.
*
Expand Down

0 comments on commit 21609ec

Please sign in to comment.