diff --git a/src/AdvancedStatement.php b/src/AdvancedStatement.php index d0ba23f..d09c46a 100644 --- a/src/AdvancedStatement.php +++ b/src/AdvancedStatement.php @@ -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; diff --git a/src/AdvancedStatementInterface.php b/src/AdvancedStatementInterface.php index 51d3ed1..4ce726e 100644 --- a/src/AdvancedStatementInterface.php +++ b/src/AdvancedStatementInterface.php @@ -38,7 +38,7 @@ public function orderBy(string $column, string $direction = ''); /** * @param LimitInterface $limit * - * @return self + * @return static */ public function limit(LimitInterface $limit); } diff --git a/src/Definition/DropUser.php b/src/Definition/DropUser.php new file mode 100644 index 0000000..ac7f53f --- /dev/null +++ b/src/Definition/DropUser.php @@ -0,0 +1,53 @@ +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; + } +} + diff --git a/src/Definition/IndexInterface.php b/src/Definition/IndexInterface.php new file mode 100644 index 0000000..d0506b8 --- /dev/null +++ b/src/Definition/IndexInterface.php @@ -0,0 +1,8 @@ +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; + } +} \ No newline at end of file diff --git a/src/Definition/UserInterface.php b/src/Definition/UserInterface.php new file mode 100644 index 0000000..d9a3177 --- /dev/null +++ b/src/Definition/UserInterface.php @@ -0,0 +1,8 @@ +method = $procedure; diff --git a/src/Statement/CallInterface.php b/src/Statement/CallInterface.php index 0c0f642..614cffa 100644 --- a/src/Statement/CallInterface.php +++ b/src/Statement/CallInterface.php @@ -15,7 +15,7 @@ interface CallInterface extends StatementInterface /** * @param MethodInterface $procedure * - * @return self + * @return static */ public function method(MethodInterface $procedure); } diff --git a/src/Statement/CreateInterface.php b/src/Statement/CreateInterface.php new file mode 100644 index 0000000..fccf9f8 --- /dev/null +++ b/src/Statement/CreateInterface.php @@ -0,0 +1,54 @@ + $table * - * @return self + * @return static */ - public function from($table): self + public function from($table) { $this->table = $table; diff --git a/src/Statement/DeleteInterface.php b/src/Statement/DeleteInterface.php index 9cb260a..d2a646f 100644 --- a/src/Statement/DeleteInterface.php +++ b/src/Statement/DeleteInterface.php @@ -14,7 +14,7 @@ interface DeleteInterface extends StatementInterface /** * @param string|array $table * - * @return self + * @return static */ public function from($table); } diff --git a/src/Statement/Insert.php b/src/Statement/Insert.php index ab100ae..860375d 100644 --- a/src/Statement/Insert.php +++ b/src/Statement/Insert.php @@ -47,9 +47,9 @@ public function __construct(Database $dbh, array $columns = []) /** * @param string $level * - * @return self + * @return static */ - public function priority(string $level): self + public function priority(string $level) { $this->priority = strtoupper(trim($level)); @@ -84,9 +84,9 @@ protected function renderPriority(): string } /** - * @return self + * @return static */ - public function ignore(): self + public function ignore() { $this->ignore = true; @@ -110,9 +110,9 @@ protected function renderIgnore(): string /** * @param string $table * - * @return self + * @return static */ - public function into(string $table): self + public function into(string $table) { $this->table = $table; @@ -134,9 +134,9 @@ protected function renderInto(): string /** * @param string ...$columns * - * @return self + * @return static */ - public function columns(string ...$columns): self + public function columns(string ...$columns) { $this->columns = $columns; @@ -160,9 +160,9 @@ protected function renderColumns(): string * @param float|int|string|RawInterface|SelectInterface $value * @param float|int|string|RawInterface ...$values * - * @return self + * @return static */ - public function values($value, ...$values): self + public function values($value, ...$values) { array_unshift($values, $value); $this->values[] = $values; @@ -236,9 +236,9 @@ protected function renderValues(): string /** * @param array $paris * - * @return self + * @return static */ - public function onDuplicateUpdate(array $paris = []): self + public function onDuplicateUpdate(array $paris = []) { $this->update = $paris; diff --git a/src/Statement/InsertInterface.php b/src/Statement/InsertInterface.php index 63a0090..857c35e 100644 --- a/src/Statement/InsertInterface.php +++ b/src/Statement/InsertInterface.php @@ -15,26 +15,26 @@ interface InsertInterface extends StatementInterface /** * @param string $level * - * @return self + * @return static */ public function priority(string $level); /** - * @return self + * @return static */ public function ignore(); /** * @param string $table * - * @return self + * @return static */ public function into(string $table); /** * @param string ...$columns * - * @return self + * @return static */ public function columns(string ...$columns); @@ -42,14 +42,14 @@ public function columns(string ...$columns); * @param float|int|string|RawInterface|SelectInterface $value * @param float|int|string|RawInterface ...$values * - * @return self + * @return static */ public function values($value, ...$values); /** * @param array $paris * - * @return self + * @return static */ public function onDuplicateUpdate(array $paris = []); } diff --git a/src/Statement/Select.php b/src/Statement/Select.php index 106c101..4531df8 100644 --- a/src/Statement/Select.php +++ b/src/Statement/Select.php @@ -48,9 +48,9 @@ public function __construct(Database $dbh, array $columns = ['*']) } /** - * @return self + * @return static */ - public function distinct(): self + public function distinct() { $this->distinct = true; @@ -73,9 +73,9 @@ protected function renderDistinct(): string /** * @param array $columns * - * @return self + * @return static */ - public function columns(array $columns = ['*']): self + public function columns(array $columns = ['*']) { if (empty($columns)) { $this->columns = ['*']; @@ -114,9 +114,9 @@ protected function renderColumns(): string /** * @param string|array $table * - * @return self + * @return static */ - public function from($table): self + public function from($table) { $this->table = $table; @@ -152,9 +152,9 @@ protected function renderFrom(): string /** * @param SelectInterface $query * - * @return self + * @return static */ - public function union(SelectInterface $query): self + public function union(SelectInterface $query) { $this->union[$this->getUnionCount()] = $query; @@ -164,9 +164,9 @@ public function union(SelectInterface $query): self /** * @param SelectInterface $query * - * @return self + * @return static */ - public function unionAll(SelectInterface $query): self + public function unionAll(SelectInterface $query) { $this->unionAll[$this->getUnionCount()] = $query; @@ -206,9 +206,9 @@ protected function renderUnion(): string /** * @param string ...$columns * - * @return self + * @return static */ - public function groupBy(string ...$columns): self + public function groupBy(string ...$columns) { $this->groupBy = array_merge($this->groupBy, $columns); @@ -231,9 +231,9 @@ protected function renderGroupBy(): string /** * @param ConditionalInterface $clause * - * @return self + * @return static */ - public function having(ConditionalInterface $clause): self + public function having(ConditionalInterface $clause) { $this->having = $clause; diff --git a/src/Statement/SelectInterface.php b/src/Statement/SelectInterface.php index 80d66e9..b4cfe4a 100644 --- a/src/Statement/SelectInterface.php +++ b/src/Statement/SelectInterface.php @@ -13,49 +13,49 @@ interface SelectInterface extends AdvancedStatementInterface { /** - * @return self + * @return static */ public function distinct(); /** * @param array $columns * - * @return self + * @return static */ public function columns(array $columns = ['*']); /** * @param string|array $table * - * @return self + * @return static */ public function from($table); /** * @param SelectInterface $query * - * @return self + * @return static */ public function union(SelectInterface $query); /** * @param SelectInterface $query * - * @return self + * @return static */ public function unionAll(SelectInterface $query); /** * @param string ...$columns * - * @return self + * @return static */ public function groupBy(string ...$columns); /** * @param ConditionalInterface $clause * - * @return self + * @return static */ public function having(ConditionalInterface $clause); } diff --git a/src/Statement/Update.php b/src/Statement/Update.php index be95dce..47b481b 100644 --- a/src/Statement/Update.php +++ b/src/Statement/Update.php @@ -35,9 +35,9 @@ public function __construct(Database $dbh, array $pairs = []) /** * @param string $table * - * @return self + * @return static */ - public function table(string $table): self + public function table(string $table) { $this->table = $table; @@ -60,9 +60,9 @@ protected function renderTable(): string * @param string $column * @param float|int|string|RawInterface|SelectInterface $value * - * @return self + * @return static */ - public function set(string $column, $value): self + public function set(string $column, $value) { $this->pairs[$column] = $value; @@ -72,9 +72,9 @@ public function set(string $column, $value): self /** * @param array $pairs * - * @return self + * @return static */ - public function pairs(array $pairs): self + public function pairs(array $pairs) { $this->pairs = array_merge($this->pairs, $pairs); diff --git a/src/Statement/UpdateInterface.php b/src/Statement/UpdateInterface.php index 3ffa46d..023dd8f 100644 --- a/src/Statement/UpdateInterface.php +++ b/src/Statement/UpdateInterface.php @@ -15,7 +15,7 @@ interface UpdateInterface extends AdvancedStatementInterface /** * @param string $table * - * @return self + * @return static */ public function table(string $table); @@ -23,14 +23,14 @@ public function table(string $table); * @param string $column * @param float|int|string|RawInterface|SelectInterface $value * - * @return self + * @return static */ public function set(string $column, $value); /** * @param array $pairs * - * @return self + * @return static */ public function pairs(array $pairs); } diff --git a/src/UserManager/MariaDB/UserManagerMariaDB.php b/src/UserManager/MariaDB/UserManagerMariaDB.php new file mode 100644 index 0000000..130a5ab --- /dev/null +++ b/src/UserManager/MariaDB/UserManagerMariaDB.php @@ -0,0 +1,42 @@ +expectException(\Exception::class); + + $user = 'bob'; + $host = 'localhost'; + $password = ''; + + $subject = new User( + + $this->createMock(PDO::class), + $host, + $user, + $password + ); + + $sql = $subject->orReplace()->ifNotExists()->__toString(); + + } + + public function testCreateUser() + { + $user = 'bob'; + $host = 'localhost'; + $password = ''; + + $subject = new User( + + $this->createMock(PDO::class), + $host, + $user, + $password + ); + + $sql = $subject->__toString(); + + $expect = "CREATE USER {$user}@{$host}"; + + $this->assertEquals($expect, $sql); + } + + public function testCreateUserWithPassword() + { + $user = 'bob'; + $host = 'localhost'; + $password = '123'; + + $subject = new \FaaPz\PDO\Definition\User( + + $this->createMock(PDO::class), + $host, + $user, + $password + ); + + $sql = $subject->__toString(); + + $expect = "CREATE USER {$user}@{$host} IDENTIFIED BY '{$password}'"; + + $this->assertEquals($expect, $sql); + } + + public function testCreateUserWithOrReplace() + { + $user = 'bob'; + $host = 'localhost'; + $password = ''; + + $subject = new \FaaPz\PDO\Definition\User( + + $this->createMock(PDO::class), + $host, + $user, + $password + ); + + $sql = $subject->orReplace()->__toString(); + + $expect = "CREATE OR REPLACE USER {$user}@{$host}"; + + $this->assertEquals($expect, $sql); + } + + public function testCreateUserWithIfNotExists() + { + $user = 'bob'; + $host = 'localhost'; + $password = ''; + + $subject = new \FaaPz\PDO\Definition\User( + + $this->createMock(PDO::class), + $host, + $user, + $password + ); + + $sql = $subject->ifNotExists()->__toString(); + + $expect = "CREATE USER IF NOT EXISTS {$user}@{$host}"; + + $this->assertEquals($expect, $sql); + } + + public function testCreateUserWithOrReplaceAndPassword() + { + $user = 'bob'; + $host = 'localhost'; + $password = '123'; + + $subject = new \FaaPz\PDO\Definition\User( + + $this->createMock(PDO::class), + $host, + $user, + $password + ); + + $sql = $subject->orReplace()->__toString(); + + $expect = "CREATE OR REPLACE USER {$user}@{$host} IDENTIFIED BY '{$password}'"; + + $this->assertEquals($expect, $sql); + } + + public function testCreateUserWithIfNotExistsAndPassword() + { + $user = 'bob'; + $host = 'localhost'; + $password = '123'; + + $subject = new \FaaPz\PDO\Definition\User( + + $this->createMock(PDO::class), + $host, + $user, + $password + ); + + $sql = $subject->ifNotExists()->__toString(); + + $expect = "CREATE USER IF NOT EXISTS {$user}@{$host} IDENTIFIED BY '{$password}'"; + + $this->assertEquals($expect, $sql); + } +} \ No newline at end of file diff --git a/tests/UserManager/MariaDB/DropUserTest.php b/tests/UserManager/MariaDB/DropUserTest.php new file mode 100644 index 0000000..2341084 --- /dev/null +++ b/tests/UserManager/MariaDB/DropUserTest.php @@ -0,0 +1,46 @@ +user = 'bob'; + $this->host = 'localhost'; + + $this->subject = new DropUser( + + $this->createMock(PDO::class), + $this->host, + $this->user + ); + } + + public function testDropUser() + { + $sql = $this->subject->__toString(); + + $expect = "DROP USER {$this->user}@{$this->host}"; + + $this->assertEquals($expect, $sql); + } + + public function testDropUserIfExists() + { + $sql = $this->subject->ifExists()->__toString(); + + $expect = "DROP USER IF EXISTS {$this->user}@{$this->host}"; + + $this->assertEquals($expect, $sql); + } +} \ No newline at end of file diff --git a/tests/UserManager/MariaDB/UserManagerMariaDBTest.php b/tests/UserManager/MariaDB/UserManagerMariaDBTest.php new file mode 100644 index 0000000..26d36f5 --- /dev/null +++ b/tests/UserManager/MariaDB/UserManagerMariaDBTest.php @@ -0,0 +1,59 @@ +subject = $this->createMock(PDO::class); + + $this->userManager = new UserManagerMariaDB(); + } + + public function testIfInstanceIsFromCreateUser() + { + $createUser = $this->userManager->createUser($this->subject, $this->host, $this->user, $this->password); + + $this->assertInstanceOf(User::class, $createUser); + } + + public function testIfInstanceIsFromDropUser() + { + $dropUser = $this->userManager->dropUser($this->subject, $this->host, $this->user); + + $this->assertInstanceOf(DropUser::class, $dropUser); + } + + public function testIfInstanceIsFromRovokePrivileges() + { + $revokeUser = $this->userManager->revokePrivileges($this->subject, $this->host, $this->user, $this->database, $this->table); + + $this->assertInstanceOf(RevokePrivileges::class, $revokeUser); + + } + + public function testIfInstanceIsFromGrantPrivileges() + { + $grantUser = $this->userManager->grantPrivileges($this->subject, $this->host, $this->user, $this->password, $this->database, $this->table); + + $this->assertInstanceOf(GrantPrivileges::class, $grantUser); + } +} \ No newline at end of file