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

Operations on tables: CREATE / ALTER TABLE or queries different to CRUD (#30) #169

Open
wants to merge 2 commits into
base: 2.x
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
4 changes: 2 additions & 2 deletions src/AdvancedStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ protected function renderOrderBy(): string
/**
* @param LimitInterface $limit
*
* @return self
* @return static
*/
public function limit(LimitInterface $limit): self
public function limit(LimitInterface $limit)
{
$this->limit = $limit;

Expand Down
2 changes: 1 addition & 1 deletion src/AdvancedStatementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function orderBy(string $column, string $direction = '');
/**
* @param LimitInterface $limit
*
* @return self
* @return static
*/
public function limit(LimitInterface $limit);
}
53 changes: 53 additions & 0 deletions src/Definition/DropUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace FaaPz\PDO\Definition;

use FaaPz\PDO\AbstractStatement;
use PDO;

class DropUser extends AbstractStatement
{

/**
* User information
*/
protected string $user;
protected string $host;

/**
* Clauses
*/
protected bool $ifExists = false;

public function __construct(PDO $pdo, string $host, string $user)
{
parent::__construct($pdo);
$this->user = $user;
$this->host = $host;
}

public function ifExists()
{
$this->ifExists = true;
return $this;
}

public function getValues(): array
{
return [];
}

public function __toString(): string
{
$sql = 'DROP USER';

if ($this->ifExists) {
$sql = "{$sql} IF EXISTS {$this->user}@{$this->host}";
} else {
$sql = "{$sql} {$this->user}@{$this->host}";
}

return $sql;
}
}

8 changes: 8 additions & 0 deletions src/Definition/IndexInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace FaaPz\PDO\Definition;

interface IndexInterface
{

}
8 changes: 8 additions & 0 deletions src/Definition/SchemaInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace FaaPz\PDO\Definition;

interface SchemaInterface
{

}
8 changes: 8 additions & 0 deletions src/Definition/TableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace FaaPz\PDO\Definition;

interface TableInterface
{

}
90 changes: 90 additions & 0 deletions src/Definition/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace FaaPz\PDO\Definition;

use FaaPz\PDO\AbstractStatement;
use PDO;

class User extends AbstractStatement
{
/** @var string $host */
protected string $host;

/** @var string $username */
protected string $username;

/** @var ?string $password */
protected ?string $password;

/**
* Clauses
*/
protected bool $orReplace = false;
protected bool $ifNotExists = false;

/**
*
*/
public function __construct(PDO $dbh, string $host, string $user, ?string $password = '')
{
parent::__construct($dbh);
$this->host = $host;
$this->user = $user;
$this->password = $password;
}

public function getValues(): array
{
return [];
}

/**
*
*/
public function orReplace()
{
$this->orReplace = true;
return $this;
}

/**
*
*/
public function ifNotExists()
{
$this->ifNotExists = true;
return $this;
}

public function __toString(): string
{

/**
* Exception: CREATE 'OR REPLACE' USER 'IF NOT EXISTS' ...
*/
if ($this->orReplace && $this->ifNotExists)
{
throw new \Exception('The clause "OR REPLACE" and "IF NOT EXISTS" cannot be used together');
}


$sql = 'CREATE';
if ($this->orReplace) {
$sql = "{$sql} OR REPLACE USER {$this->user}@{$this->host}";
}

if ($this->ifNotExists) {
$sql = "{$sql} USER IF NOT EXISTS {$this->user}@{$this->host}";
}

if (!$this->ifNotExists && !$this->orReplace) {
$sql = "{$sql} USER {$this->user}@{$this->host}";
}

if ($this->password) {
$sql = "{$sql} IDENTIFIED BY '{$this->password}'";
}

return $sql;
}
}
8 changes: 8 additions & 0 deletions src/Definition/UserInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace FaaPz\PDO\Definition;

interface UserInterface
{

}
8 changes: 8 additions & 0 deletions src/Definition/ViewInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace FaaPz\PDO\Definition;

interface ViewInterface
{

}
49 changes: 49 additions & 0 deletions src/Statement/AlterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* @license MIT
* @license http://opensource.org/licenses/MIT
*/

namespace FaaPz\PDO\Statement;

use FaaPz\PDO\Clause\MethodInterface;
use FaaPz\PDO\StatementInterface;

interface AlterInterface extends StatementInterface
{
/**
* @param string $name
*
* @return SchemaInterface
*/
public function schema(string $name);

/**
* @param string $name
*
* @return IndexInterface
*/
public function index(string $name);

/**
* @param string $name
*
* @return TableInterface
*/
public function table(string $name);

/**
* @param string $name
*
* @return UserInterface
*/
public function user(string $name);

/**
* @param string $name
*
* @return ViewInterface
*/
public function view(string $name);
}
4 changes: 2 additions & 2 deletions src/Statement/Call.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function __construct(Database $dbh, ?MethodInterface $procedure = null)
/**
* @param MethodInterface $procedure
*
* @return self
* @return static
*/
public function method(MethodInterface $procedure): self
public function method(MethodInterface $procedure)
{
$this->method = $procedure;

Expand Down
2 changes: 1 addition & 1 deletion src/Statement/CallInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface CallInterface extends StatementInterface
/**
* @param MethodInterface $procedure
*
* @return self
* @return static
*/
public function method(MethodInterface $procedure);
}
54 changes: 54 additions & 0 deletions src/Statement/CreateInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* @license MIT
* @license http://opensource.org/licenses/MIT
*/

namespace FaaPz\PDO\Statement;

use FaaPz\PDO\Clause\MethodInterface;
use FaaPz\PDO\Definition\IndexInterface;
use FaaPz\PDO\Definition\SchemaInterface;
use FaaPz\PDO\Definition\TableInterface;
use FaaPz\PDO\Definition\UserInterface;
use FaaPz\PDO\Definition\ViewInterface;
use FaaPz\PDO\StatementInterface;

interface CreateInterface extends StatementInterface
{
/**
* @param string $name
*
* @return SchemaInterface
*/
public function schema(string $name);

/**
* @param string $name
*
* @return IndexInterface
*/
public function index(string $name);

/**
* @param string $name
*
* @return TableInterface
*/
public function table(string $name);

/**
* @param string $name
*
* @return UserInterface
*/
public function user(string $name);

/**
* @param string $name
*
* @return ViewInterface
*/
public function view(string $name);
}
4 changes: 2 additions & 2 deletions src/Statement/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public function __construct(Database $dbh, $table = null)
/**
* @param string|array<string, string> $table
*
* @return self
* @return static
*/
public function from($table): self
public function from($table)
{
$this->table = $table;

Expand Down
2 changes: 1 addition & 1 deletion src/Statement/DeleteInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface DeleteInterface extends StatementInterface
/**
* @param string|array<string, string> $table
*
* @return self
* @return static
*/
public function from($table);
}
Loading