123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- class SqliteDatabase {
- private static $instance;
- public static function getSingleton() {
- if(SqliteDatabase::$instance == null) {
- SqliteDatabase::$instance = new SqliteDatabase();
- }
- return SqliteDatabase::$instance;
- }
- private $sqlite;
- public function __construct() {
- $this->sqlite = new sqlite3(DATA_PATH . DBNAME);
- }
- public function __destruct() {
- $this->sqlite->close();
- }
- public function exec($sql) {
- $return = $this->sqlite->exec($sql);
- if(!$return) {
- throw new Exception($this->lastErrorMsg());
- }
- return $return;
- }
- public function query($sql) {
- return $this->sqlite->query($sql);
- }
- public function queryArray($sql) {
- $data = array();
- $return = $this->sqlite->query($sql);
- while($row = $return->fetchArray(SQLITE3_ASSOC)) {
- $data[] = $row;
- }
- return $data;
- }
- public function prepare($sql) {
- return $this->sqlite->prepare($sql);
- }
- public function preparedQueryArray($sql, array $keyValue) {
- $preparedQuery = $this->prepare($sql);
- foreach($keyValue as $key => $value) {
- $preparedQuery->bindValue(':' . $key, $value);
- }
- $result = $preparedQuery->execute();
- $resultData = array();
- while($row = $result->fetchArray(SQLITE3_ASSOC)) {
- $resultData[] = $row;
- }
- return $resultData;
- }
- public function lastErrorMsg() {
- return $this->sqlite->lastErrorMsg();
- }
- }
|