多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > 數據庫 > sybase > PB9寫的一個拆解SQL語句的通用函數

PB9寫的一個拆解SQL語句的通用函數

來源:程序員人生   發布時間:2014-02-01 11:17:40 閱讀次數:3692次
將以下內容復制,另存為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 
        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 
 
調用時,5自學網,這樣寫:
[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的專欄 ,5自學網
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 91免费福利| 亚洲视频在线观看免费 | 噜噜色.com| 亚洲综合国产一区在线 | 国产精品一区二区三区免费视频 | 欧美日韩性视频一区二区三区 | 欧美jizzhd精品欧美巨大 | 在线观看www成人影院 | 亚洲图片在线 | 性色生活免费看性大片 | 成人看片又黄又爽 | 国产精品一区二区三区免费 | 亚洲精品久久久久久久久久ty | 黄色天堂网站 | 国产日韩高清一区二区三区 | 国产精品亚洲片夜色在线 | 亚洲黄色小说视频 | 激情久久久久久久久久久 | 爱爱欧美在线观看视频 | 亚州第一视频 | 亚洲 欧美 手机 在线观看 | 久久亚洲美女久久久久 | 亚洲精品国产专区一区 | 亚洲大片免费观看 | 亚洲精品视频在线看 | 日本一二三四区免费视频 | 黄色aa| 日本欧美一区二区三区视频 | 婷婷激情五月 | 中文字幕亚洲图片 | 欧美 日本 亚洲 | 一区二区三区视频免费观看 | 国产欧美日韩精品高清二区综合区 | 国产成人精品区在线观看 | 一级毛片高清大全免费观看 | 亚洲艹 | 亚洲国产爱 | 亚洲黄色在线观看网站 | 黄色免费大全 | 五月天免费在线视频 | 国产亚洲欧美另类专区 |