as_select = leftw(as_sql, ll_fr_p)
exit
end if
if ll_fr_p <= 0 then ll_fr_p = 999999
if ll_se_p <= 0 then ll_se_p = 999999
ll_tmp = min(ll_fr_p, ll_se_p) + 5
loop
if as_select = '' then return
//******************************** group by
ll_gb_p = lastpos(ls_sql, " group by ")
ll_ob_p = lastpos(ls_sql, " order by ")
if ll_gb_p > 0 then
if posw(ls_sql, " from ", ll_gb_p + 1) > 0 or posw(ls_sql, " where ", ll_gb_p + 1) > 0 then
as_group = ''
ll_gb_p = 0
else
if ll_ob_p > ll_gb_p then
as_group = midw(as_sql, ll_gb_p, ll_ob_p - ll_gb_p)
else
as_group = midw(as_sql, ll_gb_p)
end if
end if
else
as_group = ''
end if
//******************************** order by
if ll_ob_p > 0 then
if posw(ls_sql, " from ", ll_ob_p + 1) > 0 or posw(ls_sql, " where ", ll_ob_p + 1) > 0 then
as_order = ''
ll_ob_p = 0
else
as_order = midw(as_sql, ll_ob_p)
end if
else
as_order = ''
end if
//取得select from where 语句的最后一个字符的位置+1
if ll_gb_p <= 0 then ll_gb_p = ll_ob_p
if ll_gb_p <= 0 then ll_gb_p = lenw(as_sql) + 1
//******************************** where
as_sql = midw(leftw(as_sql,ll_gb_p - 1), ll_fr_p)
ls_sql = midw(leftw(ls_sql,ll_gb_p - 1), ll_fr_p)
ll_wh_p = lastpos(ls_sql, " where ")
if ll_wh_p <= 0 then
as_where = ''
else
ll_tmp = 5
li_leap = 0
do while true
ll_se_p = posw(ls_sql, "(", ll_tmp)
ll_fr_p = posw(ls_sql, ")", ll_tmp)
if ll_se_p > ll_wh_p then ll_se_p = 0
if ll_fr_p > ll_wh_p then ll_fr_p = 0
if (ll_se_p = 0 and ll_fr_p = 0) then exit
if ll_fr_p < ll_se_p or ll_se_p = 0 then
li_leap ++
else
li_leap --
end if
if ll_fr_p <= 0 then ll_fr_p = 999999
if ll_se_p <= 0 then ll_se_p = 999999
ll_tmp = min(ll_fr_p, ll_se_p) + 1
loop
if li_leap <> 0 then
as_where = ''
else
as_where = midw(as_sql, ll_wh_p)
as_sql = leftw(as_sql, ll_wh_p)
end if
end if
//******************************** from
as_from = as_sql
end subroutine
调用时,这样写:
[cpp] view plaincopy
string ls_sql, ls_select, ls_from, ls_where, ls_group, ls_order
ls_sql = dw_1.getsqlselect()
f_split_sql(ls_sql, ls_select, ls_from, ls_where, ls_group, ls_order)
即可将SQL语句ls_sql ,拆解为5个部分,之后就可以对各个部分进行添加、修改,然后重新组成新的SQL语句了
作者 yyoinge的专栏
|