Skip to content

Commit efdddc5

Browse files
committed
Operations on tables: CREATE / ALTER TABLE or queries different to CRUD (#30)
1 parent 3ef0476 commit efdddc5

13 files changed

Lines changed: 590 additions & 0 deletions

src/Definition/DropUser.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace FaaPz\PDO\Definition;
4+
5+
use FaaPz\PDO\AbstractStatement;
6+
use PDO;
7+
8+
class DropUser extends AbstractStatement
9+
{
10+
11+
/**
12+
* User information
13+
*/
14+
protected string $user;
15+
protected string $host;
16+
17+
/**
18+
* Clauses
19+
*/
20+
protected bool $ifExists = false;
21+
22+
public function __construct(PDO $pdo, string $host, string $user)
23+
{
24+
parent::__construct($pdo);
25+
$this->user = $user;
26+
$this->host = $host;
27+
}
28+
29+
public function ifExists()
30+
{
31+
$this->ifExists = true;
32+
return $this;
33+
}
34+
35+
public function getValues(): array
36+
{
37+
return [];
38+
}
39+
40+
public function __toString(): string
41+
{
42+
$sql = 'DROP USER';
43+
44+
if ($this->ifExists) {
45+
$sql = "{$sql} IF EXISTS {$this->user}@{$this->host}";
46+
} else {
47+
$sql = "{$sql} {$this->user}@{$this->host}";
48+
}
49+
50+
return $sql;
51+
}
52+
}
53+

src/Definition/IndexInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace FaaPz\PDO\Definition;
4+
5+
interface IndexInterface
6+
{
7+
8+
}

src/Definition/SchemaInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace FaaPz\PDO\Definition;
4+
5+
interface SchemaInterface
6+
{
7+
8+
}

src/Definition/TableInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace FaaPz\PDO\Definition;
4+
5+
interface TableInterface
6+
{
7+
8+
}

src/Definition/User.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace FaaPz\PDO\Definition;
4+
5+
use FaaPz\PDO\AbstractStatement;
6+
use PDO;
7+
8+
class User extends AbstractStatement
9+
{
10+
/** @var string $host */
11+
protected string $host;
12+
13+
/** @var string $username */
14+
protected string $username;
15+
16+
/** @var ?string $password */
17+
protected ?string $password;
18+
19+
/**
20+
* Clauses
21+
*/
22+
protected bool $orReplace = false;
23+
protected bool $ifNotExists = false;
24+
25+
/**
26+
*
27+
*/
28+
public function __construct(PDO $dbh, string $host, string $user, ?string $password = '')
29+
{
30+
parent::__construct($dbh);
31+
$this->host = $host;
32+
$this->user = $user;
33+
$this->password = $password;
34+
}
35+
36+
public function getValues(): array
37+
{
38+
return [];
39+
}
40+
41+
/**
42+
*
43+
*/
44+
public function orReplace()
45+
{
46+
$this->orReplace = true;
47+
return $this;
48+
}
49+
50+
/**
51+
*
52+
*/
53+
public function ifNotExists()
54+
{
55+
$this->ifNotExists = true;
56+
return $this;
57+
}
58+
59+
public function __toString(): string
60+
{
61+
62+
/**
63+
* Exception: CREATE 'OR REPLACE' USER 'IF NOT EXISTS' ...
64+
*/
65+
if ($this->orReplace && $this->ifNotExists)
66+
{
67+
throw new \Exception('The clause "OR REPLACE" and "IF NOT EXISTS" cannot be used together');
68+
}
69+
70+
71+
$sql = 'CREATE';
72+
if ($this->orReplace) {
73+
$sql = "{$sql} OR REPLACE USER {$this->user}@{$this->host}";
74+
}
75+
76+
if ($this->ifNotExists) {
77+
$sql = "{$sql} USER IF NOT EXISTS {$this->user}@{$this->host}";
78+
}
79+
80+
if (!$this->ifNotExists && !$this->orReplace) {
81+
$sql = "{$sql} USER {$this->user}@{$this->host}";
82+
}
83+
84+
if ($this->password) {
85+
$sql = "{$sql} IDENTIFIED BY '{$this->password}'";
86+
}
87+
88+
return $sql;
89+
}
90+
}

src/Definition/UserInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace FaaPz\PDO\Definition;
4+
5+
interface UserInterface
6+
{
7+
8+
}

src/Definition/ViewInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace FaaPz\PDO\Definition;
4+
5+
interface ViewInterface
6+
{
7+
8+
}

src/Statement/AlterInterface.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/**
4+
* @license MIT
5+
* @license http://opensource.org/licenses/MIT
6+
*/
7+
8+
namespace FaaPz\PDO\Statement;
9+
10+
use FaaPz\PDO\Clause\MethodInterface;
11+
use FaaPz\PDO\StatementInterface;
12+
13+
interface AlterInterface extends StatementInterface
14+
{
15+
/**
16+
* @param string $name
17+
*
18+
* @return SchemaInterface
19+
*/
20+
public function schema(string $name);
21+
22+
/**
23+
* @param string $name
24+
*
25+
* @return IndexInterface
26+
*/
27+
public function index(string $name);
28+
29+
/**
30+
* @param string $name
31+
*
32+
* @return TableInterface
33+
*/
34+
public function table(string $name);
35+
36+
/**
37+
* @param string $name
38+
*
39+
* @return UserInterface
40+
*/
41+
public function user(string $name);
42+
43+
/**
44+
* @param string $name
45+
*
46+
* @return ViewInterface
47+
*/
48+
public function view(string $name);
49+
}

src/Statement/CreateInterface.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* @license MIT
5+
* @license http://opensource.org/licenses/MIT
6+
*/
7+
8+
namespace FaaPz\PDO\Statement;
9+
10+
use FaaPz\PDO\Clause\MethodInterface;
11+
use FaaPz\PDO\Definition\IndexInterface;
12+
use FaaPz\PDO\Definition\SchemaInterface;
13+
use FaaPz\PDO\Definition\TableInterface;
14+
use FaaPz\PDO\Definition\UserInterface;
15+
use FaaPz\PDO\Definition\ViewInterface;
16+
use FaaPz\PDO\StatementInterface;
17+
18+
interface CreateInterface extends StatementInterface
19+
{
20+
/**
21+
* @param string $name
22+
*
23+
* @return SchemaInterface
24+
*/
25+
public function schema(string $name);
26+
27+
/**
28+
* @param string $name
29+
*
30+
* @return IndexInterface
31+
*/
32+
public function index(string $name);
33+
34+
/**
35+
* @param string $name
36+
*
37+
* @return TableInterface
38+
*/
39+
public function table(string $name);
40+
41+
/**
42+
* @param string $name
43+
*
44+
* @return UserInterface
45+
*/
46+
public function user(string $name);
47+
48+
/**
49+
* @param string $name
50+
*
51+
* @return ViewInterface
52+
*/
53+
public function view(string $name);
54+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* For the grant and revoke commands to work correctly,
4+
* you must start the application with super user (root),
5+
* that is, 'sudo php index.php'.
6+
*/
7+
namespace FaaPz\PDO\UserManager\MariaDB;
8+
9+
use FaaPz\PDO\Definition\User;
10+
use FaaPz\PDO\Definition\DropUser;
11+
use FaaPz\PDO\UserManager\UserManagerInterface;
12+
use PDO;
13+
14+
class UserManagerMariaDB implements UserManagerInterface
15+
{
16+
public function __construct()
17+
{
18+
}
19+
20+
public function createUser(PDO $pdo, string $host, string $user, ?string $password = ''): User
21+
{
22+
return new User($pdo, $host, $user, $password);
23+
}
24+
25+
public function dropUser(PDO $pdo, string $host, string $user): DropUser
26+
{
27+
return new DropUser($pdo, $host, $user);
28+
}
29+
30+
31+
public function grantPrivileges(PDO $pdo, string $host, string $user, ?string $password = '', ?string $database = '', ?string $table = ''): GrantPrivileges
32+
{
33+
return new GrantPrivileges($pdo, $host, $user, $password, $database, $table);
34+
}
35+
36+
37+
public function revokePrivileges(PDO $pdo, string $host, string $user, ?string $database = '', ?string $table = ''): RevokePrivileges
38+
{
39+
return new RevokePrivileges($pdo, $host, $user, $database, $table);
40+
}
41+
42+
}

0 commit comments

Comments
 (0)