Phppdo encapsulation class code (supports transactions)

Source: Internet
Author: User
Phppdo encapsulation class code (supports transactions)

  1. /**
  2. * PDO database
  3. * @ Copyright By GOOGLE
  4. */
  5. Class pdo_db
  6. {
  7. /**
  8. * PDO instance
  9. * @ Var PDO
  10. */
  11. Protected $ _ db;
  12. /**
  13. * PDO preparation statement
  14. * @ Var PDOStatement
  15. */
  16. Protected $ _ stmt;
  17. /**
  18. * The final SQL statement
  19. * @ Var string
  20. */
  21. Protected $ _ SQL;
  22. /**
  23. * Configuration information $ config = array ('dsn '=> xxx, 'name' => xxx, 'password' => xxx, 'option' => xxx)
  24. * @ Var array
  25. */
  26. Protected $ _ config;
  27. /**
  28. * Constructor
  29. * @ Param array $ config
  30. */
  31. Public function _ construct ($ config)
  32. {
  33. $ This-> _ config = $ config;
  34. }
  35. /**
  36. * Connect to the database
  37. * @ Return void
  38. */
  39. Public function connect ()
  40. {
  41. $ This-> _ db = new PDO ($ this-> _ config ['dsn '], $ this-> _ config ['name'], $ this-> _ config ['password'], $ this-> _ config ['option']);
  42. // Serialize the result to stdClass by default
  43. // $ This-> _ db-> setAttribute (PDO: ATTR_DEFAULT_FETCH_MODE, PDO: FETCH_OBJ );
  44. $ This-> _ db-> setAttribute (PDO: ATTR_DEFAULT_FETCH_MODE, PDO: FETCH_ASSOC );
  45. // Write your own code to catch Exception
  46. $ This-> _ db-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_SILENT );
  47. }
  48. /**
  49. * Disconnect
  50. * @ Return void
  51. */
  52. Public function disConnect ()
  53. {
  54. $ This-> _ db = null;
  55. $ This-> _ stmt = null;
  56. }
  57. /**
  58. * Execute the SQL statement and return the newly added id.
  59. * @ Param string $ statement
  60. * @ Return string
  61. */
  62. Public function exec ($ statement)
  63. {
  64. If ($ this-> _ db-> exec ($ statement )){
  65. $ This-> _ SQL = $ statement;
  66. Return $ this-> lastId ();
  67. }
  68. $ This-> errorMessage ();
  69. }
  70. /**
  71. * Querying SQL
  72. * @ Param string $ statement
  73. * @ Return pdo_db
  74. */
  75. Public function query ($ statement)
  76. {
  77. $ Res = $ this-> _ db-> query ($ statement );
  78. If ($ res ){
  79. $ This-> _ stmt = $ res;
  80. $ This-> _ SQL = $ statement;
  81. Return $ this;
  82. }
  83. $ This-> errorMessage ();
  84. }
  85. /**
  86. * Serialize data once
  87. * @ Return mixed
  88. */
  89. Public function fetchOne ()
  90. {
  91. Return $ this-> _ stmt-> fetch ();
  92. }
  93. /**
  94. * Serialize all data
  95. * @ Return array
  96. */
  97. Public function fetchAll ()
  98. {
  99. Return $ this-> _ stmt-> fetchAll ();
  100. }
  101. /**
  102. * Last added id
  103. * @ Return string
  104. */
  105. Public function lastId ()
  106. {
  107. Return $ this-> _ db-> lastInsertId ();
  108. }
  109. /**
  110. * Number of affected rows
  111. * @ Return int
  112. */
  113. Public function affectRows ()
  114. {
  115. Return $ this-> _ stmt-> rowCount ();
  116. }
  117. /**
  118. * Prepared statement
  119. * @ Param string $ statement
  120. * @ Return pdo_db
  121. */
  122. Public function prepare ($ statement)
  123. {
  124. $ Res = $ this-> _ db-> prepare ($ statement );
  125. If ($ res ){
  126. $ This-> _ stmt = $ res;
  127. $ This-> _ SQL = $ statement;
  128. Return $ this;
  129. }
  130. $ This-> errorMessage ();
  131. }
  132. /**
  133. * Bind data
  134. * @ Param array $ array
  135. * @ Return pdo_db
  136. */
  137. Public function bindArray ($ array)
  138. {
  139. Foreach ($ array as $ k => $ v ){
  140. If (is_array ($ v )){
  141. // Effective structure of array ('value' => xxx, 'type' => PDO: PARAM_XXX)
  142. $ This-> _ stmt-> bindValue ($ k + 1, $ v ['value'], $ v ['type']);
  143. } Else {
  144. $ This-> _ stmt-> bindValue ($ k + 1, $ v, PDO: PARAM_STR );
  145. }
  146. }
  147. Return $ this;
  148. }
  149. /**
  150. * Execute the prepared statement
  151. * @ Return bool
  152. */
  153. Public function execute ()
  154. {
  155. If ($ this-> _ stmt-> execute ()){
  156. Return true;
  157. }
  158. $ This-> errorMessage ();
  159. }
  160. /**
  161. * Start a transaction
  162. * @ Return bool
  163. */
  164. Public function beginTransaction ()
  165. {
  166. Return $ this-> _ db-> beginTransaction ();
  167. }
  168. /**
  169. * Execute transactions
  170. * @ Return bool
  171. */
  172. Public function commitTransaction ()
  173. {
  174. Return $ this-> _ db-> commit ();
  175. }
  176. /**
  177. * Roll back a transaction
  178. * @ Return bool
  179. */
  180. Public function rollbackTransaction ()
  181. {
  182. Return $ this-> _ db-> rollBack ();
  183. }
  184. /**
  185. * Throw an error.
  186. * @ Throws Error
  187. * @ Return void
  188. */
  189. Public function errorMessage ()
  190. {
  191. Return;
  192. $ Msg = $ this-> _ db-> errorInfo ();
  193. Throw new Exception ('database error: '. $ msg [2]);
  194. }
  195. //---------------------
  196. /**
  197. * Singleton instance
  198. * @ Var pdo_db
  199. */
  200. Protected static $ _ instance;
  201. /**
  202. * Default database
  203. * @ Static
  204. * @ Param array $ config
  205. * @ Return pdo_db
  206. */
  207. Public static function instance ($ config)
  208. {
  209. If (! Self: $ _ instance instanceof pdo_db ){
  210. Self: $ _ instance = new pdo_db ($ config );
  211. Self: $ _ instance-> connect ();
  212. }
  213. Return self: $ _ instance;
  214. }
  215. //----------------------
  216. /**
  217. * Obtain databases supported by PDO
  218. * @ Static
  219. * @ Return array
  220. */
  221. Public static function getSupportDriver (){
  222. Return PDO: getAvailableDrivers ();
  223. }
  224. /**
  225. * Obtain the database version information.
  226. * @ Return array
  227. */
  228. Public function getDriverVersion (){
  229. $ Name = $ this-> _ db-> getAttribute (PDO: ATTR_DRIVER_NAME );
  230. Return array ($ name => $ this-> _ db-> getAttribute (PDO: ATTR_CLIENT_VERSION ));
  231. }
  232. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.