Skip to content

Commit

Permalink
Operations on tables: CREATE / ALTER TABLE or queries different to CR…
Browse files Browse the repository at this point in the history
…UD (#30)
  • Loading branch information
kwhat committed Aug 11, 2022
1 parent 6313caf commit 7f3c3a7
Show file tree
Hide file tree
Showing 13 changed files with 590 additions and 0 deletions.
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);
}
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);
}
42 changes: 42 additions & 0 deletions src/UserManager/MariaDB/UserManagerMariaDB.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* For the grant and revoke commands to work correctly,
* you must start the application with super user (root),
* that is, 'sudo php index.php'.
*/
namespace FaaPz\PDO\UserManager\MariaDB;

use FaaPz\PDO\Definition\User;
use FaaPz\PDO\Definition\DropUser;
use FaaPz\PDO\UserManager\UserManagerInterface;
use PDO;

class UserManagerMariaDB implements UserManagerInterface
{
public function __construct()
{
}

public function createUser(PDO $pdo, string $host, string $user, ?string $password = ''): User
{
return new User($pdo, $host, $user, $password);
}

public function dropUser(PDO $pdo, string $host, string $user): DropUser
{
return new DropUser($pdo, $host, $user);
}


public function grantPrivileges(PDO $pdo, string $host, string $user, ?string $password = '', ?string $database = '', ?string $table = ''): GrantPrivileges
{
return new GrantPrivileges($pdo, $host, $user, $password, $database, $table);
}


public function revokePrivileges(PDO $pdo, string $host, string $user, ?string $database = '', ?string $table = ''): RevokePrivileges
{
return new RevokePrivileges($pdo, $host, $user, $database, $table);
}

}
Loading

0 comments on commit 7f3c3a7

Please sign in to comment.