1. 類文件位置 Core/lib/activerecord.php
調用方法:
~~~
dbpre將被替換成數據庫配置中配置的表前綴 dns['dbpre']
一維數組(有數據)或NULL(無數據)$_G['db']->from('dbpre_product_package') -> where('id',1) -> get_one();
二維數組(有數據)或NULL(無數據) = $_G['db']->from('dbpre_product_package') -> where('id',1) -> get_all();
數據庫句柄(資源類型Resource)=$_G['db']->from('dbpre_product_package') -> where('id',1) -> get();
數據庫句柄(資源類型Resource)=$_G['db']->from('dbpre_product_package') -> where('id',1) -> get();
//2張表進行left join 操作并查詢數據
$atts_q = $_G['db'] ->join('dbpre_product','p.id', 'dbpre_product_package','pkg.pids', 'LEFT JOIN') ->where('pids','6') -> get_sql();
~~~
類文件內容:
~~~
<?php
/**
* 數據庫操作
* 至少依賴ms_mysql,ms_sqlbuild類
*/
class ms_activerecord
{
//兼容舊版變量
public $dns = array();
protected $db;
protected $sqlbuild;
function __construct($dsn = null)
{
if ($dsn) {
$this->db = new ms_mysql($dsn);
$this->db->connect();
$this->dns = $this->db->dns;
}
$this->new_sqlbuild();
}
/**
* 對調用本類的函數進行解析,本類不存在的從ms_mysql和ms_sqlbuild中查找
* @param string $method
* @param array $args
* @return mixed
*/
function __call($method, $args)
{
if (! $this->sqlbuild) {
$this->new_sqlbuild();
}
if (method_exists($this, $method)) { //從本類查找
return call_user_func_array(array($this, $method), $args);
} elseif (method_exists($this->db, $method)) { //從 ms_mysql 類查找
return call_user_func_array(array($this->db, $method), $args);
} elseif (method_exists($this->sqlbuild, $method)) { //從 ms_sqlbuild 類查找
return call_user_func_array(array($this->sqlbuild, $method), $args);
} else {
//找不到方法,則拋出異常
throw new Exception(sprintf(
'The required method "%s" does not exist for %s',
$method,
get_class($this))
);
}
}
/**
* 設置一個mysql操作類
* @param ms_mysql $db [description]
*/
function set_db(ms_mysql $db)
{
$this->db = $db;
$this->dns = $this->db->dns;
}
/**
* 獲取db類
* @return ms_mysql
*/
function get_db()
{
return $this->db;
}
/**
* 新建一個sqlbuild類
*/
function new_sqlbuild()
{
$this->sqlbuild = new ms_sqlbuild($this);
}
/**
* 從組合的sql中獲取 mysql_result 類
* @param string $method 查詢模式,可輸入:unbuffer
* @param boolean $clear_var
* @return mixed
*/
function get($method = '', $clear_var = TRUE)
{
$SQL = $this->sqlbuild->get_sql(0);
$result = $this->db->query($SQL, $method);
$this->sqlbuild->_cache_sql(); //sql cache
if($clear_var) {
$this->sqlbuild->clear();
}
return $result;
}
/**
* 從數據中獲取數據
* @param string|array $select 需要獲取的表字段名
* @param string $from 表名
* @param array $where 查詢條件
* @param string|array $group_by group by
* @param string|array $order_by order by
* @param string|array $limit limit
* @return array
*/
function get_easy($select, $from, $where = null, $group_by = null, $order_by = null, $limit = null)
{
$this->select($select);
$this->from($from);
if($where) {
foreach($where as $sk => $sv) {
$this->where($sk, $sv);
}
}
if($group_by) $this->group_by($group_by);
if($order_by) $this->order_by($order_by[0], $order_by[1]);
if($limit) $this->limit($limit[0],$limit[1]);
return $this->get();
}
/**
* 獲取一條數據
* @param string $method
* @param boolean $clear_var
* @return mixed
*/
function get_one($method = '', $clear_var = TRUE)
{
if(!$this->limit) $this->sqlbuild->limit(0,1);
$SQL = $this->sqlbuild->get_sql(0);
$result = $this->db->query($SQL, $method);
$this->sqlbuild->_cache_sql(); //sql cache
if($clear_var) {
$this->sqlbuild->clear();
}
if($result) {
return $result->fetch_array();
} else {
return false;
}
}
/**
* 獲取一個字段數據
* @param boolean $clear_var
* @return mixed
*/
function get_value($clear_var = TRUE)
{
$this->sqlbuild->limit(0,1);
$SQL = $this->sqlbuild->get_sql(0);
$result = $this->db->query($SQL);
$this->sqlbuild->_cache_sql(); //sql cache
if($clear_var) {
$this->sqlbuild->clear();
}
return $result ? $result->result(0) : FALSE;
}
/**
* 獲取查詢到的所有結果,并以數組方式返回
* @param string $keyname 自定義數組鍵名,通過數據集內指定的字段名填充
* @param string $value_name 自定義數據值,通過數據集內指定的數據值填充
* (數組鍵值不返回SQL里指定的字段值,返回一個指定的字段名稱對應的值)
* @param boolean $clear_var [description]
* @return array
*/
function get_all($key_name = '', $value_name = '', $clear_var = TRUE) {
$q = $this->get();
if(!$q) return;
$value_name_list = trim($value_name_list);
if($value_name_list) $value_name_list = explode(',',$value_name_list);
$result = array();
while ($v = $q->fetch_array()) {
if($key_name && isset($v[$key_name])) {
$result[$v[$key_name]] = isset($v[$value_name])?$v[$value_name]:$v;
} else {
$result[] = isset($v[$value_name])?$v[$value_name]:$v;
}
}
return $result;
}
/**
* 返回數據條數
* @param boolean $clear_var
* @return int
*/
function count($clear_var = TRUE)
{
$SQL = $this->sqlbuild->get_sql(1);
$row = $this->db->query($SQL);
$this->sqlbuild->_cache_sql();
if($clear_var) {
$this->sqlbuild->clear();
}
return $row->result(0);
}
/**
* 執行插入(替換)表操作,返回受影響數據行數
* @param boolean $replace
* @param boolean $clear_var
* @return int
*/
function insert($replace = FALSE, $clear_var = TRUE)
{
$SQL = $this->sqlbuild->insert_sql($replace, 0);
$arows = $this->db->exec($SQL);
$this->sqlbuild->_cache_sql();
if($clear_var) {
$this->sqlbuild->clear();
}
return $arows;
}
/**
* 更新表數據,返回受影響數據行數
* @param boolean $clear_var
* @return int
*/
function replace($clear_var=TRUE)
{
$SQL = $this->sqlbuild->insert_sql(true, 0);
$arows = $this->db->exec($SQL);
$this->sqlbuild->_cache_sql();
if($clear_var) {
$this->sqlbuild->clear();
}
return $arows;
}
/**
* 更新數據,返回受影響數據行數
* @param boolean $clear_var
* @return int
*/
function update($clear_var = TRUE)
{
$SQL = $this->sqlbuild->insert_sql(0, 1);
$arows = $this->db->exec($SQL);
$this->sqlbuild->_cache_sql();
if($clear_var) {
$this->sqlbuild->clear();
}
return $arows;
}
/**
* 執行刪除操作,返回受影響數據行數
* @param boolean $clear_var
* @return int
*/
function delete($clear_var=TRUE)
{
$SQL = $this->sqlbuild->delete_sql();
$arows = $this->db->exec($SQL);
$this->sqlbuild->_cache_sql();
if($clear_var) {
$this->sqlbuild->clear();
}
return $arows;
}
/**
* 清空一個表
*/
function clear_table() {
if(!$this->sqlbuild->from) {
show_error(lang('global_sql_invalid', 'FROM(Clear Table)'));
}
$this->db->exec("TRUNCATE TABLE " . $this->sqlbuild->from);
return $this->affected_rows();
}
/**
* 刪除一個表
*/
function drop_table() {
if(!$this->sqlbuild->from) {
show_error(lang('global_sql_invalid', 'FROM(Dorp Table)'));
}
$this->db->exec("DROP TABLE IF EXISTS " . $this->sqlbuild->from);
}
}
/** end **/
~~~