123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- namespace core\database;
- use core\basic\Config;
- class Sqlite implements Builder
- {
- protected static $sqlite;
- protected $master;
- protected $slave;
- private $begin = false;
- private function __construct()
- {}
- public function __destruct()
- {
- if ($this->begin) {
- $this->master->exec('commit;');
- }
- }
-
- public static function getInstance()
- {
- if (! self::$sqlite) {
- self::$sqlite = new self();
- }
- return self::$sqlite;
- }
-
- public function conn($cfg)
- {
- if (extension_loaded('SQLite3')) {
- try {
- $conn = new \SQLite3($cfg);
- $conn->busyTimeout(15 * 1000);
- } catch (\Exception $e) {
- error("读取数据库文件失败:" . iconv('gbk', 'utf-8', $e->getMessage()));
- }
- } else {
- if (extension_loaded('pdo_sqlite')) {
- error('未检测到您服务器环境的sqlite3数据库扩展,请检查php.ini中是否已经开启该扩展!<br>另外,检测到您服务器支持pdo_sqlite扩展,您也可以修改数据库配置连接驱动为pdo_sqlite试试!');
- } else {
- error('未检测到您服务器环境的sqlite3数据库扩展,请检查php.ini中是否已经开启该扩展!');
- }
- }
- return $conn;
- }
-
- public function query($sql, $type = 'master')
- {
- $time_s = microtime(true);
- if (! $this->master || ! $this->slave) {
- $cfg = ROOT_PATH . Config::get('database.dbname');
- $conn = $this->conn($cfg);
- $this->master = $conn;
- $this->slave = $conn;
- }
- switch ($type) {
- case 'master':
- if (! $this->begin) {
- $this->master->exec('begin;');
- $this->begin = true;
- }
- $result = $this->master->exec($sql) or $this->error($sql, 'master');
- break;
- case 'slave':
- $result = $this->slave->query($sql) or $this->error($sql, 'slave');
- break;
- }
- return $result;
- }
-
- public function isExist($sql)
- {
- $result = $this->query($sql, 'slave');
- if ($result->fetchArray()) {
- $result->finalize();
- return true;
- } else {
- return false;
- }
- }
-
- public function rows($table)
- {
- $sql = "SELECT count(*) FROM $table";
- $result = $this->query($sql, 'slave');
- if (! ! $row = $result->fetchArray(2)) {
- $result->finalize();
- return $row[0];
- } else {
- return 0;
- }
- }
-
- public function fields($table)
- {
- $sql = "SELECT * FROM $table LIMIT 1";
- $result = $this->query($sql, 'slave');
- if ($result) {
- return $result->numColumns();
- } else {
- return false;
- }
- }
-
- public function tableFields($table)
- {
- $sql = "pragma table_info($table)";
- $result = $this->query($sql, 'slave');
- $rows = array();
- while (! ! $row = $result->fetchArray(SQLITE3_ASSOC)) {
- $rows[] = $row['name'];
- }
- $result->finalize();
- return $rows;
- }
-
- public function one($sql, $type = null)
- {
- if (! $type) {
- $my_type = SQLITE3_ASSOC;
- } else {
- $my_type = $type;
- }
- $row = array();
- $result = $this->query($sql, 'slave');
- if (! ! $row = $result->fetchArray($my_type)) {
- if (! $type && $row) {
- $out = new \stdClass();
- foreach ($row as $key => $value) {
- $out->$key = $value;
- }
- $row = $out;
- }
- $result->finalize();
- }
- return $row;
- }
-
- public function all($sql, $type = null)
- {
- if (! $type) {
- $my_type = SQLITE3_ASSOC;
- } else {
- $my_type = $type;
- }
- $result = $this->query($sql, 'slave');
- $rows = array();
- while (! ! $row = $result->fetchArray($my_type)) {
- if (! $type && $row) {
- $out = new \stdClass();
- foreach ($row as $key => $value) {
- $out->$key = $value;
- }
- $row = $out;
- }
- $rows[] = $row;
- }
- $result->finalize();
- return $rows;
- }
-
- public function amd($sql)
- {
- $result = $this->query($sql, 'master');
- if ($result) {
- return $result;
- } else {
- return 0;
- }
- }
-
- public function insertId()
- {
- return $this->master->lastInsertRowID();
- }
-
- public function multi($sql)
- {
- $sqls = explode(';', $sql);
- foreach ($sqls as $key => $value) {
- $result = $this->query($value, 'master');
- }
- if ($result) {
- return true;
- } else {
- return false;
- }
- }
-
- protected function error($sql, $conn)
- {
- $err = '错误:' . $this->$conn->lastErrorMsg();
- if ($this->begin) {
- $this->master->exec('rollback;');
- $this->begin = false;
- }
-
- error('执行SQL发生错误!' . $err);
- }
- }
|