使用正则表达式c#从文本文件中解析SQL命令

时间:2016-05-02 14:05:16

标签: c# sql parsing text

我有一个文本文件,其中包含sql命令除以' /'但是它还包含/ ** /和空格之类的注释。 我需要解析文件,以便我可以运行每个命令,但我需要忽略这些注释。 我已经完成了这个小代码,但由于某种原因它甚至没有给我文本文件中的一半行:

public string[] Parser(string caminho)
{
    using (StreamReader sr = new StreamReader(caminho, Encoding.Default))
    {
        string text = sr.ReadToEnd();
        string[] lines = text.Split('/');
        return lines;
    }
}
  

无论如何使用正则表达式来解决这个问题吗?

     

以下是带有命令的.txt文件的链接:

https://drive.google.com/file/d/0B1xc1ft-s78NTHdZdlpTTWlfLVE/view?usp=sharing

任何帮助都会得到很好的帮助。谢谢

以下是该文件的示例:

UPDATE APPLCONFIG SET GGDSC='3.5.96' WHERE GGCOD='APP_NAME'
/

UPDATE APPLCONFIG SET GGDSC='3.5.96' WHERE GGCOD='NEG_NAME'
/
UPDATE APPLCONFIG SET GGDSC='3.5.96' WHERE GGCOD='DAT_NAME'
/

ALTER TABLE PROPOSTAS ADD (COEFGERAL_PLT NUMBER(3,6) DEFAULT 0)
/

ALTER TABLE ORC
    ADD MercadoInt  VARCHAR2(1)
    ADD Coef_KrMo   NUMBER(20,10)
    ADD Coef_KrMt   NUMBER(20,10)
    ADD Coef_KrEq   NUMBER(20,10)
    ADD Coef_KrSb   NUMBER(20,10)
    ADD Coef_KrGb   NUMBER(20,10)
    ADD Coef_MDEmp  NUMBER(20,10)
    ADD Coef_MDLoc  NUMBER(20,10)
    ADD Abrv_MDLoc  VARCHAR2(10)
    ADD Dsc_MDLoc   VARCHAR2(50)
    ADD Arred_MDLoc VARCHAR2(1)
    ADD Arred_NDecs NUMBER(1)
/
UPDATE Orc set MercadoInt='N', Coef_KrMo=1, Coef_KrMt=1, Coef_KrEq=1, Coef_KrSb=1, Coef_KrGb=1, Coef_MDEmp=1, Coef_MDLoc=1, Abrv_MDLoc='', Dsc_MDLoc='', Arred_MDLoc='N', Arred_NDecs=0 WHERE MercadoInt IS NULL
/

DROP VIEW  VW_EXPSUBEEXCELL
/

另一部分有评论:

/

---------------------------------------------------------------------

UPDATE CDPREPORTSQL
SET COMANDOSQL_FROM =
'SELECT DESCONTO,EMPCOD,EMPDSC,LINVER,NOMESISTEMA,OBS,ORCCOD,ORCVER,PEDCOD,PEDDSC,
ROUND(PRCUNIT*#CAMBIO#,5) PRCUNIT,
ROUND(PRCUNITSEMDESC*#CAMBIO#,5) PRCUNITSEMDESC,
PROPCHECK,QTDGLOB,QTDPROP,REFCOD,REFDSC,EMPCODVER, COEFGERAL_PLT FROM #OWNER#.VW_PROPOSTAS', 
COMANDOSQL_WHERE = 
'WHERE ORCCOD=#ORCCOD# AND ORCVER=#ORCVER# AND NOMESISTEMA=#NOMESISTEMA# AND PEDCOD=#MYCOD#'
WHERE REPID = 'CDP0000057'
/

---------------------------------------------------------------------

DROP VIEW VW_PROPSRECEPSOUTROS
/

CREATE OR REPLACE FORCE VIEW VW_PROPSRECEPSOUTROS (orccod,
                                                              orcver,
                                                              nomesistema,
                                                              pedcod,
                                                              peddsc,
                                                              empcod,
                                                              empcodver,
                                                              empdsc,
                                                              pedtprec,
                                                              linver,
                                                              totproposta,
                                                              totdesconto,
                                                              totpropsel,
                                                              totmin
                                                             )
AS
SELECT   b.orccod, b.orcver, b.nomesistema, b.pedcod, b.peddsc, p.empcod,
            p.empcodver, h.empdsc, b.pedtprec, l.linver,
            SUM (  ((l.qtdorc * pp.indfis) * (  (p.prcunit * (1 + p.deconto))) * (1 + p.coefgeral_plt)
                    * (CASE
                          WHEN r.tprec = '1'
                             THEN o.coef_krmo
                          WHEN r.tprec = '2'
                             THEN o.coef_krmt
                          WHEN r.tprec = '3'
                             THEN o.coef_kreq
                          WHEN r.tprec = '4'
                             THEN o.coef_krsb
                          ELSE 1
                       END
                      )
                   )
                ) totproposta,

            SUM( (l.qtdorc * pp.indfis * p.prcunit ) -    ((l.qtdorc * pp.indfis) * (  (p.prcunit * (1 + p.deconto))) * (1 + p.coefgeral_plt)
                    * (CASE
                          WHEN r.tprec = '1'
                             THEN o.coef_krmo
                          WHEN r.tprec = '2'
                             THEN o.coef_krmt
                          WHEN r.tprec = '3'
                             THEN o.coef_kreq
                          WHEN r.tprec = '4'
                             THEN o.coef_krsb
                          ELSE 1
                       END
                      )
                   ) 
                ) * -1 as TOTDESCONTO,
           /* SUM
               (  (CASE
                      WHEN (NVL (p.deconto, 0) + NVL(p.coefgeral_plt,0)) <> 0
                         THEN (l.qtdorc * pp.indfis) * ((
                                   (p.prcunit * (case when p.deconto <> 0 then p.deconto else 1 end)) * 
                                    case when p.coefgeral_plt <> 0 then p.coefgeral_plt else 0 end)
                                 * (CASE
                                       WHEN r.tprec = '1'
                                          THEN o.coef_krmo
                                       WHEN r.tprec = '2'
                                          THEN o.coef_krmt
                                       WHEN r.tprec = '3'
                                          THEN o.coef_kreq
                                       WHEN r.tprec = '4'
                                          THEN o.coef_krsb
                                       ELSE 1
                                    END
                                   )
                                )
                      ELSE 0
                   END
                  )
                * (-1)
               ) totdesconto_old,*/
            SUM
               ((CASE
                    WHEN p.empsel = 1
                       THEN   ( (l.qtdorc * pp.indfis) * ((p.prcunit * (1 + p.deconto)) * (1 + p.coefgeral_plt))
                               * (CASE
                                     WHEN r.tprec = '1'
                                        THEN o.coef_krmo
                                     WHEN r.tprec = '2'
                                        THEN o.coef_krmt
                                     WHEN r.tprec = '3'
                                        THEN o.coef_kreq
                                     WHEN r.tprec = '4'
                                        THEN o.coef_krsb
                                     ELSE 1
                                  END
                                 )
                              )
                    ELSE 0
                 END
                )
               ) propsel,
              NVL (SUM (  (l.qtdorc * pp.indfis) * (SELECT MIN ((pp.prcunit * (1 + pp.deconto)) * ( 1 + pp.coefgeral_plt))
                             FROM propostas pp
                            WHERE p.orccod = pp.orccod
                              AND p.orcver = pp.orcver
                              AND p.nomesistema = pp.nomesistema
                              AND p.pedcod = pp.pedcod
                              AND p.recurcod = pp.recurcod
                              AND pp.prcunit > 0
                              AND p.prcunit = 0
                              AND NVL (p.obs, '@') = '@')
                       ),
                   0
                  )
            + NVL (SUM (  (l.qtdorc * pp.indfis) * (((p.prcunit * (1 + p.deconto)) * (1+p.coefgeral_plt))
                           * (CASE
                                 WHEN r.tprec = '1'
                                    THEN o.coef_krmo
                                 WHEN r.tprec = '2'
                                    THEN o.coef_krmt
                                 WHEN r.tprec = '3'
                                    THEN o.coef_kreq
                                 WHEN r.tprec = '4'
                                    THEN o.coef_krsb
                                 ELSE 1
                              END
                             )
                          )
                       ),
                   0
                  ) totmin
       FROM pedproposta b,
            propostas p,
            empresash h,
            recursos r,
            prcficha pp,
            articlin l,
            orc o
      WHERE o.orccod = b.orccod
        AND o.orcver = b.orcver
        AND o.nomesistema = b.nomesistema
        AND b.orccod = p.orccod
        AND b.orcver = p.orcver
        AND b.nomesistema = p.nomesistema
        AND b.pedcod = p.pedcod
        AND p.empcod = h.empcod
        AND p.orccod = r.orccod
        AND p.orcver = r.orcver
        AND p.nomesistema = r.nomesistema
        AND p.recurcod = r.recurcod
        AND p.recver = r.recver
        AND p.rgmcod = r.rgmcod
        AND p.orccod = pp.orccod
        AND p.orcver = pp.orcver
        AND p.nomesistema = pp.nomesistema
        AND p.recurcod = pp.recurcod
        AND p.recver = pp.recver
        AND p.rgmcod = pp.rgmcod
        AND pp.orccod = l.orccod
        AND pp.orcver = l.orcver
        AND pp.nomesistema = l.nomesistema
        AND pp.prccod = l.prccod
        AND pp.prcver = l.prcver
        and b.pedtprec = '2'
   GROUP BY b.orccod,
            b.orcver,
            b.nomesistema,
            p.empcod,
            p.empcodver,
            h.empdsc,
            b.pedtprec,
            l.linver,
            b.pedcod,
            b.peddsc
/

我需要获取命令并忽略其他所有内容。

1 个答案:

答案 0 :(得分:0)

一种方法是使用空字符串对/* ... */进行正则表达式替换,然后执行Split()。像

这样的东西
var foo = Regex.Replace(text, @"\/\**?\*\/", " ");

删除评论。当你进行拆分时,告诉它忽略空行,并过滤掉只包含空格的行:

var lines = foo.Split(new[]{'/'}, StringSplitOptions.RemoveEmptyEntries)
   .Where(line => !string.IsNullOrWhiteSpace(line))
   .ToArray();

但是,请注意,如果其中一个查询在引用字符串中包含/字符,则仍会出现问题。

使用可识别注释,引用字符串等的解析器,您可能最好这样做。但如果你能忍受这些限制,上述情况应该会很好。

这里是完整的代码:

public string[] Parser(string caminho)
{
    string text = File.ReadAllText(caminho);
    var foo = Regex.Replace(text, @"\/\**?\*\/", " ");
    var lines = foo.Split(new[]{'/'}, StringSplitOptions.RemoveEmptyEntries)
       .Where(line => !string.IsNullOrWhiteSpace(line))
       .ToArray();
    return lines;
}