搜索
首页php教程PHP源码SQL语句解析函数
SQL语句解析函数May 25, 2016 pm 05:14 PM

function SQL语句解析函数($sql)				{
	global $db,$MetaTables;
	//判断自定义表是否存在,如果不存在直接返回

	//判断是否是联合全操作,是否有子查询,是否用left 
	//如果有,则表示为手写SQL代码,不是系统生成,则直接返回,不进行过滤
	$sql		= trim($sql);
	$sqllower	= strtolower($sqllower);
	if(substr($sqllower,0,strlen("create table"))=="create table")		{
		return $sql;
	}
	if(substr($sqllower,0,strlen("drop table"))=="drop table")			{
		return $sql;
	}
	if(substr($sqllower,0,strlen("check table"))=="check table")		{
		return $sql;
	}
	if(substr($sqllower,0,strlen("optimize table"))=="optimize table")	{
		return $sql;
	}
	if(substr($sqllower,0,strlen("repair table"))=="repair table")		{
		return $sql;
	}
	if(substr($sqllower,0,strlen("analyze table"))=="analyze table")	{
		return $sql;
	}
	
	//进行关键字过滤
	$sql = eregi_replace(" From "," from ",$sql);
	$sql = eregi_replace(" FROM "," from ",$sql);

	$sql = eregi_replace(" Where "," where ",$sql);
	$sql = eregi_replace(" WHERE "," where ",$sql);

	$sql = eregi_replace(" Select "," select ",$sql);
	$sql = eregi_replace(" SELECT "," select ",$sql);

	$sql = eregi_replace(" Order By "," order by ",$sql);
	$sql = eregi_replace(" ORDER BY "," order by ",$sql);

	$sql = eregi_replace(" Update "," update ",$sql);
	$sql = eregi_replace(" UPDATE "," update ",$sql);

	$sql = eregi_replace(" Delete "," delete ",$sql);
	$sql = eregi_replace(" DELETE "," delete ",$sql);

	$sql = eregi_replace(" Limit "," limit ",$sql);
	$sql = eregi_replace(" LIMITE "," limit ",$sql);

	$sql = eregi_replace(" Left "," left ",$sql);
	$sql = eregi_replace(" LEFT "," left ",$sql);

	//处理SELECT
	if(substr($sql,0,strlen("select "))=="select ")		{
		$FromArray = explode(" from ",$sql);
		//分析旧的SQL
		if($FromArray[1]!="")					{
			$FromSelectArray		= explode("select ",$FromArray[0]);
			$SQLArray['SelectText'] = $FromSelectArray[1];
			$FromWhereArray			= explode(" where ",$FromArray[1]);
			$SQLArray['FromText']	= $FromWhereArray[0];
			//如果是两个表,直接返回,不做处理
			$FromTablesArray		= explode(",",$SQLArray['FromText']);
			if($FromTablesArray[1]!='')			{
				print "两个表";
				return $sql;
			}
			//拆分数据库和表
			$FromDBArray			= explode(".",$SQLArray['FromText']);
			if($FromDBArray[1]!="")					{
				$SQLArray['FromText']	= $FromDBArray[1];
				$SQLArray['DBText']		= $FromDBArray[0];
			}
			$SQLArray['WhereText']	= $FromWhereArray[1];
			$FromOrderByArray		= explode(" order by ",$SQLArray['WhereText']);
			if($FromOrderByArray[1]!="")					{
				$SQLArray['WhereText']	= $FromOrderByArray[0];
				$SQLArray['OrderByText']= $FromOrderByArray[1];
			}
		}
		//处理新的SQL,之前要进行判断表自定义表是否存在
		$TABLENAME = $SQLArray['FromText'];
		$TABLENAME2 = "view_".$TABLENAME;
		if(in_array($TABLENAME2,$MetaTables))					{
			//自定义表存在
			$MetaColumnNames	= $db->MetaColumnNames($TABLENAME);
			$MetaColumnNames	= array_keys($MetaColumnNames);
			$原表主键	= $MetaColumnNames[0];
			$MetaColumnNames2	= $db->MetaColumnNames($TABLENAME2);
			$MetaColumnNames2	= array_keys($MetaColumnNames2);
			$新表主键	= $MetaColumnNames2[0];
			array_shift($MetaColumnNames2);
			$自定义表字段列表 = join(',',$MetaColumnNames2);
			$SQLArray['SelectText'] .= ",".$自定义表字段列表;
			$SQLArray['FromText']	.= ",".$TABLENAME2;
			if($SQLArray['WhereText']!="")		{
				$SQLArray['WhereText'] .= " and ".$TABLENAME.".".$原表主键."=".$TABLENAME2.".".$新表主键."";
			}
			else	{
				$SQLArray['WhereText']  = " ".$TABLENAME.".".$原表主键."=".$TABLENAME2.".".$新表主键."";
			}
		}
		else	{
			//不存在,直接返回
			return $sql;
		}
		//形成新的SQL文件
		$NEWTEXTSQL = "select ".$SQLArray['SelectText']." from ".$SQLArray['FromText']."";
		if(TRIM($SQLArray['WhereText'])!="")		{
			$NEWTEXTSQL .=" where ".$SQLArray['WhereText'];
		}
		if(TRIM($SQLArray['OrderByText'])!="")		{
			$NEWTEXTSQL .=" order by ".$SQLArray['WhereText'];
		}
		//形成后返回
		return $NEWTEXTSQL;
		//SELECT  部分结束
	}
	
	//UPDATE

	//DELETE

	//INSERT INTO


	
	print_R($NEWTEXTSQL);
	print_R($SQLArray);
	

}

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
1 个月前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用