A lightweight PHP database Toolkit, a lightweight PHP database toolkit, was born for two days (proven to be light ).
Two classes: one Connection to manage the PDO Connection (supporting multiple databases) and one QuickQuery to quickly perform database operations (no need to go back and forth between PDO and PDOStatement)
You don't need to download it. you can view it online.
1. [Code]Example
Use Persistence \ DbAccess; // add the Connection information DbAccess \ Connection: add ('default', 'sqlite ','/db/mydb. sqlite ', null, null, false); // The following uses $ conn = DbAccess \ Connection: instance ('default '); // query $ query = new DbAccess \ QuickQuery ($ conn); $ query-> prepare ('select: name as name')-> execute (array (': name' => 'tonyseek '); // The object is used as an iterator to generate a data array foreach ($ query as $ I) {var_dump ($ I );} // If you are paranoid, manually disconnect DbAccess \ Connection: disconnectAll ();
2. [Code]Persistence/DbAccess/QuickQuery. php
Connection = $ connection ;} /*** pre-compiled SQL statement ** @ param string $ sqlCommand SQL statement * @ return \ Persistence \ DbAccess \ QuickQuery */public function prepare ($ sqlCommand) {// Obtain PDO from the connection and execute prepare on the SQL Statement to generate PDO Statement $ this-> stmt = $ this-> connection-> getPDO () -> prepare ($ sqlCommand); // modify the PDO Statement mode to associate array data $ this-> stmt-> setFetchMode (PDO: FETCH_ASSOC ); // return method chain return $ this;}/*** execute data query **@ Throws PDOException * @ return \ Persistence \ DbAccess \ QuickQuery */public function execute ($ params = array () {$ stmt = $ this-> getStatement (); // parameter check $ diff = array_diff ($ this-> checkedParams, array_keys ($ params); if (count ($ this-> checkedParams) & count ($ diff )) {throw new InvalidArgumentException ('required parameter missing :'. implode ('|', $ diff);} // maps the PHP Data type to the database data type foreach ($ params as $ key => $ value) {$ typ E = null; switch (true) {case is_int ($ value): $ type = PDO: PARAM_INT; break; case is_bool ($ value): $ type = PDO :: PARAM_BOOL; break; case ($ value instanceof DateTime): $ type = PDO: PARAM_STR; $ value = $ value-> format (\ DateTime: W3C); break; case is_null ($ value): $ type = PDO: PARAM_NULL; break; default: $ type = PDO: PARAM_STR ;}$ stmt-> bindValue ($ key, $ value, $ type) ;}$ stmt-> execute (); $ this-> checked Params = array (); // clear the parameter check return $ this;}/*** get Statement object ** the returned object can be re-executed by the bound parameter, it can also be used as an iterator to traverse and obtain data. ** @ Return \ PDOStatement */public function getStatement () {if (! $ This-> stmt) {throw new LogicException ('SQL statement should be first QuickQuery. prepare preprocessing ');} return $ this-> stmt;}/*** substitute name of the getStatement method ** implement the IteratorAggregate interface in the PHP Standard Library, this object can be directly used as an iterator for * calendar. The only difference from getStatment is that this method does not throw a LogicException. If * prepare and execute are not used in advance, an empty iterator is returned. ** @ Return Traversable */public function getIterator () {try {return $ this-> getStatement ();} catch (LogicException $ ex) {return new \ ArrayObject () ;}/ *** set query parameter check ** the checked query parameters imported here. if no value is assigned, logicException is thrown during query. ** @ Param array $ params * @ return \ Persistence \ DbAccess \ QuickQuery */public function setParamsCheck (array $ params) {$ this-> checkedParams = $ params; return $ this;}/*** converts the result set to an array ** @ return array */public function toArray () {return iterator_to_array ($ this-> getStatement ();}/*** get the last insert result (or sequence) ID ** @ param string $ name * @ return int */public function getLastInsertId ($ name = null) {return $ this-> connection-> getPDO () -> lastInsertId ($ name );}}
3. [Code]Persistence/DbAccess/Connection. php
Disconnect ();}} /*** ADD database *** add Connector ** to the instance group @ param string $ name identification name * @ param string $ driver name * @ param string $ dsn connection string * @ param string $ usr database username * @ param string $ pwd database password * @ param bool $ emulate simulation precompiled query * @ param bool $ persisten persistent connection */static public function registry ($ name, $ driver, $ dsn, $ usr, $ pwd, $ emulate = false, $ persisten = false) {if (isset (self ::$ instances [$ name]) {// if the added instance If the instance name already exists, throw the exception throw new BadMethodCallException ("[{$ name}] already registered");} // instantiate itself and push itself into the array :: $ instances [$ name] = new self (); self: $ instances [$ name]-> dsn = $ driver. ':'. $ dsn; self: $ instances [$ name]-> username = $ usr; self: $ instances [$ name]-> password = $ pwd; self :: $ instances [$ name]-> driver = $ driver; self: $ instances [$ name]-> isPersisten = (bool) $ persisten; self :: $ instances [$ name]-> isEmulate = (bool) $ emulate ;} /*** Get PHP Database Object *** @ return \ PDO */public function getPDO () {if (! $ This-> pdo) {// check whether the PDO has been instantiated. Otherwise, the PDO $ this-> pdo = new PDO ($ this-> dsn, $ this-> username, $ this-> password); // The error mode is to throw a PDOException exception $ this-> pdo-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION ); // enable query cache $ this-> pdo-> setAttribute (PDO: ATTR_EMULATE_PREPARES, $ this-> isEmulate ); // enable persistent connection $ this-> pdo-> setAttribute (PDO: ATTR_PERSISTENT, $ this-> isPersisten);} return $ this-> pdo ;} /*** get the database driver name ** @ return string */public function getDriverName () {return $ this-> driver ;} /*** start transaction */public function transationBegin () {$ this-> isInTransation = $ this-> getPDO ()-> beginTransaction ();} /*** submit the transaction */public function transationCommit () {if ($ this-> isInTransation) {$ this-> getPDO ()-> commit ();} else {trigger_error ('transationbegin should be called before transationCommit ');}/*** roll back transaction * @ return bool */public function transationRollback () {if ($ this-> isInTransation) {$ this-> getPDO ()-> rollBack ();} else {trigger_error ('transationbegin should be called before transationrollback ');}} /*** whether the connection is in the transaction ** @ return bool */public function isInTransation () {return $ this-> isInTransation ;} /*** execute the callback function in the transaction ** @ param function $ callback anonymous function or closure function * @ param bool $ whether to automatically roll back when an autoRollback exception occurs * @ throws \ PDOException * @ return bool */public function transationExecute ($ callback, $ autoRollback = true) {try {// start the transaction $ this-> transationBegin (); // call the callback function if (is_callable ($ callback) {$ callback ();} else {throw new InvalidArgumentException ('$ callback should be the callback function');} // submit the transaction return $ this-> transationCommit ();} catch (PDOException $ pex) {// if automatic rollback is enabled, rollback is performed first when a PDO exception is caught and then if ($ autoRollback) {$ this-> transationRollback ();} throw $ pex;}/*** safely disconnect the database */public function disconnect () {if ($ this-> pdo) {// check whether the PDO has been instantiated, yes, it is set to null $ this-> pdo = null;}/*** block clone */public function _ clone () {trigger_error ('blocked _ clone method, ctor is a singleton class ');}}