CrossPHP
  • Namespace
  • Class
  • Download

Namespaces

  • Cross
    • Auth
    • Cache
      • Driver
      • Request
    • Core
    • DB
      • Connecter
      • Drivers
      • SQLAssembler
    • Exception
    • Http
    • I
    • Lib
      • Document
    • Module
    • MVC
    • Runtime
  • None

Classes

  • Cross\Auth\CookieAuth
  • Cross\Auth\SessionAuth
  • Cross\Cache\Driver\FileCacheDriver
  • Cross\Cache\Driver\MemcacheDriver
  • Cross\Cache\Driver\RedisDriver
  • Cross\Cache\Request\FileCache
  • Cross\Cache\Request\Memcache
  • Cross\Cache\Request\RedisCache
  • Cross\Cache\RequestCache
  • Cross\Core\Annotate
  • Cross\Core\Application
  • Cross\Core\ArrayMap
  • Cross\Core\Config
  • Cross\Core\CrossArray
  • Cross\Core\Delegate
  • Cross\Core\FrameBase
  • Cross\Core\Helper
  • Cross\Core\HttpAuth
  • Cross\Core\Loader
  • Cross\Core\Rest
  • Cross\Core\Router
  • Cross\DB\Connecter\BaseConnecter
  • Cross\DB\Connecter\MySQLConnecter
  • Cross\DB\Connecter\PgSQLConnecter
  • Cross\DB\Connecter\SQLiteConnecter
  • Cross\DB\DBFactory
  • Cross\DB\Drivers\CouchDriver
  • Cross\DB\Drivers\MongoDriver
  • Cross\DB\Drivers\PDOSqlDriver
  • Cross\DB\SQLAssembler\MySQLAssembler
  • Cross\DB\SQLAssembler\PgSQLAssembler
  • Cross\DB\SQLAssembler\SQLAssembler
  • Cross\DB\SQLAssembler\SQLiteAssembler
  • Cross\Http\Request
  • Cross\Http\Response
  • Cross\Lib\Array2XML
  • Cross\Lib\Document\CallTree
  • Cross\Lib\Document\CallTreeToHTML
  • Cross\Lib\Document\HTML
  • Cross\Lib\StringToPHPStream
  • Cross\Module\SQLModule
  • Cross\MVC\Controller
  • Cross\MVC\Module
  • Cross\MVC\View
  • Cross\Runtime\ClosureContainer

Interfaces

  • Cross\I\CacheInterface
  • Cross\I\HttpAuthInterface
  • Cross\I\PDOConnecter
  • Cross\I\RequestCacheInterface
  • Cross\I\RouterInterface
  • Cross\I\SqlInterface

Exceptions

  • Cross\Exception\CacheException
  • Cross\Exception\CoreException
  • Cross\Exception\CrossException
  • Cross\Exception\FrontException

Functions

  • ascLogo
  • line
  • tBody
  • th
  • tHead
  1 <?php
  2 /**
  3  * Cross - a micro PHP 5 framework
  4  *
  5  * @link        http://www.crossphp.com
  6  * @license     MIT License
  7  */
  8 
  9 namespace Cross\DB\Connecter;
 10 
 11 use Cross\Exception\CoreException;
 12 use Exception;
 13 use PDO;
 14 
 15 /**
 16  * @author wonli <wonli@live.com>
 17  * Class MySQLConnecter
 18  * @package Cross\DB\Connecter
 19  */
 20 class MySQLConnecter extends BaseConnecter
 21 {
 22     /**
 23      * 数据库连接实例
 24      *
 25      * @var object
 26      */
 27     private static $instance;
 28 
 29     /**
 30      * 默认连接参数
 31      * <ul>
 32      *  <li>PDO::ATTR_PERSISTENT => false //禁用长连接</li>
 33      *  <li>PDO::ATTR_EMULATE_PREPARES => false //禁用模拟预处理</li>
 34      *  <li>PDO::ATTR_STRINGIFY_FETCHES => false //禁止数值转换成字符串</li>
 35      *  <li>PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true //使用缓冲查询</li>
 36      *  <li>PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION //发生错误时抛出异常 </li>
 37      *  <li>PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8" </li>
 38      * </ul>
 39      *
 40      * @var array
 41      */
 42     private static $options = array(
 43         PDO::ATTR_PERSISTENT => false,
 44         PDO::ATTR_EMULATE_PREPARES => false,
 45         PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
 46         PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
 47         PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
 48     );
 49 
 50     /**
 51      * 创建Mysql的PDO连接
 52      *
 53      * @param string $dsn dsn
 54      * @param string $user 数据库用户名
 55      * @param string $password 数据库密码
 56      * @param array $options
 57      * @throws CoreException
 58      */
 59     private function __construct($dsn, $user, $password, array $options = array())
 60     {
 61         try {
 62             $this->pdo = new PDO($dsn, $user, $password, parent::getOptions(self::$options, $options));
 63         } catch (Exception $e) {
 64             throw new CoreException($e->getMessage());
 65         }
 66     }
 67 
 68     /**
 69      * @see MysqlModel::__construct
 70      *
 71      * @param string $dsn
 72      * @param string $user
 73      * @param string $password
 74      * @param array $option
 75      * @return mixed
 76      * @throws CoreException
 77      */
 78     static function getInstance($dsn, $user, $password, array $option = array())
 79     {
 80         //同时建立多个连接时候已dsn的md5值为key
 81         $key = md5($dsn);
 82         if (!isset(self::$instance[$key])) {
 83             self::$instance [$key] = new self($dsn, $user, $password, $option);
 84         }
 85 
 86         return self::$instance [$key];
 87     }
 88 
 89     /**
 90      * 返回PDO连接的实例
 91      *
 92      * @return PDO
 93      */
 94     public function getPDO()
 95     {
 96         return $this->pdo;
 97     }
 98 
 99     /**
100      * 获取表的主键名
101      *
102      * @param string $table
103      * @return bool
104      */
105     public function getPK($table)
106     {
107         $table_info = $this->getMetaData($table, false);
108         foreach ($table_info as $ti) {
109             if ($ti['Extra'] == 'auto_increment') {
110                 return $ti['Field'];
111             }
112         }
113 
114         return false;
115     }
116 
117     /**
118      * 最后插入时的id
119      *
120      * @return string
121      */
122     function lastInsertId()
123     {
124         return $this->pdo->lastInsertId();
125     }
126 
127     /**
128      * 获取表的字段信息
129      *
130      * @param string $table
131      * @param bool $fields_map
132      * @return mixed
133      */
134     function getMetaData($table, $fields_map = true)
135     {
136         $data = $this->pdo->query("DESCRIBE {$table}");
137         try {
138             if ($fields_map) {
139                 $result = array();
140                 $data->fetchAll(PDO::FETCH_FUNC, function ($field, $type, $null, $key, $default, $extra) use (&$result) {
141                     $result[$field] = array(
142                         'primary' => $key == 'PRI',
143                         'auto_increment' => $extra == 'auto_increment',
144                         'default_value' => strval($default),
145                         'not_null' => $null == 'NO',
146                     );
147                 });
148                 return $result;
149             } else {
150                 return $data->fetchAll(PDO::FETCH_ASSOC);
151             }
152         } catch (Exception $e) {
153             return array();
154         }
155     }
156 }
157 
CrossPHP API documentation generated by ApiGen