[Xunsearch PHP-SDK](http://www.xunsearch.com) v1.4.8 API 參考文檔
# XSDatabaseDataSource
[All Packages](#)| [屬性](#)| [方法(函數)](#)
| 包 | [XS.util](#) |
|-----|-----|
| 繼承關系 | class XSDatabaseDataSource ?[XSDataSource](#) |
| 版本 | 1.0.0 |
| 源代碼 | [sdk/php/util/XSDataSource.class.php](https://github.com/hightman/xunsearch/blob/master/sdk/php/util/XSDataSource.class.php) |
SQL 數據庫源
### Protected 屬性
[隱去繼承來的屬性](#)
| 名稱 | 類型 | 描述 | 定義于 |
|-----|-----|-----|-----|
| [arg](#) | | | [XSDataSource](#) |
| [inCli](#) | | | [XSDataSource](#) |
| [type](#) | | | [XSDataSource](#) |
### Public 方法
[隱去繼承來的方法](#)
| 名稱 | 描述 | 定義于 |
|-----|-----|-----|
| [__construct()](#) | 構造函數 | [XSDataSource](#) |
| [getCharset()](#) | 返回數據庫輸出字符集 | XSDatabaseDataSource |
| [getData()](#) | 從數據源中提取一條數據 | [XSDataSource](#) |
| [instance()](#) | 取得數據源對象實例 | [XSDataSource](#) |
### Protected 方法
[隱去繼承來的方法](#)
| 名稱 | 描述 | 定義于 |
|-----|-----|-----|
| [deinit()](#) | | XSDatabaseDataSource |
| [getDataList()](#) | 返回一批數據 | XSDatabaseDataSource |
| [init()](#) | | XSDatabaseDataSource |
### 方法明細
deinit()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">protected void <b>deinit</b>()</div></td></tr></table>
**源碼:**[sdk/php/util/XSDataSource.class.php#L211](https://github.com/hightman/xunsearch/blob/master/sdk/php/util/XSDataSource.class.php#L211) (**[顯示](#)**)
`protected?function?deinit()
{
????$this->db->close();
}`
getCharset()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">public mixed <b>getCharset</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">mixed</td> <td class="paramDescCol">如果數據庫不支持 UTF-8 轉換則返回 false</td></tr></table>
**源碼:**[sdk/php/util/XSDataSource.class.php#L138](https://github.com/hightman/xunsearch/blob/master/sdk/php/util/XSDataSource.class.php#L138) (**[顯示](#)**)
`public?function?getCharset()
{
????if?($this->db->setUtf8())?{
????????return?'UTF-8';
????}
????return?parent::getCharset();
}`
返回數據庫輸出字符集
getDataList()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">protected 結果數組, <b>getDataList</b>()</div></td></tr><tr><td class="paramNameCol">{return}</td> <td class="paramTypeCol">結果數組,</td> <td class="paramDescCol">沒有更多數據時返回 false</td></tr></table>
**源碼:**[sdk/php/util/XSDataSource.class.php#L220](https://github.com/hightman/xunsearch/blob/master/sdk/php/util/XSDataSource.class.php#L220) (**[顯示](#)**)
`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);
}`
返回一批數據
init()方法
<table class="summaryTable"><tr><td colspan="3"><div class="signature2">protected void <b>init</b>()</div></td></tr></table>
**源碼:**[sdk/php/util/XSDataSource.class.php#L146](https://github.com/hightman/xunsearch/blob/master/sdk/php/util/XSDataSource.class.php#L146) (**[顯示](#)**)
`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+.+?FROM\s/i',?'SELECT?COUNT(*)?AS?count?FROM?',?$sql);
????????$res?=?$this->db->query1($sql);
????????$this->limit?=?$res['count']?-?$this->offset;
????}
}`
Copyright ? 2008-2011 by [杭州云圣網絡科技有限公司](http://www.xunsearch.com)
All Rights Reserved.
- 權威指南
- 新手上路
- 最新主要變動
- 概述
- 關于 Xunsearch PHP-SDK
- 安裝、升級
- 體驗 demo 項目
- 開發規范
- 開發流程
- 了解基礎對象
- 基礎對象概述
- XS 項目
- XSException 異常
- XSDocument 文檔
- XSIndex 索引管理
- XSSearch 搜索
- XSTokenizer 分詞接口
- 編寫項目配置文件
- 項目配置詳解
- 自定義分詞器
- 編寫第一個配置文件
- 管理索引
- 索引概述
- 添加文檔
- 更新、修改文檔
- 刪除文檔
- 清空索引
- 平滑重建索引
- 使用索引緩沖區
- 自定義SCWS詞庫
- 使用搜索
- 搜索概述
- 構建搜索語句
- 獲取搜索匹配結果
- 獲取搜索匹配數量
- 獲取熱門搜索詞
- 獲取相關搜索詞
- 搜索建議和糾錯
- 按字段值分面搜索
- 使用輔助工具
- RequiredCheck 運行檢測
- Indexer 索引管理器
- Quest 搜索測試工具
- SearchSkel 生成搜索代碼
- IniWizzard 配置文件向導
- Logger 搜索日志管理
- 專題
- 同義詞搜索功能
- 在SDK中使用SCWS分詞
- API 指南
- XS
- XS
- XSCommand
- XSComponent
- XSDocument
- XSErrorException
- XSException
- XSFieldMeta
- XSFieldScheme
- XSIndex
- XSSearch
- XSServer
- XS.tokenizer
- XSTokenizer
- XSTokenizerFull
- XSTokenizerNone
- XSTokenizerScws
- XSTokenizerSplit
- XSTokenizerXlen
- XSTokenizerXstep
- XS.util
- XSCsvDataSource
- XSDataFilter
- XSDatabaseDataSource
- XSDebugFilter
- XSJsonDataSource
- XSUtil
- XS.util.db
- XSDatabase
- XSDatabaseMySQL
- XSDatabaseMySQLI
- XSDatabasePDO
- XSDatabasePDO_MySQL
- XSDatabasePDO_PgSQL
- XSDatabasePDO_SQLite
- XSDatabasePgSQL
- XSDatabaseSQLite
- XSDatabaseSQLite3
- XS.utilf
- XSDataSource
- 其它文檔
- 關于 xunsearch
- 特色和優勢
- Xunsearch 架構簡圖
- 下載 Xunsearch
- 商業服務與支持
- XunSearch 授權許可證