/**

* SQL 简单查询工具类

*

* $tools = new SQLTools("表名", "数据库操作对象实例");

*$tools->query("字段默认为*") //(如无后续操作此处返回查询结果集)

* ->where( '条件', PDO参数化查询参数 ) //(如无后续操作此处返回查询结果集)

* ->group( 'id' ) //(如无后续操作此处返回查询结果集)

* ->order( 'id', 'desc' ) //(如无后续操作此处返回查询结果集)

* ->limit( 0, 100 ) //(如无后续操作此处返回查询结果集)

* ->toSQL(); // 返回拼接出来的SQL

*

*

*/

defined( 'SQL_TAG_QUERY' ) OR define( 'SQL_TAG_QUERY', 'query' );

defined( 'SQL_TAG_LIMIG' ) OR define( 'SQL_TAG_LIMIT', 'limit' );

defined( 'SQL_TAG_WHERE' ) OR define( 'SQL_TAG_WHERE', 'where' );

defined( 'SQL_TAG_ORDER' ) OR define( 'SQL_TAG_ORDER', 'order' );

defined( 'SQL_TAG_GROUP' ) OR define( 'SQL_TAG_GROUP', 'group' );

// to xx 自己加吧

defined( 'TO_SQL' ) OR define( 'TO_SQL', 'toSQL' );

class SQLTools{

public $id = null;

public $db = null;

public $tableName = null;

private $__code = null;

private $__query = null;

private $__where = null;

private $__param = null;

private $__limit = null;

private $__order = null;

private $__group = null;

/**

* 实例化

* @param $tableName stirng 表名

* @param $db 一个数据库操作对象,且必须有个叫query的方法,接受两个参数 sql 及 params

* @param $id 主键字段名

*/

public function __construct( $tableName, $db, $id=null ){

$this->db = $db;

$this->id = $id;

$this->tableName = $tableName;

}

public function query( $fields='*', $tableName=null ){

$tableName === null && ( $tableName = $this->tableName );

$this->__query = "SELECT $fields FROM $tableName ";

return $this->__setResultByCallCode( SQL_TAG_QUERY );

}

public function where( $where, $params ){

$this->__where = "WHERE $where ";

$this->__param = $params;

return $this->__setResultByCallCode( SQL_TAG_WHERE );

}

public function order( $fields, $sort ){

$this->__order = "ORDER BY $fields $sort ";

return $this->__setResultByCallCode( SQL_TAG_ORDER );

}

public function group( $fields ){

$this->__group = "GROUP BY $fields";

return $this->__setResultByCallCode( SQL_TAG_GROUP );

}

public function limit( $m, $n ){

$this->__limit = sprintf( 'LIMIT %d,%s ', $m, $n );

return $this->__setResultByCallCode( SQL_TAG_LIMIT );

}

public function toSQL(){ return $this->__setResultByCallCode( TO_SQL ); }

public function clear(){

$this->__code = null;

$this->__query = null;

$this->__where = null;

$this->__param = null;

$this->__limit = null;

$this->__order = null;

$this->__group = null;

}

// 真正查询的地方

private function __query( $tag ){

$__sql = $this->__query;

$this->__where !== null && ( $__sql .= $this->__where );

$this->__group !== null && ( $__sql .= $this->__group );

$this->__order !== null && ( $__sql .= $this->__order );

$this->__limit !== null && ( $__sql .= $this->__limit );

$result = $tag === TO_SQL ? $__sql : $this->db->query( $__sql, $this->__param );

$this->clear();

return $result;

}

/**

* 通过堆栈信息获取调用脚本后面调用方法,

* 根据方法生成相关返回对象

* @param $tag sql标签

* @return object

**/

private function __setResultByCallCode( $tag ){

if( $this->__code !== null ){

return $this->__createResult( $this->__code, $tag );

}

$info = debug_backtrace();

if( !is_array($info) ){

return null;

}

// 找到调用文件索引 ( 这里是通过文件名匹配的,如果改了文件名请自行修改这段代码 )

$index = -1;

foreach( $info as $counter => $item ){

if( isset($item['file']) ){

if( stripos($item['file'], 'SQLTools.class.php') > 0 ){

$index = $counter + 1; // 下一个item即调用文件

break;

}

}

}

// 没有找到调用信息

if( $index === -1 ){

return null;

}

// 堆栈中没有找到相关信息

$caller = $info[$index];

if( !isset($caller['file']) || !file_exists($caller['file']) || !isset($caller['line']) ){

return null;

}

$line = $caller['line'];

$file = @fopen( $caller['file'], "r" );

$counter = 1;

$code = '';

while( ($buffer = fgets($file)) !== false ){

if( $counter >= $line ){

$code .= $buffer;

if( substr( $buffer, -2, 1 ) == ';' ){

goto end;

}

}

$counter++;

}

end: isset( $file ) && @fclose( $file );

$code = str_replace( ' ', '', $code );

$code = str_replace( "\t", '', $code );

$code = str_replace( "\n", '', $code );

$code = explode( '->', $code );

return $this->__createResult( $code, $tag );

}

// 返回$this起到链接作用,又判断当前调用tag是否已经结束

private function __createResult( $code, $tag ){

$this->__code = $code;

foreach( $this->__code as $code){

if( stripos($code, $tag) === 0 && substr( $code, -1 ) === ';' ){ // 判断查询结束

return $this->__query( $tag );

}

}

return $this;

}

}

php sql desc,PHP SQL 查询封装相关推荐

  1. SQL语法之排序查询(进阶3)and常见函数(进阶4)

    SQL语法体系学习笔记 SQL语法之基础查询(进阶1)and条件查询(进阶2) SQL语法之排序查询(进阶3)and常见函数(进阶4) SQL语法之分组函数,分组查询(进阶5)and连接查询(sql9 ...

  2. MySQL中SQL语句——DQL(数据查询语句)

    前言: 首先对于数据库有一定的了解,会对于Mysql的学习有一定的帮助: 数据库主要分为 DB(数据库),DBMS(数据库管理系统),SQL(结构化查询语言,用于和DBMS通信的语言):这篇要讲的DQ ...

  3. MySQL 数据库 之 高级 SQL 语句(常用查询,正则表达式,运算符,库函数,存储过程)

    文章目录 前言 一 . 常用查询介绍 1.按关键字排序 1.1默认升序 1.2 降序 1.3 多字段的排序 2. 对结果进行分组 2.1 分组统计 3.限制结果条目 3.1 查看前4行 3.2 查看第 ...

  4. Hive、Hadoop完全分布式安装,基本SQL,基本SQL,基本表结构,数据类型,函数,窗口函数,jion,查询和排序,beeline,文件格式及基本架构汇总

    目录 Hive简介 一.概述 二.Hive和数据库的比较 三.特点 Hadoop完全分布式安装 一.配置 二.安装步骤 Hive安装 一.概述 二.安装步骤 其他 一.Hive运行日志 二.Hive的 ...

  5. ylb:SQL 表的高级查询-多表连接和子查询

    ylbtech-SQL Server: SQL Server-表的高级查询-多表连接和子查询 SQL Server 表的高级查询-多表连接和子查询. 1,ylb:表的高级查询-多表连接和子查询 返回顶 ...

  6. SQL进阶,子查询与窗口函数

    本节给大家讲解SQL在实际过程中用途比较多的子查询与窗口函数,下面一起学习. 示例工具:MySQL8.0.Navicat Premium 12 本文讲解内容:子查询与窗口函数 适用范围:SQL进阶应用 ...

  7. Hibernate使用原生SQL适应复杂数据查询

    HQL尽管容易使用,但是在一些复杂的数据操作上功能有限.特别是在实现复杂的报表统计与计算,以及多表连接查询上往往无能为力,这时可以使用SQL(Native SQL)实现HQL无法完成的任务. 1.使用 ...

  8. 从SQL过渡至MongoDB查询对照表

    从SQL过渡至MongoDB查询对照表 SQL 说明 Mongo 说明 CREATE TABLE USERS (a Number, b Number) Implicit or use MongoDB: ...

  9. Mongo DB教程及SQL与Mongo DB查询的映射

    目录 介绍 在机器上设置Mongo DB 启动Mongo DB 下载RoboMongo MongoDB术语 MongoDB的要点 查询时间到了 MongoDB函数 MongoDB中的自动递增ID(SQ ...

最新文章

  1. 调用webservice 的时候没法输入参数
  2. oracle中常见ck和fk是什么,Oracle常用知识总结
  3. python中二分查找什么意思_python中二分查找法的实现方法
  4. 直接运行内存中的代码
  5. Android之使用HandlerThread 以及如何退出总结
  6. 随想录(做自己代码的测试工程师)
  7. php自定义函数记录一
  8. gimp 架构_常见GUI 框架
  9. 整理发布一些关于VMware vSphere的文档
  10. 高质量程序设计指南c++/c语言(19)--赋值操作符“=”
  11. SEO之搜索引擎简史
  12. 使用spack mirror简化离线环境的包安装
  13. PHP爬虫最全总结 -phpQuery,PHPcrawer,snoopy框架中文介绍
  14. 不想重置路由器,如何由已连接设备快速获取wifi密码?
  15. 两年过去了,4399依旧坚挺,Flash还能继续?
  16. 【无标题】2.8Multisim模电实验
  17. 在 Python 中通过知识图谱进行语义搜索(spaCy 教程含源码)
  18. 水晶报表之分页预留空白方便打印信纸
  19. android Camera自动拍照、自动对焦并获取指定大小图片
  20. 机房三维可视化动力环境监控系统

热门文章

  1. android 继承listview,Android listView 继承ListActivity的用法
  2. 后台窗口截图_万人拥捧的截图软件来啦!让你的工作效率提升不止一倍!
  3. 8051 管脚定义_8051微控制器的引脚说明
  4. 轮询锁使用时遇到的问题与解决方案!
  5. Redis 如何处理已经过期的数据?
  6. 面试官 | 如何优雅的设计Java 异常?
  7. Servlet页面跳转实现方法的区别
  8. current of 使用
  9. 精通ASP.NET MVC ——控制器可扩展性
  10. ASP.NET MVC 使用Log4Net在不同目录中记录不同类型的日志