1 Package Com.dx.efuwu.core2 3 Import Org.apache.commons.lang.StringUtils4 Import Java.sql.PreparedStatement5 6 /**7 * SQL Template processing8 * @author sunzq9 * 2017/06/02Ten */ One A /** - * A conditional sentence for a query - */ the classQuerybranch (Val content:string, Val key:string, Val type:string) { - - OverrideFun toString (): String { - return "{content: $content, Key: $key, type: $type}" + } - +Fun Build (input:string?): String { A return if(Stringutils.isnotblank (input)) Content.replace ("""(?:#|##|@|@@)\{.*}""". Toregex (), replacement ="?") at Else if(Type.length >1)"1=0" - Else "1=1" - } - -Fun Dosetparameter (input:string?, Index:int, pstmt:preparedstatement): Boolean { -Input?. Takeif { in Stringutils.isnotblank (IT) -}?. Run { to When (type) { + "#","##"-pstmt.setstring (index, input) - "@","@@"-Pstmt.setlong (Index, Input.tolong ()) the } * return true $ }Panax Notoginseng return false - } the + } A the + /** - * SQL query template processor, does not provide the correct parsing of SQL. $ * Fill in as is, do not autofill single quotes, null true @@ 为 null false $ * # automatically wrap quotation marks, empty true # # is empty then false - */ - classSqltemplateexecutor (sql:string) { the - Wuyi /** the * Query condition sentence - */ WuVal querybranches = arraylist<querybranch>() - About /** $ * post-cut query SQL - */ -Val splitquerystrings = arraylist<string>() - A Init { +Val _sql = Sql.replace ("""\ n""". Toregex ()," ") theVal regex ="""\b\s+ (?: \ s|=|>|<|in|not|like| in| Not| Like) + (#|##|@|@@) \{([^}]+)}""". Toregex () - Splitquerystrings.addall (_sql.split (regex)) $Regex.findall (_sql). Iterator (). ForEach {Matchresult- the matchResult.groupValues.apply { theQuerybranches.add (querybranch (content =Get(0), key =Get(2). Trim (), type =Get(1))) the } the } - } in theFun Doquery (dict:map<string, string?>): Arraylist<map<string, any>> { theVal Sqlbuilder =StringBuilder () AboutVal Querybranchiterator =Querybranches.iterator () theSplitquerystrings.foreach {queryString the sqlbuilder.append (queryString) the if(Querybranchiterator.hasnext ()) { + querybranchiterator.next (). Apply { - Sqlbuilder.append (Build (Dict[key])) the }Bayi } the } the -Val queryresults = arraylist<map<string, any>>() - SQL_SERVER_DATA_SOURCE.connection.apply { the preparestatement (Sqlbuilder.tostring ()). Apply { the //Set Query Parameters the varParamindex =1 theQuerybranches.foreach {Querybranch -val param =Dict[querybranch.key] the if(Querybranch.dosetparameter (param, Paramindex, This)) { theparamindex++ the }94 } the //Execute Query the executeQuery (). Apply { the while(Next ()) {98Val result = hashmap<string, any> (8) About metadata.apply { -(1.. ColumnCount). ForEach {i-101Result[getcolumnlabel (i)] =102 When (Getcolumntypename (i)) {103 "int"-getInt (i)104 Else-getString (i) the }106 }107 }108 Queryresults.add (Result)109 } the Close ()111 } the Close ()113 } the Close () the } the returnQueryResults117 }118 }119 -Fun Main (args:array<string>) {121Val sql ="""SELECT * FROM Test"""122 println (Sqltemplateexecutor (SQL). Doquery (Hashmapof (123 "userName1"To"Large%",124 " Age"To" -", the "S2"To"Hello,world",126 "userId"To"10000"127 ))) -}
A simple SQL query parser written by Kotlin