Phppdo encapsulation class code (supports transactions)
- /**
- * PDO database
- * @ Copyright By GOOGLE
- */
- Class pdo_db
- {
- /**
- * PDO instance
- * @ Var PDO
- */
- Protected $ _ db;
- /**
- * PDO preparation statement
- * @ Var PDOStatement
- */
- Protected $ _ stmt;
- /**
- * The final SQL statement
- * @ Var string
- */
- Protected $ _ SQL;
- /**
- * Configuration information $ config = array ('dsn '=> xxx, 'name' => xxx, 'password' => xxx, 'option' => xxx)
- * @ Var array
- */
- Protected $ _ config;
- /**
- * Constructor
- * @ Param array $ config
- */
- Public function _ construct ($ config)
- {
- $ This-> _ config = $ config;
- }
- /**
- * Connect to the database
- * @ Return void
- */
- Public function connect ()
- {
- $ This-> _ db = new PDO ($ this-> _ config ['dsn '], $ this-> _ config ['name'], $ this-> _ config ['password'], $ this-> _ config ['option']);
- // Serialize the result to stdClass by default
- // $ This-> _ db-> setAttribute (PDO: ATTR_DEFAULT_FETCH_MODE, PDO: FETCH_OBJ );
- $ This-> _ db-> setAttribute (PDO: ATTR_DEFAULT_FETCH_MODE, PDO: FETCH_ASSOC );
- // Write your own code to catch Exception
- $ This-> _ db-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_SILENT );
- }
- /**
- * Disconnect
- * @ Return void
- */
- Public function disConnect ()
- {
- $ This-> _ db = null;
- $ This-> _ stmt = null;
- }
- /**
- * Execute the SQL statement and return the newly added id.
- * @ Param string $ statement
- * @ Return string
- */
- Public function exec ($ statement)
- {
- If ($ this-> _ db-> exec ($ statement )){
- $ This-> _ SQL = $ statement;
- Return $ this-> lastId ();
- }
- $ This-> errorMessage ();
- }
- /**
- * Querying SQL
- * @ Param string $ statement
- * @ Return pdo_db
- */
- Public function query ($ statement)
- {
- $ Res = $ this-> _ db-> query ($ statement );
- If ($ res ){
- $ This-> _ stmt = $ res;
- $ This-> _ SQL = $ statement;
- Return $ this;
- }
- $ This-> errorMessage ();
- }
- /**
- * Serialize data once
- * @ Return mixed
- */
- Public function fetchOne ()
- {
- Return $ this-> _ stmt-> fetch ();
- }
- /**
- * Serialize all data
- * @ Return array
- */
- Public function fetchAll ()
- {
- Return $ this-> _ stmt-> fetchAll ();
- }
- /**
- * Last added id
- * @ Return string
- */
- Public function lastId ()
- {
- Return $ this-> _ db-> lastInsertId ();
- }
- /**
- * Number of affected rows
- * @ Return int
- */
- Public function affectRows ()
- {
- Return $ this-> _ stmt-> rowCount ();
- }
- /**
- * Prepared statement
- * @ Param string $ statement
- * @ Return pdo_db
- */
- Public function prepare ($ statement)
- {
- $ Res = $ this-> _ db-> prepare ($ statement );
- If ($ res ){
- $ This-> _ stmt = $ res;
- $ This-> _ SQL = $ statement;
- Return $ this;
- }
- $ This-> errorMessage ();
- }
- /**
- * Bind data
- * @ Param array $ array
- * @ Return pdo_db
- */
- Public function bindArray ($ array)
- {
- Foreach ($ array as $ k => $ v ){
- If (is_array ($ v )){
- // Effective structure of array ('value' => xxx, 'type' => PDO: PARAM_XXX)
- $ This-> _ stmt-> bindValue ($ k + 1, $ v ['value'], $ v ['type']);
- } Else {
- $ This-> _ stmt-> bindValue ($ k + 1, $ v, PDO: PARAM_STR );
- }
- }
- Return $ this;
- }
- /**
- * Execute the prepared statement
- * @ Return bool
- */
- Public function execute ()
- {
- If ($ this-> _ stmt-> execute ()){
- Return true;
- }
- $ This-> errorMessage ();
- }
- /**
- * Start a transaction
- * @ Return bool
- */
- Public function beginTransaction ()
- {
- Return $ this-> _ db-> beginTransaction ();
- }
- /**
- * Execute transactions
- * @ Return bool
- */
- Public function commitTransaction ()
- {
- Return $ this-> _ db-> commit ();
- }
- /**
- * Roll back a transaction
- * @ Return bool
- */
- Public function rollbackTransaction ()
- {
- Return $ this-> _ db-> rollBack ();
- }
- /**
- * Throw an error.
- * @ Throws Error
- * @ Return void
- */
- Public function errorMessage ()
- {
- Return;
- $ Msg = $ this-> _ db-> errorInfo ();
- Throw new Exception ('database error: '. $ msg [2]);
- }
- //---------------------
- /**
- * Singleton instance
- * @ Var pdo_db
- */
- Protected static $ _ instance;
- /**
- * Default database
- * @ Static
- * @ Param array $ config
- * @ Return pdo_db
- */
- Public static function instance ($ config)
- {
- If (! Self: $ _ instance instanceof pdo_db ){
- Self: $ _ instance = new pdo_db ($ config );
- Self: $ _ instance-> connect ();
- }
- Return self: $ _ instance;
- }
- //----------------------
- /**
- * Obtain databases supported by PDO
- * @ Static
- * @ Return array
- */
- Public static function getSupportDriver (){
- Return PDO: getAvailableDrivers ();
- }
- /**
- * Obtain the database version information.
- * @ Return array
- */
- Public function getDriverVersion (){
- $ Name = $ this-> _ db-> getAttribute (PDO: ATTR_DRIVER_NAME );
- Return array ($ name => $ this-> _ db-> getAttribute (PDO: ATTR_CLIENT_VERSION ));
- }
- }
|