From 80128932e21e453fadcfb4544c2dbcc6b68402e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Ratay=20/=20=C5=A0imon=20Rataj?= Date: Mon, 2 Aug 2021 16:11:49 +0200 Subject: [PATCH] Update README.md --- README.md | 112 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 69 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index f7264ee..7504d8f 100644 --- a/README.md +++ b/README.md @@ -1,79 +1,81 @@ -# Super MySQL -Super MySQL is an easy-to-use powerful SQL query builder. +# SuperSQL +SuperSQL is an easy-to-use powerful SQL query builder for SQLite and MySQL. ## Requirements * PHP 5.6+ -* MySQL -* PDO MySQL extension +* PDO SQLite / MySQL driver ## Examples Let’s have a table “users” with 5 columns: `uid`, `username`, `password`, `sign_up_time` and `nickname` ```php q($q[, $a]), FETCH_ALL returns an array of rows, FETCH_OBJECT and FETCH_ARRAY return one row per call - $smysql->q("SELECT * FROM users")->fetch(SMQ::FETCH_ALL); + //Connect to SQLite with just the first parameter + $ssql = new Ssql("db.sqlite"); + + //To execute raw SQL query use $ssql->q($q[, $a]), FETCH_ALL returns an array of rows, FETCH_OBJECT and FETCH_ARRAY return one row per call + $ssql->q("SELECT * FROM users")->fetch(SMQ::FETCH_ALL); //You can use wildcards for escaping - $smysql->q("SELECT * FROM users WHERE `username`=%0 OR `nickname`=%1", [$name, $nick])->fetch(); + $ssql->q("SELECT * FROM users WHERE `username`=%0 OR `nickname`=%1", [$name, $nick])->fetch(); //You can use queries as methods - $smysql->getUser = "SELECT * FROM users WHERE `username`=%0 OR `nickname`=%1"; - $user = $smysql->getUser($name, $nick)->fetch(); + $ssql->getUser = "SELECT * FROM users WHERE `username`=%0 OR `nickname`=%1"; + $user = $ssql->getUser($name, $nick)->fetch(); - //For simple requests use $smysql->read($table[, $flags]) or $smysql->read($table, $cond[, $flags]) + //For simple requests use $ssql->read($table[, $flags]) or $ssql->read($table, $cond[, $flags]) //Read function uses FETCH_SMART as default (FETCH_OBJECT for one row, FETCH_ALL for more), so you need to use the FETCH_ALL flag to return an array even when there is only one result - $users = $smysql->read("users", SMQ::FETCH_ALL); - $user = $smysql->read("users", ['uid' => $id]); + $users = $ssql->read("users", SMQ::FETCH_ALL); + $user = $ssql->read("users", ['uid' => $id]); //You can use more conditions - $user = $smysql->read("users", ['username' => $name, 'password' => $pass]); + $user = $ssql->read("users", ['username' => $name, 'password' => $pass]); //You can use the COND_OR flag for OR operator instead of AND - $user = $smysql->read("users", ['username' => $name, 'nickname' => $nick], SMQ::FETCH_ALL | SMQ::COND_OR)[0]; + $user = $ssql->read("users", ['username' => $name, 'nickname' => $nick], SMQ::FETCH_ALL | SMQ::COND_OR)[0]; //You can use custom conditions - $users = $smyslq->read("users", "`sign_up_time` > " . bcsub(time(), 3600)); + $users = $ssql->read("users", "`sign_up_time` > " . bcsub(time(), 3600)); //You can use more of them - $users = $smyslq->read("users", ["`sign_up_time` > " . bcsub(time(), 3600), "`nickname` IS NOT NULL"], SMQ::FETCH_ALL); + $users = $ssql->read("users", ["`sign_up_time` > " . bcsub(time(), 3600), "`nickname` IS NOT NULL"], SMQ::FETCH_ALL); - //For more complicated requests use $smysql->select($table[, $order[, $cols[, $limit[, $flags]]]]), you can use array keys for aliases - $users = $smysql->select("users", "sign_up_time", ['id' => "uid", 'name' => "username", "sign_up_time", "nickname"], NULL, SMQ::ORDER_DESC)->fetch(SMQ::FETCH_ALL); + //For more complicated requests use $ssql->select($table[, $order[, $cols[, $limit[, $flags]]]]), you can use array keys for aliases + $users = $ssql->select("users", "sign_up_time", ['id' => "uid", 'name' => "username", "sign_up_time", "nickname"], NULL, SMQ::ORDER_DESC)->fetch(SMQ::FETCH_ALL); - //Or $smysql->selectWhere($table, $cond[, $order[, $cols[, $limit[, $flags]]]]) - $user = $smysql->selectWhere("users", ['uid' => $id], "name", ['id' => "uid", 'name' => "username", "sign_up_time", 'nick' => "nickname"])->fetch(); + //Or $ssql->selectWhere($table, $cond[, $order[, $cols[, $limit[, $flags]]]]) + $user = $ssql->selectWhere("users", ['uid' => $id], "name", ['id' => "uid", 'name' => "username", "sign_up_time", 'nick' => "nickname"])->fetch(); //To quickly display tables use ->dump() - $smysql->select("users")->dump(); + $ssql->select("users")->dump(); - //Insert with $smysql->insert($table, $values[, $flags]) - $smysql->insert("users", [$id, $name, $pass, time(), NULL]); + //Insert with $ssql->insert($table, $values[, $flags]) + $ssql->insert("users", [$id, $name, $pass, time(), NULL]); //You can use array keys as col names - $smysql->insert("users", ['username' => $name, 'password' => $pass, 'sign_up_time' => time()]); + $ssql->insert("users", ['username' => $name, 'password' => $pass, 'sign_up_time' => time()]); //You can use INSERT_RETURN_ID flag for returning the first auto increment col value (depends on the database type) - $id = $smysql->insert("users", ['username' => $name, 'password' => $pass, 'sign_up_time' => time()], SMQ::INSERT_RETURN_ID); + $id = $ssql->insert("users", ['username' => $name, 'password' => $pass, 'sign_up_time' => time()], SMQ::INSERT_RETURN_ID); - //Use $smysql->update($table, $cond, $values[, $flags]) to update rows - $smysql->update("users", ['id' => $id], ['nickname' => $nick]); + //Use $ssql->update($table, $cond, $values[, $flags]) to update rows + $ssql->update("users", ['id' => $id], ['nickname' => $nick]); - //You can delete rows with $smysql->delete($table, $cond[, $flags]) - $smysql->delete("users", ['id' => $id]); + //You can delete rows with $ssql->delete($table, $cond[, $flags]) + $ssql->delete("users", ['id' => $id]); - //Use $smysql->truncate($table[, $flags]) to delete all rows in a table - $smysql->truncate("users"); + //Use $ssql->truncate($table[, $flags]) to delete all rows in a table + $ssql->truncate("users"); ?> ``` ## Flags -Here is list of all Super-MySQL flags: +Here is list of all SuperSQL flags: * ORDER_ASC * ORDER_DESC * JOIN_INNER @@ -93,27 +95,51 @@ Here is list of all Super-MySQL flags: ```php changeDb("newDB"); + $ssql->changeDb("newDB"); //Join - //Use $smysql->selectJoin($table, $join, $on[, $order[, $cols[, $limit[, $flags]]]]) to execute a JOIN command - $result = $smysql->selectJoin("users", "messages", ['from_user' => 'uid'], "time", "*", 5, SMQ::ORDER_DESC)->fetch(SMQ::FETCH_ALL); + //Use $ssql->selectJoin($table, $join, $on[, $order[, $cols[, $limit[, $flags]]]]) to execute a JOIN command + $result = $ssql->selectJoin("users", "messages", ['from_user' => 'uid'], "time", "*", 5, SMQ::ORDER_DESC)->fetch(SMQ::FETCH_ALL); //Use JOIN_LEFT, JOIN_RIGHT and JOIN_FULL flags to other types of JOIN - //To combine JOIN and WHERE use $smysql->selectJoinWhere($table, $join, $on, $cond[, $order[, $cols[, $limit[, $flags]]]]) - $result = $smysql->selectJoinWhere("users", "messages", ['from_user' => 'uid'], ['from_user' => $uid])->fetch(SMQ::FETCH_ALL); + //To combine JOIN and WHERE use $ssql->selectJoinWhere($table, $join, $on, $cond[, $order[, $cols[, $limit[, $flags]]]]) + $result = $ssql->selectJoinWhere("users", "messages", ['from_user' => 'uid'], ['from_user' => $uid])->fetch(SMQ::FETCH_ALL); + + + + //Table creation and deletion + + + //For basic table creation use $ssql->createTable($table, $params[, $primary[, $flags]]) + $ssql->createTable("myTable", [ + 'number' => [ // Column name + 'type' => "int", // Column type + 'NULL' => false // NULL + ], + 'text' => [ + 'type' => "varchar", + 'length' => 64, // Max length + 'NULL' => true + ] + ], "number"); + + //Use $ssql->deleteTable($table[, $flags]) to delete a table + $c->deleteTable("myTable"); + + //To see a list of all tables in your database use $ssql->tableList([$flags]) + $c->tableList(); ?> ```