PHP lightweight database operation components
# RapidDB lightweight database operation component * supports transaction nesting * PDO supports * JSON support * class instance sets support * lightweight * can be easily integrated with other frameworks * Multi-database multi-connection support # Use method ### install "$ composer require shiwolang/db" ### use git address" http://git.oschina.net/shiwolang/RapidDB "### Initializing a connection ** only supports mysql and sqlite ** ##### single connection scenario" phpDB :: init (['database _ type' => 'mysql', 'database _ name' => 'dbname', 'server' => 'localhost ', 'username' => 'username', 'password' => 'yourpass', 'charset' => 'utf8']); "##### multiple connection scenarios" phpDB: init (['database _ type' => 'mysql', 'database _ name' => 'dbname ', 'server' => 'localhost', 'username' => 'username', 'password' => 'yourpass', 'charset' => 'utf8'], "connec Tion1 ");" ##### PDO settings initialize a single connection scenario "phpDB: init ($ PDO ); "##### PDO settings initialize multiple connection scenarios" phpDB: init ($ PDO, "connection1 "); "### obtain database connections ------------------------------------- ##### single connection scenario" phpDB: connection (); "#### multiple connection scenarios" phpDB :: connection ("connection1"); "### add data (single table) ---------------------------------------" php $ lastInsertId = DB: connection ()-> insert ("content ", ["title" => "title1", "content" => "content1", "time" => time ()]); "### Delete data (single table) ---------------------------------------" php $ lastInsertId = DB: connection ()-> delete ("content", "id =: id ", [": id "=> 1]);" ### modify data (single table) ----------------------------------------- "php $ lastInsertId = DB: connection () -> update ("content", ["title" => "title1", "content" => "content1", "time" => time ()], "id =: id", [": id" => 1]); "### query data ----------------------------------------- ** note that when using limit Must be an integer of int type !! ** "PhpDB: connection ()-> query (" SELECT * FROM content where title = 'title1 'limit 10 ")-> all (); DB: connection () -> query ("SELECT * FROM content WHERE title =: title LIMIT: limit", [": title" => "title1", ": limit" => 10]) -> all (); DB: connection ()-> query ("SELECT * FROM content WHERE id =? LIMIT? ", [" Title1 ", 10])-> all ();" #### set the acquisition mode #### set it to the array acquisition mode (default) "phpDB: connection ()-> query (" SELECT * FROM content where title = 'title1 'limit 10 ")-> all (); "##### how to obtain the set of instances for the class" phpDB: connection () -> query ("SELECT * FROM content where title = 'title1 'limit 10")-> bindToClass (Content: class)-> all (); DB: connection () -> query ("SELECT * FROM content where title = 'title1 'limit 10")-> all (Content: class ); "##### The column of the row is passed as a parameter to the specified function, and the method for obtaining the result after the function is called is returned. "phpDB: connection () -> query ("SELECT * FROM content where title = 'title1 'limit 10")-> all (function ($ title, $ content, $ time) {return ["title" => $ title, "content" => $ content, "time" => date ("Y-m-d H: I: s ", $ time)] ;}); "##### obtain the next array in the relevant result set." phpDB: connection () -> query ("SELECT * FROM content LIMIT 10")-> each (function ($ row) {print_r ($ row );}); "Class instance acquisition method" phpDB: conne Ction ()-> query ("SELECT * FROM content LIMIT 10")-> each (function ($ row) {print_r ($ row) ;}, Content: class ); "##### JSON-format data array obtaining method: json" phpDB: connection ()-> query ("SELECT * FROM content LIMIT 10")-> json (); "json format used to obtain the instance set of the class **!! Please note !! DB: json (& $ fetchResult = null, $ className = null, $ args = []) The first parameter is the returned result set, not the bound class name !! ** "PhpDB: connection ()-> query (" SELECT * FROM content LIMIT 10 ")-> json ($ data, Content: className ()); "**!! Note !! ** When the collection method of json data is an object set, automatic data formatting is supported. you can use the @ json annotation to annotate a public method in the class, the corresponding json key name is the first character of the method in lowercase [remove the get character (if any)] method name. you can also implement the ObjectContainerInterface and \ JsonSerializable interfaces and use Statement :: setJsonObjectContainerClassName ($ jsonObjectContainerClassName) perform custom settings for the object collection container ### transaction support --------------------- ** this function depends on the data transaction function ##### use the statement method of transactions "php $ db = DB:: connection (); $ db-> beginTransaction (); try {$ lastInsertId = $ db-> insert ("content ",[" Title "=>" title1 "," content "=>" content1 "," time "=> time ()]); $ db-> commit ();} catch (\ Exception $ e) {$ db-> rollBack (); throw $ e ;} "##### use the statement to support infinitely nested transactions" php $ db = DB: connection (); $ db-> beginTransaction (); try {$ lastInsertId = $ db-> insert ("content", ["title" => "title1", "content" => "content1 ", "time" => time ()]); $ db-> commit ();} catch (\ Exception $ e) {$ db-> rollBack (); throw $ e;} $ last InsertId = $ db-> insert ("content", ["title" => "title2", "content" => "content2 ", "time" => time ()]); $ db-> beginTransaction (); try {$ lastInsertId = $ db-> insert ("content ", ["title" => "title3", "content" => "content3", "time" => time ()]); $ db-> commit ();} catch (\ Exception $ e) {$ db-> rollBack (); throw $ e;} $ db-> commit ();} catch (\ Exception $ e) {$ db-> rollBack (); throw $ e;} "##### the transaction uses the callback function and supports unlimited Hierarchical Nesting "phpDB: connection ()-> transaction (function () {DB: connection () -> insert ("content", ["title" => "title1", "content" => "content1 ", "time" => time ()]) ;}}); DB: connection ()-> insert ("content", ["title" => "title2 ", "content" => "content2", "time" => time ()]); DB: connection ()-> transaction (function () {DB: connection () -> insert ("content", ["title" => "Title3", "content" => "content3", "time" => time ()]) ;}}); "### query execution records --------------------- ##### obtain all execution records" phpDB: connection ()-> query ("SELECT * FROM content LIMIT 1 ") -> all (); DB: connection ()-> query ("SELECT * FROM content WHERE title =: title LIMIT: limit ",[": title "=>" title1 ",": limit "=> 10])-> all (); print_r (DB: connection ()-> getLog ()); "##### single unexecuted SQL" php $ SQL = DB: connection ()-> quer Y ("SELECT * FROM content WHERE title =: title LIMIT: limit", [": title" => "title1", ": limit" => 10], true ); "## Appendix ##### Content class" phpclass Content {private $ id; private $ title; private $ content; /*** @ return mixed */public function getId () {return $ this-> id ;} /*** @ param mixed $ id */public function setId ($ id) {$ this-> id = $ id ;} /*** @ return mixed */public function getTitle () {return $ this-> Title;}/*** @ param mixed $ title */public function setTitle ($ title) {$ this-> title = $ title ;} /*** For json formater * @ json * @ return mixed */public function getContent () {return $ this-> content. "_ nihao";}/*** @ param mixed $ content */public function setContent ($ content) {$ this-> content = $ content ;} public function _ get ($ name) {$ getter = 'get '. self: camelName ($ name); if (method _ Exists ($ this, $ getter) {return $ this-> $ getter ();} throw new \ Exception ('Getting unknown property :'. get_class ($ this ). '::'. $ name);} public function _ set ($ name, $ value) {$ setter = 'set '. self: camelName ($ name); if (method_exists ($ this, $ setter) {$ this-> $ setter ($ value); return ;} throw new \ Exception ('setting unknown property :'. get_class ($ this ). '::'. $ name);} protected static fu Nction camelName ($ name, $ ucfirst = true) {if (strpos ($ name ,"_")! = False) {$ name = str_replace ("_", "", strtolower ($ name); $ name = ucwords ($ name ); $ name = str_replace ("", "", $ name);} return $ ucfirst? Ucfirst ($ name): $ name ;}" ##### database creation statement "sqlCREATE TABLE 'content' ('id' int (11) not null AUTO_INCREMENT, 'title' varchar (255) default null, 'content' varchar (255) default null, 'Time' int (10) default null, primary key ('id ')) ENGINE = InnoDB AUTO_INCREMENT = 1 default charset = utf8"