Skip to content

Commit f705308

Browse files
committed
增加相关驱动
1 parent 3dec2ee commit f705308

File tree

5 files changed

+566
-0
lines changed

5 files changed

+566
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
// +----------------------------------------------------------------------
3+
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
4+
// +----------------------------------------------------------------------
5+
// | Copyright (c) 2006-2013 http://thinkphp.cn All rights reserved.
6+
// +----------------------------------------------------------------------
7+
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8+
// +----------------------------------------------------------------------
9+
// | Author: 何辉 <[email protected]>
10+
// +----------------------------------------------------------------------
11+
12+
namespace Think\Cache\Driver;
13+
14+
use Memcached as MemcachedResource;
15+
use Think\Cache;
16+
17+
/**
18+
* Memcached缓存驱动
19+
*/
20+
class Memcached extends Cache
21+
{
22+
23+
/**
24+
*
25+
* @param array $options
26+
*/
27+
public function __construct($options = array())
28+
{
29+
if (!extension_loaded('memcached')) {
30+
E(L('_NOT_SUPPORT_') . ':memcached');
31+
}
32+
33+
$options = array_merge(array(
34+
'servers' => C('MEMCACHED_SERVER') ?: null,
35+
'lib_options' => C('MEMCACHED_LIB') ?: null,
36+
), $options);
37+
38+
$this->options = $options;
39+
$this->options['expire'] = isset($options['expire']) ? $options['expire'] : C('DATA_CACHE_TIME');
40+
$this->options['prefix'] = isset($options['prefix']) ? $options['prefix'] : C('DATA_CACHE_PREFIX');
41+
$this->options['length'] = isset($options['length']) ? $options['length'] : 0;
42+
43+
$this->handler = new MemcachedResource;
44+
$options['servers'] && $this->handler->addServers($options['servers']);
45+
$options['lib_options'] && $this->handler->setOptions($options['lib_options']);
46+
}
47+
48+
/**
49+
* 读取缓存
50+
* @access public
51+
* @param string $name 缓存变量名
52+
* @return mixed
53+
*/
54+
public function get($name)
55+
{
56+
N('cache_read', 1);
57+
return $this->handler->get($this->options['prefix'] . $name);
58+
}
59+
60+
/**
61+
* 写入缓存
62+
* @access public
63+
* @param string $name 缓存变量名
64+
* @param mixed $value 存储数据
65+
* @param integer $expire 有效时间(秒)
66+
* @return boolean
67+
*/
68+
public function set($name, $value, $expire = null)
69+
{
70+
N('cache_write', 1);
71+
if (is_null($expire)) {
72+
$expire = $this->options['expire'];
73+
}
74+
$name = $this->options['prefix'] . $name;
75+
if ($this->handler->set($name, $value, time() + $expire)) {
76+
if ($this->options['length'] > 0) {
77+
// 记录缓存队列
78+
$this->queue($name);
79+
}
80+
return true;
81+
}
82+
return false;
83+
}
84+
85+
/**
86+
* 删除缓存
87+
* @access public
88+
* @param string $name 缓存变量名
89+
* @return boolean
90+
*/
91+
public function rm($name, $ttl = false)
92+
{
93+
$name = $this->options['prefix'] . $name;
94+
return false === $ttl ?
95+
$this->handler->delete($name) :
96+
$this->handler->delete($name, $ttl);
97+
}
98+
99+
/**
100+
* 清除缓存
101+
* @access public
102+
* @return boolean
103+
*/
104+
public function clear()
105+
{
106+
return $this->handler->flush();
107+
}
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?php
2+
// +----------------------------------------------------------------------
3+
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
4+
// +----------------------------------------------------------------------
5+
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
6+
// +----------------------------------------------------------------------
7+
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8+
// +----------------------------------------------------------------------
9+
// | Author: liu21st <[email protected]>
10+
// +----------------------------------------------------------------------
11+
namespace Think\Db\Driver;
12+
13+
use Think\Db\Driver;
14+
15+
/**
16+
* Firebird数据库驱动
17+
*/
18+
class Firebird extends Driver
19+
{
20+
protected $selectSql = 'SELECT %LIMIT% %DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%';
21+
22+
/**
23+
* 解析pdo连接的dsn信息
24+
* @access public
25+
* @param array $config 连接信息
26+
* @return string
27+
*/
28+
protected function parseDsn($config)
29+
{
30+
$dsn = 'firebird:dbname=' . $config['hostname'] . '/' . ($config['hostport'] ?: 3050) . ':' . $config['database'];
31+
return $dsn;
32+
}
33+
34+
/**
35+
* 执行语句
36+
* @access public
37+
* @param string $str sql指令
38+
* @param boolean $fetchSql 不执行只是获取SQL
39+
* @return mixed
40+
*/
41+
public function execute($str, $fetchSql = false)
42+
{
43+
$this->initConnect(true);
44+
if (!$this->_linkID) {
45+
return false;
46+
}
47+
48+
$this->queryStr = $str;
49+
if (!empty($this->bind)) {
50+
$that = $this;
51+
$this->queryStr = strtr($this->queryStr, array_map(function ($val) use ($that) {return '\'' . $that->escapeString($val) . '\'';}, $this->bind));
52+
}
53+
if ($fetchSql) {
54+
return $this->queryStr;
55+
}
56+
//释放前次的查询结果
57+
if (!empty($this->PDOStatement)) {
58+
$this->free();
59+
}
60+
61+
$this->executeTimes++;
62+
N('db_write', 1); // 兼容代码
63+
// 记录开始执行时间
64+
$this->debug(true);
65+
$this->PDOStatement = $this->_linkID->prepare($str);
66+
if (false === $this->PDOStatement) {
67+
E($this->error());
68+
}
69+
foreach ($this->bind as $key => $val) {
70+
if (is_array($val)) {
71+
$this->PDOStatement->bindValue($key, $val[0], $val[1]);
72+
} else {
73+
$this->PDOStatement->bindValue($key, $val);
74+
}
75+
}
76+
$this->bind = array();
77+
$result = $this->PDOStatement->execute();
78+
$this->debug(false);
79+
if (false === $result) {
80+
$this->error();
81+
return false;
82+
} else {
83+
$this->numRows = $this->PDOStatement->rowCount();
84+
return $this->numRows;
85+
}
86+
}
87+
88+
/**
89+
* 取得数据表的字段信息
90+
* @access public
91+
*/
92+
public function getFields($tableName)
93+
{
94+
$this->initConnect(true);
95+
list($tableName) = explode(' ', $tableName);
96+
$sql = 'SELECT RF.RDB$FIELD_NAME AS FIELD,RF.RDB$DEFAULT_VALUE AS DEFAULT1,RF.RDB$NULL_FLAG AS NULL1,TRIM(T.RDB$TYPE_NAME) || \'(\' || F.RDB$FIELD_LENGTH || \')\' as TYPE FROM RDB$RELATION_FIELDS RF LEFT JOIN RDB$FIELDS F ON (F.RDB$FIELD_NAME = RF.RDB$FIELD_SOURCE) LEFT JOIN RDB$TYPES T ON (T.RDB$TYPE = F.RDB$FIELD_TYPE) WHERE RDB$RELATION_NAME=UPPER(\'' . $tableName . '\') AND T.RDB$FIELD_NAME = \'RDB$FIELD_TYPE\' ORDER By RDB$FIELD_POSITION';
97+
$result = $this->query($sql);
98+
$info = array();
99+
if ($result) {
100+
foreach ($result as $key => $val) {
101+
$info[trim($val['field'])] = array(
102+
'name' => trim($val['field']),
103+
'type' => $val['type'],
104+
'notnull' => (bool) (1 == $val['null1']), // 1表示不为Null
105+
'default' => $val['default1'],
106+
'primary' => false,
107+
'autoinc' => false,
108+
);
109+
}
110+
}
111+
//获取主键
112+
$sql = 'select b.rdb$field_name as field_name from rdb$relation_constraints a join rdb$index_segments b on a.rdb$index_name=b.rdb$index_name where a.rdb$constraint_type=\'PRIMARY KEY\' and a.rdb$relation_name=UPPER(\'' . $tableName . '\')';
113+
$rs_temp = $this->query($sql);
114+
foreach ($rs_temp as $row) {
115+
$info[trim($row['field_name'])]['primary'] = true;
116+
}
117+
return $info;
118+
}
119+
120+
/**
121+
* 取得数据库的表信息
122+
* @access public
123+
*/
124+
public function getTables($dbName = '')
125+
{
126+
$sql = 'SELECT DISTINCT RDB$RELATION_NAME FROM RDB$RELATION_FIELDS WHERE RDB$SYSTEM_FLAG=0';
127+
$result = $this->query($sql);
128+
$info = array();
129+
foreach ($result as $key => $val) {
130+
$info[$key] = trim(current($val));
131+
}
132+
return $info;
133+
}
134+
135+
/**
136+
* SQL指令安全过滤
137+
* @access public
138+
* @param string $str SQL指令
139+
* @return string
140+
*/
141+
public function escapeString($str)
142+
{
143+
return str_replace("'", "''", $str);
144+
}
145+
146+
/**
147+
* limit
148+
* @access public
149+
* @param $limit limit表达式
150+
* @return string
151+
*/
152+
public function parseLimit($limit)
153+
{
154+
$limitStr = '';
155+
if (!empty($limit)) {
156+
$limit = explode(',', $limit);
157+
if (count($limit) > 1) {
158+
$limitStr = ' FIRST ' . $limit[1] . ' SKIP ' . $limit[0] . ' ';
159+
} else {
160+
$limitStr = ' FIRST ' . $limit[0] . ' ';
161+
}
162+
}
163+
return $limitStr;
164+
}
165+
166+
/**
167+
* 随机排序
168+
* @access protected
169+
* @return string
170+
*/
171+
protected function parseRand()
172+
{
173+
return 'rand()';
174+
}
175+
176+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
namespace Think\Session\Driver;
3+
4+
class Memcache
5+
{
6+
protected $lifeTime = 3600;
7+
protected $sessionName = '';
8+
protected $handle = null;
9+
10+
/**
11+
* 打开Session
12+
* @access public
13+
* @param string $savePath
14+
* @param mixed $sessName
15+
*/
16+
public function open($savePath, $sessName)
17+
{
18+
$this->lifeTime = C('SESSION_EXPIRE') ? C('SESSION_EXPIRE') : $this->lifeTime;
19+
// $this->sessionName = $sessName;
20+
$options = array(
21+
'timeout' => C('SESSION_TIMEOUT') ? C('SESSION_TIMEOUT') : 1,
22+
'persistent' => C('SESSION_PERSISTENT') ? C('SESSION_PERSISTENT') : 0,
23+
);
24+
$this->handle = new \Memcache;
25+
$hosts = explode(',', C('MEMCACHE_HOST'));
26+
$ports = explode(',', C('MEMCACHE_PORT'));
27+
foreach ($hosts as $i => $host) {
28+
$port = isset($ports[$i]) ? $ports[$i] : $ports[0];
29+
$this->handle->addServer($host, $port, true, 1, $options['timeout']);
30+
}
31+
return true;
32+
}
33+
34+
/**
35+
* 关闭Session
36+
* @access public
37+
*/
38+
public function close()
39+
{
40+
$this->gc(ini_get('session.gc_maxlifetime'));
41+
$this->handle->close();
42+
$this->handle = null;
43+
return true;
44+
}
45+
46+
/**
47+
* 读取Session
48+
* @access public
49+
* @param string $sessID
50+
*/
51+
public function read($sessID)
52+
{
53+
return $this->handle->get($this->sessionName . $sessID);
54+
}
55+
56+
/**
57+
* 写入Session
58+
* @access public
59+
* @param string $sessID
60+
* @param String $sessData
61+
*/
62+
public function write($sessID, $sessData)
63+
{
64+
return $this->handle->set($this->sessionName . $sessID, $sessData, 0, $this->lifeTime);
65+
}
66+
67+
/**
68+
* 删除Session
69+
* @access public
70+
* @param string $sessID
71+
*/
72+
public function destroy($sessID)
73+
{
74+
return $this->handle->delete($this->sessionName . $sessID);
75+
}
76+
77+
/**
78+
* Session 垃圾回收
79+
* @access public
80+
* @param string $sessMaxLifeTime
81+
*/
82+
public function gc($sessMaxLifeTime)
83+
{
84+
return true;
85+
}
86+
}

0 commit comments

Comments
 (0)