-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Operations on tables: CREATE / ALTER TABLE or queries different to CR…
…UD (#30)
- Loading branch information
Showing
13 changed files
with
590 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace FaaPz\PDO\Definition; | ||
|
||
interface IndexInterface | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace FaaPz\PDO\Definition; | ||
|
||
interface SchemaInterface | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace FaaPz\PDO\Definition; | ||
|
||
interface TableInterface | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace FaaPz\PDO\Definition; | ||
|
||
interface UserInterface | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace FaaPz\PDO\Definition; | ||
|
||
interface ViewInterface | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.