设为首页 加入收藏

TOP

PB9写的一个拆解SQL语句的通用函数(一)
2014-11-23 22:54:10 来源: 作者: 【 】 浏览:16
Tags:PB9 一个 拆解 SQL 语句 通用 函数
将以下内容复制,另存为f_split_sql.srf 然后导入pbl中
[cpp] view plaincopy
$PBExportHeader$f_split_sql.srf
$PBExportComments$SQL操作:分割SQL语句为select、from、where、group by、order by 5个子句
global type f_split_sql from function_object
end type

forward prototypes
global subroutine replace_n (ref string input_str,readonly string replaced_str,readonly string replace_str)
global subroutine f_split_sql (string as_sql, ref string as_select, ref string as_from, ref string as_where, ref string as_group, ref string as_order)
end prototypes

global subroutine replace_n (ref string input_str,readonly string replaced_str,readonly string replace_str);string s1
string s2
long p
long n

p = 1

do while 1 = 1
p = pos(input_str,replaced_str,p)

if p = 0 then
exit
end if

s1 = mid(input_str,1,p - 1)
s2 = mid(input_str,p + len(replaced_str))
input_str = s1 + replace_str + s2
p = p + len(replace_str)
loop
end subroutine

global subroutine f_split_sql (string as_sql, ref string as_select, ref string as_from, ref string as_where, ref string as_group, ref string as_order);//====================================================================
// 过程(函数|Function): f_split_sql()
//--------------------------------------------------------------------
// 描述(Description): 拆解SQL语句
//--------------------------------------------------------------------
// 变量(Arguments):
// string as_sql 传入的SQL语句(如:select a from t where b = 1 group by c order by c)
// ref string as_select 拆解后的select语句(如:select a)
// ref string as_from 拆解后的from 语句(如:from t)
// ref string as_where 拆解后的where 语句(如:where b = 1)
// ref string as_group 拆解后的group 语句(如:group by c)
// ref string as_order 拆解后的order 语句(如:order by c)
//--------------------------------------------------------------------
// 返回(Returns): (none)
//--------------------------------------------------------------------
// 作者(Author): yyoinge Date: 2012-02-17 13:26
//--------------------------------------------------------------------
// 修改历史(Modify History):
//
//--------------------------------------------------------------------
as_sql = lower(trim(as_sql))
if as_sql = '' then return
as_select = ''; as_from = ''; as_group = ''; as_order = ''

replace_n(as_sql,'~r',' ')
replace_n(as_sql,'~n',' ')
replace_n(as_sql,'~r~n',' ')
replace_n(as_sql,'~t',' ')
replace_n(as_sql, '(select ',' ( select ')
replace_n(as_sql, ',select ',' , select ')
replace_n(as_sql,';',' ')
if leftw(as_sql, 7) <> 'select ' then return
long ll_gb_p, ll_ob_p, ll_fr_p, ll_se_p, ll_wh_p, ll_tmp, ll_yh_p
int li_leap = 0
//先剔除''内的内容,如:'abc'替换为@@@@@
string ls_sql
ls_sql = as_sql
do while true
ll_yh_p = posw(ls_sql, "'")
if ll_yh_p <= 0 then exit
ll_tmp = posw(ls_sql, "'", ll_yh_p + 1)
if ll_tmp <= 0 then return
ls_sql = leftw(ls_sql, ll_yh_p - 1) + fill('@', ll_tmp - ll_yh_p + 1) + midw(ls_sql, ll_tmp + 1)
loop

//******************************** select
ll_tmp = 8
li_leap = -1
do while true
ll_fr_p = posw(ls_sql, " from ", ll_tmp)
ll_se_p = posw(ls_sql, " select ", ll_tmp)
if ll_fr_p <= 0 then return
if ll_fr_p < ll_se_p or ll_se_p = 0 then
li_leap ++
else
li_leap --
end if
if li_leap = 0 then
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇SybaseIQ语句之BEGIN ATOMIC子句 下一篇pb9实现在两个工作簿之间进行工作..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: