1 Star 0 Fork 10

hainuo/sql.class.php

forked from vb2005xu/sql.class.php 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sqlms.class.php 4.20 KB
一键复制 编辑 原始数据 按行查看 历史
<?php
# 本文件是依据wenjun童鞋希望有个代码层的主从支持,所提供的雏形...
# 通常情况下 我们使用主从的目的是进行 "读写分离"
# 那此处也不出意外是根据此方向来定义 "主写从读"
/**
* 简易的 主从 实现
*/
class SqlMS
{
/**
* 主库数据源
* @var SqlDataSource
*/
private static $ds_master;
/**
* 从库数据源
* @var SqlDataSource
*/
private static $ds_slaver;
static function init(array $config)
{
self::$ds_master = Sql::ds($config['master']);
self::$ds_slaver = Sql::ds($config['slaver']);
}
static function master()
{
static $master = null;
if (is_null($master)) $master = new SqlMaster(self::$ds_master);
return $master;
}
static function slaver()
{
static $slaver = null;
if (is_null($slaver)) $slaver = new SqlSlaver(self::$ds_slaver);
return $slaver;
}
}
class SqlMaster
{
function __construct(SqlDataSource $ds)
{
$this->ds = $ds;
}
## 自己封装一些 读操作
/**
* 从表中检索符合条件的一条记录
*
* @param string $table
* @param mixed $cond
* @param string $fields
* @param string $sort
*
* @return array
*/
function select_row($table, $cond=null, $fields='*', $sort=null)
{
return Sql::assistant( $this->ds )->select_row($table, $cond, $fields, $sort);
}
/**
* 从表中检索符合条件的多条记录
*
* @param string $table
* @param mixed $cond
* @param string $fields
* @param string $sort
* @param int|array $limit 数组的话遵循格式 ( offset,length )
* @param bool $calc 计算总个数
*
* @return array
*/
function select($table, $cond=null, $fields='*', $sort=null, $limit=null, $calc=false)
{
return Sql::assistant( $this->ds )->select($table, $cond, $fields, $sort, $limit, $calc);
}
/**
* 统计符合条件的记录的总数
*
* @param string $table
* @param mixed $cond
* @param string|array $fields
* @param boolean $distinct
*
* @return int
*/
function count($table, $cond=null, $fields='*', $distinct=false)
{
return Sql::assistant( $this->ds )->count($table, $cond, $fields, $distinct);
}
/**
* 执行 读 操作
*
* @param string $mode 模式 [MODE_READ_GETALL,MODE_READ_GETROW,MODE_READ_GETONE,MODE_READ_GETCOL]
* @param mixed $args 参数[不同模式参数不同,缺省为sql字符串]
* @param callback $cb 查询记录集的回调处理函数
*
* @return mixed
*/
function custom($mode, $args, $cb=NULL)
{
return Sql::read( $this->ds, $mode, $args, $cb)
}
}
class SqlSlaver
{
function __construct(SqlDataSource $ds)
{
$this->ds = $ds;
}
## 自己封装一些 写操作
/**
* 插入一条记录
*
* @param string $table
* @param array $row
* @param bool $pkval 是否获取插入的主键值
*
* @return mixed
*/
function insert($table, array $row, $pkval=false)
{
return Sql::assistant( $this->ds )->insert($table, $row, $pkval);
}
/**
* 更新表中记录
*
* @param string $table
* @param array $row
* @param mixed $cond 条件
*
* @return int
*/
function update($table, array $row, $cond=null)
{
return Sql::assistant( $this->ds )->update($table, $row, $cond);
}
/**
* 删除 表中记录
*
* @param string $table
* @param mixed $cond
*
* @return int
*/
function del($table, $cond=null)
{
return Sql::assistant( $this->ds )->del($table, $cond);
}
/**
* 向表中 某字段的值做 "加"运算
*
* @param string $table
* @param string $field
* @param int $incr
* @param mixed $cond
*
* @return int
*/
function incr_field($table, $field, $incr = 1, $cond=null)
{
return Sql::assistant( $this->ds )->incr_field($table, $field, $incr, $cond);
}
/**
* 执行 更新/删除 操作
*
* @param string $mode 模式 [MODE_WRITE_INSERT,MODE_WRITE_UPDATE,MODE_WRITE_DELETE]
* @param mixed $args 参数[不同模式参数不同,缺省为sql字符串]
* @param callback $cb 查询结果集的回调处理函数
*
* @return mixed
*/
function custom($mode, $args, $cb=NULL)
{
return Sql::write( $this->ds, $mode, $args, $cb)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hainuo/sql.class.php.git
git@gitee.com:hainuo/sql.class.php.git
hainuo
sql.class.php
sql.class.php
master

搜索帮助