From 1d37851aacb9c2513402089f10f6bc32db036975 Mon Sep 17 00:00:00 2001 From: JaroslawZielinski Date: Tue, 24 May 2022 09:41:43 +0200 Subject: [PATCH 1/7] Create SQLite3DB.php --- SQLite3DB.php | 265 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 265 insertions(+) create mode 100644 SQLite3DB.php diff --git a/SQLite3DB.php b/SQLite3DB.php new file mode 100644 index 0000000..dcb1b2c --- /dev/null +++ b/SQLite3DB.php @@ -0,0 +1,265 @@ +dbfile)) { + if (empty($dbfile)) { + exit("Please write sqlite database address!"); + } else { + $this->open($dbfile); + } + } else { + $this->open($this->dbfile); + } + if (!empty($prefix)) { + $this->prefix = $prefix; + } + } + + public function create($table, $data) { + if (count($data) == count($data, 1)) { + $id = $this->insert($table, $data); + } else { + $id = $this->insertMulti($table, $data); + } + $this->reset(); + if ($id) { + return $id; + } else { + return $this->lastErrorMsg(); + } + } + + public function update($table, $data, $array = []) { + $set = []; + if (!empty($array)) { + foreach ($array as $k => $v) { + $this->where($k, $v); + } + } + $query = "UPDATE " . $this->prefix . $table . " SET "; + foreach ($data as $k => $v) { + $set[] = "[" . $k . "] = '" . $v . "'"; + } + $query .= implode(", ", $set) . $this->_where() . ";"; + $result = $this->exec($query); + $this->reset(); + return $result; + } + + public function delete($table, $array = []) { + if (!empty($array)) { + foreach ($array as $k => $v) { + $this->where($k, $v); + } + } + $query = "DELETE FROM [" . $this->prefix . $table . "]" . $this->_where() . ";"; + $result = $this->exec($query); + $this->reset(); + return $result; + } + + public function detail($table, $array = []) { + if (!empty($array)) { + foreach ($array as $k => $v) { + $this->where($k, $v); + } + } + if (is_array($table)) { + if ($table["join"]["table"]) { + $this->_db->join($table["join"]["table"], $table["join"]["condition"], $table["join"]["type"]); + } else { + foreach ($table["join"] as $v) { + $this->_db->join($v["table"], $v["condition"], $v["type"]); + } + } + $tables["table"] = $table["table"]; + } else { + $tables["table"] = $table; + } + $result = $this->querySingle($this->_select($tables["table"]) . $this->_join() . $this->_where(), true); + $this->reset(); + return $result; + } + + public function items($table, $limit = null, &$callback = [], $columns = "*") { + if (is_array($table)) { + if ($table["join"]["table"]) { + $this->join($table["join"]["table"], $table["join"]["condition"], $table["join"]["type"]); + } else { + foreach ($table["join"] as $v) { + $this->join($v["table"], $v["condition"], $v["type"]); + } + } + $tables["table"] = $table["table"]; + } else { + $tables["table"] = $table; + } + $this->columns = $columns; + $result = $this->query($this->_select($tables["table"]) . $this->_join() . $this->_where() . $this->_orderby() . $this->_limit($limit)); + $totalCount = $this->querySingle($this->_select($tables["table"], "count(*)") . $this->_join() . $this->_where()); + $this->reset(); + $callback = $this->_pageinfo($totalCount, $limit); + $row = []; + while ($res = $result->fetchArray(SQLITE3_ASSOC)) { + $row[] = $res; + } + return $row; + } + + public function join($table, $condition, $type = "INNER") { + $_join[] = $type . " JOIN " . $table . " ON " . $condition; + } + + public function where($prop, $value = "", $operator = "=", $cond = "AND") { + $where = []; + if (count($this->_where) == 0) { + $cond = ""; + } + $this->_where[] = [$cond, $prop, $operator, $value]; + return $this; + } + + public function orwhere($prop, $value = "", $operator = "=") { + return $this->where($prop, $value, $operator, "OR"); + } + + public function orderby($field, $direction = "DESC") { + $this->_orderby[$field] = $direction; + } + + protected function reset() { + $this->columns = "*"; + $this->_join = []; + $this->_where = []; + $this->_orderby = []; + } + + protected function insert($table, $data, &$error = "") { + $sql = "INSERT INTO [" . $this->prefix . $table . "] "; + $sql .= "(" . implode(", ", array_keys($data)) . ")"; + $sql .= " VALUES "; + $sql .= "('" . implode("', '", array_values($data)) . "');"; + if ($this->exec($sql)) { + return $this->lastInsertRowID(); + } + $error = $this->lastErrorMsg(); + return false; + } + + protected function insertMulti($table, $data) { + $ids = []; + $this->exec("begin;"); + foreach ($data as $v) { + $ids[] = $this->insert($table, $v); + } + $this->exec("commit;"); + return $ids; + } + + protected function _select($table, $columns = "") { + if (empty($columns)) { + $columns = $this->columns; + } + return "SELECT " . $columns . " FROM [" . $this->prefix . $table . "]"; + } + + protected function _join() { + if (empty($this->_join)) { + return; + } + return " " . implode(" ", $this->_join); + } + + protected function _where() { + if (empty($this->_where)) { + return; + } + $build = " WHERE"; + foreach ($this->_where as $cond) { + list($concat, $varName, $operator, $val) = $cond; + $build .= " " . $concat . " " . $varName . " "; + switch (strtolower($operator)) { + case "not in": + case "in": + $build .= $operator . " ('" . implode("', '", $val) . "')"; + break; + case "not between": + case "between": + $build .= $operator . " " . sprintf("%u AND %u", $val[0], $val[1]); + break; + case "not exists": + case "exists": + $build .= $operator . " (" . $val . ")"; + break; + default: + $build .= $operator . " '" . $val . "'"; + } + } + return $build; + } + + protected function _orderby() { + if (empty($this->_orderby)) { + return; + } + $build = " ORDER BY "; + foreach ($this->_orderby as $prop => $value) { + if (strtolower(str_replace(" ", "", $prop)) == "rand()") { + $build .= "rand(), "; + } else { + $build .= $prop . " " . $value . ", "; + } + } + return rtrim($build, ", ") . " "; + } + + protected function _limit($numRows) { + if (empty($numRows)) { + return; + } + if (is_array($numRows)) { + if ($numRows[0] < 1) { + $numRows[0] = 1; + } + return " LIMIT " . (int) $numRows[0] . ", " . (int) $numRows[1]; + } else { + return " LIMIT " . (int) $numRows; + } + } + + protected function _pageinfo($totalCount, $numRows) { + $pageinfo = []; + if (empty($numRows)) { + $pageinfo["page"] = 1; + } else { + if (is_array($limit)) { + $pageinfo["page"] = $numRows[0]; + $pageinfo["pageSize"] = $numRows[1]; + $pageinfo["totalPages"] = ceil($totalCount / $numRows[1]); + } else { + $pageinfo["page"] = 1; + $pageinfo["pageSize"] = $numRows; + $pageinfo["totalPages"] = ceil($totalCount / $numRows); + } + } + $pageinfo["totalCount"] = $totalCount; + return $pageinfo; + } +} From be2fea70e6d58b1a6ec1f12b0eb5f84821d8ce9e Mon Sep 17 00:00:00 2001 From: JaroslawZielinski Date: Tue, 24 May 2022 09:42:02 +0200 Subject: [PATCH 2/7] Delete sqlite3db.php --- sqlite3db.php | 265 -------------------------------------------------- 1 file changed, 265 deletions(-) delete mode 100644 sqlite3db.php diff --git a/sqlite3db.php b/sqlite3db.php deleted file mode 100644 index dcb1b2c..0000000 --- a/sqlite3db.php +++ /dev/null @@ -1,265 +0,0 @@ -dbfile)) { - if (empty($dbfile)) { - exit("Please write sqlite database address!"); - } else { - $this->open($dbfile); - } - } else { - $this->open($this->dbfile); - } - if (!empty($prefix)) { - $this->prefix = $prefix; - } - } - - public function create($table, $data) { - if (count($data) == count($data, 1)) { - $id = $this->insert($table, $data); - } else { - $id = $this->insertMulti($table, $data); - } - $this->reset(); - if ($id) { - return $id; - } else { - return $this->lastErrorMsg(); - } - } - - public function update($table, $data, $array = []) { - $set = []; - if (!empty($array)) { - foreach ($array as $k => $v) { - $this->where($k, $v); - } - } - $query = "UPDATE " . $this->prefix . $table . " SET "; - foreach ($data as $k => $v) { - $set[] = "[" . $k . "] = '" . $v . "'"; - } - $query .= implode(", ", $set) . $this->_where() . ";"; - $result = $this->exec($query); - $this->reset(); - return $result; - } - - public function delete($table, $array = []) { - if (!empty($array)) { - foreach ($array as $k => $v) { - $this->where($k, $v); - } - } - $query = "DELETE FROM [" . $this->prefix . $table . "]" . $this->_where() . ";"; - $result = $this->exec($query); - $this->reset(); - return $result; - } - - public function detail($table, $array = []) { - if (!empty($array)) { - foreach ($array as $k => $v) { - $this->where($k, $v); - } - } - if (is_array($table)) { - if ($table["join"]["table"]) { - $this->_db->join($table["join"]["table"], $table["join"]["condition"], $table["join"]["type"]); - } else { - foreach ($table["join"] as $v) { - $this->_db->join($v["table"], $v["condition"], $v["type"]); - } - } - $tables["table"] = $table["table"]; - } else { - $tables["table"] = $table; - } - $result = $this->querySingle($this->_select($tables["table"]) . $this->_join() . $this->_where(), true); - $this->reset(); - return $result; - } - - public function items($table, $limit = null, &$callback = [], $columns = "*") { - if (is_array($table)) { - if ($table["join"]["table"]) { - $this->join($table["join"]["table"], $table["join"]["condition"], $table["join"]["type"]); - } else { - foreach ($table["join"] as $v) { - $this->join($v["table"], $v["condition"], $v["type"]); - } - } - $tables["table"] = $table["table"]; - } else { - $tables["table"] = $table; - } - $this->columns = $columns; - $result = $this->query($this->_select($tables["table"]) . $this->_join() . $this->_where() . $this->_orderby() . $this->_limit($limit)); - $totalCount = $this->querySingle($this->_select($tables["table"], "count(*)") . $this->_join() . $this->_where()); - $this->reset(); - $callback = $this->_pageinfo($totalCount, $limit); - $row = []; - while ($res = $result->fetchArray(SQLITE3_ASSOC)) { - $row[] = $res; - } - return $row; - } - - public function join($table, $condition, $type = "INNER") { - $_join[] = $type . " JOIN " . $table . " ON " . $condition; - } - - public function where($prop, $value = "", $operator = "=", $cond = "AND") { - $where = []; - if (count($this->_where) == 0) { - $cond = ""; - } - $this->_where[] = [$cond, $prop, $operator, $value]; - return $this; - } - - public function orwhere($prop, $value = "", $operator = "=") { - return $this->where($prop, $value, $operator, "OR"); - } - - public function orderby($field, $direction = "DESC") { - $this->_orderby[$field] = $direction; - } - - protected function reset() { - $this->columns = "*"; - $this->_join = []; - $this->_where = []; - $this->_orderby = []; - } - - protected function insert($table, $data, &$error = "") { - $sql = "INSERT INTO [" . $this->prefix . $table . "] "; - $sql .= "(" . implode(", ", array_keys($data)) . ")"; - $sql .= " VALUES "; - $sql .= "('" . implode("', '", array_values($data)) . "');"; - if ($this->exec($sql)) { - return $this->lastInsertRowID(); - } - $error = $this->lastErrorMsg(); - return false; - } - - protected function insertMulti($table, $data) { - $ids = []; - $this->exec("begin;"); - foreach ($data as $v) { - $ids[] = $this->insert($table, $v); - } - $this->exec("commit;"); - return $ids; - } - - protected function _select($table, $columns = "") { - if (empty($columns)) { - $columns = $this->columns; - } - return "SELECT " . $columns . " FROM [" . $this->prefix . $table . "]"; - } - - protected function _join() { - if (empty($this->_join)) { - return; - } - return " " . implode(" ", $this->_join); - } - - protected function _where() { - if (empty($this->_where)) { - return; - } - $build = " WHERE"; - foreach ($this->_where as $cond) { - list($concat, $varName, $operator, $val) = $cond; - $build .= " " . $concat . " " . $varName . " "; - switch (strtolower($operator)) { - case "not in": - case "in": - $build .= $operator . " ('" . implode("', '", $val) . "')"; - break; - case "not between": - case "between": - $build .= $operator . " " . sprintf("%u AND %u", $val[0], $val[1]); - break; - case "not exists": - case "exists": - $build .= $operator . " (" . $val . ")"; - break; - default: - $build .= $operator . " '" . $val . "'"; - } - } - return $build; - } - - protected function _orderby() { - if (empty($this->_orderby)) { - return; - } - $build = " ORDER BY "; - foreach ($this->_orderby as $prop => $value) { - if (strtolower(str_replace(" ", "", $prop)) == "rand()") { - $build .= "rand(), "; - } else { - $build .= $prop . " " . $value . ", "; - } - } - return rtrim($build, ", ") . " "; - } - - protected function _limit($numRows) { - if (empty($numRows)) { - return; - } - if (is_array($numRows)) { - if ($numRows[0] < 1) { - $numRows[0] = 1; - } - return " LIMIT " . (int) $numRows[0] . ", " . (int) $numRows[1]; - } else { - return " LIMIT " . (int) $numRows; - } - } - - protected function _pageinfo($totalCount, $numRows) { - $pageinfo = []; - if (empty($numRows)) { - $pageinfo["page"] = 1; - } else { - if (is_array($limit)) { - $pageinfo["page"] = $numRows[0]; - $pageinfo["pageSize"] = $numRows[1]; - $pageinfo["totalPages"] = ceil($totalCount / $numRows[1]); - } else { - $pageinfo["page"] = 1; - $pageinfo["pageSize"] = $numRows; - $pageinfo["totalPages"] = ceil($totalCount / $numRows); - } - } - $pageinfo["totalCount"] = $totalCount; - return $pageinfo; - } -} From 7c11340a0f12dfbab0b2b93d2d9e63240904f103 Mon Sep 17 00:00:00 2001 From: JaroslawZielinski Date: Tue, 24 May 2022 09:56:07 +0200 Subject: [PATCH 3/7] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9d0d860..d7cb203 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "tenglin/sqlite3-database-class", + "name": "jaroslawzielinski/sqlite3-database-class", "description": "Wrapper for a PHP sqlite3 class, which utilizes sqlite3 and prepared statements.", "keywords": ["php", "sqlite3", "database"], "authors": [ From 105b904c1243be8f2a204fef4cc5b72ace7603e3 Mon Sep 17 00:00:00 2001 From: JaroslawZielinski Date: Wed, 6 Mar 2024 08:27:24 +0100 Subject: [PATCH 4/7] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 91ea752..53f9ec8 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ ## 通过 `Composer` 安装 ``` -composer require tenglin/sqlite3-database-class +composer require jaroslawzielinski/sqlite3-database-class +in [packagist](https://packagist.org/packages/jaroslawzielinski/sqlite3-database-class) ``` ## 函数说明 From 958e88bd18543a6c8ba0c42c906657e4bccc0969 Mon Sep 17 00:00:00 2001 From: JaroslawZielinski Date: Wed, 6 Mar 2024 08:28:32 +0100 Subject: [PATCH 5/7] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 53f9ec8..8990209 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ ## 通过 `Composer` 安装 ``` composer require jaroslawzielinski/sqlite3-database-class -in [packagist](https://packagist.org/packages/jaroslawzielinski/sqlite3-database-class) ``` +in [packagist](https://packagist.org/packages/jaroslawzielinski/sqlite3-database-class) ## 函数说明 ``` From 8a42f4ca1ed1099e620d8f00b69d6e87fffe8cf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaros=C5=82aw=20Zieli=C5=84ski?= Date: Mon, 23 Sep 2024 06:51:23 +0200 Subject: [PATCH 6/7] New Like Operator --- SQLite3DB.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/SQLite3DB.php b/SQLite3DB.php index dcb1b2c..80017d6 100644 --- a/SQLite3DB.php +++ b/SQLite3DB.php @@ -208,6 +208,9 @@ protected function _where() { case "exists": $build .= $operator . " (" . $val . ")"; break; + case "like": + $build .= $operator . " '" . $val; + break; default: $build .= $operator . " '" . $val . "'"; } From 842a98fc810611f606d36c9cf0b75488d2aa3e7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaros=C5=82aw=20Zieli=C5=84ski?= Date: Mon, 23 Sep 2024 06:58:42 +0200 Subject: [PATCH 7/7] New Operator Like cd.. --- SQLite3DB.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SQLite3DB.php b/SQLite3DB.php index 80017d6..966839c 100644 --- a/SQLite3DB.php +++ b/SQLite3DB.php @@ -209,7 +209,7 @@ protected function _where() { $build .= $operator . " (" . $val . ")"; break; case "like": - $build .= $operator . " '" . $val; + $build .= $operator . " " . $val; break; default: $build .= $operator . " '" . $val . "'";