A simple PDO class package. . For learning and communication only
PdoDb database class
- /**
- * @throws Error
- * PDO database
- */
- class PdoDb extends DatabaseAbstract
- {
- /**
- * PDO instance
- * @var PDO
- */
- protected $DB;
- /**
- * PDO prepared statement
- * @var PDOStatement
- */
- protected $Stmt;
- /**
- * Last 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 database
- * @return void
- */
- public function connect()
- {
- $this->DB = new PDO($this->Config['dsn'], $this->Config['name'], $this->Config['password'], $this->Config['option']);
- //默认把结果序列化成stdClass
- $this->DB->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
- //自己写代码捕获Exception
- $this->DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
- }
-
- /**
- * Disconnect
- * @return void
- */
- public function disConnect()
- {
- $this->DB = null;
- $this->Stmt = null;
- }
-
- /**
- * Execute sql 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();
- }
-
- /**
- * Query sql
- * @param string $statement
- * @return PdoDb
- */
- 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 rows affected
- * @return int
- */
- public function affectRows()
- {
- return $this->Stmt->rowCount();
- }
-
- /**
- * Prepared statement
- * @param string $statement
- * @return PdoDb
- */
- 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 PdoDb
- */
- public function bindArray($array)
- {
- foreach ($array as $k => $v) {
- if (is_array($v)) {
- //array的有效结构 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 prepared statements
- * @return bool
- */
- public function execute()
- {
- if ($this->Stmt->execute()) {
- return true;
- }
- $this->errorMessage();
- }
-
- /**
- * Start transaction
- * @return bool
- */
- public function beginTransaction()
- {
- return $this->DB->beginTransaction();
- }
-
- /**
- *Execute transaction
- * @return bool
- */
- public function commitTransaction()
- {
- return $this->DB->commit();
- }
-
- /**
- * Rollback transaction
- * @return bool
- */
- public function rollbackTransaction()
- {
- return $this->DB->rollBack();
- }
-
- /**
- * Throws Error
- * @throws Error
- * @return void
- */
- public function errorMessage()
- {
- $msg = $this->DB->errorInfo();
- throw new Error('数据库错误:' . $msg[2]);
- }
-
- //---------------------
- /**
- * Singleton instance
- * @var PdoDb
- */
- protected static $_instance;
-
- /**
- * Default database
- * @static
- * @param array $config
- * @return PdoDb
- */
- public static function instance($config)
- {
- if (!self::$_instance instanceof PdoDb) {
- self::$_instance = new PdoDb($config);
- self::$_instance->connect();
- }
- return self::$_instance;
- }
-
- //----------------------
-
- /**
- * Get the database supported by PDO
- * @static
- * @return array
- */
- public static function getSupportDriver(){
- return PDO::getAvailableDrivers();
- }
- /**
- * Get the version information of the database
- * @return array
- */
- public function getDriverVersion(){
- $name = $this->DB->getAttribute(PDO::ATTR_DRIVER_NAME);
- return array($name=>$this->DB->getAttribute(PDO::ATTR_CLIENT_VERSION));
- }
-
- }
复制代码
使用的时候
- PdoDb::instance($config);
复制代码
|