3 * Copyright (C)2007-2008 Versabanq Innovations Inc. and contributors.
4 * See the included file named LICENSE for license information.
7 using System
.Collections
.Generic
;
8 using System
.Text
.RegularExpressions
;
18 public SPArg(string _name
, string _type
, string _defval
)
28 public List
<SPArg
> args
;
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+",
43 static Regex args_re
= new Regex(@"
45 ([^,=]+ (?: \([\d,]*\))? \s*) # type
46 ((?: = \s* (?: '[^']*')|(?: [^,]+) )?) # default
49 static Regex output_re
= new Regex(@"\s+ OUTPUT \s* $", x
|i
|compiled
);
51 public StoredProcedure()
53 args
= new List
<SPArg
>();
57 public StoredProcedure(string sp
)
63 public static string StripComments(string sp
)
65 sp
= commentline_re
.Replace(sp
, "");
66 sp
= comment_re
.Replace(sp
, "");
71 public static string FixDefaultValue(string 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
, "");
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
));
114 log
.print("Not matched\n");