private static void WriteStoreProcedures(ref StringBuilder nSourceCode, XsdDataBase nDataBaseMap, XsdDataBase.TBL_ObjectRow[] nObjetos) { foreach (var Objeto in nObjetos) { var ObjetNameFormated = FormatCode.ToIdentifier(Objeto.Object_Name); var ObjetNameFormatedType = ObjetNameFormated + "StoreProcedure"; DataBaseClassWriter.Write(nSourceCode, 2, "/// <summary>"); DataBaseClassWriter.Write(nSourceCode, 2, "/// Clase encargada de encapsular las funcionalidades de acceso al procedimiento almacenado " + Objeto.Object_Name); DataBaseClassWriter.Write(nSourceCode, 2, "/// </summary>"); DataBaseClassWriter.Write(nSourceCode, 2, "public class " + ObjetNameFormatedType + " : StoreProcedureManager"); DataBaseClassWriter.Write(nSourceCode, 2, "{"); // Constructores DataBaseClassWriter.Write(nSourceCode, 3, "#region Constructores"); DataBaseClassWriter.Write(nSourceCode, 0, ""); DataBaseClassWriter.Write(nSourceCode, 3, "internal " + ObjetNameFormatedType + "(SchemaManager nSchemaManager)"); DataBaseClassWriter.Write(nSourceCode, 4, ": base(nSchemaManager)"); DataBaseClassWriter.Write(nSourceCode, 3, "{"); DataBaseClassWriter.Write(nSourceCode, 4, "base._ObjectName = \"" + Objeto.Object_Name + "\";"); DataBaseClassWriter.Write(nSourceCode, 3, "}"); DataBaseClassWriter.Write(nSourceCode, 0, ""); DataBaseClassWriter.Write(nSourceCode, 3, "#endregion"); DataBaseClassWriter.Write(nSourceCode, 0, ""); var opetarionParams = ""; var returnRows = Objeto.GetTBL_SP_ReturnRows(); var retType = ReturnType.TablaGenerica; XsdDataBase.TBL_ObjectRow objectReturned = null; var dataTypeReturned = DbType.Object; if (returnRows.Length > 0) { retType = (ReturnType) (Enum.Parse(typeof (ReturnType), returnRows[0].Return_Type)); switch (retType) { case ReturnType.TablaTipada: DataRow[] Filas = nDataBaseMap.TBL_Object.Select("Schema_Name = '" + returnRows[0].Schema_Name_Returned + "' AND Object_Name = '" + returnRows[0].Object_Name_Returned + "'"); if (Filas.Length > 0) objectReturned = (XsdDataBase.TBL_ObjectRow) (Filas[0]); else throw new Exception("El procedimiento almacenado " + Objeto.Object_Name + ", retorna un objeto de tipo " + returnRows[0].Object_Name_Returned + " que no se encuentra definido"); //objectReturned = (XsdDataBase.TBL_ObjectRow)(nDataBaseMap.TBL_Object.Select("Schema_Name = '" + returnRows[0].Schema_Name_Returned + "' AND Object_Name = '" + returnRows[0].Object_Name_Returned + "'")[0]); break; case ReturnType.Escalar: dataTypeReturned = (DbType) (Enum.Parse(typeof (DbType), returnRows[0].Data_Type_Returned)); break; } } var nParameters = nDataBaseMap.GetParameters(Objeto); opetarionParams = ""; foreach (var par in nParameters) { if (opetarionParams != "") opetarionParams += ", "; opetarionParams = opetarionParams + DataBaseClassWriter.GetApplicationStringType(par.Type, LanguajeType.CSharp, true) + " n" + par.Name.TrimStart('@'); } DataBaseClassWriter.Write(nSourceCode, 3, "#region Metodos"); DataBaseClassWriter.Write(nSourceCode, 0, ""); if (retType == ReturnType.Nada) { DataBaseClassWriter.Write(nSourceCode, 3, "/// <summary>"); DataBaseClassWriter.Write(nSourceCode, 3, "///Permite la ejecución del procedimiento almacenado " + Objeto.Object_Name); DataBaseClassWriter.Write(nSourceCode, 3, "/// </summary>"); DataBaseClassWriter.Write(nSourceCode, 3, "[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]"); DataBaseClassWriter.Write(nSourceCode, 3, "public void DBExecute(" + opetarionParams + ")"); DataBaseClassWriter.Write(nSourceCode, 3, "{"); DataBaseClassWriter.Write(nSourceCode, 4, "List<Parameter> nParameters = new List<Parameter>();"); foreach (var par in nParameters) { DataBaseClassWriter.Write(nSourceCode, 4, "nParameters.Add(new Parameter(\"" + par.Name + "\", DbType." + par.Type.ToString() + ", \"" + par.SpecificType + "\", n" + par.Name.TrimStart('@') + ", " + DataBaseClassWriter.getBoolean(par.IsNullable, LanguajeType.CSharp) + ", " + par.MaxLength + ", " + par.Precision + ", " + par.Scale + ", ParameterDirection." + par.Direction.ToString() + "));"); } DataBaseClassWriter.Write(nSourceCode, 4, "base.DBExecuteSp(nParameters);"); DataBaseClassWriter.Write(nSourceCode, 3, "}"); DataBaseClassWriter.Write(nSourceCode, 0, ""); } else { var strReturnType = ""; switch (retType) { case ReturnType.Escalar: strReturnType = DataBaseClassWriter.GetApplicationStringType(dataTypeReturned.ToString(), LanguajeType.CSharp); break; case ReturnType.TablaTipada: if (objectReturned != null) strReturnType = "Schema" + FormatCode.ToIdentifier(objectReturned.Schema_Name) + "." + FormatCode.ToIdentifier(objectReturned.Object_Name) + "DataTable"; break; case ReturnType.TablaGenerica: strReturnType = "DataTable"; break; } DataBaseClassWriter.Write(nSourceCode, 3, "///<summary>"); DataBaseClassWriter.Write(nSourceCode, 3, "///Permite la ejecución del procedimiento almacenado " + Objeto.Object_Name + " y retorna los datos encontrados"); DataBaseClassWriter.Write(nSourceCode, 3, "///</summary>"); DataBaseClassWriter.Write(nSourceCode, 3, "[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]"); DataBaseClassWriter.Write(nSourceCode, 3, "public " + strReturnType + " DBExecute(" + opetarionParams + ")"); DataBaseClassWriter.Write(nSourceCode, 3, "{"); DataBaseClassWriter.Write(nSourceCode, 4, "List<Parameter> nParameters = new List<Parameter>();"); foreach (var par in nParameters) { DataBaseClassWriter.Write(nSourceCode, 4, "nParameters.Add(new Parameter(\"" + par.Name + "\", DbType." + par.Type.ToString() + ", \"" + par.SpecificType + "\", n" + par.Name.TrimStart('@') + ", " + DataBaseClassWriter.getBoolean(par.IsNullable, LanguajeType.CSharp) + ", " + par.MaxLength + ", " + par.Precision + ", " + par.Scale + ", ParameterDirection." + par.Direction.ToString() + "));"); } if (retType == ReturnType.Escalar) { DataBaseClassWriter.Write(nSourceCode, 4, "try"); DataBaseClassWriter.Write(nSourceCode, 4, "{"); DataBaseClassWriter.Write(nSourceCode, 5, strReturnType + " Value;"); DataBaseClassWriter.Write(nSourceCode, 5, "DataTable tableData = new DataTable();"); DataBaseClassWriter.Write(nSourceCode, 5, "base.DBExecuteSp(tableData, nParameters);"); DataBaseClassWriter.Write(nSourceCode, 5, "if(tableData.Rows.Count > 0)"); DataBaseClassWriter.Write(nSourceCode, 5, "{"); if (strReturnType.ToUpper() == "STRING") DataBaseClassWriter.Write(nSourceCode, 6, "Value = tableData.Rows[0][0].ToString();"); if (dataTypeReturned.ToString().ToUpper() == "BINARY") DataBaseClassWriter.Write(nSourceCode, 6, "Value = (byte[])tableData.Rows[0][0];"); else DataBaseClassWriter.Write(nSourceCode, 6, "Value = " + GetConvertFuncion(dataTypeReturned.ToString()) + "(tableData.Rows[0][0].ToString());"); DataBaseClassWriter.Write(nSourceCode, 6, "return Value;"); DataBaseClassWriter.Write(nSourceCode, 5, "}"); DataBaseClassWriter.Write(nSourceCode, 5, "else"); DataBaseClassWriter.Write(nSourceCode, 5, "{"); DataBaseClassWriter.Write(nSourceCode, 5, "throw new Exception(\"El procedimiento no retornó valor\");"); DataBaseClassWriter.Write(nSourceCode, 5, "}"); DataBaseClassWriter.Write(nSourceCode, 4, "}"); DataBaseClassWriter.Write(nSourceCode, 4, "catch( Exception ex)"); DataBaseClassWriter.Write(nSourceCode, 4, "{"); DataBaseClassWriter.Write(nSourceCode, 5, "throw new Exception(\"No fue posible ejecutar el procedimiento almacenado " + Objeto.Object_Name + ", \" + ex.Message, ex);"); DataBaseClassWriter.Write(nSourceCode, 4, "}"); } else { DataBaseClassWriter.Write(nSourceCode, 4, "DataTable tableData = new " + strReturnType + "();"); DataBaseClassWriter.Write(nSourceCode, 4, "try"); DataBaseClassWriter.Write(nSourceCode, 4, "{"); DataBaseClassWriter.Write(nSourceCode, 5, "base.DBExecuteSp(tableData, nParameters);"); DataBaseClassWriter.Write(nSourceCode, 4, "}"); DataBaseClassWriter.Write(nSourceCode, 4, "catch(Exception ex)"); DataBaseClassWriter.Write(nSourceCode, 4, "{"); DataBaseClassWriter.Write(nSourceCode, 5, "throw new Exception(\"No fue posible ejecutar el procedimiento almacenado " + Objeto.Object_Name + ", \" + ex.Message, ex);"); DataBaseClassWriter.Write(nSourceCode, 4, "}"); DataBaseClassWriter.Write(nSourceCode, 4, "return (" + strReturnType + ")tableData;"); } DataBaseClassWriter.Write(nSourceCode, 3, "}"); DataBaseClassWriter.Write(nSourceCode, 0, ""); } if (retType == ReturnType.TablaTipada || retType == ReturnType.TablaGenerica) { DataBaseClassWriter.Write(nSourceCode, 3, "///<summary>"); DataBaseClassWriter.Write(nSourceCode, 3, "///Permite la ejecución del procedimiento almacenado " + Objeto.Object_Name + " y retorna los datos encontrados"); DataBaseClassWriter.Write(nSourceCode, 3, "///</summary>"); DataBaseClassWriter.Write(nSourceCode, 3, "[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]"); DataBaseClassWriter.Write(nSourceCode, 3, "public void DBExecute(DataTable tableData" + (opetarionParams == "" ? "" : ", " + opetarionParams) + ")"); DataBaseClassWriter.Write(nSourceCode, 3, "{"); DataBaseClassWriter.Write(nSourceCode, 4, "List<Parameter> nParameters = new List<Parameter>();"); foreach (var par in nParameters) { DataBaseClassWriter.Write(nSourceCode, 4, "nParameters.Add(new Parameter(\"" + par.Name + "\", DbType." + par.Type.ToString() + ", \"" + par.SpecificType + "\", n" + par.Name.TrimStart('@') + ", " + DataBaseClassWriter.getBoolean(par.IsNullable, LanguajeType.CSharp) + ", " + par.MaxLength + ", " + par.Precision + ", " + par.Scale + ", ParameterDirection." + par.Direction.ToString() + "));"); } DataBaseClassWriter.Write(nSourceCode, 4, "try"); DataBaseClassWriter.Write(nSourceCode, 4, "{"); DataBaseClassWriter.Write(nSourceCode, 5, "base.DBExecuteSp(tableData, nParameters);"); DataBaseClassWriter.Write(nSourceCode, 4, "}"); DataBaseClassWriter.Write(nSourceCode, 4, "catch(Exception ex)"); DataBaseClassWriter.Write(nSourceCode, 4, "{"); DataBaseClassWriter.Write(nSourceCode, 5, "throw new Exception(\"No fue posible ejecutar el procedimiento almacenado " + Objeto.Object_Name + ", \" + ex.Message, ex);"); DataBaseClassWriter.Write(nSourceCode, 4, "}"); DataBaseClassWriter.Write(nSourceCode, 3, "}"); DataBaseClassWriter.Write(nSourceCode, 0, ""); } DataBaseClassWriter.Write(nSourceCode, 3, "#endregion"); DataBaseClassWriter.Write(nSourceCode, 2, "}"); // Fin de la clase DataBaseClassWriter.Write(nSourceCode, 0, ""); } }
private static void WriteStoreProcedures(ref StringBuilder nSourceCode, XsdDataBase nDataBaseMap, XsdDataBase.TBL_ObjectRow[] nObjetos) { foreach (var Objeto in nObjetos) { var ObjetNameFormated = FormatCode.ToIdentifier(Objeto.Object_Name); var ObjetNameFormatedType = ObjetNameFormated + "StoreProcedure"; DataBaseClassWriter.Write(nSourceCode, 2, "''' <summary>"); DataBaseClassWriter.Write(nSourceCode, 2, "''' Clase encargada de encapsular las funcionalidades de acceso al procedimiento almacenado " + Objeto.Object_Name); DataBaseClassWriter.Write(nSourceCode, 2, "''' </summary>"); DataBaseClassWriter.Write(nSourceCode, 2, "Public Class " + ObjetNameFormatedType); DataBaseClassWriter.Write(nSourceCode, 3, "Inherits StoreProcedureManager"); DataBaseClassWriter.Write(nSourceCode, 0, ""); // Constructores DataBaseClassWriter.Write(nSourceCode, 3, "#Region \" Constructores"); DataBaseClassWriter.Write(nSourceCode, 0, ""); DataBaseClassWriter.Write(nSourceCode, 3, "Friend Sub New(ByVal nSchemaManager As SchemaManager)"); DataBaseClassWriter.Write(nSourceCode, 4, "MyBase.New(nSchemaManager)"); DataBaseClassWriter.Write(nSourceCode, 4, "MyBase._ObjectName = \"" + Objeto.Object_Name + "\""); DataBaseClassWriter.Write(nSourceCode, 3, "End Sub"); DataBaseClassWriter.Write(nSourceCode, 0, ""); DataBaseClassWriter.Write(nSourceCode, 3, "#End Region"); DataBaseClassWriter.Write(nSourceCode, 0, ""); string opetarionParams = ""; var returnRows = Objeto.GetTBL_SP_ReturnRows(); var retType = ReturnType.TablaGenerica; XsdDataBase.TBL_ObjectRow objectReturned = null; var dataTypeReturned = DbType.Object; if (returnRows.Length > 0) { retType = (ReturnType) (Enum.Parse(typeof (ReturnType), returnRows[0].Return_Type)); switch (retType) { case ReturnType.TablaTipada: var Filas = nDataBaseMap.TBL_Object.Select("Schema_Name = '" + returnRows[0].Schema_Name_Returned + "' AND Object_Name = '" + returnRows[0].Object_Name_Returned + "'"); if (Filas.Length > 0) objectReturned = (XsdDataBase.TBL_ObjectRow) (Filas[0]); else throw new Exception("El procedimiento almacenado " + Objeto.Object_Name + ", retorna un objeto de tipo " + returnRows[0].Object_Name_Returned + " que no se encuentra definido"); break; case ReturnType.Escalar: dataTypeReturned = (DbType) (Enum.Parse(typeof (DbType), returnRows[0].Data_Type_Returned)); break; } } var nParameters = nDataBaseMap.GetParameters(Objeto); opetarionParams = ""; foreach (var par in nParameters) { if (opetarionParams != "") opetarionParams += ", "; opetarionParams = opetarionParams + "ByVal n" + par.Name.TrimStart('@') + " As " + DataBaseClassWriter.GetApplicationStringType(par.Type, LanguajeType.VB, true); } DataBaseClassWriter.Write(nSourceCode, 3, "#Region \"Metodos\""); DataBaseClassWriter.Write(nSourceCode, 0, ""); if (retType == ReturnType.Nada) { DataBaseClassWriter.Write(nSourceCode, 3, "''' <summary>"); DataBaseClassWriter.Write(nSourceCode, 3, "'''Permite la ejecución del procedimiento almacenado " + Objeto.Object_Name); DataBaseClassWriter.Write(nSourceCode, 3, "''' </summary>"); DataBaseClassWriter.Write(nSourceCode, 3, "<Global.System.Diagnostics.DebuggerNonUserCodeAttribute()> _"); DataBaseClassWriter.Write(nSourceCode, 3, "Public Overloads Sub DBExecute(" + opetarionParams + ")"); DataBaseClassWriter.Write(nSourceCode, 4, "Dim nParameters As New List(Of Parameter)"); foreach (var par in nParameters) { DataBaseClassWriter.Write(nSourceCode, 4, "nParameters.Add(New Parameter(\"" + par.Name + "\", DbType." + par.Type.ToString() + ", \"" + par.SpecificType + "\", n" + par.Name.TrimStart('@') + ", " + DataBaseClassWriter.getBoolean(par.IsNullable, LanguajeType.VB) + ", " + par.MaxLength + ", " + par.Precision + ", " + par.Scale + ", ParameterDirection." + par.Direction.ToString() + "))"); } DataBaseClassWriter.Write(nSourceCode, 4, "MyBase.DBExecuteSp(nParameters)"); DataBaseClassWriter.Write(nSourceCode, 3, "End Sub"); DataBaseClassWriter.Write(nSourceCode, 0, ""); } else { var strReturnType = ""; switch (retType) { case ReturnType.Escalar: strReturnType = DataBaseClassWriter.GetApplicationStringType(dataTypeReturned.ToString(), LanguajeType.VB); break; case ReturnType.TablaTipada: if (objectReturned != null) strReturnType = "Schema" + FormatCode.ToIdentifier(objectReturned.Schema_Name) + "." + FormatCode.ToIdentifier(objectReturned.Object_Name) + "DataTable"; break; case ReturnType.TablaGenerica: strReturnType = "DataTable"; break; } DataBaseClassWriter.Write(nSourceCode, 3, "'''<summary>"); DataBaseClassWriter.Write(nSourceCode, 3, "'''Permite la ejecución del procedimiento almacenado " + Objeto.Object_Name + " y retorna los datos encontrados"); DataBaseClassWriter.Write(nSourceCode, 3, "'''</summary>"); DataBaseClassWriter.Write(nSourceCode, 3, "<Global.System.Diagnostics.DebuggerNonUserCodeAttribute()> _"); DataBaseClassWriter.Write(nSourceCode, 3, "Public Overloads Function DBExecute(" + opetarionParams + ") As " + strReturnType); DataBaseClassWriter.Write(nSourceCode, 4, "Dim nParameters As New List(Of Parameter)"); foreach (var par in nParameters) { DataBaseClassWriter.Write(nSourceCode, 4, "nParameters.Add(New Parameter(\"" + par.Name + "\", DbType." + par.Type.ToString() + ", \"" + par.SpecificType + "\", n" + par.Name.TrimStart('@') + ", " + DataBaseClassWriter.getBoolean(par.IsNullable, LanguajeType.VB) + ", " + par.MaxLength + ", " + par.Precision + ", " + par.Scale + ", ParameterDirection." + par.Direction.ToString() + "))"); } if (retType == ReturnType.Escalar) { DataBaseClassWriter.Write(nSourceCode, 4, "Try"); DataBaseClassWriter.Write(nSourceCode, 5, "Dim Value As " + strReturnType); DataBaseClassWriter.Write(nSourceCode, 5, "Dim tableData As New DataTable"); DataBaseClassWriter.Write(nSourceCode, 5, "MyBase.DBExecuteSp(tableData, nParameters)"); DataBaseClassWriter.Write(nSourceCode, 5, "If(tableData.Rows.Count > 0) Then"); if (strReturnType.ToUpper() == "BINARY") DataBaseClassWriter.Write(nSourceCode, 6,"Value = CType(tableData.Rows(0)(0), Byte())"); else DataBaseClassWriter.Write(nSourceCode, 6, "Value = CType(tableData.Rows(0)(0), " + strReturnType + ")"); DataBaseClassWriter.Write(nSourceCode, 6, "Return Value"); DataBaseClassWriter.Write(nSourceCode, 5, "Else"); DataBaseClassWriter.Write(nSourceCode, 6, "Throw New Exception(\"El procedimiento no retornó valor\")"); DataBaseClassWriter.Write(nSourceCode, 5, "End If"); DataBaseClassWriter.Write(nSourceCode, 4, "Catch ex As Exception"); DataBaseClassWriter.Write(nSourceCode, 5, "Throw New Exception(\"No fue posible ejecutar el procedimiento almacenado " + Objeto.Object_Name + ", \" + ex.Message, ex)"); DataBaseClassWriter.Write(nSourceCode, 4, "End Try"); } else { DataBaseClassWriter.Write(nSourceCode, 4, "Dim tableData As New " + strReturnType); DataBaseClassWriter.Write(nSourceCode, 4, "Try"); DataBaseClassWriter.Write(nSourceCode, 5, "MyBase.DBExecuteSp( CType(tableData, DataTable), nParameters)"); DataBaseClassWriter.Write(nSourceCode, 4, "Catch ex As Exception"); DataBaseClassWriter.Write(nSourceCode, 5, "Throw New Exception(\"No fue posible ejecutar el procedimiento almacenado " + Objeto.Object_Name + ", \" + ex.Message, ex)"); DataBaseClassWriter.Write(nSourceCode, 4, "End Try"); DataBaseClassWriter.Write(nSourceCode, 4, "Return tableData"); } DataBaseClassWriter.Write(nSourceCode, 3, "End Function"); DataBaseClassWriter.Write(nSourceCode, 0, ""); } if (retType == ReturnType.TablaTipada || retType == ReturnType.TablaTipada) { DataBaseClassWriter.Write(nSourceCode, 3, "'''<summary>"); DataBaseClassWriter.Write(nSourceCode, 3, "'''Permite la ejecución del procedimiento almacenado " + Objeto.Object_Name + " y retorna los datos encontrados"); DataBaseClassWriter.Write(nSourceCode, 3, "'''</summary>"); DataBaseClassWriter.Write(nSourceCode, 3, "<Global.System.Diagnostics.DebuggerNonUserCodeAttribute()> _"); DataBaseClassWriter.Write(nSourceCode, 3, "Public Overloads Sub DBExecute(tableData As DataTable" + (opetarionParams == "" ? "" : ", " + opetarionParams) + ")"); DataBaseClassWriter.Write(nSourceCode, 4, "Dim nParameters As New List(Of Parameter)"); foreach (var par in nParameters) { DataBaseClassWriter.Write(nSourceCode, 4, "nParameters.Add(New Parameter(\"" + par.Name + "\", DbType." + par.Type.ToString() + ", \"" + par.SpecificType + "\", n" + par.Name.TrimStart('@') + ", " + DataBaseClassWriter.getBoolean(par.IsNullable, LanguajeType.VB) + ", " + par.MaxLength + ", " + par.Precision + ", " + par.Scale + ", ParameterDirection." + par.Direction.ToString() + "))"); } DataBaseClassWriter.Write(nSourceCode, 4, "Try"); DataBaseClassWriter.Write(nSourceCode, 5, "MyBase.DBExecuteSp(tableData, nParameters)"); DataBaseClassWriter.Write(nSourceCode, 4, "Catch ex As Exception"); DataBaseClassWriter.Write(nSourceCode, 5, "Throw New Exception(\"No fue posible ejecutar el procedimiento almacenado " + Objeto.Object_Name + ", \" + ex.Message, ex)"); DataBaseClassWriter.Write(nSourceCode, 4, "End Try"); DataBaseClassWriter.Write(nSourceCode, 3, "End Sub"); DataBaseClassWriter.Write(nSourceCode, 0, ""); } DataBaseClassWriter.Write(nSourceCode, 0, ""); DataBaseClassWriter.Write(nSourceCode, 3, "#End Region"); DataBaseClassWriter.Write(nSourceCode, 0, ""); DataBaseClassWriter.Write(nSourceCode, 2, "End Class"); DataBaseClassWriter.Write(nSourceCode, 0, ""); } }