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 SQLiteConnecter
 18  * @package Cross\DB\Connecter
 19  */
 20 class SQLiteConnecter extends BaseConnecter
 21 {
 22     /**
 23      * @var PDO
 24      */
 25     private static $instance;
 26 
 27     /**
 28      * 默认连接参数
 29      *
 30      * @var array
 31      */
 32     private static $options = array();
 33 
 34     /**
 35      * 创建一个SQLite的PDO连接
 36      *
 37      * @param string $dsn
 38      * @param array $options
 39      * @throws CoreException
 40      */
 41     private function __construct($dsn, array $options)
 42     {
 43         try {
 44             $this->pdo = new PDO($dsn, null, null, parent::getOptions(self::$options, $options));
 45         } catch (Exception $e) {
 46             throw new CoreException($e->getMessage());
 47         }
 48     }
 49 
 50     /**
 51      * @param string $dsn
 52      * @param null $user
 53      * @param null $pwd
 54      * @param array $options
 55      * @return SQLiteConnecter|PDO
 56      * @throws CoreException
 57      */
 58     static function getInstance($dsn, $user = null, $pwd = null, array $options = array())
 59     {
 60         $key = md5($dsn);
 61         if (empty(self::$instance[$key])) {
 62             self::$instance[$key] = new SqliteConnecter($dsn, $options);
 63         }
 64 
 65         return self::$instance[$key];
 66     }
 67 
 68     /**
 69      * 返回一个PDO连接对象的实例
 70      *
 71      * @return mixed
 72      */
 73     function getPDO()
 74     {
 75         return $this->pdo;
 76     }
 77 
 78     /**
 79      * 获取表的主键名
 80      *
 81      * @param string $table
 82      * @return bool|mixed
 83      */
 84     function getPK($table)
 85     {
 86         $info = $this->getMetaData($table, false);
 87         if (!empty($info)) {
 88             foreach ($info as $i) {
 89                 if ($i['pk'] == 1) {
 90                     return $i['name'];
 91                 }
 92             }
 93         }
 94 
 95         return false;
 96     }
 97 
 98     /**
 99      * 最后插入的id
100      */
101     function lastInsertId()
102     {
103         return $this->pdo->lastInsertId();
104     }
105 
106     /**
107      * 获取表的字段信息
108      *
109      * @param string $table
110      * @param bool $fields_map
111      * @return mixed
112      */
113     function getMetaData($table, $fields_map = true)
114     {
115         $sql = "PRAGMA table_info('{$table}')";
116         try {
117             $data = $this->pdo->query($sql);
118             if ($fields_map) {
119                 $result = array();
120                 $data->fetchAll(PDO::FETCH_FUNC, function ($cid, $name, $type, $notnull, $dflt_value, $pk) use (&$result) {
121                     $result[$name] = array(
122                         'primary' => $pk == 1,
123                         'auto_increment' => (bool)(($pk == 1) && ($type == 'INTEGER')), //INTEGER && PRIMARY KEY.
124                         'default_value' => strval($dflt_value),
125                         'not_null' => $notnull == 1
126                     );
127                 });
128                 return $result;
129             } else {
130                 return $data->fetchAll(PDO::FETCH_ASSOC);
131             }
132         } catch (Exception $e) {
133             return array();
134         }
135     }
136 }
137 
CrossPHP API documentation generated by ApiGen