| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace cx_newsletter;
- use \wpdb;
- abstract class database_worker {
- public function __construct(
- wpdb $database,
- table_names $tables,
- ) {
- $this->database = $database;
- $this->tables = $tables;
- }
- protected function parse_limit(?int $limit) : string {
- if ($limit === null) {
- return '';
- }
- return 'limit '.strval($limit).' ';
- }
- protected function parse_filters(
- array $filters,
- filter_type $type = filter_type::equal,
- filter_glue $glue = filter_glue::all
- ) : string {
- if (empty($filters)) {
- return '';
- }
- $result = new \cx_appengine\string_builder('where ');
-
- foreach ($filters as $name => $content) {
- $result->push($name);
- if ($content === null) {
- $result->push(' is null');
- } elseif (is_numeric($content)) {
- $result->push($type->value.strval($content));
- } elseif (is_string($content)) {
- $result->push($type->value.'"'.$content.'"');
- } else {
- throw $this->get_exception('parse_filters');
- }
- $result->push($glue->value);
- }
- return $result->cut(strlen($glue->value))->push(' ')->get();
- }
- protected function get_exception(string $operation) : \exception {
- $exception = "\n\n";
- $exception .= 'Can not make operation on database in class: ';
- $exception .= get_class($this).'. Trying to make: '.$operation.'.';
- $exception .= "\n\n";
- return new \exception($exception);
- }
- protected function get_has_id_exception() : \exception {
- $exception = "\n\n";
- $exception .= 'Can not make operation on database in class: ';
- $exception .= get_class($this).'. Trying to insert item with ID.';
- $exception .= "\n\n";
- return new \exception($exception);
- }
- protected function get_blank_id_exception() : \exception {
- $exception = "\n\n";
- $exception .= 'Can not make operation on database in class: ';
- $exception .= get_class($this).'. Trying to load target without ID.';
- $exception .= "\n\n";
- return new \exception($exception);
- }
- protected function get_tables() : table_names {
- return $this->tables;
- }
- protected function get_database() : wpdb {
- return $this->database;
- }
- private table_names $tables;
- private wpdb $database;
- }
|