| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace UserAccessManager\Database; |
| 6 |
|
| 7 |
use UserAccessManager\Wrapper\Wordpress; |
| 8 |
use wpdb; |
| 9 |
|
| 10 |
class Database |
| 11 |
{ |
| 12 |
public const USER_GROUP_TABLE_NAME = 'uam_accessgroups'; |
| 13 |
public const USER_GROUP_TO_OBJECT_TABLE_NAME = 'uam_accessgroup_to_object'; |
| 14 |
|
| 15 |
private wpdb $wpDatabase; |
| 16 |
|
| 17 |
public function __construct( |
| 18 |
private Wordpress $wordpress |
| 19 |
) { |
| 20 |
$this->wpDatabase = $wordpress->getDatabase(); |
| 21 |
} |
| 22 |
|
| 23 |
public function getWordpressDatabase(): wpdb |
| 24 |
{ |
| 25 |
return $this->wpDatabase; |
| 26 |
} |
| 27 |
|
| 28 |
public function getUserGroupTable(): string |
| 29 |
{ |
| 30 |
return $this->wpDatabase->prefix . self::USER_GROUP_TABLE_NAME; |
| 31 |
} |
| 32 |
|
| 33 |
public function getUserGroupToObjectTable(): string |
| 34 |
{ |
| 35 |
return $this->wpDatabase->prefix . self::USER_GROUP_TO_OBJECT_TABLE_NAME; |
| 36 |
} |
| 37 |
|
| 38 |
public function dbDelta(string $queries = '', bool $execute = true): array |
| 39 |
{ |
| 40 |
return $this->wordpress->dbDelta($queries, $execute); |
| 41 |
} |
| 42 |
|
| 43 |
public function getPrefix(): string |
| 44 |
{ |
| 45 |
return $this->wpDatabase->prefix; |
| 46 |
} |
| 47 |
|
| 48 |
public function getLastInsertId(): int|string |
| 49 |
{ |
| 50 |
return $this->wpDatabase->insert_id; |
| 51 |
} |
| 52 |
|
| 53 |
public function getCurrentBlogId(): int|string |
| 54 |
{ |
| 55 |
return $this->wpDatabase->blogid; |
| 56 |
} |
| 57 |
|
| 58 |
public function getBlogsTable(): string |
| 59 |
{ |
| 60 |
return $this->wpDatabase->blogs; |
| 61 |
} |
| 62 |
|
| 63 |
public function getPostsTable(): string |
| 64 |
{ |
| 65 |
return $this->wpDatabase->posts; |
| 66 |
} |
| 67 |
|
| 68 |
public function getTermRelationshipsTable(): string |
| 69 |
{ |
| 70 |
return $this->wpDatabase->term_relationships; |
| 71 |
} |
| 72 |
|
| 73 |
public function getTermTaxonomyTable(): string |
| 74 |
{ |
| 75 |
return $this->wpDatabase->term_taxonomy; |
| 76 |
} |
| 77 |
|
| 78 |
public function getUsersTable(): string |
| 79 |
{ |
| 80 |
return $this->wpDatabase->users; |
| 81 |
} |
| 82 |
|
| 83 |
public function getCapabilitiesTable(): string |
| 84 |
{ |
| 85 |
return $this->wpDatabase->prefix . 'capabilities'; |
| 86 |
} |
| 87 |
|
| 88 |
public function getColumn(?string $query = null, int $column = 0): array |
| 89 |
{ |
| 90 |
return $this->wpDatabase->get_col($query, $column); |
| 91 |
} |
| 92 |
|
| 93 |
public function getRow(?string $query = null, string $output = OBJECT, int $row = 0): object|array|null |
| 94 |
{ |
| 95 |
return $this->wpDatabase->get_row($query, $output, $row); |
| 96 |
} |
| 97 |
|
| 98 |
public function getVariable(?string $query = null, int $column = 0, int $row = 0): int|string|null |
| 99 |
{ |
| 100 |
return $this->wpDatabase->get_var($query, $column, $row); |
| 101 |
} |
| 102 |
|
| 103 |
public function getBlogPrefix(int|string|null $blogId = null): string |
| 104 |
{ |
| 105 |
return $this->wpDatabase->get_blog_prefix($blogId); |
| 106 |
} |
| 107 |
|
| 108 |
public function prepare(string $query, mixed $arguments): string |
| 109 |
{ |
| 110 |
return $this->wpDatabase->prepare($query, $arguments); |
| 111 |
} |
| 112 |
|
| 113 |
public function query(string $query): bool|int |
| 114 |
{ |
| 115 |
return $this->wpDatabase->query($query); |
| 116 |
} |
| 117 |
|
| 118 |
public function getResults(?string $query = null, string $output = OBJECT): object|array|null |
| 119 |
{ |
| 120 |
return $this->wpDatabase->get_results($query, $output); |
| 121 |
} |
| 122 |
|
| 123 |
public function insert(string $table, array $data, array|string|null $format = null): bool|int |
| 124 |
{ |
| 125 |
return $this->wpDatabase->insert($table, $data, $format); |
| 126 |
} |
| 127 |
|
| 128 |
public function update( |
| 129 |
string $table, |
| 130 |
array $data, |
| 131 |
array $where, |
| 132 |
array|string|null $format = null, |
| 133 |
array|string|null $whereFormat = null |
| 134 |
): bool|int { |
| 135 |
return $this->wpDatabase->update($table, $data, $where, $format, $whereFormat); |
| 136 |
} |
| 137 |
|
| 138 |
public function replace(string $table, array $data, array|string|null $format = null): bool|int |
| 139 |
{ |
| 140 |
return $this->wpDatabase->replace($table, $data, $format); |
| 141 |
} |
| 142 |
|
| 143 |
public function delete(string $table, array $where, array|string|null $whereFormat = null): bool|int |
| 144 |
{ |
| 145 |
return $this->wpDatabase->delete($table, $where, $whereFormat); |
| 146 |
} |
| 147 |
|
| 148 |
public function getCharset(): string |
| 149 |
{ |
| 150 |
return $this->getCharsetCollate('DEFAULT '); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* The charset clause for a column definition. It carries no DEFAULT, because that |
| 155 |
* makes it a table option, which a column definition does not accept. |
| 156 |
*/ |
| 157 |
public function getColumnCharset(): string |
| 158 |
{ |
| 159 |
return $this->getCharsetCollate(''); |
| 160 |
} |
| 161 |
|
| 162 |
private function getCharsetCollate(string $charsetPrefix): string |
| 163 |
{ |
| 164 |
$mySqlVersion = (string) $this->getVariable('SELECT VERSION() as mysql_version'); |
| 165 |
|
| 166 |
if (version_compare($mySqlVersion, '4.1.0', '<')) { |
| 167 |
return ''; |
| 168 |
} |
| 169 |
|
| 170 |
$charsetCollate = ''; |
| 171 |
|
| 172 |
if (!empty($this->wpDatabase->charset)) { |
| 173 |
$charsetCollate = "{$charsetPrefix}CHARACTER SET {$this->wpDatabase->charset}"; |
| 174 |
} |
| 175 |
|
| 176 |
if (!empty($this->wpDatabase->collate)) { |
| 177 |
$charsetCollate .= " COLLATE {$this->wpDatabase->collate}"; |
| 178 |
} |
| 179 |
|
| 180 |
return $charsetCollate; |
| 181 |
} |
| 182 |
} |
| 183 |
|