PB9写的一个拆解SQL语句的通用函数

将以下内容复制,另存为 f_split_sql.srf 然后导入pbl中

[cpp]  view plain copy
  1. $PBExportHeader$f_split_sql.srf  
  2. $PBExportComments$SQL操作:分割SQL语句为select、from、where、group by、order by 5个子句  
  3. global type f_split_sql from function_object  
  4. end type  
  5.   
  6. forward prototypes  
  7. global subroutine replace_n (ref string input_str,readonly string replaced_str,readonly string replace_str)  
  8. 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)  
  9. end prototypes  
  10.   
  11. global subroutine replace_n (ref string input_str,readonly string replaced_str,readonly string replace_str);string s1  
  12. string s2  
  13. long p  
  14. long n  
  15.   
  16. p = 1  
  17.   
  18. do while 1 = 1  
  19.     p = pos(input_str,replaced_str,p)  
  20.   
  21.     if p = 0 then  
  22.         exit  
  23.     end if  
  24.   
  25.     s1 = mid(input_str,1,p - 1)  
  26.     s2 = mid(input_str,p + len(replaced_str))  
  27.     input_str = s1 + replace_str + s2  
  28.     p = p + len(replace_str)  
  29. loop  
  30. end subroutine  
  31.   
  32. 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);//====================================================================  
  33. // 过程(函数|Function): f_split_sql()  
  34. //--------------------------------------------------------------------  
  35. // 描述(Description): 拆解SQL语句  
  36. //--------------------------------------------------------------------  
  37. // 变量(Arguments):   
  38. //              string  as_sql      传入的SQL语句(如:select a from t where b = 1 group by c order by c)  
  39. //      ref     string  as_select   拆解后的select语句(如:select a)  
  40. //      ref     string  as_from     拆解后的from  语句(如:from t)  
  41. //      ref     string  as_where    拆解后的where 语句(如:where b = 1)   
  42. //      ref     string  as_group    拆解后的group 语句(如:group by c)    
  43. //      ref     string  as_order    拆解后的order 语句(如:order by c)    
  44. //--------------------------------------------------------------------  
  45. // 返回(Returns):  (none)  
  46. //--------------------------------------------------------------------  
  47. // 作者(Author):  yyoinge     Date: 2012-02-17 13:26  
  48. //--------------------------------------------------------------------  
  49. // 修改历史(Modify History):   
  50. //    
  51. //--------------------------------------------------------------------  
  52. as_sql = lower(trim(as_sql))  
  53. if as_sql = '' then return  
  54. as_select = ''; as_from = ''; as_group = ''; as_order = ''  
  55.   
  56. replace_n(as_sql,'~r',' ')   
  57. replace_n(as_sql,'~n',' ')   
  58. replace_n(as_sql,'~r~n',' ')   
  59. replace_n(as_sql,'~t',' ')   
  60. replace_n(as_sql, '(select ',' ( select ')  
  61. replace_n(as_sql, ',select ',' , select ')  
  62. replace_n(as_sql,';','  ')   
  63. if leftw(as_sql, 7) <> 'select ' then return  
  64. long ll_gb_p, ll_ob_p, ll_fr_p, ll_se_p, ll_wh_p, ll_tmp, ll_yh_p  
  65. int li_leap = 0  
  66. //先剔除''内的内容,如:'abc'替换为@@@@@  
  67. string ls_sql  
  68. ls_sql = as_sql  
  69. do while true  
  70.     ll_yh_p = posw(ls_sql, "'")   
  71.     if ll_yh_p <= 0 then exit  
  72.     ll_tmp = posw(ls_sql, "'", ll_yh_p + 1)  
  73.     if ll_tmp <= 0 then return  
  74.     ls_sql = leftw(ls_sql, ll_yh_p - 1) + fill('@', ll_tmp - ll_yh_p + 1) + midw(ls_sql, ll_tmp + 1)      
  75. loop  
  76.   
  77. //********************************  select  
  78. ll_tmp = 8  
  79. li_leap = -1  
  80. do while true  
  81.     ll_fr_p = posw(ls_sql, " from ", ll_tmp)  
  82.     ll_se_p = posw(ls_sql, " select ", ll_tmp)  
  83.     if ll_fr_p <= 0 then return  
  84.     if ll_fr_p < ll_se_p or ll_se_p = 0 then  
  85.         li_leap ++  
  86.     else  
  87.         li_leap --  
  88.     end if  
  89.     if li_leap = 0 then  
  90.         as_select = leftw(as_sql, ll_fr_p)  
  91.         exit  
  92.     end if  
  93.     if ll_fr_p <= 0 then ll_fr_p = 999999  
  94.     if ll_se_p <= 0 then ll_se_p = 999999  
  95.     ll_tmp = min(ll_fr_p, ll_se_p) + 5  
  96. loop  
  97.   
  98. if as_select = '' then return  
  99.   
  100.   
  101. //********************************  group by  
  102. ll_gb_p = lastpos(ls_sql, " group by ")  
  103. ll_ob_p = lastpos(ls_sql, " order by ")  
  104. if ll_gb_p > 0 then  
  105.     if posw(ls_sql, " from ", ll_gb_p + 1) > 0 or posw(ls_sql, " where ", ll_gb_p + 1) > 0 then  
  106.         as_group = ''  
  107.         ll_gb_p = 0  
  108.     else  
  109.         if ll_ob_p > ll_gb_p then  
  110.             as_group = midw(as_sql, ll_gb_p, ll_ob_p - ll_gb_p)  
  111.         else  
  112.             as_group = midw(as_sql, ll_gb_p)  
  113.         end if  
  114.     end if  
  115. else  
  116.     as_group = ''  
  117. end if  
  118.   
  119. //********************************  order by  
  120. if ll_ob_p > 0 then  
  121.     if posw(ls_sql, " from ", ll_ob_p + 1) > 0 or posw(ls_sql, " where ", ll_ob_p + 1) > 0 then  
  122.         as_order = ''  
  123.         ll_ob_p = 0  
  124.     else  
  125.         as_order = midw(as_sql, ll_ob_p)  
  126.     end if  
  127. else  
  128.     as_order = ''  
  129. end if  
  130. //取得select from where 语句的最后一个字符的位置+1  
  131. if ll_gb_p <= 0 then ll_gb_p = ll_ob_p  
  132. if ll_gb_p <= 0 then ll_gb_p = lenw(as_sql) + 1  
  133.   
  134. //********************************  where  
  135. as_sql = midw(leftw(as_sql,ll_gb_p - 1), ll_fr_p)  
  136. ls_sql = midw(leftw(ls_sql,ll_gb_p - 1), ll_fr_p)  
  137. ll_wh_p = lastpos(ls_sql, " where ")  
  138. if ll_wh_p <= 0 then  
  139.     as_where = ''  
  140. else  
  141.     ll_tmp = 5  
  142.     li_leap = 0  
  143.     do while true  
  144.         ll_se_p = posw(ls_sql, "(", ll_tmp)  
  145.         ll_fr_p = posw(ls_sql, ")", ll_tmp)  
  146.         if ll_se_p > ll_wh_p then ll_se_p = 0  
  147.         if ll_fr_p > ll_wh_p then ll_fr_p = 0  
  148.         if (ll_se_p = 0 and ll_fr_p = 0) then exit  
  149.         if ll_fr_p < ll_se_p or ll_se_p = 0 then  
  150.             li_leap ++  
  151.         else  
  152.             li_leap --  
  153.         end if  
  154.         if ll_fr_p <= 0 then ll_fr_p = 999999  
  155.         if ll_se_p <= 0 then ll_se_p = 999999  
  156.         ll_tmp = min(ll_fr_p, ll_se_p) + 1  
  157.     loop  
  158.     if li_leap <> 0 then  
  159.         as_where = ''  
  160.     else  
  161.         as_where = midw(as_sql, ll_wh_p)  
  162.         as_sql = leftw(as_sql, ll_wh_p)  
  163.     end if  
  164. end if  
  165.   
  166. //********************************  from  
  167. as_from = as_sql  
  168.   
  169. end subroutine  


调用时,这样写:

[cpp]  view plain copy
  1. string ls_sql, ls_select, ls_from, ls_where, ls_group, ls_order  
  2.   
  3. ls_sql = dw_1.getsqlselect()  
  4.   
  5. f_split_sql(ls_sql, ls_select, ls_from, ls_where, ls_group, ls_order)  


 

即可将SQL语句ls_sql ,拆解为5个部分,之后就可以对各个部分进行添加、修改,然后重新组成新的SQL语句了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据窗口源码介绍 (举例见d_dbit_example.srd,测试数据库为PB自带的EAS Demo DB V120) 1.第一行:release 12; 数据窗口所属PB的版本号,如果想把高版本的datawindow导入低版本的PB中要修改此数字; 2.datawindow()行 通常用修改processing属性,可以把你的数据窗口由grid变成freeform或其它类型 例:freeform:0 grid:1 3.header()行 可以整行复制粘贴,并命名为header[1],header[2],这样可以让数据窗口有多个header 4.table()行 这里全是定义的数据属性,是setitem,update,object.data等方法访问的根源 如果存在table()里的column,数据窗口里可以不存在任何对象,仍然可以访问数据窗口的数据 如果想创建一个空的数据窗口,可以只定义: release 12; table() 5.table(column行) 是第几个column,它的ID就是多少,其中也定义了是否可以update时和更新到数据表的字段名 6.table(retrieve行) 检索数据时使用的SQL语句,可以通过setsqlselect或modify("table.sqlselect")方法去动态修改 修改后要重新定义数据窗口的更新属性 7.column()行 id即是对应table(column行)里的行次,名称(name)可以随便命名,也可以没有,则默认为table(column行)里的name 8.compute()行 计算列,可以通过表达式计算显示特定值(具体函数可以见共享里的画笔函数). 9.text()行 有时可以代替计算列; 通过定义text的表达式,可以实现一列中显示两个字段值,注意结果必须要转换成string类型(而column和compute则不行); 10.htmltable()行 按照格式导出(saveas)html文件,为了保证格式的完整,数据窗口的单位最好设置成pixels GenerateCSS='1' //保持格式及颜色 Border='0' //不要table边框 CellSpacing='0' //单元格无间距(此项在界面上经常设置不上) 注: (1)Grid生成<Table>标签,Freeform生成<DIV> + <SPAN>标签; (2)生成htmltable时,不知为何会在CSS中的开始自动加一个分号";",导致excel等浏览器打开不能正常显示格式; (3)band为foreground或background的对象导不出来,可以动态修改后再导出. 11.export.xml()行 定义导出xml文件的格式,并为模板保存名称 使用时一定要设置usetemplate值 12.import.xml()行 定义导入xml文件的格式,并为模板保存名称 使用时一定要设置usetemplate值 13.表达式介绍 略 14.其它也略
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值