| | |
| | | namespace JVBase\managers; |
| | | |
| | | use Exception; |
| | | use JVBase\inc\managers\ErrorHelper; |
| | | use wpdb; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | |
| | | */ |
| | | class CustomTable |
| | | { |
| | | |
| | | use ErrorHelper; |
| | | private static ?wpdb $wpdb = null; |
| | | private static string $prefix; |
| | | protected string $tableName; |
| | |
| | | ); |
| | | |
| | | if ($result === false) { |
| | | $this->logError('insert', $data); |
| | | $this->logError(self::$wpdb->last_error, 'insert', $data); |
| | | return false; |
| | | } |
| | | |
| | |
| | | $result = self::$wpdb->query(self::$wpdb->prepare($query, $values)); |
| | | |
| | | if ($result === false) { |
| | | $this->logError('bulkInsert', ['rows' => count($rows)]); |
| | | $this->logError(self::$wpdb->last_error, 'bulkInsert', ['rows' => count($rows)]); |
| | | return false; |
| | | } |
| | | |
| | |
| | | $key, |
| | | function() use ($column, $where, $orderby, $order, $limit) { |
| | | if (!empty($this->columns) && !array_key_exists($column, $this->columns)) { |
| | | $this->logError('pluck', ['reason' => "Column '{$column}' not in definition"]); |
| | | $this->logError("Column '{$column}' not in definition", 'pluck'); |
| | | return []; |
| | | } |
| | | $args = ['where' => $where]; |
| | |
| | | ); |
| | | |
| | | if ($result === false) { |
| | | $this->logError('update', ['data' => $data, 'where' => $where]); |
| | | $this->logError(self::$wpdb->last_error,'update', ['data' => $data, 'where' => $where]); |
| | | } |
| | | $this->cache->flush(); |
| | | |
| | |
| | | ); |
| | | |
| | | if ($result === false) { |
| | | $this->logError('delete', ['where' => $where]); |
| | | $this->logError(self::$wpdb->last_error, 'delete', ['where' => $where]); |
| | | } |
| | | $this->cache->flush(); |
| | | |
| | |
| | | return $result; |
| | | } catch (Exception $e) { |
| | | $this->rollback(); |
| | | $this->logError('transaction', ['error' => $e->getMessage()]); |
| | | $this->logError(self::$wpdb->last_error,'transaction', ['error' => $e->getMessage()]); |
| | | return false; |
| | | } |
| | | } |
| | |
| | | return isset($cache[$this->tableName][$column]); |
| | | } |
| | | |
| | | /** |
| | | * Log database errors |
| | | */ |
| | | private function logError(string $operation, array $context = []): void |
| | | { |
| | | if (function_exists('JVB')) { |
| | | Errors::getInstance()->log( |
| | | $this->tableName, |
| | | "CustomTable {$operation} failed: " . self::$wpdb->last_error, |
| | | $context |
| | | ); |
| | | } else { |
| | | error_log("[CustomTable:{$this->tableName}] {$operation} failed: " . self::$wpdb->last_error); |
| | | } |
| | | } |
| | | |
| | | public function sum(string $column, array $where):float |
| | | { |
| | | $rows = $this->pluck($column, $where); |