db = new PDO('sqlite:'.$options['file']); } else { $this->db = new PDO('mysql:host='.$options['host'].';dbname='.$options['name'], $options['user'], $options['password']); } } catch (PDOException $e) { die('Connection to database faild: '.$e->getMessage()); } $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); } public function query($sql, array $args = null) { try { if (!is_null($args)) { $this->lastQuery = $this->db->prepare($sql); $this->lastQuery->execute($args); } else { $this->lastQuery = $this->db->query($sql); } return $this->lastQuery; } catch (PDOException $e) { if ($this->handleError) { $this->error($e->getMessage(), $sql, $e->getCode()); } else { throw $e; } } } public function prepare($sql) { try { return $this->db->prepare($sql); } catch (PDOException $e) { if ($this->handleError) { $this->error($e->getMessage(), $sql, $e->getCode()); } else { throw $e; } } } public function fetchObject(PDOStatement $stmt = null) { if (!is_null($stmt)) { return $stmt->fetch(PDO::FETCH_OBJ); } else { return $this->lastQuery->fetch(PDO::FETCH_OBJ); } } public function fetch(PDOStatement $stmt = null, $fetch_style = FETCH_ASSOC) { if (!is_null($stmt)) { return $stmt->fetch(PDO::$fetch_style); } else { return $this->lastQuery->fetch(PDO::$fetch_style); } } public function numRows(PDOStatement $stmt = null) { if (!is_null($stmt)) { return $stmt->rowCount(); } else { return $this->lastQuery->rowCount(); } } function tableExists($table) { try { $result = $this->query('SHOW TABLES LIKE ?', [$table])->numRows(); } catch (Exception $e) { return $e->getMessage(); } return $result > 0 ? true : false; } public function insertID() { return $this->db->lastInsertId(); } public function beginTransaction() { return $this->db->beginTransaction(); } public function commit() { return $this->db->commit(); } public function rollback() { return $this->db->rollBack(); } public function errorInfo() { return $this->db->errorinfo(); } protected function error($msg, $sql, $code) { Helpers::log(Helpers::LOG_ERROR, $msg . '

' . $sql . ''); } }