php pdoカプセル化クラスコード(トランザクションをサポート)

WBOY
リリース: 2016-07-25 08:51:50
オリジナル
1843 人が閲覧しました
  1. /**
  2. * PDO データベース
  3. * @copyright By GOOGLE
  4. */
  5. class pdo_db
  6. {
  7. /**
  8. * PDO インスタンス
  9. * @var PDO
  10. */
  11. protected $_db;
  12. /**
  13. * PDO 準備済みステートメント
  14. * @var PDOStatement
  15. */
  16. protected $_stmt ;
  17. /**
  18. * 最後の SQL ステートメント
  19. * @var string
  20. */
  21. protected $_sql;
  22. /**
  23. * 構成情報 $config=array('dsn'=>xxx,'name'=>xxx,'password'=>xxx,'option'=>xxx)
  24. * @var array
  25. */
  26. protected $_config;
  27. /**
  28. * コンストラクター
  29. * @param array $config
  30. */
  31. public function __construct($config)
  32. {
  33. $ this->_config = $config;
  34. }
  35. /**
  36. * データベースに接続します
  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. //默认握结果序列化成stdClass
  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. //自己写代捕获Exception
  46. $this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
  47. }
  48. /**
  49. * 切断
  50. * @return void
  51. */
  52. public function disConnect()
  53. {
  54. $this-> ;_db = null;
  55. $this->_stmt = null;
  56. }
  57. /**
  58. * SQLを実行し、新しく追加されたIDを返します
  59. * @param string $statement
  60. * @return string
  61. */
  62. public function exec($statement)
  63. {
  64. if ($this->gt;_db->e​​xec( $statement)){
  65. $this->_sql = $statement;
  66. return $this->lastId();
  67. }
  68. $this->errorMessage();
  69. }
  70. /**
  71. * クエリ SQL
  72. * @param string $statement
  73. * @return pdo_db
  74. */
  75. パブリック関数クエリ($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. * データを一度シリアル化します
  87. * @returnmixed
  88. */
  89. public function fetchOne()
  90. {
  91. return $this-> ;_stmt->fetch();
  92. }
  93. /**
  94. * すべてのデータをシリアル化します
  95. * @return array
  96. */
  97. public function fetchAll()
  98. {
  99. return $this->_stmt->fetchAll();
  100. }
  101. /**
  102. * 最後に追加された ID
  103. * @return string
  104. */
  105. public function lastId()
  106. {
  107. return $this->_db->lastInsertId();
  108. }
  109. /**
  110. * 影響を受ける行数
  111. * @return int
  112. */
  113. public functionaffectRows()
  114. {
  115. return $this ->_stmt->rowCount();
  116. }
  117. /**
  118. * プリペアドステートメント
  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->gt;_sql = $statement;
  128. return $this;
  129. }
  130. $this->errorMessage();
  131. }
  132. /**
  133. * バインドデータ
  134. * @param array $array
  135. * @return pdo_db
  136. */
  137. public function bindingArray($array)
  138. {
  139. foreach ($array as $k => $v){
  140. if (is_array($v)){
  141. //array の有能な構造 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. * 準備されたステートメントを実行します
  151. * @return bool
  152. */
  153. public functionexecute()
  154. {
  155. if ($this->_stmt->execute()) {
  156. return true;
  157. }
  158. $this->errorMessage();
  159. }
  160. /**
  161. * トランザクションを開始します
  162. * @return bool
  163. */
  164. public function beginTransaction()
  165. {
  166. return $this->_db->beginTransaction() ;
  167. }
  168. /**
  169. * トランザクションを実行します
  170. * @return bool
  171. */
  172. public function commitTransaction()
  173. {
  174. return $this->_db->commit();
  175. }
  176. /**
  177. * ロールバックトランザクション
  178. * @return bool
  179. */
  180. public function rollbackTransaction ()
  181. {
  182. return $this->_db->rollBack();
  183. }
  184. /**
  185. * エラーをスローします
  186. * @throws エラー
  187. * @return void
  188. */
  189. public function errorMessage()
  190. {
  191. return;
  192. $msg = $this-> _db->errorInfo();
  193. throw new Exception('数据库错误:' . $msg[2]);
  194. }
  195. //-------- --
  196. /**
  197. * シングルトン インスタンス
  198. * @var pdo_db
  199. */
  200. protected static $_instance;
  201. /**
  202. * デフォルトデータベース
  203. * @static
  204. * @param array $config
  205. * @return pdo_db
  206. */
  207. public static function インスタンス($config)
  208. {
  209. if (!self::$_instanceinstanceof pdo_db){
  210. self::$_instance = new pdo_db($config);
  211. self::$_instance->connect();
  212. }
  213. return self::$_instance;
  214. }
  215. //-------- --------------
  216. /**
  217. * PDO でサポートされているデータベースを取得します
  218. * @static
  219. * @return array
  220. */
  221. パブリック静的関数 getSupportDriver(){
  222. return PDO::getAvailableDrivers();
  223. }
  224. /**
  225. * データベースのバージョン情報を取得します
  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. }
复制代


ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!