简体   繁体   English

使用带有 Scala 异常的正则表达式进行模式匹配

[英]Pattern matching using regex with Scala Anorm

I'm Using Scala(2.11) and playframework(2.3) and trying to run a query using a helper function to get results through pattern matching.我正在使用 Scala(2.11) 和 playframework(2.3) 并尝试使用帮助程序 function 运行查询,以通过模式匹配获得结果。 The function is as follows function如下

def resultsfunc() = {

   val gradeRegex = "^Class 5\."
   val currRegex = "\.NCERT$"

   DB.withConnection{ implicit c =>
     val filterQuery = SQL(
      """
        select * from tbl_graphs
        where graph_name REGEXP '{grade_regex}' and
        graph_name REGEXP '{curr_regex}' and org_id = 4
      """)
      .on("grade_regex" -> gradeRegex,
          "curr_regex" -> currRegex)

    filterQuery().map{ graphRecord =>
        
        new ResultObj(graphRecord[Long]("id"),
                    graphRecord[String]("name"))

       }.toList
    }
}

I don't get any errors but I get empty result even though there are multiple records that match the pattern.我没有收到任何错误,但即使有多个与模式匹配的记录,我也会得到空结果。 The same query works if I try to run in mysql workbench and when I tried to print filterQuery the arguments were also mapped correctly.如果我尝试在 mysql 工作台中运行,并且当我尝试打印filterQuery时,同样的查询也有效,arguments 也被正确映射。

Should Pattern matching with regex must be carried out differently in Scala Anorm?是否应该在 Scala 异常中以不同方式执行与正则表达式的模式匹配?

It has absolutely nothing to do specifically with Anorm.它与 Anorm 完全没有关系。

  1. Make sure that executing manually the query with exactly the same data and parameter, you get result.确保使用完全相同的数据和参数手动执行查询,得到结果。
  2. When using JDBC (even through Anorm), string parameter must not be quoted in the query statement ( ... '{grade_regex}'... ).使用 JDBC 时(即使通过 Anorm),查询语句中不得引用字符串参数( ... '{grade_regex}'... )。
  3. Since a long time, it's recommended to use Anorm interpolation ( SQL"SELECT x FROM y WHERE z = ${v}" ) rather than SQL(..) function.长期以来,建议使用 Anorm 插值 ( SQL"SELECT x FROM y WHERE z = ${v}" ) 而不是SQL(..) function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM