- /**
- * Auther soulence
- * Call a data file
- * Modify 2015/06/12
- */
- Class DBConnect
- {
- Private $ dbname = null;
- Private $ pdo = null;
- Private $ persistent = false;
- Private $ statement = null;
- Private $ lastInsID = null;
- Private static $ _ instance = [];
-
- Private function _ construct ($ dbname, $ attr)
- {
- $ This-> dbname = $ dbname;
- $ This-> persistent = $ attr;
- }
-
- Public static function db ($ flag = 'R', $ persistent = false)
- {
- If (! Isset ($ flag )){
- $ Flag = 'R ';
- }
-
- If (! Class_exists ('pdo '))
- {
- Throw new Exception ('not found pdo ');
- Return false;
- }
- $ Mysql_server = Yaf_Registry: get ('mysql ');
- If (! Isset ($ mysql_server [$ flag]) {
- Return false;
- }
-
- $ Options_arr = array (PDO: MYSQL_ATTR_INIT_COMMAND => 'set names'. $ mysql_server [$ flag] ['charset'], PDO: Signature => PDO: FETCH_ASSOC );
- If ($ persistent === true ){
- $ Options_arr [PDO: ATTR_PERSISTENT] = true;
- }
-
- Try {
- $ Pdo = new PDO ($ mysql_server [$ flag] ['ononstring'], $ mysql_server [$ flag] ['username'], $ mysql_server [$ flag] ['password'], $ options_arr );
- } Catch (PDOException $ e ){
- Throw new Exception ($ e-> getMessage ());
- // Exit ('connection failed: '. $ e-> getMessage ());
- Return false;
- }
-
- If (! $ Pdo ){
- Throw new Exception ('pdo CONNECT error ');
- Return false;
- }
-
- Return $ pdo;
- }
-
- /**
- * Get the operation database object
- * @ Param string $ Who is the database corresponding to dbname?
- * @ Param bool $ whether the attr is a persistent connection
- * Return false indicates that the specified database does not exist.
- */
- Public static function getInstance ($ dbname = 'R', $ attr = false)
- {
- $ Mysql_server = Yaf_Registry: get ('mysql ');
- If (! Isset ($ mysql_server [$ dbname]) {
- Return false;
- }
- $ Key = md5 (md5 ($ dbname. $ attr, true ));
- If (! Isset (self ::$ _ instance [$ key]) |! Is_object (self ::$ _ instance [$ key])
- Self: $ _ instance [$ key] = new self ($ dbname, $ attr );
- Return self: $ _ instance [$ key];
- }
-
- Private function getConnect (){
- $ This-> pdo = self: db ($ this-> dbname, $ this-> persistent );
- }
-
- /**
- * Query operations
- * @ Param string $ SQL statement used to execute the query
- * @ Param array $ the condition format of data query is [': ID' => $ id,': name' => $ name] (recommended) or [1 => $ id, 2 => $ name]
- * @ Param bool $ one: whether to return a message. the default value is No.
- */
- Public function query ($ SQL, $ data = [], $ one = false)
- {
- If (! Is_array ($ data) | empty ($ SQL) |! Is_string ($ SQL ))
- Return false;
-
- $ This-> free ();
-
- Return $ this-> queryCommon ($ data, $ SQL, $ one );
- }
-
- /**
- * Shared methods for internal queries
- */
- Private function queryCommon ($ data, $ SQL, $ one)
- {
- $ This-> pdoExec ($ data, $ SQL );
-
- If ($ one ){
- Return $ this-> statement-> fetch (PDO: FETCH_ASSOC );
- } Else {
- Return $ this-> statement-> fetchAll (PDO: FETCH_ASSOC );
- }
- }
-
- /**
- * Query operations for multiple SQL statements
- * @ Param array $ arr_ SQL the array format of the SQL statement for query execution is [$ sql1, $ sql2]
- * @ Param array $ arr_data the condition format corresponding to $ arr_ SQL is [[': ID' => $ id,': name' => $ name], [': ID' => $ id, ': name' => $ name] (recommended) or [[1 => $ id, 2 => $ name], [1 => $ id, 2 => $ name]
- * @ Param bool $ one: whether to return a piece of content; default value: No. if it is set to true, only one data record is returned for each SQL statement.
- */
- Public function queryes ($ arr_ SQL, $ arr_data = [], $ one = false)
- {
- If (! Is_array ($ arr_ SQL) | empty ($ arr_ SQL) |! Is_array ($ arr_data ))
- Return false;
-
- $ This-> free ();
-
- $ Res = []; $ I = 0;
- Foreach ($ arr_ SQL as $ val ){
- If (! Isset ($ arr_data [$ I])
- $ Arr_data [$ I] = [];
- Elseif (! Is_array ($ arr_data [$ I])
- Throw new Exception ('error where queryes SQL: '. $ val. 'where:'. $ arr_data [$ I]);
-
- $ Res [] = $ this-> queryCommon ($ arr_data [$ I], $ val, $ one );
- $ I ++;
- }
- Return $ res;
- }
-
- /**
- * Paging encapsulation
- *
- * @ Param string $ SQL
- * @ Param int $ page indicates the number of pages
- * @ Param int $ pageSize indicates the number of entries per page.
- * @ Param array $ conditions for data query
- */
- Public function limitQuery ($ SQL, $ page = 0, $ pageSize = 20, $ data = [])
- {
- $ Page = intval ($ page );
- If ($ page <0 ){
- Return [];
- }
- $ PageSize = intval ($ pageSize );
- If ($ pageSize> 0) {// if pageSize is 0, all data is retrieved.
- $ SQL. = 'limit'. $ pageSize;
- If ($ page> 0 ){
- $ Start_limit = ($ page-1) * $ pageSize;
- $ SQL. = 'Offset'. $ start_limit;
- }
- }
- Return $ this-> query ($ SQL, $ data );
- }
-
- /**
- * This operation is used to add, delete, modify, and use transaction operations.
- * @ Param string $ SQL statement used to execute the query
- * @ Param array $ the condition format of data query is [': ID' => $ id,': name' => $ name] (recommended) or [1 => $ id, 2 => $ name]
- * @ Param bool $ whether the Transaction operation defaults to no
- */
- Public function executeDDL ($ SQL, $ data = [], $ Transaction = false ){
- If (! Is_array ($ data) |! Is_string ($ SQL ))
- Return false;
-
- $ This-> free ();
-
- If ($ Transaction)
- $ This-> pdo-> beginTransaction (); // start the transaction
- Try {
- $ This-> execRes ($ data, $ SQL );
- If ($ Transaction)
- $ This-> pdo-> commit (); // transaction commit
- Return $ this-> lastInsID;
- } Catch (Exception $ e ){
- If ($ Transaction)
- $ This-> pdo-> rollBack (); // transaction rollBack
- Throw new Exception ('error DDLExecute <===> '. $ e-> getMessage ());
- Return false;
- }
- }
-
- /**
- * This operation is used to add, delete, modify, and use transaction operations.
- * It executes multiple entries.
- * @ Param array $ array of SQL statements to be operated by arr_ SQL
- * @ Param array $ conditions of the SQL statement corresponding to the array and arr_data
- * @ Param bool $ whether the Transaction operation defaults to no
- */
- Public function executeDDLes ($ arr_ SQL, $ arr_data = [], $ Transaction = false ){
-
- If (! Is_array ($ arr_ SQL) | empty ($ arr_ SQL) |! Is_array ($ arr_data ))
- Return false;
-
- $ Res = [];
-
- $ This-> free ();
-
- If ($ Transaction)
- $ This-> pdo-> beginTransaction (); // start the transaction
- Try {
- $ I = 0;
- Foreach ($ arr_ SQL as $ val ){
- If (! Isset ($ arr_data [$ I])
- $ Arr_data [$ I] = [];
- Elseif (! Is_array ($ arr_data [$ I]) {
- If ($ Transaction)
- $ This-> pdo-> rollBack (); // transaction rollBack
- Throw new Exception ('error where DDLExecutees SQL: '. $ val. 'where:'. $ arr_data [$ I]);
- }
-
- $ This-> execRes ($ arr_data [$ I], $ val );
- $ Res [] = $ this-> lastInsID;
- $ I ++;
- }
-
- If ($ Transaction)
- $ This-> pdo-> commit (); // transaction commit
-
- Return $ res;
- } Catch (Exception $ e ){
- If ($ Transaction)
- $ This-> pdo-> rollBack (); // transaction rollBack
- Throw new Exception ('error DDLExecutees array_ SQL: '. json_encode ($ arr_ SQL).' <===> '. $ e-> getMessage ());
- Return false;
- }
- Return $ res;
- }
-
- /**
- * This method is used to calculate the number of items returned by the query. Note that it only supports the select count (*) from table... or select count (0) from table... method.
- * @ Param string $ SQL query SQL statement
- * @ Param array $ conditions of data SQL statements
- */
- Public function countRows ($ SQL, $ data = []) {
- If (! Is_array ($ data) | empty ($ SQL) |! Is_string ($ SQL ))
- Return false;
- $ This-> free ();
-
- $ Res = $ this-> pdoExec ($ data, $ SQL );
-
- If ($ res = false)
- Return false;
-
- Return $ this-> statement-> fetchColumn ();
- }
-
- /**
- * This method is used to calculate the number of items returned by a query. it executes multiple SQL statements.
- * @ Param string $ SQL query SQL statement
- * @ Param array $ conditions of data SQL statements
- */
- Public function countRowses ($ arr_ SQL, $ arr_data = []) {
-
- If (! Is_array ($ arr_ SQL) | empty ($ arr_ SQL) |! Is_array ($ arr_data ))
- Return false;
-
- $ Res = [];
-
- $ This-> free ();
- $ I = 0;
- Foreach ($ arr_ SQL as $ val ){
- If (! Isset ($ arr_data [$ I])
- $ Arr_data [$ I] = [];
- Elseif (! Is_array ($ arr_data [$ I])
- Throw new Exception ('error where CountRowses SQL: '. $ val. 'where:'. $ arr_data [$ I]);
-
- $ Res1 = $ this-> pdoExec ($ arr_data [$ I], $ val );
-
- If ($ res1 = false)
- $ Res [] = false;
- Else
- $ Res [] = $ this-> statement-> fetchColumn ();
- }
-
- Return $ res;
- }
-
- /**
- * Here we provide another method. because there are many projects that need to be provided to start transactions and then perform the final commit operation.
- * @ Param bool $ whether the Transaction operation defaults to no
- */
- Public function getDB ($ Transaction = false)
- {
- $ This-> Transaction = $ Transaction;
- $ This-> getConnect ();
- If ($ Transaction = true)
- $ This-> pdo-> beginTransaction (); // start the transaction
- Return $ this;
- }
-
- /**
- * This method can be executed multiple times. it executes DDL statements.
- * Note that it must be used together with getDB and sQCommit and cannot be used separately.
- * If the transaction sQCommit method is not enabled, you can not call it.
- * @ Param string $ SQL query SQL statement
- * @ Param array $ conditions of data SQL statements
- */
- Public function execSq ($ SQL, $ data = [])
- {
- If ($ this-> checkParams ($ SQL, $ data) === false)
- Return false;
-
- Try {
- $ This-> execRes ($ data, $ SQL );
- Return $ this-> lastInsID;
- } Catch (Exception $ e ){
- If (isset ($ this-> Transaction) & $ this-> Transaction = true)
- $ This-> pdo-> rollBack (); // transaction rollBack
- Throw new Exception ('error execSq <===> '. $ e-> getMessage ());
- Return false;
- } Finally {
- If (! Empty ($ this-> statement ))
- {
- $ This-> statement-> closeCursor ();
- Unset ($ this-> statement );
- }
- }
- }
-
- /**
- * To execute the query method, you need to upload a database connection object.
- * @ Param string $ SQL statement used to execute the query
- * @ Param array $ the condition format of data query is [': ID' => $ id,': name' => $ name] (recommended) or [1 => $ id, 2 => $ name]
- * @ Param bool $ one: whether to return a message. the default value is No.
- */
- Public function querySq ($ SQL, $ data = [], $ one = false)
- {
- If ($ this-> checkParams ($ SQL, $ data) === false)
- Return false;
-
- Return $ this-> pdoExecSq ($ SQL, $ data, [1, $ one]);
- }
-
- /**
- * Paging encapsulation
- *
- * @ Param string $ SQL
- * @ Param int $ page indicates the number of pages
- * @ Param int $ pageSize indicates the number of entries per page.
- * @ Param array $ conditions for data query
- */
- Public function limitQuerySq ($ SQL, $ page = 0, $ pageSize = 20, $ data = [])
- {
- $ Page = intval ($ page );
- If ($ page <0 ){
- Return [];
- }
- $ PageSize = intval ($ pageSize );
- If ($ pageSize> 0) {// if pageSize is 0, all data is retrieved.
- $ SQL. = 'limit'. $ pageSize;
- If ($ page> 0 ){
- $ Start_limit = ($ page-1) * $ pageSize;
- $ SQL. = 'Offset'. $ start_limit;
- }
- }
- Return $ this-> querySq ($ SQL, $ data );
- }
-
- /**
- * This method is used to calculate the number of items returned by the query. Note that it only supports the select count (*) from table... or select count (0) from table... method.
- * @ Param string $ SQL query SQL statement
- * @ Param array $ conditions of data SQL statements
- */
- Public function countRowsSq ($ SQL, $ data = []) {
- If ($ this-> checkParams ($ SQL, $ data) === false)
- Return false;
- Return $ this-> pdoExecSq ($ SQL, $ data, [2]);
- }
-
- /**
- * Here we provide another method. this is the final commit operation. if the transaction is not enabled, this method can not be called at the end.
- */
- Public function sQCommit ()
- {
- If (empty ($ this-> pdo) |! Is_object ($ this-> pdo ))
- Return false;
- If (isset ($ this-> Transaction) & $ this-> Transaction = true)
- $ This-> pdo-> commit (); // submit the transaction
- Unset ($ this-> pdo );
- }
-
- /**
- * Internal call method
- */
- Public function checkParams ($ SQL, $ data)
- {
- If (empty ($ this-> pdo) |! Is_object ($ this-> pdo) |! Is_array ($ data) | empty ($ SQL) |! Is_string ($ SQL ))
- Return false;
-
- Return true;
- }
-
- /**
- * Internal call method
- */
- Private function pdoExecSq ($ SQL, $ data, $ select = []) {
- Try {
- $ Res = $ this-> pdoExec ($ data, $ SQL );
- If (empty ($ select ))
- Return $ res;
- Else {
- If ($ select [0] === 1 ){
- If ($ select [1] === true)
- Return $ this-> statement-> fetch (PDO: FETCH_ASSOC );
- Else
- Return $ this-> statement-> fetchAll (PDO: FETCH_ASSOC );
- } Elseif ($ select [0] === 2)
- Return $ this-> statement-> fetchColumn ();
- Else
- Return false;
- }
- } Catch (Exception $ e ){
- Throw new Exception ($ e-> getMessage ());
- Return false;
- } Finally {
- If (! Empty ($ this-> statement ))
- {
- $ This-> statement-> closeCursor ();
- Unset ($ this-> statement );
- }
- }
- }
-
- /**
- * Internal call method
- */
- Private function execRes ($ data, $ SQL ){
-
- $ Res = $ this-> pdoExec ($ data, $ SQL );
-
- $ In_id = $ this-> pdo-> lastInsertId ();
-
- If (preg_match ("/^ \ s * (INSERT \ s + INTO | REPLACE \ s + INTO) \ s +/I", $ SQL )&&! Empty ($ in_id ))
- $ This-> lastInsID = $ in_id;
- Else
- $ This-> lastInsID = $ res;
- }
-
- /**
- * The internal call method is used to directly execute SQL statements.
- */
- Private function pdoExec ($ data, $ SQL ){
- $ This-> statement = $ this-> pdo-> prepare ($ SQL );
- If (false ===$ this-> statement)
- Return false;
- If (! Empty ($ data ))
- {
- Foreach ($ data as $ k => $ v)
- {
- $ This-> statement-> bindValue ($ k, $ v );
- }
- }
- $ Res = $ this-> statement-> execute ();
- If (! $ Res)
- {
- Throw new Exception ('SQL :'. $ SQL. '<=> where :'. json_encode ($ data ). '<===> error :'. json_encode ($ this-> statement-> errorInfo ()));
- } Else {
- Return $ res;
- }
- }
-
- /**
- * Internal call methods are used to release
- */
- Private function free ()
- {
- If (is_null ($ this-> pdo ))
- $ This-> getConnect ();
-
- If (! Empty ($ this-> statement ))
- {
- $ This-> statement-> closeCursor ();
- $ This-> statement = null;
- }
- }
- }
- ?>
|