包 | XS.util |
---|---|
继承关系 | class XSDatabaseDataSource » XSDataSource |
版本 | 1.0.0 |
源代码 | sdk/php/util/XSDataSource.class.php |
名称 | 描述 | 定义于 |
---|---|---|
__construct() | 构造函数 | XSDataSource |
getCharset() | 返回数据库输出字符集 | XSDatabaseDataSource |
getData() | 从数据源中提取一条数据 | XSDataSource |
instance() | 取得数据源对象实例 | XSDataSource |
名称 | 描述 | 定义于 |
---|---|---|
deinit() | XSDatabaseDataSource | |
getDataList() | 返回一批数据 | XSDatabaseDataSource |
init() | XSDatabaseDataSource |
protected void deinit()
|
public mixed getCharset()
| ||
{return} | mixed | 如果数据库不支持 UTF-8 转换则返回 false |
public function getCharset()
{
if ($this->db->setUtf8()) {
return 'UTF-8';
}
return parent::getCharset();
}
返回数据库输出字符集
protected 结果数组, getDataList()
| ||
{return} | 结果数组, | 没有更多数据时返回 false |
protected function getDataList()
{
if ($this->limit <= 0) {
return false;
}
$sql = $this->sql . ' LIMIT ' . min(self::PLIMIT, $this->limit) . ' OFFSET ' . $this->offset;
$this->limit -= self::PLIMIT;
$this->offset += self::PLIMIT;
return $this->db->query($sql);
}
返回一批数据
protected void init()
|
protected function init()
{
if (strstr($this->type, 'sqlite')) {
$pos = strpos($this->type, ':');
$param = array('scheme' => substr($this->type, 0, $pos));
$param['path'] = substr($this->type, $pos + (substr($this->type, $pos + 1, 2) === '//' ? 3 : 1));
} elseif (!($param = parse_url($this->type))) {
throw new XSException('Wrong format of DB connection parameter');
} else {
if (isset($param['user'])) {
$param['user'] = urldecode($param['user']);
}
if (isset($param['pass'])) {
$param['pass'] = urldecode($param['pass']);
}
$param['path'] = isset($param['path']) ? trim($param['path'], '/') : '';
if (empty($param['path'])) {
throw new XSException('Not contain dbname of DB connection parameter');
}
if (($pos = strpos($param['path'], '/')) === false) {
$param['dbname'] = $param['path'];
} else {
$param['dbname'] = substr($param['path'], 0, $pos);
$param['table'] = substr($param['path'], $pos + 1);
}
}
// get driver
$driver = self::getDriverName($param['scheme']);
$class = 'XSDatabase' . ucfirst($driver);
if (!class_exists($class)) {
throw new XSException("Undefined database driver: '$driver'");
}
$this->db = new $class;
$this->db->connect($param);
// set SQL & parse limit/offset
$this->limit = $this->offset = 0;
$sql = $this->arg;
if (empty($sql)) {
if (!isset($param['table'])) {
throw new XSException('Not specified any query SQL or db table');
}
$sql = 'SELECT * FROM ' . $param['table'];
} elseif (preg_match('/ limit\s+(\d+)(?:\s*,\s*(\d+)|\s+offset\s+(\d+))?\s*$/i', $sql, $match)) {
if (isset($match[3])) { // LIMIT xxx OFFSET yyy
$this->offset = intval($match[3]);
$this->limit = intval($match[1]);
} elseif (isset($match[2])) { // LIMIT yyy, xxx
$this->offset = intval($match[1]);
$this->limit = intval($match[2]);
} else { // lIMIT xxx
$this->limit = intval($match[1]);
}
$sql = substr($sql, 0, strlen($sql) - strlen($match[0]));
}
$this->sql = $sql;
if ($this->limit == 0) {
$sql = preg_replace('/SELECT\s+.+?\sFROM\s/is', 'SELECT COUNT(*) AS count FROM ', $sql);
$res = $this->db->query1($sql);
$this->limit = $res['count'] - $this->offset;
}
}
包 | XS.util |
---|---|
继承关系 | class XSDatabaseDataSource » XSDataSource |
版本 | 1.0.0 |
源代码 | sdk/php/util/XSDataSource.class.php |
名称 | 描述 | 定义于 |
---|---|---|
__construct() | 构造函数 | XSDataSource |
getCharset() | 返回数据库输出字符集 | XSDatabaseDataSource |
getData() | 从数据源中提取一条数据 | XSDataSource |
instance() | 取得数据源对象实例 | XSDataSource |
名称 | 描述 | 定义于 |
---|---|---|
deinit() | XSDatabaseDataSource | |
getDataList() | 返回一批数据 | XSDatabaseDataSource |
init() | XSDatabaseDataSource |
protected void deinit()
|
public mixed getCharset()
| ||
{return} | mixed | 如果数据库不支持 UTF-8 转换则返回 false |
public function getCharset()
{
if ($this->db->setUtf8()) {
return 'UTF-8';
}
return parent::getCharset();
}
返回数据库输出字符集
protected 结果数组, getDataList()
| ||
{return} | 结果数组, | 没有更多数据时返回 false |
protected function getDataList()
{
if ($this->limit <= 0) {
return false;
}
$sql = $this->sql . ' LIMIT ' . min(self::PLIMIT, $this->limit) . ' OFFSET ' . $this->offset;
$this->limit -= self::PLIMIT;
$this->offset += self::PLIMIT;
return $this->db->query($sql);
}
返回一批数据
protected void init()
|
protected function init()
{
if (strstr($this->type, 'sqlite')) {
$pos = strpos($this->type, ':');
$param = array('scheme' => substr($this->type, 0, $pos));
$param['path'] = substr($this->type, $pos + (substr($this->type, $pos + 1, 2) === '//' ? 3 : 1));
} elseif (!($param = parse_url($this->type))) {
throw new XSException('Wrong format of DB connection parameter');
} else {
if (isset($param['user'])) {
$param['user'] = urldecode($param['user']);
}
if (isset($param['pass'])) {
$param['pass'] = urldecode($param['pass']);
}
$param['path'] = isset($param['path']) ? trim($param['path'], '/') : '';
if (empty($param['path'])) {
throw new XSException('Not contain dbname of DB connection parameter');
}
if (($pos = strpos($param['path'], '/')) === false) {
$param['dbname'] = $param['path'];
} else {
$param['dbname'] = substr($param['path'], 0, $pos);
$param['table'] = substr($param['path'], $pos + 1);
}
}
// get driver
$driver = self::getDriverName($param['scheme']);
$class = 'XSDatabase' . ucfirst($driver);
if (!class_exists($class)) {
throw new XSException("Undefined database driver: '$driver'");
}
$this->db = new $class;
$this->db->connect($param);
// set SQL & parse limit/offset
$this->limit = $this->offset = 0;
$sql = $this->arg;
if (empty($sql)) {
if (!isset($param['table'])) {
throw new XSException('Not specified any query SQL or db table');
}
$sql = 'SELECT * FROM ' . $param['table'];
} elseif (preg_match('/ limit\s+(\d+)(?:\s*,\s*(\d+)|\s+offset\s+(\d+))?\s*$/i', $sql, $match)) {
if (isset($match[3])) { // LIMIT xxx OFFSET yyy
$this->offset = intval($match[3]);
$this->limit = intval($match[1]);
} elseif (isset($match[2])) { // LIMIT yyy, xxx
$this->offset = intval($match[1]);
$this->limit = intval($match[2]);
} else { // lIMIT xxx
$this->limit = intval($match[1]);
}
$sql = substr($sql, 0, strlen($sql) - strlen($match[0]));
}
$this->sql = $sql;
if ($this->limit == 0) {
$sql = preg_replace('/SELECT\s+.+?\sFROM\s/is', 'SELECT COUNT(*) AS count FROM ', $sql);
$res = $this->db->query1($sql);
$this->limit = $res['count'] - $this->offset;
}
}
留下一条评论吧!
请到论坛 登录 后刷新本页面!