Shadowrun: Awakened 29 September 2011 - Build 871
RoutineParamBase.cs
Go to the documentation of this file.
00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 
00006 namespace RoutineAnalyzer
00007 {
00013     public enum ParameterTypes
00014     {
00015         IntByValue,
00016         IntByReference,
00017         StringByValue,
00018         StringByReference
00019     };
00020 
00025     public abstract class RoutineParamBase
00026     {
00027         #region Static Methods
00028 
00036         static ParameterTypes ConvertParameterType(string paramType, bool isIn)
00037         {
00038             paramType = paramType.ToUpper();
00039             if (isIn)
00040             {
00041                 if (paramType.Contains("VARCHAR"))
00042                     return ParameterTypes.StringByValue;
00043                 else
00044                     return ParameterTypes.IntByValue;
00045             }
00046             else
00047             {
00048                 if (paramType.Contains("VARCHAR"))
00049                     return ParameterTypes.StringByReference;
00050                 else
00051                     return ParameterTypes.IntByReference;
00052             }
00053         }
00054 
00055         #endregion
00056 
00057         #region Fields
00058 
00059         protected string _name;
00060         protected string _sql;
00061         protected int _index;
00062         protected bool _isInParam;
00063         protected ParameterTypes _type;
00064 
00065         public string Name
00066         {
00067             get
00068             {
00069                 return _name;
00070             }
00071         }
00072         #endregion
00073 
00074         #region Methods
00075 
00076         public RoutineParamBase(string sql, int index)
00077         {
00078             _index = index;
00079 
00080             Parse(sql);
00081         }
00082 
00087         void Parse(string sql)
00088         {
00089             //save the original SQL
00090             _sql = sql;
00091 
00092             //break up the parameters by their spaces
00093             //NOTE: This is a very brittle way of parsing and depends on consistent SQL coding style in the database
00094             string[] parts = sql.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
00095 
00096             string direction;
00097             string name;
00098             string type;
00099 
00100             //We assuming the delcaration takes the form: DIRECTION NAME TYPE
00101             //if they didn't use an explicit IN declaration, then it is assumed to be one
00102             if (parts.Length == 2)
00103             {
00104                 direction = "IN";
00105                 name = parts[0];
00106                 type = parts[1];
00107             }
00108             else
00109             {
00110                 direction = parts[0].ToUpper();
00111                 name = parts[1];
00112                 type = parts[2];
00113             }
00114 
00115             //if the direction is IN, then it's an in paramter (otherwise it is an OUT)
00116             _isInParam = direction.ToUpper() == "IN";
00117             _type = ConvertParameterType(type, _isInParam);
00118             _name = name;
00119         }
00120 
00128         public virtual string GetParameter()
00129         {
00130             //if it is just an input param, then we use a quotation mark as the param maker
00131             if (_isInParam)
00132                 return "?";
00133 
00134             //if it is an output parameter, then we create a variable name based on the index
00135             return string.Format("@param{0}", _index);
00136         }
00137 
00138         public abstract string GetDeclaration();
00139 
00140         public abstract string GetPreExecuteCode();
00141 
00142         public abstract string GetPostExecuteCode();
00143 
00144         #endregion
00145     }
00146 }

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net