在CI框架下执行存储的方法
我直接把代码摆在这里分享哈
<?php
/**
*
* Created by JetBrains PhpStorm.
* User: lsl
* Date: 14-1-8
* Time: 下午2:55
* To change this template use File | Settings | File Templates.
*/ define("QUERY_KEY", 1);
define("QUERY_INDEX", 2);
define("QUERY_BOTH", 3); /**
* 自定义数据库工具类,为了满足CI框架不能
* 在CI里面调用方式如下
* 1、在model的构造函数里面加载该类 $this->load->library('pdb');
* 2、在任一方法内调用方式如下
* $sqlParams[0] = new SqlParam('参数1',参数值1);
$sqlParams[1] = new SqlParam('参数2',参数2);
$sqlParams[2] = new SqlParam('@returnValue');参数返回值
$sqlParams[2]->Direction = Direction::$ReturnValue;
$procname = '存储过程名称';
$dt = $this->pdb->setDatabase("db_account")->runProcReturnTable($procname,$sqlParams,QUERY_KEY);
* $result = $sqlParams[2]->ParamValue;
* Class Pdb
*/
class Pdb { public $db_handler = array();
private $db_name; /**
* 构造函数
*/
function __construct(){
//TODO 这里暂时不需要做任何事情
} function &setDatabase($db_name){
$this->db_name = $db_name;
return $this;
} /**
* 析构函数
*/
function __destruct() { if(!is_null($this->db_handler[$this->db_name])){
$this->db_handler[$this->db_name]->close();//断开数据库连接
unset($this->db_handler[$this->db_name]);
}
} public function getDb(){
$db = array();
//加载配置文件
if ( file_exists($file_path = APPPATH.'config/'.'database.php'))
{
include($file_path);
} else {
show_error('The configuration file database.php does not exist.');
}
if(is_null($this->db_handler[$this->db_name])){
$this->db_handler[$this->db_name] = mysqli_connect($db[$this->db_name]['hostname'], $db[$this->db_name]['username'],$db[$this->db_name]['password'],$db[$this->db_name]['database']) or die("Could not connect: " . mysql_error() . "<br/>");
$this->db_handler[$this->db_name]->query("SET names ".$db[$this->db_name]['char_set']);
}
return $this->db_handler[$this->db_name];
} /**
* 获取单行结果集
* @param $sql
* @return mixed
*/
public function getRow($sql){
$db = $this->getDb();
$result = $db->query($sql);
$row = $result->fetch_array();
return $row;
} /**
* 执行sql语句,返回结果集
* @param $sql sql语句
* @return $result 返回结果集
*/
public function getResult($sql){
$db = $this->getDb();
$result = $db->query($sql);
return $result;
} /**
* 执行sql语句,返回结果集
* @param $sql sql语句
* @return $datatable array(0=>array("id"=>1),1=>array("id"=>2))
*/
public function returnDataTable($sql,$type = 3){
$db = $this->getDb();
$result = $db->query($sql);
if($result){
$row = $result->fetch_array($type);
$i=0;
while ($row){
$datatable[$i] = $row;
$i++;
$row = $result->fetch_array($type);
}
}
$datatable = $datatable?$datatable:array();
@mysqli_free_result($result);
return $datatable;
} /**
* 执行sql同时将结果集已多维数组形式返回
* @param $sql
* @param int $type
* @return mixed
*/
public function returnDataTables($sql,$type = 3){
$db = $this->getDb();
if($db->multi_query($sql)){
$j=0;
do{
$result = $db->store_result();
if ($result){
//获取第一个结果集
$row = $result->fetch_array($type);
$i=0;
while ($row){
$datatable[$j][$i] = $row;
$i++;
$row = $result->fetch_array($type);
}
$result->close(); //关闭一个打开的结果集
}
$j++;
} while($db->next_result());
}
@mysqli_free_result($result);
return $datatable;
} /**
* 通过存储参数获得sql语句
* @param $procname
* @param null $params
* @return string
*/
public function getProcSql($procname,$params=NULL){
$sql = "call ".$procname."(";
if($params){
$sqlOutPut = "select ";
for($i=0;$i<count($params);$i++){
if($params[$i]->Direction == Direction::$Output){
$sql .= $params[$i]->SqlParamName;
$sqlOutPut = $sqlOutPut.$params[$i]->SqlParamName.",";
$sql .= ",";
} else if($params[$i]->Direction == Direction::$Intput){
$sql .= "'".$params[$i]->ParamValue."',";
}
}
if(count($params)>0){
$sql = substr($sql, 0, strlen($sql)-1).");";
$sqlOutPut = substr($sqlOutPut, 0, strlen($sqlOutPut)-1).";";
}
}else {
$sql .= ");";
}
if(strlen($sqlOutPut)>7){
$sql .= $sqlOutPut;
}
return $sql;
} /**
* 执行存储同时返回结果集
* @param $procname
* @param null $params
* @param int $type
* @return array
*/
public function runProcReturnTable($procname,$params=NULL,$type = 3){
$db = $this->getDb();
//构建存储过程语句
$sql = $this->serializationProc($procname, $params,$db);
$result = $db->query($sql);
if($result){
$row = $result->fetch_array($type);
$i=0;
while ($row){
$datatable[$i] = $row;
$i++;
$row = $result->fetch_array($type);
}
}
$datatable = $datatable?$datatable:array();
@mysqli_free_result($result);
return $datatable;
} /**
* 执行存储过程
* @param string 存储过程名称
* @param array 参数数组 array(0=>SqlParam)
* @return string 返回构建的sql语句,用于调试
*/
public function runProc($procname,$params=NULL){
//执行存储过程,取回返回值与输出参数
$db = $this->getDb();
//构建存储过程语句
$sql = $this->serializationProc($procname, $params, $db);
if($db->multi_query($sql)){
$result = $db->store_result();
if($result){
$row = $result->fetch_array(2);
if($row){
for($i=0;$i<count($params);$i++){
if($params[$i]->Direction == Direction::$ReturnValue){
$params[$i]->ParamValue = $row[0];
}
}
}
}
do{
$result = $db->store_result();
if ($result) {
//获取第一个结果集
$row = $result->fetch_array(1);
for($i=0;$i<count($params);$i++){
if($params[$i]->Direction == Direction::$Output){
$params[$i]->ParamValue = $row[$params[$i]->SqlParamName];
}
}
$result->close(); //关闭一个打开的结果集
}
} while($db->next_result());
}
@mysqli_free_result($result);
return true;
} /**
* 序列号存储过程,将参数转换成sql的形式
* @param string 存储过程名称
* @param array 参数数组 array(0=>SqlParam)
* @param db 存储连接DB
* @return string 返回构建的sql语句
*/
private function serializationProc($procname,$params,&$db){
$sql = "call ".$procname."(";
if(count($params)>0){
$sqlOutPut = "select ";
foreach ($params as $v) {
if($v->Direction == Direction::$ReturnValue){
continue;
}
if(strpos($v->SqlParamName, "@") === FALSE){
$v->SqlParamName = "@".$v->SqlParamName;
}
$db->query("set ".$v->SqlParamName."='".$v->ParamValue."';");
$sql .= $v->SqlParamName;
$sql .= ",";
if($v->Direction == Direction::$Output){
$sqlOutPut .= $v->SqlParamName.",";
}
}
if(count($params)>0){
$sql = substr($sql, 0, strlen($sql)-1).");";
$sqlOutPut = substr($sqlOutPut, 0, strlen($sqlOutPut)-1).";";
}
}else {
$sql .= ");";
}
if(strlen($sqlOutPut)>7){
$sql .= $sqlOutPut;
}
return $sql;
} } /**
* 定义存储参数类型
* Class Direction
*/
class Direction{
public static $Intput = 1;
public static $Output = 2;
public static $ReturnValue = 3;
} /**
*
* Class SqlDBType
*/
class SqlDBType{
public static $Int = 1;
public static $Varchar = 2;
public static $DateTime = 3;
} /**
* 存储过程参数定义
* Class SqlParam
*/
class SqlParam{
public $Direction = 1;
public $SqlDBType = 1;
public $SqlParamName;
public $ParamValue;
public function SqlParam($ParamName = null,$ParamValue = null){
$this->SqlParamName = $ParamName;
if(!is_numeric($ParamName)){
$this->ParamValue = addslashes($ParamValue);
}else{
$this->ParamValue = $ParamValue;
}
}
public function setDirection($SqlDirection){
$this->Direction = $SqlDirection;
}
public function setSqlDBType($SqlDBType){
$this->SqlDBType=$SqlDBType;
}
public function setParamName($ParamName){
$this->ParamName=$ParamName;
}
public function setParamValue($ParamValue){
$this->ParamValue=$ParamValue;
}
}
在CI框架下执行存储的方法的更多相关文章
- CI 框架下执行CLI(命令行)
1.可以按照Ci官方文件的指导来进行操作 让我们先创建一个简单的控制器,打开你的文本编辑器,新建一个文件并命名为 Tools.php,然后输入如下的代码: <?php class Tools e ...
- Scrapy爬虫框架下执行爬虫的方法
在使用Scrapy框架进行爬虫时,执行爬虫文件的方法是 scrapy crawl xxx ,其中 xxx 是爬虫文件名. 但是,当我们在建立了多个文件时,使用上面的命令时会比较繁琐麻烦,我们就可以使用 ...
- CI框架下CSS和JS的路径问题
注意:CI框架下的CSS和JS的引用必须放在框架外面,比如,可建立resource文件夹与application同级,用来封装CSS和JS. 在view层用resource里面CSS和JS可采用以下几 ...
- 在shell下执行命令的方法
在shell下执行命令的方法 1. #!/bin/sh 语法:在shell.sh的开头写入 #!/bin/sh 一般的shell脚本就是这种用法.这种方法调用脚本开头的shell执行命令,子shell ...
- CI框架下 新浪微博登录接口完整版
https://www.cnblogs.com/yznyzcw/p/3756622.html#top 说明:本贴只适合CI框架.功能实现:登录接口跳转链接成功,获取用户信息(包括最重要的u_id)成功 ...
- 全面解析Pytorch框架下模型存储,加载以及冻结
最近在做试验中遇到了一些深度网络模型加载以及存储的问题,因此整理了一份比较全面的在 PyTorch 框架下有关模型的问题.首先咱们先定义一个网络来进行后续的分析: 1.本文通用的网络模型 import ...
- CI框架下的get_instance() 函数
你随便下个CI框架的源码都会看到很多的get_instance() 函数,这个函数是用来获取CI 的全局超级对象,CI 是单例模式的框架,所有全局有一个超级对象.因为只有一个实例,所以无论这个函数使用 ...
- CI框架下的PHP增删改查总结
controllers下的 cquery.php文件 <?php class CQuery extends Controller { //构造函数 function CQuery() { par ...
- PHP 之 Ci框架下隐藏index.php
1. 修改 apache 配置文件 开启重写模块 conf/httpd.conf 去掉前面的# LoadModule rewrite_module modules/mod_rewrite.so 对于U ...
随机推荐
- Storm的数据可靠性(理论)
Storm的数据可靠性(理论) .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB& ...
- javascript 中字符串之比较
<script type="text/javascript"> var string1="apple"; var string2="Ban ...
- 学习iOS开发的前言
一.什么是iOS 要想学习iOS开发,首先要搞清楚什么是iOS.iOS其实是一款操作系统,就像平时我们在电脑上用的XP.Win7,都是操作系统. 那什么是操作系统呢?操作系统其实是一种软件,是直接运行 ...
- 【翻译】MVC Music Store 教程-概述(三)
Controller 与传统的Web框架,将传入的URL通常映射到磁盘上的文件.例如:一个URL请求“/Products.aspx" 或"/Products.php”是处理一个Pr ...
- SQL Server 向堆表中插入数据的过程
堆表中 IAM 记录着的数据页,表的各个数据页之间没有联系.也就是说一个页面它不会知道自己的前一页是谁,也不知道自己的后一页是谁. 插入数据时先找到IAM页,再由pfs(page free spac ...
- visual leak dector内存泄漏检测方法
http://vld.codeplex.com/ QT 内存泄露时,你们一般用什么工具检测啊 ------解决方案--------------------这篇你觉得详细么 :http://newfac ...
- oracle字符集
oracle server端字符集
- ios webview 加载含有中文的URL网页显示白屏
1. ios中的webview加载的URL不可以含有中文,解决办法说将中文字符转码, 如下: - (NSString *)URLEncodeString { NSCharacterSet *set = ...
- 【翻译】在Ext JS 5种使用ViewControllers
原文:Using ViewControllers in Ext JS 5 简单介绍 在Ext JS 5中,在应用程序架构方面提供了一些令人兴奋的改进,如加入了ViewModels.MVVM以及view ...
- JAVA 内存的认识【转】
[转]:http://blog.sina.com.cn/s/blog_68158ebf0100wp83.html 一.Java内存的构成 先上一个官方java document里的图: 由上图 ...