public OneParam(string p) { var s = p.Trim().Split(' '); if (s.Length > 3 || s.Length < 2) { valid = false; return; } bool p_count_3 = s.Length == 3; if (p_count_3) { if (s[0].Contains("OUT")) { pct = ParamCallType.OUT; } else { valid = false; return; } } param_name = (p_count_3 ? s[1] : s[0]).Trim(); var type = SqlToJava.ToJavaType((p_count_3 ? s[2] : s[1]).Trim()); if (type.Item1 == false) { valid = false; return; } param_type = type.Item2; }
static void Main(string[] args) { bool open = false; StreamReader sr = null; while (!open) { try { Console.WriteLine("path to file with SQL functions"); sr = new StreamReader(Console.ReadLine()); open = true; } catch { Console.WriteLine("cant open file, try again"); } } StreamWriter sw = new StreamWriter("SQL_FuncCall.java"); sw.WriteLine(); sw.WriteLine("import org.hibernate.Session;"); sw.WriteLine("import javax.persistence.ParameterMode;"); sw.WriteLine("import org.hibernate.procedure.ProcedureCall;"); sw.WriteLine("import java.sql.Date;"); sw.WriteLine("import java.util.List;"); sw.WriteLine(); sw.WriteLine("public class SQL_FuncCall {"); CodeTab.AddTab(); int func_count = 0; while (!sr.EndOfStream) { const string CREATE = "CREATE"; const string FUNCTION = "FUNCTION"; const string PROCEDURE = "PROCEDURE"; var line = sr.ReadLine().Trim(); if (!line.StartsWith(CREATE)) { continue; } int ind = line.IndexOf(FUNCTION); bool is_function = ind != -1; if (!is_function) { ind = line.IndexOf(PROCEDURE); } if (ind == -1) { continue; } line = line.Substring(ind + (is_function ? FUNCTION : PROCEDURE).Length); int ind_open = line.IndexOf('('); int ind_close = line.LastIndexOf(')'); if (ind_open < 0 || ind_close < 0) { Console.WriteLine("Hmm... [1]"); continue; } string func_str = "\n"; string name_of_func = line.Substring(0, ind_open).Trim(); var params_info = line.Substring(ind_open + 1, ind_close - ind_open - 1).Split(','); Parameters ps = new Parameters(params_info); if (!ps.IsValid()) { Console.WriteLine("Hmm... [2]"); continue; } if (is_function) { line = sr.ReadLine().Trim(); if (line.Contains("RETURNS")) { var temp0 = SqlToJava.ToJavaType(line.Split(' ')[1]); if (!temp0.Item1) { Console.WriteLine("Hmm... [3]"); continue; } ps.AddParameter(new OneParam("", temp0.Item2, ParamCallType.RET)); } } var temp1 = ps.GetRetValue(); if (!temp1.Item1) { Console.WriteLine("Hmm... [4]"); continue; } func_str = func_str.AddCodeLine("public static " + temp1.Item2 + " " + name_of_func.ToJavaFuncName() + "(Session session, " + ps.ToJavaCode() + ") {"); CodeTab.AddTab(); func_str = func_str.AddCodeLine("ProcedureCall query = session.createStoredProcedureCall(\"" + name_of_func + "\");"); func_str += ps.RegParam(); func_str += ps.SetParam(); func_str = func_str.AddCodeLine("query.execute();"); func_str += ps.ReturnLine(); CodeTab.DecTab(); func_str = func_str.AddCodeLine("}"); sw.WriteLine(func_str); func_count++; } CodeTab.DecTab(); sw.WriteLine("}"); sw.Close(); sr.Close(); Console.WriteLine("F amount:" + func_count); Console.WriteLine("Press any key (except for power off, win ...)"); Console.ReadKey(); }