static List <IndigoSQLFunction> GenerateFunctions(IEnumerable <MethodInfo> methods) { List <IndigoSQLFunction> functions = new List <IndigoSQLFunction>(); foreach (MethodInfo m in methods) { IndigoSQLFunction f = new IndigoSQLFunction(); f.comment = m.Name; object[] attributes = m.GetCustomAttributes(typeof(BingoSqlFunctionAttribute), true); BingoSqlFunctionAttribute attr = (BingoSqlFunctionAttribute)(attributes[0]); IndigoSQLFunctionInfo mf = f.main_function; mf.name = String.Format("[$(bingo)].z_{0}", m.Name); mf.ret_type = SqlType(m.ReturnType.Name, m); mf.access_level = attr.access_level; foreach (ParameterInfo p in m.GetParameters()) { mf.arguments.Add(new KeyValuePair <string, string>(p.Name, SqlType(p.ParameterType.Name, m))); } mf.code = String.Format(" EXTERNAL NAME [$(bingo)_assembly].[{0}].{1}", m.ReflectedType.FullName, m.Name); // Create wrappers f.wrappers.Add(CreateWrapper(mf, false, m)); if (attr.str_bin != null) { f.wrappers.Add(CreateWrapper(mf, true, m)); } functions.Add(f); } return(functions); }
static IndigoSQLFunctionInfo CreateWrapper(IndigoSQLFunctionInfo parent, bool binary, MethodInfo m) { StringBuilder code_builder = new StringBuilder(); IndigoSQLFunctionInfo w = new IndigoSQLFunctionInfo(); w.name = String.Format("[$(bingo)].{0}{1}", m.Name, binary ? "B" : ""); if (parent.ret_type.StartsWith("TABLE")) { w.ret_type = "TABLE"; } else { w.ret_type = parent.ret_type; code_builder.Append("BEGIN\n"); } w.access_level = parent.access_level; object[] attributes = m.GetCustomAttributes(typeof(BingoSqlFunctionAttribute), true); BingoSqlFunctionAttribute attr = (BingoSqlFunctionAttribute)(attributes[0]); if (parent.ret_type != "") { code_builder.Append(" RETURN "); } else { code_builder.Append(" EXEC "); } if (parent.ret_type.StartsWith("TABLE")) { code_builder.Append("(SELECT * FROM "); } code_builder.AppendFormat("{0} ", parent.name); if (parent.ret_type != "") { code_builder.Append("("); } bool is_first = true; bool str_bin_found = false; foreach (KeyValuePair <string, string> p in parent.arguments) { if (!is_first) { code_builder.Append(", "); } if (attr.substitute_bingo && p.Key == "bingo_schema") { code_builder.Append("'$(bingo)'"); } else if (attr.substitute_bingo && p.Key == "bingo_db") { code_builder.Append("'$(database)'"); } else if (attr.str_bin == p.Key && !binary) { code_builder.AppendFormat("cast(@{0} as VARBINARY(max))", p.Key); w.arguments.Add(new KeyValuePair <string, string>(p.Key, "varchar(max)")); str_bin_found = true; } else { code_builder.AppendFormat("@{0}", p.Key); w.arguments.Add(p); } is_first = false; } if (!binary && attr.str_bin != null && !str_bin_found) { String msg = String.Format("Cannot find {0} in {1}", attr.str_bin, m.Name); System.Console.WriteLine(msg); throw new Exception(msg); } if (parent.ret_type != "") { code_builder.Append(")"); } if (parent.ret_type.StartsWith("TABLE")) { code_builder.Append(")"); } code_builder.Append("\n"); if (!parent.ret_type.StartsWith("TABLE")) { code_builder.Append("END"); } w.code = code_builder.ToString(); return(w); }