2017-08-12 5 views
0

とSQLスクリプトファイルを実行し、私は取得する必要がありQuery1.sqlは、私は動的SQLを使用した多数のSQLファイルを実行する必要性を持っている選択クエリと動的SQL

set @a= (select col_value1 from table1 where x=y);  
set @b= (select col_value2 from table2 where [email protected]); 

prepare script from @b; 
execute script; 
deallocate prepare script; 

を、次のようなものを問い合わせますさらなる処理のために実行後のResultSetオブジェクト

iBatisフレームワークのScriptRunnerを使用しようとしました。そこでクエリを実行できますが、ResultSetオブジェクトを取得する方法はありません。

また、RunTimeによってSystemコマンドとして実行する方法もあります。環境はスレッドベースなので、私はこのアプローチの使用をお勧めしません。

これを行う方法は他にありますか? ?

ありがとうございます。

+0

使用しているデータベースであなたの質問にタグを私はこのような何かを思い付きました。 –

答えて

0

あなたのファイルをregexpsで解析し、後で置き換えられたパラメータでクエリを呼び出すのは簡単でしょうか?

package com.company; 

import java.util.HashMap; 
import java.util.LinkedHashMap; 
import java.util.Map; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class Main { 

private static String queryFile() { 
    return "set @a= (select col_value1 from table1 where x=y);\n" + 
      "set @b= (select col_value2 from table2 where [email protected]);\n" + 
      "prepare script from @b;\n" + 
      "execute script;\n" + 
      "deallocate prepare script;"; 
} 

private static final Pattern QUERY_PATTERN = Pattern.compile("set (@.)= (.*.)"); 
private static final Pattern QUERY_VARIABLE_MATCHER = Pattern.compile("@."); 

public static void main(String[] args) { 
    Map<String, String> queryMap = new LinkedHashMap<String, String>(); 
    String lines[] = queryFile().split("\n"); 
    for (String line : lines) { 
     Matcher matcher = QUERY_PATTERN.matcher(line); 
     if (matcher.matches()) { 
      String key = matcher.group(1); 
      String query = matcher.group(2); 
      queryMap.put(key, query); 
     } 
    } 

    invokeQueries(queryMap); 
} 

/** 
* Invokes queries parsed from a file. Works only if queries are sorted properly (query B uses a query A result). 
*/ 
private static void invokeQueries(Map<String, String> queryMap) { 
    Map<String, String> queryResults = new HashMap<String, String>(); 
    for (Map.Entry<String, String> entry : queryMap.entrySet()) { 
     String query = entry.getValue(); 
     Matcher matcher = QUERY_VARIABLE_MATCHER.matcher(query); 
     if (!matcher.find()) { 
      String queryResult = mockInvokeQuery(query); 
      queryResults.put(entry.getKey(), queryResult); 
      System.out.println("Invoked query: " + query + " with result : " + queryResult); 
     } else { 
      matcher.reset(); 
      System.out.println("Replacing query parameters for query: " + query); 
      while (matcher.find()) { 
       String variable = matcher.group(); 
       String replacedQuery = query.replaceAll(variable, queryResults.get(variable)); 
       String queryResult = mockInvokeQuery(query); 
       queryResults.put(entry.getKey(), queryResult); 
       System.out.println("Invoked query with replaced parameters:: " + replacedQuery + " with result: " + queryResult); 
      } 
     } 
    } 
} 

/** 
* Invoke your query and get your result set 
*/ 
private static String mockInvokeQuery(String query) { 
    return String.valueOf(query.hashCode()); 
} 

/** 
* Replaces @ parameter for all queries within a hashmap. It won't work properly if queries are dependent on each other. 
*/ 
private static Map<String, String> replaceQueryVariables(Map<String, String> queryMap) { 
    Map<String, String> replacedQueries = new HashMap<String, String>(); 
    for (Map.Entry<String, String> entry : queryMap.entrySet()) { 
     String query = entry.getValue(); 
     Matcher matcher = QUERY_VARIABLE_MATCHER.matcher(query); 
     while (matcher.find()) { 
      String variable = matcher.group(); 
      String replacedQuery = query.replaceAll(variable, queryMap.get(variable)); 
      replacedQueries.put(entry.getKey(), replacedQuery); 

     } 
    } 
    return replacedQueries; 
} 

} 

結果は次のとおりです:

Invoked query: (select col_value1 from table1 where x=y); with result : -316456543 
Replacing query parameters for query: (select col_value2 from table2 where [email protected]); 
Invoked query with replaced parameters:: (select col_value2 from table2 where x1=-316456543); with result: -1396099308 
+0

おかげでMichal。これは役に立ちました。 – renGth

+0

私の要件は少し異なります。 2番目の 'セット'にconcat()が含まれています。つまり、 @b = concat(....(aからbを選択します。c = @ a)....)。個別に分割して実行すると、2番目のステートメントはエラーをスローします。 – renGth

関連する問題