zhaoyi
unread,Nov 21, 2009, 1:58:12 AM11/21/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to kiwiphp
有时需要做一些很小的应用, 有时需要一个快速展示给客户的原型, 希望能增加sqlite支持.
下面是参考mysql做的修改.没有优化整理代码,但已经能够工作.
<?php
/**
* Database adapter PDO sqlite class
*/
class DbAdapterPdoSqlite extends DbAdapterPdo
{
/**
* The default database configuration for sqlite
*
* @var array
*/
protected $_config = array('port' => '3306', 'username' => '',
'sqlite_ver' => 'sqlite');
/**
* The PDO construct options
*
* @var array
*/
//protected $_options = array(PDO::ATTR_PERSISTENT => false);
/**
* Create a PDO DSN for the adapter
*
* @param array $config
* @return string
*/
protected function _dsn($config)
{
//sqlite2:d:\www\localhost\ftp\ftp.db
$config['sqlite_ver'] = isset($config['sqlite_ver']) ? $config
['sqlite_ver'] : 'sqlite';
return $config['sqlite_ver'].':' . Kiwi::$appOptions['proj_dir'] .
$config['dbpath'] .DIRECTORY_SEPARATOR. $config['dbname'];
}
/**
* Get current db configuration
*
* @param string $group
* @param string $node
* @param string $role
* @param string $host
* @return array
*/
protected function _getConfig($group, $node, $role = 'master', $host
= null)
{
return $this->_getBasicConfig($group, $node, $role, $host);
}
/**
* Change current schema
*
* @return void
*/
protected function _useSchema()
{
}
/**
* Add an adapter-specific LIMIT clause to the SELECT statement.
*
* @param string $sql
* @param integer $limit
* @param integer $offset
* @return string
*/
public function limit($sql, $limit, $offset)
{
if ($limit > 0)
{
$offset = 0 < $offset ? $offset : 0;
$sql .= " LIMIT $limit OFFSET $offset";
}
return $sql;
}
/**
* Set encoding for a database connection.
*
* @param string $encoding
* @param resource $connection
* @return void
*/
public function setCharset($charset, $connection)
{
//$sql = 'PRAGMA encoding = "' . $charset . '"';
//$connection->exec($sql);
}
/**
* Return the column descriptions for a table.
*
* @param string $table
* @return array
*/
public function showFields($table)
{
$sql = "PRAGMA table_info('".$table."')";
$queryResult = $this->query($sql);
$result = $queryResult['rows'];
// echo '<pre>';
// print_r($result);
// echo '</pre>';
$fields = array();
foreach ($result as $key => $value)
{
//字段名
$fields[$value['name']]['name'] = $value['name'];
//字段类型
$fulltype = $value['type'];
$size = null;
$precision = null;
$scale = null;
if (preg_match('/^([^\(]+)\(\s*(\d+)\s*,\s*(\d+)\s*\)$/',
$fulltype, $matches)) {
$type = $matches[1];
$precision = $matches[2];
$scale = $matches[3]; // aka precision
} elseif (preg_match('/^([^\(]+)\(\s*(\d+)\s*\)$/',
$fulltype, $matches)) {
$type = $matches[1];
$size = $matches[2];
} else {
$type = $fulltype;
}
$fields[$value['name']]['type'] = $type;
/*
* not null is 99, null is 0
*/
$fields[$value['name']]['notnull'] = (bool) ($value['notnull'] !=
0);
$fields[$value['name']]['default'] = $value['dflt_value'];
$fields[$value['name']]['primary'] = (bool) ($value['pk'] == 1 &&
strtoupper($fulltype) == 'INTEGER');
}
return $fields;
}
}