1 | <?php |
||
28 | class Database |
||
29 | { |
||
30 | use User; |
||
31 | |||
32 | /** @} */ |
||
33 | |||
34 | private $bot; |
||
35 | |||
36 | /** PDO connection to the database. */ |
||
37 | public $pdo; |
||
38 | |||
39 | /** \brief Table contaning bot users data in the SQL database. */ |
||
40 | public $user_table = 'User'; |
||
41 | |||
42 | /** |
||
43 | * \addtogroup Database |
||
44 | * @{ |
||
45 | */ |
||
46 | |||
47 | /** |
||
48 | * @internal |
||
49 | * \brief Create a Database handler object. |
||
50 | * @param BasicBot $bot Reference to the bot that use this object. |
||
51 | */ |
||
52 | 1 | public function __construct(BasicBot &$bot) |
|
56 | |||
57 | /** |
||
58 | * \brief Open a database connection using PDO. |
||
59 | * \details Provides a simple way to initialize a database connection |
||
60 | * and create a PDO instance. |
||
61 | * @param array $params Parameters for initialize connection. |
||
62 | * Index required: |
||
63 | * - <code>username</code> |
||
64 | * - <code>password</code> (can be an empty string) |
||
65 | * Optional index: |
||
66 | * - <code>dbname</code> |
||
67 | * - <code>adapter</code> <b>Default</b>: <code>mysql</code> |
||
68 | * - <code>host</code> <b>Default</b>: <code>localhost</code> |
||
69 | * - <code>options</code> (<i>Array of options passed when creating PDO object</i>) |
||
70 | * @return bool True when the connection is succefully created. |
||
71 | */ |
||
72 | public function connect(array $params) : bool |
||
88 | |||
89 | /** |
||
90 | * @internal |
||
91 | * \brief Add default connection value to parameter passed to PDO. |
||
92 | * @param array $params Parameter for PDO connection. |
||
93 | * @return array Parameter with defaults value. |
||
94 | */ |
||
95 | 1 | public static function addDefaultValue(array $params) : array |
|
96 | { |
||
97 | 1 | static $defaults = [ 'adapter' => PDO_DEFAULT_ADAPTER, 'host' => 'localhost', 'options' => [] ]; |
|
98 | 1 | return array_merge($defaults, $params); |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * @internal |
||
103 | * \brief Returns a string that can passed to PDO as DNS parameter in order to open connection. |
||
104 | * @param array $params Array containing parameter of the connection |
||
105 | * @return string Parameters contained in array $params sanitized in a string that can be passed as DNS param of PDO object creation. |
||
106 | */ |
||
107 | 2 | public static function getDns($params) : string |
|
108 | { |
||
109 | 2 | $response = $params['adapter'] . ':'; |
|
110 | 2 | unset($params['adapter']); |
|
111 | 2 | $fields = []; |
|
112 | |||
113 | 2 | foreach ($params as $field => $value) { |
|
114 | /** |
||
115 | *Check if the current field matches one of the fields |
||
116 | * that are passed to PDO in another way and so don't need |
||
117 | * to be included in the string. |
||
118 | */ |
||
119 | 2 | if ($field === 'username' || $field === 'password' || $field === 'options') { |
|
120 | 1 | unset($params[$field]); |
|
121 | 1 | continue; |
|
122 | } |
||
123 | |||
124 | 2 | $fields[] = $field . '=' . $value; |
|
125 | } |
||
126 | |||
127 | 2 | return $response . join(';', $fields); |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * @internal |
||
132 | * \brief Sanitize name of the user table depending on database used. |
||
133 | */ |
||
134 | protected function sanitizeUserTable() |
||
150 | |||
151 | /** @} */ |
||
152 | } |
||
153 |