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 namespace Cross\DB\Connecter;
  9 
 10 use Cross\Exception\CoreException;
 11 use Exception;
 12 use PDO;
 13 
 14 /**
 15  * @author wonli <wonli@live.com>
 16  * Class PgSQLConnecter
 17  * @package Cross\DB\Connecter
 18  */
 19 class PgSQLConnecter extends BaseConnecter
 20 {
 21     /**
 22      * 数据库连接实例
 23      *
 24      * @var object
 25      */
 26     private static $instance;
 27 
 28     /**
 29      * 默认连接参数
 30      *
 31      * @var array
 32      */
 33     private static $options = array();
 34 
 35     /**
 36      * 创建PgSQL的PDO连接
 37      *
 38      * @param string $dsn dsn
 39      * @param string $user 数据库用户名
 40      * @param string $password 数据库密码
 41      * @param array $options
 42      * @throws CoreException
 43      */
 44     private function __construct($dsn, $user, $password, array $options = array())
 45     {
 46         try {
 47             $this->pdo = new PDO($dsn, $user, $password, parent::getOptions(self::$options, $options));
 48         } catch (Exception $e) {
 49             throw new CoreException($e->getMessage());
 50         }
 51     }
 52 
 53     /**
 54      * @see MysqlModel::__construct
 55      * @param string $dsn
 56      * @param string $user
 57      * @param string $password
 58      * @param array $option
 59      * @return mixed
 60      * @throws CoreException
 61      */
 62     static function getInstance($dsn, $user, $password, array $option = array())
 63     {
 64         //同时建立多个连接时候已dsn的md5值为key
 65         $key = md5($dsn);
 66         if (!isset(self::$instance[$key])) {
 67             self::$instance [$key] = new self($dsn, $user, $password, $option);
 68         }
 69 
 70         return self::$instance [$key];
 71     }
 72 
 73     /**
 74      * 返回PDO连接的实例
 75      *
 76      * @return PDO
 77      */
 78     public function getPDO()
 79     {
 80         return $this->pdo;
 81     }
 82 
 83     /**
 84      * 获取表的主键名
 85      *
 86      * @param string $table_name
 87      * @return bool
 88      */
 89     public function getPK($table_name)
 90     {
 91         $table_info = $this->getMetaData($table_name, false);
 92         foreach ($table_info as $info) {
 93             if ($info['contype'] == 'p') {
 94                 return $info['column_name'];
 95             }
 96         }
 97         return false;
 98     }
 99 
100     /**
101      * 获取最后插入时的ID
102      *
103      * @return mixed
104      */
105     public function lastInsertId()
106     {
107         $sql = "SELECT LASTVAL() as insert_id";
108         try {
109             $data = $this->pdo->query($sql)->fetch(PDO::FETCH_ASSOC);;
110             return $data['insert_id'];
111         } catch (Exception $e) {
112             return false;
113         }
114     }
115 
116     /**
117      * 获取表的字段信息
118      *
119      * @param string $table
120      * @param bool $fields_map
121      * @return array
122      */
123     function getMetaData($table, $fields_map = true)
124     {
125         $sql = "select a.column_name, a.is_nullable, a.column_default, p.contype from (
126                     select i.column_name, i.is_nullable, i.column_default, i.ordinal_position, c.oid
127                     from information_schema.columns i left join pg_class c on c.relname=i.table_name
128                     where i.table_name='{$table}'
129                 ) a left join pg_constraint p on p.conrelid=a.oid and a.ordinal_position = ANY (p.conkey)";
130 
131         try {
132             $data = $this->pdo->query($sql);
133             if ($fields_map) {
134                 $result = array();
135                 $data->fetchAll(PDO::FETCH_FUNC, function ($column_name, $is_null, $column_default, $con_type) use (&$result) {
136                     $auto_increment = preg_match("/nextval\((.*)\)/", $column_default);
137                     $result[$column_name] = array(
138                         'primary' => $con_type == 'p',
139                         'auto_increment' => $auto_increment,
140                         'default_value' => $auto_increment ? '' : strval($column_default),
141                         'not_null' => $is_null == 'NO',
142                     );
143                 });
144                 return $result;
145             } else {
146                 return $data->fetchAll(PDO::FETCH_ASSOC);
147             }
148         } catch (Exception $e) {
149             return array();
150         }
151     }
152 }
153 
CrossPHP API documentation generated by ApiGen