Allow schema files that are missing checksums on the !!SCHEMAMATIC line.
[versaplex.git] / versaplexd / storedprocedure.cs
blob36fc4490fc00b9a47affc7589df2cfd6bb5da9b2
1 /*
2 * Versaplex:
3 * Copyright (C)2007-2008 Versabanq Innovations Inc. and contributors.
4 * See the included file named LICENSE for license information.
5 */
6 using System;
7 using System.Collections.Generic;
8 using System.Text.RegularExpressions;
9 using Wv;
10 using Wv.Extensions;
12 struct SPArg
14 public string name;
15 public string type;
16 public string defval;
18 public SPArg(string _name, string _type, string _defval)
20 name = _name;
21 type = _type;
22 defval = _defval;
26 class StoredProcedure
28 public List<SPArg> args;
29 public string name;
31 // These are mindnumbingly long to type out. Use the perl shorthands.
32 private static RegexOptions i = RegexOptions.IgnoreCase;
33 private static RegexOptions x = RegexOptions.IgnorePatternWhitespace;
34 private static RegexOptions m = RegexOptions.Multiline;
35 private static RegexOptions s = RegexOptions.Singleline;
36 private static RegexOptions compiled = RegexOptions.Compiled;
38 static Regex commentline_re = new Regex(@"--.*$", m|compiled);
39 static Regex comment_re = new Regex(@"/\* .*? \*/", x|s|compiled);
40 static Regex decl_re = new Regex(
41 @"CREATE \s+ (PROC|PROCEDURE) \s+ (\S*) (.*?) \s+ AS \s+",
42 x|s|i|compiled);
43 static Regex args_re = new Regex(@"
44 @(\S+) \s+ # name
45 ([^,=]+ (?: \([\d,]*\))? \s*) # type
46 ((?: = \s* (?: '[^']*')|(?: [^,]+) )?) # default
47 , # trailing comma
48 ", x|s|i|compiled);
49 static Regex output_re = new Regex(@"\s+ OUTPUT \s* $", x|i|compiled);
51 public StoredProcedure()
53 args = new List<SPArg>();
54 name = null;
57 public StoredProcedure(string sp)
59 name = null;
60 Parse(sp);
63 public static string StripComments(string sp)
65 sp = commentline_re.Replace(sp, "");
66 sp = comment_re.Replace(sp, "");
68 return sp;
71 public static string FixDefaultValue(string def)
73 if (def.e())
74 return def;
76 // Strip leading spaces and equal signs, and any "output" qualifiers
77 def = Regex.Replace(def, @"^ \s* = \s*", "", x);
78 def = output_re.Replace(def, "");
80 return def;
83 public void Parse(string sp)
85 args = new List<SPArg>();
87 var log = new WvLog("SP.Parse", WvLog.L.Debug2);
88 log.print(WvLog.L.Debug3, "Parsing {0}\n", sp);
90 sp = StripComments(sp);
92 var decl_match = decl_re.Match(sp);
93 if (decl_match.Success)
95 // Group 0 is the whole thing, group 1 is PROC or PROCEDURE.
96 this.name = decl_match.Groups[2].Value;
97 string args_str = decl_match.Groups[3].Value + ",";
98 log.print("name={0}, args_str={1}\n", name, args_str);
100 // Match the arguments
101 foreach (Match m in args_re.Matches(args_str))
103 log.print("Checking new match, val={0}\n", m.Value);
104 string paramname = m.Groups[1].Value.Trim();
105 string paramtype = m.Groups[2].Value.Trim();
106 string defval = m.Groups[3].Value.Trim();
108 paramtype = output_re.Replace(paramtype, "");
109 defval = FixDefaultValue(defval);
110 args.Add(new SPArg(paramname, paramtype, defval));
113 else
114 log.print("Not matched\n");