Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added table checking to create and drop commands #87

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "opis/database",
"description": "A database abstraction layer over PDO, that provides a powerful and intuitive query builder, bundled with an easy to use schema builder",
"name": "adampweb/database",
"description": "Forked from opis/database and extended with check table existence.",
"keywords": ["database", "query", "abstraction layer", "mysql", "postgre", "sql", "sqlite", "query builder", "schema builder"],
"homepage": "https://opis.io/database",
"license": "Apache-2.0",
Expand All @@ -18,7 +18,7 @@
"test": "vendor/bin/phpunit"
},
"require": {
"php": "^7.0",
"php": "^8.0",
"ext-pdo": "*"
},
"require-dev": {
Expand Down
10 changes: 6 additions & 4 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,18 @@ public function getColumns(string $table, bool $clear = false, bool $names = tru
*
* @param string $table Table name
* @param callable $callback A callback that will define table's fields and indexes
* @param bool $check_exists
* @throws \Exception
*/
public function create(string $table, callable $callback)
public function create(string $table, callable $callback, bool $check_exists = false)
{
$compiler = $this->connection->schemaCompiler();

$schema = new CreateTable($table);

$callback($schema);

foreach ($compiler->create($schema) as $result) {
foreach ($compiler->create($schema, $check_exists) as $result) {
$this->connection->command($result['sql'], $result['params']);
}

Expand Down Expand Up @@ -228,13 +229,14 @@ public function renameTable(string $table, string $name)
* Deletes a table
*
* @param string $table Table name
* @param bool $check_exists
* @throws \Exception
*/
public function drop(string $table)
public function drop(string $table, bool $check_exists = false)
{
$compiler = $this->connection->schemaCompiler();

$result = $compiler->drop($table);
$result = $compiler->drop($table, $check_exists);

$this->connection->command($result['sql'], $result['params']);

Expand Down
10 changes: 6 additions & 4 deletions src/Schema/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,11 +641,12 @@ public function getColumns(string $database, string $table): array

/**
* @param CreateTable $schema
* @param bool $check_exists
* @return array
*/
public function create(CreateTable $schema): array
public function create(CreateTable $schema, bool $check_exists = false): array
{
$sql = 'CREATE TABLE ' . $this->wrap($schema->getTableName());
$sql = 'CREATE TABLE ' . ($check_exists ? 'IF NOT EXISTS' : '') . $this->wrap($schema->getTableName());
$sql .= "(\n";
$sql .= $this->handleColumns($schema->getColumns());
$sql .= $this->handlePrimaryKey($schema);
Expand Down Expand Up @@ -697,12 +698,13 @@ public function alter(AlterTable $schema): array

/**
* @param string $table
* @param bool $check_exists
* @return array
*/
public function drop(string $table): array
public function drop(string $table, bool $check_exists = false): array
{
return [
'sql' => 'DROP TABLE ' . $this->wrap($table),
'sql' => 'DROP TABLE ' . ($check_exists ? 'IF EXISTS' : '') . $this->wrap($table),
'params' => [],
];
}
Expand Down