has_id()) { throw $this->get_has_id_exception(); } $converter = new customers_converter(); $converter->load_object($target); $result = $this->get_database()->insert( $this->get_tables()->customers(), $converter->get_array() ); if ($result === false) { throw $this->get_exception('create'); } return $this->complete(new customer($this->get_database()->insert_id)); } public function save(database_item $target) : database_item { if (!$target->has_id()) { throw $this->get_blank_id_exception(); } $converter = new customers_converter(); $converter->load_object($target); $condition = []; $condition['id'] = $target->get_id(); $result = $this->get_database()->update( $this->get_tables()->customers(), $converter->get_array(), $condition ); return $this->complete($target); } public function load_all(array $filters = [], ?int $limit = null) : array { $query = new \cx_appengine\string_builder(); $query->push('select * '); $query->push('from '.$this->get_tables()->customers().' '); $query->push($this->parse_filters($filters)); $query->push($this->parse_limit($limit)); $result = $this->get_database()->get_results($query->get(), ARRAY_A); if ($result === null) { throw $this->get_exception(); } $customers = []; $converter = new customers_converter(); foreach ($result as $row) { array_push($customers, $converter->load_array($row)->get_object()); } return $customers; } public function complete(database_item $target) : database_item { if (!$target->has_id()) { throw $this->get_blank_id_exception(); } $query = new \cx_appengine\string_builder(); $query->push('select * '); $query->push('from '.$this->get_tables()->customers().' '); $query->push('where id=%d'); $sql = $this->get_database()->prepare($query, $target->get_id()); $row = $this->get_database()->get_row($sql, ARRAY_A); if ($row === null) { throw $this->get_exception('complete'); } $converter = new customers_converter(); $converter->load_array($row); return $converter->get_object(); } public function remove(database_item $target) : void { if (!$target->has_id()) { throw $this->get_blank_id_exception(); } $this->get_database()->delete( $this->get_tables()->send_log(), [ 'customer' => $target->get_id()] ); $result = $this->get_database()->delete( $this->get_tables()->customers(), [ 'id' => $target->get_id() ] ); if ($result === false) { throw $this->get_exception('remove'); } } public function count(array $filters = []) : int { $query = new \cx_appengine\string_builder(); $query->push('select count(id) '); $query->push('from '.$this->get_tables()->customers().' '); $query->push($this->parse_filters($filters)); $result = $this->get_database()->get_var($query->get()); if ($result === null) { throw $this->get_exception('count'); } return intval($result); } public function find(string $phrase, ?int $limit = null) : array { $columns = [ 'name' => '%'.$phrase.'%', 'surname' => '%'.$phrase.'%', 'company' => '%'.$phrase.'%', 'comments' => '%'.$phrase.'%', 'email' => '%'.$phrase.'%', 'phone_number' => '%'.$phrase.'%', 'tester' => '%'.$phrase.'%', ]; $filters = $this->parse_filters( $columns, filter_type::like, filter_glue::single ); $query = new \cx_appengine\string_builder(); $query->push('select * '); $query->push('from '.$this->get_tables()->customers().' '); $query->push($filters); $query->push($this->parse_limit($limit)); $result = $this->get_database()->get_results($query->get(), ARRAY_A); if ($result === null) { throw $this->get_exception(); } $customers = []; $converter = new customers_converter(); foreach ($result as $row) { array_push($customers, $converter->load_array($row)->get_object()); } return $customers; } }