SqliteDatabase.class.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. class SqliteDatabase {
  3. private static $instance;
  4. public static function getSingleton() {
  5. if(SqliteDatabase::$instance == null) {
  6. SqliteDatabase::$instance = new SqliteDatabase();
  7. }
  8. return SqliteDatabase::$instance;
  9. }
  10. private $sqlite;
  11. public function __construct() {
  12. $this->sqlite = new sqlite3(DATA_PATH . DBNAME);
  13. }
  14. public function __destruct() {
  15. $this->sqlite->close();
  16. }
  17. public function exec($sql) {
  18. $return = $this->sqlite->exec($sql);
  19. if(!$return) {
  20. throw new Exception($this->lastErrorMsg());
  21. }
  22. return $return;
  23. }
  24. public function query($sql) {
  25. return $this->sqlite->query($sql);
  26. }
  27. public function queryArray($sql) {
  28. $data = array();
  29. $return = $this->sqlite->query($sql);
  30. while($row = $return->fetchArray(SQLITE3_ASSOC)) {
  31. $data[] = $row;
  32. }
  33. return $data;
  34. }
  35. public function prepare($sql) {
  36. return $this->sqlite->prepare($sql);
  37. }
  38. public function preparedQueryArray($sql, array $keyValue) {
  39. $preparedQuery = $this->prepare($sql);
  40. foreach($keyValue as $key => $value) {
  41. $preparedQuery->bindValue(':' . $key, $value);
  42. }
  43. $result = $preparedQuery->execute();
  44. $resultData = array();
  45. while($row = $result->fetchArray(SQLITE3_ASSOC)) {
  46. $resultData[] = $row;
  47. }
  48. return $resultData;
  49. }
  50. public function lastErrorMsg() {
  51. return $this->sqlite->lastErrorMsg();
  52. }
  53. }