From 7f3c3a70baa9dec74181cc6877286920575fc598 Mon Sep 17 00:00:00 2001 From: Alexander Barker Date: Wed, 10 Aug 2022 11:20:14 -0700 Subject: [PATCH] Operations on tables: CREATE / ALTER TABLE or queries different to CRUD (#30) --- src/Definition/DropUser.php | 53 ++++++ src/Definition/IndexInterface.php | 8 + src/Definition/SchemaInterface.php | 8 + src/Definition/TableInterface.php | 8 + src/Definition/User.php | 90 ++++++++++ src/Definition/UserInterface.php | 8 + src/Definition/ViewInterface.php | 8 + src/Statement/AlterInterface.php | 49 ++++++ src/Statement/CreateInterface.php | 54 ++++++ .../MariaDB/UserManagerMariaDB.php | 42 +++++ tests/UserManager/MariaDB/CreateUserTest.php | 157 ++++++++++++++++++ tests/UserManager/MariaDB/DropUserTest.php | 46 +++++ .../MariaDB/UserManagerMariaDBTest.php | 59 +++++++ 13 files changed, 590 insertions(+) create mode 100644 src/Definition/DropUser.php create mode 100644 src/Definition/IndexInterface.php create mode 100644 src/Definition/SchemaInterface.php create mode 100644 src/Definition/TableInterface.php create mode 100644 src/Definition/User.php create mode 100644 src/Definition/UserInterface.php create mode 100644 src/Definition/ViewInterface.php create mode 100644 src/Statement/AlterInterface.php create mode 100644 src/Statement/CreateInterface.php create mode 100644 src/UserManager/MariaDB/UserManagerMariaDB.php create mode 100644 tests/UserManager/MariaDB/CreateUserTest.php create mode 100644 tests/UserManager/MariaDB/DropUserTest.php create mode 100644 tests/UserManager/MariaDB/UserManagerMariaDBTest.php 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 @@ +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