| 1 |
<?php |
| 2 |
|
| 3 |
namespace Booktics\Abstracts; |
| 4 |
|
| 5 |
use Booktics\Contracts\Database_Interface; |
| 6 |
use RuntimeException; |
| 7 |
use wpdb; |
| 8 |
|
| 9 |
/** |
| 10 |
* Database Manager for booktics database |
| 11 |
* |
| 12 |
* @package Booktics/Abstracts |
| 13 |
*/ |
| 14 |
class Booktics_Database implements Database_Interface { |
| 15 |
|
| 16 |
/** |
| 17 |
* WordPress database global variable |
| 18 |
* |
| 19 |
* @var wpdb |
| 20 |
*/ |
| 21 |
private $db; |
| 22 |
|
| 23 |
/** |
| 24 |
* Set global wpdb instance |
| 25 |
*/ |
| 26 |
public function __construct() { |
| 27 |
global $wpdb; |
| 28 |
if ( ! $wpdb instanceof wpdb ) { |
| 29 |
throw new RuntimeException( 'WordPress database object not available' ); |
| 30 |
} |
| 31 |
$this->db = $wpdb; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Begin database transaction |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function begin_transaction(): void { |
| 40 |
$this->db->query( 'START TRANSACTION' ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Commit transactions to database |
| 45 |
* |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public function commit(): void { |
| 49 |
$this->db->query( 'COMMIT' ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Rollback uncommited transactions |
| 54 |
* |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
public function roll_back(): void { |
| 58 |
$this->db->query( 'ROLLBACK' ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Delete records of specific table |
| 63 |
* |
| 64 |
* @param string $table |
| 65 |
* @param array $where |
| 66 |
* |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function delete( string $table, array $where ): void { |
| 70 |
$table = booktics_get_table_name( $table ); |
| 71 |
$conditions = array(); |
| 72 |
$values = array(); |
| 73 |
|
| 74 |
foreach ( $where as $key => $value ) { |
| 75 |
$conditions[] = "$key = %s"; |
| 76 |
$values[] = $value; |
| 77 |
} |
| 78 |
|
| 79 |
$condition_string = implode( ' AND ', $conditions ); |
| 80 |
|
| 81 |
$sql = "DELETE FROM $table WHERE $condition_string"; |
| 82 |
|
| 83 |
$result = $this->db->query( |
| 84 |
$this->db->prepare( $sql, ...$values ) |
| 85 |
); |
| 86 |
|
| 87 |
if ( $result === false ) { |
| 88 |
throw new RuntimeException( |
| 89 |
// translators: %s is the database error message. |
| 90 |
sprintf( esc_html__( 'Failed to delete records: %s', 'booktics' ), esc_html( $this->db->last_error ) ) |
| 91 |
); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Batch insert records |
| 97 |
* |
| 98 |
* @param string $table |
| 99 |
* @param array $records |
| 100 |
* |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public function insert_batch( string $table, array $records ): void { |
| 104 |
if ( empty( $records ) ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
$table = booktics_get_table_name( $table ); |
| 109 |
$columns = array_keys( $records[0] ); |
| 110 |
$placeholders = array_fill( 0, count( $columns ), '%s' ); |
| 111 |
$placeholder_string = '(' . implode( ',', $placeholders ) . ')'; |
| 112 |
|
| 113 |
$values = array(); |
| 114 |
$placeholders_groups = array(); |
| 115 |
|
| 116 |
foreach ( $records as $record ) { |
| 117 |
$placeholders_groups[] = $placeholder_string; |
| 118 |
foreach ( $columns as $column ) { |
| 119 |
$values[] = $record[ $column ] ?? ''; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
$column_string = implode( ',', $columns ); |
| 124 |
$placeholders_string = implode( ',', $placeholders_groups ); |
| 125 |
|
| 126 |
$sql = "INSERT INTO $table ($column_string) VALUES $placeholders_string"; |
| 127 |
|
| 128 |
$result = $this->db->query( |
| 129 |
$this->db->prepare( $sql, ...$values ) |
| 130 |
); |
| 131 |
|
| 132 |
if ( $result === false ) { |
| 133 |
// translators: %s is the database error message. |
| 134 |
sprintf( esc_html__( 'Failed to insert records: %s', 'booktics' ), esc_html( $this->db->last_error ) ); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Select records from a table |
| 140 |
* |
| 141 |
* @param string $table |
| 142 |
* @param array $where Optional WHERE conditions |
| 143 |
* @param array $columns Columns to select |
| 144 |
* |
| 145 |
* @return array |
| 146 |
*/ |
| 147 |
public function select( string $table, array $where = array(), array $columns = array( '*' ) ) { |
| 148 |
$table = booktics_get_table_name( $table ); |
| 149 |
$columns_s = implode( ', ', $columns ); |
| 150 |
$sql = "SELECT $columns_s FROM $table"; |
| 151 |
|
| 152 |
$values = array(); |
| 153 |
if ( ! empty( $where ) ) { |
| 154 |
$conditions = array(); |
| 155 |
|
| 156 |
foreach ( $where as $key => $value ) { |
| 157 |
$conditions[] = "$key = %s"; |
| 158 |
$values[] = $value; |
| 159 |
} |
| 160 |
|
| 161 |
$condition_string = implode( ' AND ', $conditions ); |
| 162 |
$sql .= " WHERE $condition_string"; |
| 163 |
} |
| 164 |
|
| 165 |
$prepared = $this->db->prepare( $sql, ...$values ); |
| 166 |
$results = $this->db->get_results( $prepared ); |
| 167 |
|
| 168 |
if ( $results === null ) { |
| 169 |
throw new RuntimeException( |
| 170 |
// translators: %s is the database error message. |
| 171 |
sprintf( esc_html__( 'Failed to select records: %s', 'booktics' ), esc_html( $this->db->last_error ) ) |
| 172 |
); |
| 173 |
} |
| 174 |
|
| 175 |
return $results; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Initialize query builder instance |
| 180 |
* |
| 181 |
* @param string $table |
| 182 |
* |
| 183 |
* @return DB_Query_Builder |
| 184 |
*/ |
| 185 |
public function table( string $table ): DB_Query_Builder { |
| 186 |
return ( new DB_Query_Builder( $this->db ) )->table( $table ); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Update records in a table |
| 191 |
* |
| 192 |
* @param string $table |
| 193 |
* @param array $data |
| 194 |
* @param array $where |
| 195 |
* |
| 196 |
* @return void |
| 197 |
*/ |
| 198 |
public function update( string $table, array $data, array $where ) { |
| 199 |
$table = booktics_get_table_name( $table ); |
| 200 |
$set_values = array(); |
| 201 |
$where_conditions = array(); |
| 202 |
$values = array(); |
| 203 |
|
| 204 |
foreach ( $data as $key => $value ) { |
| 205 |
$set_values[] = "$key = %s"; |
| 206 |
$values[] = $value; |
| 207 |
} |
| 208 |
|
| 209 |
foreach ( $where as $key => $value ) { |
| 210 |
$where_conditions[] = "$key = %s"; |
| 211 |
$values[] = $value; |
| 212 |
} |
| 213 |
|
| 214 |
$set_string = implode( ', ', $set_values ); |
| 215 |
$where_string = implode( ' AND ', $where_conditions ); |
| 216 |
|
| 217 |
$sql = "UPDATE $table SET $set_string WHERE $where_string"; |
| 218 |
|
| 219 |
$result = $this->db->query( |
| 220 |
$this->db->prepare( $sql, ...$values ) |
| 221 |
); |
| 222 |
|
| 223 |
if ( $result === false ) { |
| 224 |
throw new RuntimeException( |
| 225 |
// translators: %s: Records Name |
| 226 |
sprintf( esc_html__( 'Failed to update records: %s', 'booktics' ), esc_html( $this->db->last_error ) ) |
| 227 |
); |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|