ServerDriver   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 91
rs 10
c 1
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
1
<?php declare(strict_types=1);
2
3
/** 
4
 *  ___      _        _
5
 * | _ \__ _| |_ __ _| |__  __ _ ___ ___
6
 * |  _/ _` |  _/ _` | '_ \/ _` (_-</ -_)
7
 * |_| \__,_|\__\__,_|_.__/\__,_/__/\___|
8
 * 
9
 * This file is part of Kristuff\Patabase.
10
 * (c) Kristuff <[email protected]>
11
 *
12
 * For the full copyright and license information, please view the LICENSE
13
 * file that was distributed with this source code.
14
 *
15
 * @version    1.0.1
16
 * @copyright  2017-2022 Christophe Buliard
17
 */
18
19
namespace Kristuff\Patabase\Driver;
20
21
use Kristuff\Patabase\Driver\DatabaseDriver;
22
23
/**
24
 *  Class ServerDriver
25
 *
26
 *  Base class for Database/Server drivers
27
 */
28
abstract class ServerDriver extends DatabaseDriver
29
{
30
    /**
31
     * Constructor
32
     *
33
     * @access public
34
     * @param array     $settings               The connection settings
35
     * @param bool      $isServerConnection     True for Server connection, default is false
36
     */
37
    public function __construct(array $settings, bool $isServerConnection = false)
38
    {
39
        // remove database attribute for server connection
40
        if ($isServerConnection){
41
            $dbKey = array_search('database', $this->dsnAttributes);
42
            if ($dbKey !== false) {
43
                unset($this->dsnAttributes[$dbKey]);
44
            }
45
        }
46
        parent::__construct($settings);
47
    }
48
49
    /**
50
     * Check if database exists
51
     *
52
     * @access public
53
     * @param string    $databaseName   The database name
54
     *
55
     * @return bool     True if the given database exists, otherwise false.
56
     */
57
    abstract public function databaseExists(string $databaseName): bool;
58
59
    /**
60
     * Create a database
61
     *
62
     * @access public
63
     * @param string    $databaseName   The database name.
64
     * @param string    $owner          The database owner. This parameter is honored in pgsql only.
65
     * @param string    $template       (optional) The template to use. Default is 'template0'
66
     *
67
     * @return bool     True if the database has been created, otherwise false.
68
     */
69
    abstract public function createDatabase(string $databaseName, ?string $owner= null, ?string $template = null): bool;
70
71
    /**
72
     * Create a user
73
     *
74
     * @access public
75
     * @param string    $userName       The user name
76
     * @param string    $userpassword   The user password
77
     *
78
     * @return bool     True if the user has been created, otherwise false. 
79
     */
80
    abstract public function createUser(string $userName, string $userPassword): bool;
81
82
    /**
83
     * Drop a user
84
     *
85
     * @access public
86
     * @param string    $userName       The user name
87
     * @param bool      $ifExists       (optional) True if the user must be deleted only when exists. Default is false.
88
     *
89
     * @return bool     True if the user has been dropped or does not exist when $ifExists is set to True, otherwise false. 
90
     */
91
    abstract public function dropUser(string $userName, bool $ifExists = false): bool;
92
    
93
    /**
94
     * Grant user permissions on given database
95
     *
96
     * @access public
97
     * @param string    $databaseName   The database name
98
     * @param string    $userName       The user name
99
     *
100
     * @return bool     True if the user has been granted, otherwise false. 
101
     */
102
    abstract public function grantUser(string $databaseName, string $userName): bool;
103
104
    /**
105
     * Get the SQL for show databases
106
     *
107
     * @access public
108
     * @return string
109
     */
110
    abstract public function sqlShowDatabases(): string;
111
112
    /**
113
     * Get the SQL for show users
114
     *
115
     * @access public
116
     * @return string
117
     */
118
    abstract public function sqlShowUsers(): string;
119
}