From d94856edf9ff4acc22f8eceea347174651af2185 Mon Sep 17 00:00:00 2001 From: "Md. Ismail" Date: Mon, 3 Oct 2016 23:53:15 +0600 Subject: [PATCH] upgraded to .NetFx4.5 and VS2016 implemented string interpolation to reduce code --- CodeGenerator.sln | 6 +- CodeGenerator/App.config | 21 +- CodeGenerator/CodeGenerator.MVP.csproj | 21 +- CodeGenerator/DAL/SPCreatorDAL.cs | 4 +- CodeGenerator/Properties/AssemblyInfo.cs | 10 +- .../Properties/Resources.Designer.cs | 4 +- CodeGenerator/Properties/Settings.Designer.cs | 4 +- CodeGenerator/Util/DbBuilderOracle.cs | 271 ++++--- CodeGenerator/Util/DbBuilderSqlServer.cs | 734 +++++++++--------- CodeGenerator/Util/Utility.cs | 41 +- 10 files changed, 593 insertions(+), 523 deletions(-) diff --git a/CodeGenerator.sln b/CodeGenerator.sln index 36a8d0b..e43d06f 100644 --- a/CodeGenerator.sln +++ b/CodeGenerator.sln @@ -1,6 +1,8 @@  -Microsoft Visual Studio Solution File, Format Version 10.00 -# Visual Studio 2008 +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25123.0 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeGenerator.MVP", "CodeGenerator\CodeGenerator.MVP.csproj", "{A7D135B5-3D2D-4E1D-80F7-CF37CC367A6F}" EndProject Global diff --git a/CodeGenerator/App.config b/CodeGenerator/App.config index 6a731c6..dd27b87 100644 --- a/CodeGenerator/App.config +++ b/CodeGenerator/App.config @@ -1,11 +1,11 @@ - + - + - + @@ -13,26 +13,27 @@ - - + + + - - + + - + - + - \ No newline at end of file + diff --git a/CodeGenerator/CodeGenerator.MVP.csproj b/CodeGenerator/CodeGenerator.MVP.csproj index 32d289f..767b2d1 100644 --- a/CodeGenerator/CodeGenerator.MVP.csproj +++ b/CodeGenerator/CodeGenerator.MVP.csproj @@ -1,5 +1,5 @@  - + Debug AnyCPU @@ -10,7 +10,7 @@ Properties CodeGenerator.MVP CodeGenerator.MVP - v3.5 + v4.6.1 512 {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 4 @@ -32,6 +32,12 @@ + + + + + 3.5 + http://localhost/CodeGenerator/ true Web @@ -56,6 +62,7 @@ DEBUG;TRACE prompt 4 + false pdbonly @@ -64,6 +71,7 @@ TRACE prompt 4 + false @@ -74,6 +82,7 @@ 3.5 + 3.5 @@ -132,7 +141,9 @@ Resources.Designer.cs Designer - + + Designer + SettingsSingleFileGenerator Settings.Designer.cs @@ -174,12 +185,12 @@ False + Auto + False - Auto - False ManifestEntryPoint diff --git a/CodeGenerator/DAL/SPCreatorDAL.cs b/CodeGenerator/DAL/SPCreatorDAL.cs index 2a167ab..22b859d 100644 --- a/CodeGenerator/DAL/SPCreatorDAL.cs +++ b/CodeGenerator/DAL/SPCreatorDAL.cs @@ -68,11 +68,11 @@ public static DataTable GetColumnsDesc(string strTableName) if (Util.Utility.GetSelectedDB().Equals("Oracle")) { - cmd = new OleDbCommand("SELECT t.COLUMN_NAME, t.DATA_TYPE, t.CHAR_LENGTH AS MAX_LENGTH, NVL(t.DATA_PRECISION, 0) AS PRECISION, NVL(t.DATA_SCALE, 0) AS SCALE from user_tab_columns t where t.TABLE_NAME = '" + strTableName + "'", con); + cmd = new OleDbCommand($"SELECT t.COLUMN_NAME, t.DATA_TYPE, t.CHAR_LENGTH AS MAX_LENGTH, NVL(t.DATA_PRECISION, 0) AS PRECISION, NVL(t.DATA_SCALE, 0) AS SCALE from user_tab_columns t where t.TABLE_NAME = '{strTableName}'", con); } else if (Util.Utility.GetSelectedDB().Equals("SqlServer")) { - cmd = new OleDbCommand("SELECT cl.name AS COLUMN_NAME, tp.name AS DATA_TYPE, cl.Max_Length AS MAX_LENGTH, cl.Precision AS PRECISION, cl.Scale AS SCALE FROM sys.columns cl JOIN sys.systypes tp ON tp.xtype = cl.system_type_id WHERE object_id = OBJECT_ID('dbo." + strTableName + "') AND tp.status = 0 Order by cl.Column_ID", con); + cmd = new OleDbCommand($"SELECT cl.name AS COLUMN_NAME, tp.name AS DATA_TYPE, cl.Max_Length AS MAX_LENGTH, cl.Precision AS PRECISION, cl.Scale AS SCALE FROM sys.columns cl JOIN sys.systypes tp ON tp.xtype = cl.system_type_id WHERE object_id = OBJECT_ID('dbo.{strTableName}') AND tp.status = 0 Order by cl.Column_ID", con); } OleDbDataReader dr = null; diff --git a/CodeGenerator/Properties/AssemblyInfo.cs b/CodeGenerator/Properties/AssemblyInfo.cs index 2180d41..429d429 100644 --- a/CodeGenerator/Properties/AssemblyInfo.cs +++ b/CodeGenerator/Properties/AssemblyInfo.cs @@ -8,12 +8,12 @@ // set of attributes. Change these attribute values to modify the information // associated with an assembly. [assembly: AssemblyTitle("CodeGenerator.MVP")] -[assembly: AssemblyDescription("Created by Md.Ismail")] +[assembly: AssemblyDescription("Utility to generate backend code like Stored Procedure, Entity, Data Acceess Layer (DAL), Business Logic Layer (BLL) for CRUD operation on tables of both Oracle and Sql Server")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("TVL")] [assembly: AssemblyProduct("CodeGenerator.MVP")] -[assembly: AssemblyCopyright("Copyright © TVL 2011")] -[assembly: AssemblyTrademark("")] +[assembly: AssemblyCopyright("Copyright © mijaved 2016")] +[assembly: AssemblyTrademark("Created by Md.Ismail")] [assembly: AssemblyCulture("")] // Setting ComVisible to false makes the types in this assembly not visible @@ -51,7 +51,7 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("2.0.0.0")] +[assembly: AssemblyFileVersion("2.0.0.0")] //IntallationPath: C:\Users\\AppData\Local\Apps\2.0\77QRC1HQ.A3N\NEBBNPEV.D98 \ No newline at end of file diff --git a/CodeGenerator/Properties/Resources.Designer.cs b/CodeGenerator/Properties/Resources.Designer.cs index b7e350e..4475c78 100644 --- a/CodeGenerator/Properties/Resources.Designer.cs +++ b/CodeGenerator/Properties/Resources.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:2.0.50727.4927 +// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -19,7 +19,7 @@ namespace CodeGenerator.MVP.Properties { // class via a tool like ResGen or Visual Studio. // To add or remove a member, edit your .ResX file then rerun ResGen // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "2.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] internal class Resources { diff --git a/CodeGenerator/Properties/Settings.Designer.cs b/CodeGenerator/Properties/Settings.Designer.cs index 33ba26e..a7ceaf3 100644 --- a/CodeGenerator/Properties/Settings.Designer.cs +++ b/CodeGenerator/Properties/Settings.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:2.0.50727.4927 +// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -12,7 +12,7 @@ namespace CodeGenerator.MVP.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); diff --git a/CodeGenerator/Util/DbBuilderOracle.cs b/CodeGenerator/Util/DbBuilderOracle.cs index 82e6ec9..e61fa11 100644 --- a/CodeGenerator/Util/DbBuilderOracle.cs +++ b/CodeGenerator/Util/DbBuilderOracle.cs @@ -27,13 +27,7 @@ public StringBuilder BuildSequence(string strTableName) { StringBuilder sb = new StringBuilder(); sb.AppendLine(); - sb.Append("CREATE SEQUENCE Seq" + strTableName); - sb.Append("\n\t START WITH 1"); - sb.Append("\n\t MAXVALUE 999999999999999999999999999"); - sb.Append("\n\t MINVALUE 1"); - sb.Append("\n\t NOCYCLE"); - sb.Append("\n\t NOCACHE"); - sb.Append("\n\t NOORDER;"); + sb.Append($"CREATE SEQUENCE Seq{strTableName} START WITH 1 MAXVALUE 999999999999999999999999999 MINVALUE 1 NOCYCLE NOCACHE NOORDER;"); return sb; } @@ -41,88 +35,84 @@ public StringBuilder BuildSequence(string strTableName) public StringBuilder BuildSaveProcedure(string strTableName, DataTable dt) { StringBuilder sb = new StringBuilder(); - sb.Append("PROCEDURE usp" + strTableName.Replace("T_", "").Replace("t_", "") + "Save("); + sb.Append($"PROCEDURE usp{strTableName.Replace("T_", "").Replace("t_", "")}Save("); foreach (DataRow row in dt.Rows) { - if (row["COLUMN_NAME"].ToString().Contains(_STRUID) || row["COLUMN_NAME"].ToString().Contains(_STRLASTUID) || - row["COLUMN_NAME"].ToString().Contains(_DTUDT) || row["COLUMN_NAME"].ToString().Contains(_DTLASTUDT)) + var type = row["DATA_TYPE"].ToString(); + var col = row["COLUMN_NAME"].ToString(); + + if (col.Contains(_STRUID) || col.Contains(_STRLASTUID) || col.Contains(_DTUDT) || col.Contains(_DTLASTUDT)) continue; - sb.AppendLine(); - sb.Append("\t\t p" + row["COLUMN_NAME"] + "\t" + row["DATA_TYPE"] + ","); + sb.Append($"p{col}\t{type},".n1t1()); } //sb.Remove(sb.Length -1, 1); //To Remove last Comma - sb.Append("\n\t\t p_" + _STRUID + " VARCHAR2, \n\t\t p_STRMODE VARCHAR2, \n\t\t po_errorcode OUT NUMBER, \n\t\t po_errormessage OUT VARCHAR2)"); - - - sb.Append("\n\t IS"); - sb.Append("\n\t BEGIN"); - sb.Append("\n\t\t po_errorcode:=0;"); - sb.Append("\n\t\t po_errormessage:='SUCCESSFUL';"); + sb.Append($"p_{_STRUID} VARCHAR2, {"p_STRMODE".n1t2()} VARCHAR2, {"po_errorcode".n1t2()} OUT NUMBER, {"po_errormessage".n1t2()} OUT VARCHAR2)".n1t2()); + + sb.Append("IS".n1t1()); + sb.Append("BEGIN".n1t1()); + sb.Append("po_errorcode:=0;".n1t2()); + sb.Append("po_errormessage:='SUCCESSFUL';".n1t2()); //Insert - sb.Append("\n\t\t IF(UPPER(p_STRMODE)='I') THEN"); - sb.Append("\n\t\t\t INSERT INTO " + strTableName + "(" + _NUMID + ","); + sb.Append("IF(UPPER(p_STRMODE)='I') THEN".n1t2()); + sb.Append($"INSERT INTO {strTableName}({_NUMID},".n1t3()); foreach (DataRow row in dt.Rows) { - if (row["COLUMN_NAME"].ToString().Contains(_NUMID) || - row["COLUMN_NAME"].ToString().Contains(_STRUID) || row["COLUMN_NAME"].ToString().Contains(_STRLASTUID) || - row["COLUMN_NAME"].ToString().Contains(_DTUDT) || row["COLUMN_NAME"].ToString().Contains(_DTLASTUDT)) + var col = row["COLUMN_NAME"].ToString(); + if (col.Contains(_NUMID) || col.Contains(_STRUID) || col.Contains(_STRLASTUID) || col.Contains(_DTUDT) || col.Contains(_DTLASTUDT)) continue; - sb.AppendLine(); - sb.Append("\t\t\t\t " + row["COLUMN_NAME"] + ","); + sb.Append($"{col},".n1t4()); } - sb.Append("\n\t\t\t\t STRUID, \n\t\t\t\t STRLASTUID, \n\t\t\t\t DTUDT, \n\t\t\t\t DTLASTUDT)"); + sb.Append($"{_STRUID.n1t4()}, {_STRLASTUID.n1t4()}, {_DTUDT.n1t4()}, {_DTLASTUDT.n1t4()})"); - sb.Append("\n\t\t\t VALUES(" + "Seq" + strTableName + ".NextVal,"); + + sb.Append($"VALUES(Seq{strTableName }.NextVal,".n1t3()); foreach (DataRow row in dt.Rows) { - if (row["COLUMN_NAME"].ToString().Contains(_NUMID) || - row["COLUMN_NAME"].ToString().Contains(_STRUID) || row["COLUMN_NAME"].ToString().Contains(_STRLASTUID) || - row["COLUMN_NAME"].ToString().Contains(_DTUDT) || row["COLUMN_NAME"].ToString().Contains(_DTLASTUDT)) + var col = row["COLUMN_NAME"].ToString(); + if (col.Contains(_NUMID) || col.Contains(_STRUID) || col.Contains(_STRLASTUID) || col.Contains(_DTUDT) || col.Contains(_DTLASTUDT)) continue; - sb.AppendLine(); - sb.Append("\t\t\t\t p" + row["COLUMN_NAME"] + ","); + sb.Append($"p{col},".n1t4()); } - sb.Append("\n\t\t\t\t p_" + _STRUID + ", \n\t\t\t\t p_" + _STRUID + ", \n\t\t\t\t sysdate, \n\t\t\t\t sysdate);"); - sb.Append("\n\t\t\t SELECT " + "Seq" + strTableName + ".CURRVAL INTO po_errorcode FROM dual;"); + sb.Append($"p_{_STRUID.n1t4()},p_{_STRUID.n1t4()},{"sysdate".n1t4()},{"sysdate".n1t4()});"); + sb.Append($"SELECT Seq{strTableName}.CURRVAL INTO po_errorcode FROM dual;".n1t3()); //Edit - sb.Append("\n\t\t ELSIF(UPPER(p_STRMODE)='U') THEN"); - sb.Append("\n\t\t\t UPDATE " + strTableName + " SET"); + sb.Append("ELSIF(UPPER(p_STRMODE)='U') THEN".n1t2()); + sb.Append($"UPDATE {strTableName} SET".n1t3()); foreach (DataRow row in dt.Rows) { - if (row["COLUMN_NAME"].ToString().Contains(_NUMID) || - row["COLUMN_NAME"].ToString().Contains(_STRUID) || row["COLUMN_NAME"].ToString().Contains(_STRLASTUID) || - row["COLUMN_NAME"].ToString().Contains(_DTUDT) || row["COLUMN_NAME"].ToString().Contains(_DTLASTUDT)) + var col = row["COLUMN_NAME"].ToString(); + if (col.Contains(_NUMID) || col.Contains(_STRUID) || col.Contains(_STRLASTUID) || col.Contains(_DTUDT) || col.Contains(_DTLASTUDT)) continue; sb.AppendLine(); - sb.Append("\t\t\t\t " + row["COLUMN_NAME"] + " \t = p" + row["COLUMN_NAME"] + ","); + sb.Append($"{col}\t= p{col},".n0t4()); } - sb.Append("\n\t\t\t\t " + _STRLASTUID + " = p_" + _STRUID + ", \n\t\t\t\t " + _DTLASTUDT + " = sysdate"); - sb.Append("\n\t\t\t\t WHERE " + _NUMID + " = p" + _NUMID + ";"); - sb.Append("\n\t\t\t SELECT p" + _NUMID + " INTO po_errorcode FROM dual;"); - sb.Append("\n\t\t\t --UPDATE TBLAUDITMASTER SET STRUSER=p_STRUID WHERE NUMSESSIONID=userenv('sessionid');"); + sb.Append($"{_STRLASTUID} = p_{_STRUID},".n1t4()); + sb.Append($"{_DTLASTUDT} = sysdate".n1t4()); + sb.Append($"WHERE {_NUMID} = p{_NUMID};".n1t4()); + sb.Append($"SELECT p{_NUMID} INTO po_errorcode FROM dual;".n1t3()); + sb.Append("--UPDATE TBLAUDITMASTER SET STRUSER=p_STRUID WHERE NUMSESSIONID=userenv('sessionid');".n1t3()); //Delete - sb.Append("\n\t\t ELSIF(UPPER(p_STRMODE)='D') THEN"); - sb.Append("\n\t\t\t DELETE FROM " + strTableName + " WHERE " + _NUMID + " = p" + _NUMID + ";"); - sb.Append("\n\t\t\t UPDATE TBLAUDITMASTER SET STRUSER=p_STRUID WHERE NUMSESSIONID=userenv('sessionid');"); - - sb.Append("\n\t\t ELSE"); - sb.Append("\n\t\t\t po_errorcode:=1;"); - sb.Append("\n\t\t\t po_errormessage:='Invalid Mode, No operations done';"); - sb.Append("\n\t\t END IF;"); + sb.Append("ELSIF(UPPER(p_STRMODE)='D') THEN".n1t2()); + sb.Append($"DELETE FROM {strTableName} WHERE {_NUMID} = p{_NUMID};".n1t3()); + sb.Append("UPDATE TBLAUDITMASTER SET STRUSER=p_STRUID WHERE NUMSESSIONID=userenv('sessionid');".n1t3()); - sb.Append("\n\n\t\t EXCEPTION WHEN OTHERS THEN"); - sb.Append("\n\t\t\t po_errorcode := SQLCODE;"); - sb.Append("\n\t\t\t po_errormessage := SQLERRM;"); + sb.Append("ELSE".n1t2()); + sb.Append("po_errorcode:=1;".n1t3()); + sb.Append("po_errormessage:='Invalid Mode, No operations done';".n1t3()); + sb.Append("END IF;".n1t2()); - sb.Append("\n\t END;"); + sb.AppendLine(); + sb.Append("EXCEPTION WHEN OTHERS THEN".n1t2()); + sb.Append("po_errorcode := SQLCODE;".n1t3()); + sb.Append("po_errormessage := SQLERRM;".n1t3()); - //MessageBox.Show(sb.ToString()); + sb.Append("END;".n1t1()); return sb; } @@ -130,117 +120,126 @@ public StringBuilder BuildSaveProcedure(string strTableName, DataTable dt) public StringBuilder BuildGetProcedure(string strTableName, DataTable dt) { StringBuilder sb = new StringBuilder(); - sb.Append("PROCEDURE usp" + strTableName.Replace("T_", "").Replace("t_", "") + "Get("); + sb.Append($"PROCEDURE usp{strTableName.Replace("T_", "").Replace("t_", "")}Get("); foreach (DataRow row in dt.Rows) { - if (row["COLUMN_NAME"].ToString().Contains(_STRUID) || row["COLUMN_NAME"].ToString().Contains(_STRLASTUID) || - row["COLUMN_NAME"].ToString().Contains(_DTUDT) || row["COLUMN_NAME"].ToString().Contains(_DTLASTUDT)) + var type = row["DATA_TYPE"].ToString(); + var col = row["COLUMN_NAME"].ToString(); + + if (col.Contains(_STRUID) || col.Contains(_STRLASTUID) || + col.Contains(_DTUDT) || col.Contains(_DTLASTUDT)) continue; sb.AppendLine(); - if (row["DATA_TYPE"].ToString().ToUpper().Equals("DATE")) + if (type.ToUpper().Equals("DATE")) { - sb.Append("\t\t p" + row["COLUMN_NAME"] + "From\t" + row["DATA_TYPE"] + ","); - sb.AppendLine(); - sb.Append("\t\t p" + row["COLUMN_NAME"] + "To\t" + row["DATA_TYPE"] + ","); + sb.Append($"p{col}From\t{type},".n0t2()); + sb.Append($"p{col}To\t{type},".n1t2()); continue; } - sb.Append("\t\t p" + row["COLUMN_NAME"] + "\t" + row["DATA_TYPE"] + ","); + sb.Append($"p{col}\t{type},".n0t2()); } //sb.Remove(sb.Length -1, 1); //To Remove last Comma - sb.Append("\n\t\t p_NUMMODE NUMBER,"); - sb.Append("\n\t\t p_SORTEXPRESSION VARCHAR2,"); - sb.Append("\n\t\t p_SORTDIRECTION VARCHAR2,"); - sb.Append("\n\t\t p_STARTROW NUMBER, -- START INDEX"); - sb.Append("\n\t\t p_MAXROWS NUMBER, -- OPTIONAL ; 0 TO GET ALL SELECTED"); - sb.Append("\n\t\t po_errorcode OUT NUMBER,"); - sb.Append("\n\t\t po_errormessage OUT VARCHAR2,"); - sb.Append("\n\t\t po_cursor OUT Types.Ref_Cursor)"); - - sb.Append("\n\t IS"); - sb.Append("\n\t\t strSql VARCHAR2(5000) DEFAULT '';"); - - sb.Append("\n\t BEGIN"); - sb.Append("\n\t\t po_errorcode:=0;"); - sb.Append("\n\t\t po_errormessage:='SUCCESSFUL';"); - sb.Append("\n"); + sb.Append("p_NUMMODE NUMBER,".n1t2()); + sb.Append("p_SORTEXPRESSION VARCHAR2,".n1t2()); + sb.Append("p_SORTDIRECTION VARCHAR2,".n1t2()); + sb.Append("p_STARTROW NUMBER, -- START INDEX".n1t2()); + sb.Append("p_MAXROWS NUMBER, -- OPTIONAL ; 0 TO GET ALL SELECTED".n1t2()); + sb.Append("po_errorcode OUT NUMBER,".n1t2()); + sb.Append("po_errormessage OUT VARCHAR2,".n1t2()); + sb.Append("po_cursor OUT Types.Ref_Cursor)".n1t2()); + + sb.Append("IS".n1t1()); + sb.Append("strSql VARCHAR2(5000) DEFAULT '';".n1t2()); + + sb.Append("BEGIN".n1t1()); + sb.Append("po_errorcode:=0;".n1t2()); + sb.Append("po_errormessage:='SUCCESSFUL';".n1t2()); + sb.AppendLine(); //RowCount - sb.Append("\n\t\t IF (p_NUMMODE=0) THEN -- FOR ROW COUNT "); - sb.Append("\n\t\t\t strSql := 'SELECT COUNT(1) NUMROWS FROM " + strTableName + " T "); - sb.Append("\n\t\t\t WHERE 0 = 0 ';"); + sb.Append("IF (p_NUMMODE=0) THEN -- FOR ROW COUNT ".n1t2()); + sb.Append("strSql := 'SELECT COUNT(1) NUMROWS FROM " + strTableName + " T ".n1t3()); + sb.Append("WHERE 0 = 0 ';".n1t3()); //SelectAll - sb.Append("\n\t\t ELSIF (p_NUMMODE=1) THEN -- GRID VIEW "); - sb.Append("\n\t\t\t strSql := 'SELECT * FROM (SELECT T.*,rowNum as ROWINDEX FROM ("); - sb.Append("\n\t\t\t SELECT T.* FROM " + strTableName + " T "); - sb.Append("\n\t\t\t WHERE 0 = 0 ';"); + sb.Append("ELSIF (p_NUMMODE=1) THEN -- GRID VIEW ".n1t2()); + sb.Append("strSql := 'SELECT * FROM (SELECT T.*,rowNum as ROWINDEX FROM (".n1t3()); + sb.Append("SELECT T.* FROM " + strTableName + " T ".n1t3()); + sb.Append("WHERE 0 = 0 ';".n1t3()); - sb.Append("\n\t\t END IF;"); + sb.Append("END IF;".n1t2()); foreach (DataRow row in dt.Rows) { - if (//row["COLUMN_NAME"].ToString().Contains(_NUMID) || - row["COLUMN_NAME"].ToString().Contains(_STRUID) || row["COLUMN_NAME"].ToString().Contains(_STRLASTUID) || - row["COLUMN_NAME"].ToString().Contains(_DTUDT) || row["COLUMN_NAME"].ToString().Contains(_DTLASTUDT)) + var type = row["DATA_TYPE"].ToString(); + var col = row["COLUMN_NAME"].ToString(); + + if (col.Contains(_STRUID) || col.Contains(_STRLASTUID) || col.Contains(_DTUDT) || col.Contains(_DTLASTUDT)) continue; sb.AppendLine(); - //Rules#:INT/NUMBER/DECIMAL/DOUBLE/FLOAT kinds of DataType must be prefixed by NUM - if (row["COLUMN_NAME"].ToString().StartsWith("NUM")) - { - sb.Append("\n\t\t IF p" + row["COLUMN_NAME"] + ">0 THEN "); - sb.Append("\n\t\t\t strSql := strSql || ' AND T." + row["COLUMN_NAME"] + " = '|| p" + row["COLUMN_NAME"].ToString() + ";"); - sb.Append("\n\t\t END IF;"); + //Rules#:INT/NUMBER/NUMERIC/DECIMAL/DOUBLE/FLOAT kinds of DataType must be prefixed by NUM + //if (type.ToUpper().Equals("NUMBER") || type.ToUpper().Equals("NUMERIC") || type.ToUpper().Equals("INT") || type.ToUpper().Equals("INTEGER") || type.ToUpper().Equals("DECIMAL") || type.ToUpper().Equals("DEC") || type.ToUpper().Equals("DOUBLE") || type.ToUpper().Equals("FLOAT") ) //if (col.StartsWith("NUM")) + string[] numericTypes = new string[] { "NUMBER", "NUMERIC", "INT", "INTEGER", "SMALLINT", "DECIMAL", "DEC", "DECIMAL", "DOUBLE", "FLOAT", "REAL", "DOUBLE PRECISION" }; + if (type.ToUpper().In(numericTypes)) + { + sb.Append($"IF p{col}>0 THEN ".n1t2()); + sb.Append($"strSql := strSql || ' AND T.{col} = '|| p" + col + ";".n1t3()); + sb.Append("END IF;".n1t2()); } - //Rules#:CHAR/VARCHAR/VARCHAR2/STRING kinds of DataType must be prefixed by STR - if (row["COLUMN_NAME"].ToString().StartsWith("STR")) - { - sb.Append("\n\t\t IF LENGTH(p" + row["COLUMN_NAME"] + ")>0 THEN "); - sb.Append("\n\t\t\t strSql := strSql || ' AND UPPER(T." + row["COLUMN_NAME"] + ") LIKE ''%' || REPLACE(UPPER(p" + row["COLUMN_NAME"] + "),'''','''''') || '%''';"); - sb.Append("\n\t\t\t -- strSql := strSql || ' AND UPPER(T." + row["COLUMN_NAME"] + ")=''' || REPLACE(UPPER(p" + row["COLUMN_NAME"] + "),'''','''''') || '''';"); - sb.Append("\n\t\t END IF;"); + //Rules#:CHAR/VARCHAR/VARCHAR2/STRING/CLOB kinds of DataType must be prefixed by STR + //if (type.ToUpper().Equals("CHAR") || type.ToUpper().Equals("VARCHAR") || type.ToUpper().Equals("VARCHAR2") || type.ToUpper().Equals("STRING") || type.ToUpper().Equals("CLOB")) //if (col.StartsWith("STR")) + string[] charTypes = new string[] { "CHAR", "NCHAR", "VARCHAR", "VARCHAR2", "NVARCHAR2", "STRING", "LONG", "RAW", "LONG RAW"}; + if (type.ToUpper().In(charTypes)) + { + sb.Append($"IF LENGTH(p{col})>0 THEN ".n1t2()); + + sb.Append($"strSql := strSql || ' AND UPPER(T.{col}) LIKE ''%' || REPLACE(UPPER(p{col}),'''','''''') || '%''';".n1t3()); + sb.Append($"-- strSql := strSql || ' AND UPPER(T.{col})=''' || REPLACE(UPPER(p{col}),'''','''''') || '''';".n1t3()); + + sb.Append("END IF;".n1t2()); } //Rules#:DATE/DATETIME kinds of DataType must be prefixed by DT - if (row["DATA_TYPE"].ToString().ToUpper().Equals("DATE")) //|| (row["COLUMN_NAME"].ToString().StartsWith("DT")) + if (type.ToUpper().Equals("DATE") || type.ToUpper().Equals("TIMESTAMP")) //|| (col.StartsWith("DT")) { - sb.Append("\n\t\t IF p" + row["COLUMN_NAME"] + "From IS NOT NULL AND p" + row["COLUMN_NAME"] + "To IS NOT NULL THEN"); - sb.Append("\n\t\t\t strSql := strSql || ' AND T." + row["COLUMN_NAME"] + " BETWEEN '''|| p" + row["COLUMN_NAME"] + "From ||''' AND '''||p" + row["COLUMN_NAME"] + "To|| '''';"); - sb.Append("\n\t\t ELSIF p" + row["COLUMN_NAME"] + "From IS NOT NULL AND p" + row["COLUMN_NAME"] + "To IS NULL THEN"); - sb.Append("\n\t\t\t strSql := strSql || ' AND T." + row["COLUMN_NAME"] + " >= '''|| p" + row["COLUMN_NAME"] + "From|| '''' ;"); - sb.Append("\n\t\t ELSIF p" + row["COLUMN_NAME"] + "From IS NULL AND p" + row["COLUMN_NAME"] + "To IS NOT NULL THEN"); - sb.Append("\n\t\t\t strSql := strSql || ' AND T." + row["COLUMN_NAME"] + " <= ''' ||p" + row["COLUMN_NAME"] + "To|| '''';"); - sb.Append("\n\t\t END IF;"); + sb.Append($"IF p{col}From IS NOT NULL AND p{col}To IS NOT NULL THEN".n1t2()); + sb.Append($"strSql := strSql || ' AND T.{col} BETWEEN '''|| p{col}From ||''' AND '''||p{col}To|| '''';".n1t3()); + sb.Append($"ELSIF p{col}From IS NOT NULL AND p{col}To IS NULL THEN".n1t2()); + sb.Append($"strSql := strSql || ' AND T.{col} >= '''|| p{col}From|| '''' ;".n1t3()); + sb.Append($"ELSIF p{col}From IS NULL AND p{col}To IS NOT NULL THEN".n1t2()); + sb.Append($"strSql := strSql || ' AND T.{col} <= ''' ||p{col}To|| '''';".n1t3()); + sb.Append($"END IF;".n1t2()); } } - sb.Append("\n\n\t\t -- Paging and Sorting Parameters"); - sb.Append("\n\t\t IF (p_NUMMODE=1) THEN"); - sb.Append("\n\t\t\t strSql :=strSql || ' ORDER BY ' || UPPER(p_SORTEXPRESSION) ||' ' || UPPER(p_SORTDIRECTION);"); - sb.Append("\n\t\t\t IF(p_MAXROWS >0)THEN"); - sb.Append("\n\t\t\t\t strSql :=strSql || ' )T)T WHERE ROWINDEX>= '||p_STARTROW||' AND ROWINDEX <'|| (p_STARTROW + p_MAXROWS);"); - sb.Append("\n\t\t\t ELSE"); - sb.Append("\n\t\t\t\t strSql :=strSql || ' )T)T';"); - sb.Append("\n\t\t\t END IF;"); - sb.Append("\n\t\t END IF;"); - - sb.Append("\n\n\t\t IF (po_errorcode = 0) THEN"); - sb.Append("\n\t\t\t OPEN po_cursor FOR strSql ;"); - sb.Append("\n\t\t END IF;"); - - sb.Append("\n\n\t\t EXCEPTION"); - sb.Append("\n\t\t\t\t WHEN OTHERS THEN"); - sb.Append("\n\t\t\t\t po_errorcode := SQLCODE;"); - sb.Append("\n\t\t\t\t po_errormessage := SQLERRM;"); - - sb.Append("\n\t END;"); - - //MessageBox.Show(sb.ToString()); + sb.AppendLine(); + sb.Append("-- Paging and Sorting Parameters".n1t2()); + sb.Append("IF (p_NUMMODE=1) THEN".n1t2()); + sb.Append("strSql :=strSql || ' ORDER BY ' || UPPER(p_SORTEXPRESSION) ||' ' || UPPER(p_SORTDIRECTION);".n1t3()); + sb.Append("IF(p_MAXROWS >0)THEN".n1t3()); + sb.Append("strSql :=strSql || ' )T)T WHERE ROWINDEX>= '||p_STARTROW||' AND ROWINDEX <'|| (p_STARTROW + p_MAXROWS);".n1t4()); + sb.Append("ELSE".n1t3()); + sb.Append("strSql :=strSql || ' )T)T';".n1t4()); + sb.Append("END IF;".n1t3()); + sb.Append("END IF;".n1t2()); + + sb.AppendLine(); + sb.Append("IF (po_errorcode = 0) THEN".n1t2()); + sb.Append("OPEN po_cursor FOR strSql ;".n1t3()); + sb.Append("END IF;".n1t2()); + + sb.AppendLine(); + sb.Append("EXCEPTION WHEN OTHERS THEN".n1t2()); + sb.Append("po_errorcode := SQLCODE;".n1t3()); + sb.Append("po_errormessage := SQLERRM;".n1t3()); + + sb.Append("END;".n1t1()); return sb; } diff --git a/CodeGenerator/Util/DbBuilderSqlServer.cs b/CodeGenerator/Util/DbBuilderSqlServer.cs index ed8d40e..004beb5 100644 --- a/CodeGenerator/Util/DbBuilderSqlServer.cs +++ b/CodeGenerator/Util/DbBuilderSqlServer.cs @@ -8,9 +8,20 @@ namespace CodeGenerator.MVP.Util { public class DbBuilderSqlServer : IDbBuilder { - public DbBuilderSqlServer() + string _NUMID = ""; + string _STRUID = ""; + string _STRLASTUID = ""; + string _DTUDT = ""; + string _DTLASTUDT = ""; + + public DbBuilderSqlServer() { - } + _NUMID = Util.Utility.GetPkColName(); + _STRUID = Util.Utility.GetRecordCreatorColName(); + _STRLASTUID = Util.Utility.GetRecordModifierColName(); + _DTUDT = Util.Utility.GetRecordCreateDateColName(); + _DTLASTUDT = Util.Utility.GetRecordModifiedDateColName(); + } public StringBuilder BuildSequence(string strTableName) { @@ -22,108 +33,120 @@ public StringBuilder BuildSequence(string strTableName) public StringBuilder BuildSaveProcedure(string strTableName, DataTable dt) { StringBuilder sb = new StringBuilder(); - string strProcName = "usp" + strTableName.Replace("T_", "").Replace("t_", "").Replace("tbl", "") + "Save"; + string strProcName = $"usp{strTableName.Replace("T_", "").Replace("t_", "").Replace("tbl", "")}Save"; - sb.Append("CREATE PROCEDURE " + strProcName + "("); + sb.Append($"CREATE PROCEDURE {strProcName}("); foreach (DataRow row in dt.Rows) { - if (row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreatorColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifierColName()) || - row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreateDateColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifiedDateColName())) + var type = row["DATA_TYPE"].ToString(); + var col = row["COLUMN_NAME"].ToString(); + + if (col.Contains(_STRUID) || col.Contains(_STRLASTUID) || + col.Contains(_DTUDT) || col.Contains(_DTLASTUDT)) continue; sb.AppendLine(); //for UNIQUEIDENTIFIER - if (row["DATA_TYPE"].ToString().ToUpper().Equals("UNIQUEIDENTIFIER")) + if (type.ToUpper().Equals("UNIQUEIDENTIFIER")) { - sb.Append("\t @" + row["COLUMN_NAME"] + "\t" + "VARCHAR(50)" + ","); + sb.Append($"@{col}" + "VARCHAR(50)".n0t1() + ",".n0t1()); continue; } - string strLength = row["DATA_TYPE"].ToString().ToUpper().Equals("VARCHAR") || row["DATA_TYPE"].ToString().ToUpper().Equals("CHAR") ? " (" + row["MAX_LENGTH"].ToString() + ")" : ""; - sb.Append("\t @" + row["COLUMN_NAME"] + "\t" + row["DATA_TYPE"].ToString().ToUpper() + strLength + ","); + string strLength = type.ToUpper().Equals("VARCHAR") || type.ToUpper().Equals("CHAR") ? " (" + row["MAX_LENGTH"].ToString() + ")" : ""; + sb.Append($"@{col}" + type.ToUpper().n0t1() + strLength + ",".n0t1()); } //sb.Remove(sb.Length -1, 1); //To Remove last Comma - sb.Append("\n\t @" + Util.Utility.GetRecordCreatorColName() + " VARCHAR(20), \n\t @strMode CHAR(1),"); + sb.Append($"@{_STRUID} VARCHAR(20), \n\t @strMode CHAR(1),".n1t1()); sb.Append("\n\t @strErrorCode VARCHAR(50) OUTPUT, --@numErrorCode INT OUTPUT, \n\t @strErrorMsg VARCHAR(200) OUTPUT)"); - sb.Append("\n AS"); - sb.Append("\n\t SET @strErrorCode = 0 --@numErrorCode = 0"); - sb.Append("\n\t SET @strErrorMsg = 'Successful'"); - sb.Append("\n\t DECLARE @strSQL VARCHAR(4000);"); - sb.Append("\n BEGIN"); + sb.Append("AS".n1t0()); + sb.Append("SET @strErrorCode = 0 --@numErrorCode = 0".n1t1()); + sb.Append("SET @strErrorMsg = 'Successful'".n1t1()); + sb.Append("DECLARE @strSQL VARCHAR(4000);".n1t1()); + sb.Append("BEGIN".n1t0()); //Insert - sb.Append("\n\t IF (@strMode='I')"); - sb.Append("\n\t BEGIN"); - sb.Append("\n\t\t --SET @" + Util.Utility.GetPkColName() + " = NEWID()"); - sb.Append("\n\t\t INSERT INTO " + strTableName + "("); + sb.Append("IF (@strMode='I')".n1t1()); + sb.Append("BEGIN".n1t1()); + sb.Append($"--SET @{_NUMID} = NEWID()".n1t2()); + sb.Append("INSERT INTO " + strTableName + "(".n1t2()); foreach (DataRow row in dt.Rows) { - if (row["COLUMN_NAME"].ToString().Equals(Util.Utility.GetPkColName()) || - row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreatorColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifierColName()) || - row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreateDateColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifiedDateColName())) + var type = row["DATA_TYPE"].ToString(); + var col = row["COLUMN_NAME"].ToString(); + + if (col.Equals(_NUMID) || + col.Contains(_STRUID) || col.Contains(_STRLASTUID) || + col.Contains(_DTUDT) || col.Contains(_DTLASTUDT)) continue; sb.AppendLine(); - sb.Append("\t\t\t " + row["COLUMN_NAME"] + ","); + sb.Append($"{col},".n0t3()); } - sb.Append("\n\t\t\t " + Util.Utility.GetRecordCreatorColName() + ", \n\t\t\t " + Util.Utility.GetRecordModifierColName() + ")"); + sb.Append($"{_STRUID}, " + _STRLASTUID.n1t3() + ")".n1t3()); - sb.Append("\n\t\t VALUES("); + sb.Append("VALUES(".n1t2()); foreach (DataRow row in dt.Rows) { - if (row["COLUMN_NAME"].ToString().Equals(Util.Utility.GetPkColName()) || - row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreatorColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifierColName()) || - row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreateDateColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifiedDateColName())) + var type = row["DATA_TYPE"].ToString(); + var col = row["COLUMN_NAME"].ToString(); + + if (col.Equals(_NUMID) || + col.Contains(_STRUID) || col.Contains(_STRLASTUID) || + col.Contains(_DTUDT) || col.Contains(_DTLASTUDT)) continue; sb.AppendLine(); - sb.Append("\t\t\t @" + row["COLUMN_NAME"] + ","); + sb.Append($"@{col},".n0t3()); } - sb.Append("\n\t\t\t @" + Util.Utility.GetRecordCreatorColName() + ", \n\t\t\t @" + Util.Utility.GetRecordCreatorColName() + ");"); - sb.Append("\n\t\t --SELECT @@IDENTITY;"); - sb.Append("\n\t\t SET @" + Util.Utility.GetPkColName() + "=@@identity;"); - sb.Append("\n\t END"); + sb.Append($"@{_STRUID}, @{ _STRUID.n1t3()};".n1t3()); + sb.Append("--SELECT @@IDENTITY;".n1t2()); + sb.Append($"SET @{_NUMID}=@@identity;".n1t2()); + sb.Append("END".n1t1()); //Edit - sb.Append("\n\t ELSE IF (@strMode='U')"); - sb.Append("\n\t BEGIN"); - sb.Append("\n\t\t UPDATE " + strTableName + " SET"); + sb.Append("ELSE IF (@strMode='U')".n1t1()); + sb.Append("BEGIN".n1t1()); + sb.Append("UPDATE " + strTableName + " SET".n1t2()); foreach (DataRow row in dt.Rows) { - if (row["COLUMN_NAME"].ToString().Equals(Util.Utility.GetPkColName()) || - row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreatorColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifierColName()) || - row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreateDateColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifiedDateColName())) + var type = row["DATA_TYPE"].ToString(); + var col = row["COLUMN_NAME"].ToString(); + + if (col.Equals(_NUMID) || + col.Contains(_STRUID) || col.Contains(_STRLASTUID) || + col.Contains(_DTUDT) || col.Contains(_DTLASTUDT)) continue; sb.AppendLine(); - sb.Append("\t\t\t " + row["COLUMN_NAME"] + " \t = @" + row["COLUMN_NAME"] + ","); + sb.Append($"{col} \t = @{col},".n0t3()); } - sb.Append("\n\t\t\t " + Util.Utility.GetRecordModifierColName() + "=@" + Util.Utility.GetRecordCreatorColName() + ""); - sb.Append("\n\t\t\t WHERE " + Util.Utility.GetPkColName() + "=@" + Util.Utility.GetPkColName() + " ;"); - sb.Append("\n\t\t --SELECT @" + Util.Utility.GetPkColName() + ";"); - sb.Append("\n\t END"); + sb.Append($"{_STRLASTUID}=@{_STRUID}".n1t3()); + sb.Append($"WHERE {_NUMID}=@{_NUMID} ;".n1t3()); + sb.Append($"--SELECT @{_NUMID};".n1t2()); + sb.Append("END".n1t1()); //Delete - sb.Append("\n\t ELSE IF (@strMode='D')"); - sb.Append("\n\t BEGIN"); - sb.Append("\n\t\t DELETE FROM " + strTableName + " WHERE " + Util.Utility.GetPkColName() + " = @" + Util.Utility.GetPkColName() + " ;"); - //sb.Append("\n\t\t UPDATE TBLAUDITMASTER SET STRUSER=p_STRUID WHERE NUMSESSIONID=userenv('sessionid');"); - sb.Append("\n\t END"); - sb.Append("\n END"); - - sb.Append("\n\t SET @strErrorCode = @" + Util.Utility.GetPkColName()); sb.Append("\t --SET @numErrorCode = @" + Util.Utility.GetPkColName()); - sb.Append("\n\t IF @@error <> 0 goto procError"); - sb.Append("\n\t\t goto procEnd"); - - sb.Append("\n\n\t procError:"); - sb.Append("\n\t\t SET @strErrorCode = @@error"); sb.Append("\t --SET @numErrorCode = @@error"); - sb.Append("\n\t\t select @strErrorMsg = [description] from master.dbo.sysmessages where error = @strErrorCode --@numErrorCode"); - sb.Append("\n\t\t insert into error_log (LogDate,Source,ErrMsg) values (getdate(),'" + strProcName + "',@strErrorMsg)"); - - sb.Append("\n procEnd:"); + sb.Append("ELSE IF (@strMode='D')".n1t1()); + sb.Append("BEGIN".n1t1()); + sb.Append($"DELETE FROM {strTableName} WHERE {_NUMID } = @{_NUMID};".n1t2()); + //sb.Append("UPDATE TBLAUDITMASTER SET STRUSER=p_STRUID WHERE NUMSESSIONID=userenv('sessionid');".n1t2()); + sb.Append("END".n1t1()); + sb.Append("END".n1t0()); + + sb.Append($"SET @strErrorCode = @{_NUMID}".n1t1()); sb.Append("--SET @numErrorCode = @" + _NUMID.n0t1()); + sb.Append("IF @@error <> 0 goto procError".n1t1()); + sb.Append("goto procEnd".n1t2()); + + sb.Append("procError:".n1t0().n1t1()); + sb.Append("SET @strErrorCode = @@error".n1t2()); sb.Append("--SET @numErrorCode = @@error".n0t1()); + sb.Append("select @strErrorMsg = [description] from master.dbo.sysmessages where error = @strErrorCode --@numErrorCode".n1t2()); + sb.Append($"insert into error_log (LogDate,Source,ErrMsg) values (getdate(),'{strProcName}',@strErrorMsg)".n1t2()); + + sb.Append("procEnd:".n1t0()); //MessageBox.Show(sb.ToString()); return sb; @@ -132,168 +155,170 @@ public StringBuilder BuildSaveProcedure(string strTableName, DataTable dt) public StringBuilder BuildGetProcedure(string strTableName, DataTable dt) { StringBuilder sb = new StringBuilder(); - string strProcName = "usp" + strTableName.Replace("T_", "").Replace("t_", "").Replace("tbl", "") + "Get"; + string strProcName = $"usp{strTableName.Replace("T_", "").Replace("t_", "").Replace("tbl", "")}Get"; - sb.Append("CREATE PROCEDURE " + strProcName + "("); + sb.Append($"CREATE PROCEDURE {strProcName}("); foreach (DataRow row in dt.Rows) { - if (row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreatorColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifierColName()) || - row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreateDateColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifiedDateColName()) || - row["DATA_TYPE"].ToString().ToUpper().Equals("IMAGE") || row["DATA_TYPE"].ToString().ToUpper().Equals("TEXT") || - row["DATA_TYPE"].ToString().ToUpper().Equals("XML")) + var type = row["DATA_TYPE"].ToString(); + var col = row["COLUMN_NAME"].ToString(); + + if (col.Contains(_STRUID) || col.Contains(_STRLASTUID) || + col.Contains(_DTUDT) || col.Contains(_DTLASTUDT) || + type.ToUpper().Equals("IMAGE") || type.ToUpper().Equals("TEXT") || + type.ToUpper().Equals("XML")) continue; sb.AppendLine(); //for UNIQUEIDENTIFIER - if (row["DATA_TYPE"].ToString().ToUpper().Equals("UNIQUEIDENTIFIER")) + if (type.ToUpper().Equals("UNIQUEIDENTIFIER")) { - sb.Append("\t @" + row["COLUMN_NAME"] + "\t" + "VARCHAR(50)" + ","); + sb.Append($"@{col}\tVARCHAR(50),".n0t1()); continue; } - if (row["DATA_TYPE"].ToString().ToUpper().Equals("DATETIME")) + if (type.ToUpper().Equals("DATETIME")) { - sb.Append("\t @" + row["COLUMN_NAME"] + "From\t" + "VARCHAR(20)" + ","); + sb.Append($"@{col}From\tVARCHAR(20),".n0t1()); sb.AppendLine(); - sb.Append("\t @" + row["COLUMN_NAME"] + "To\t" + "VARCHAR(20)" + ","); + sb.Append($"@{col}ToVARCHAR(20),".n0t1()); continue; } - string strLength = row["DATA_TYPE"].ToString().ToUpper().Equals("VARCHAR") || row["DATA_TYPE"].ToString().ToUpper().Equals("CHAR") ? " (" + row["MAX_LENGTH"].ToString() + ")" : ""; - sb.Append("\t @" + row["COLUMN_NAME"] + "\t" + row["DATA_TYPE"].ToString().ToUpper() + strLength + ","); + string strLength = type.ToUpper().Equals("VARCHAR") || type.ToUpper().Equals("CHAR") ? " (" + row["MAX_LENGTH"].ToString() + ")" : ""; + sb.Append($"@{col}\t{type.ToUpper() + strLength},".n0t1()); } //sb.Remove(sb.Length -1, 1); //To Remove last Comma - sb.Append("\n\t @strMode VARCHAR(1), --0 for total count, 1 for single detail view, 2 for grid view"); - sb.Append("\n\t @strSortBy VARCHAR(50),"); - sb.Append("\n\t @strSortType VARCHAR(4),"); - sb.Append("\n\t @startRowIndex INT,"); - sb.Append("\n\t @maximumRows INT, -- OPTIONAL ; 0 TO GET ALL SELECTED"); - sb.Append("\n\t @numErrorCode INT OUTPUT,"); - sb.Append("\n\t @strErrorMsg VARCHAR(200) OUTPUT)"); - - sb.Append("\n AS"); - sb.Append("\n\t SET NOCOUNT ON"); - sb.Append("\n\t SET ANSI_NULLS OFF"); - sb.Append("\n\t SET @numErrorCode = 0"); - sb.Append("\n\t SET @strErrorMsg = 'Successful'"); - sb.Append("\n\t DECLARE @strSQL VARCHAR(4000);"); - sb.Append("\n\t DECLARE @strQry VARCHAR(4000);"); - - sb.Append("\n BEGIN"); - sb.Append("\n\t SET @strSQL=''"); - sb.Append("\n\t SET @strQry =''"); + sb.Append("@strMode VARCHAR(1), --0 for total count, 1 for single detail view, 2 for grid view".n1t1()); + sb.Append("@strSortBy VARCHAR(50),".n1t1()); + sb.Append("@strSortType VARCHAR(4),".n1t1()); + sb.Append("@startRowIndex INT,".n1t1()); + sb.Append("@maximumRows INT, -- OPTIONAL ; 0 TO GET ALL SELECTED".n1t1()); + sb.Append("@numErrorCode INT OUTPUT,".n1t1()); + sb.Append("@strErrorMsg VARCHAR(200) OUTPUT)".n1t1()); + + sb.Append("AS".n1t0()); + sb.Append("SET NOCOUNT ON".n1t1()); + sb.Append("SET ANSI_NULLS OFF".n1t1()); + sb.Append("SET @numErrorCode = 0".n1t1()); + sb.Append("SET @strErrorMsg = 'Successful'".n1t1()); + sb.Append("DECLARE @strSQL VARCHAR(4000);".n1t1()); + sb.Append("DECLARE @strQry VARCHAR(4000);".n1t1()); + + sb.Append("BEGIN".n1t0()); + sb.Append("SET @strSQL=''".n1t1()); + sb.Append("SET @strQry =''".n1t1()); foreach (DataRow row in dt.Rows) { - if (row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreatorColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifierColName()) || - row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreateDateColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifiedDateColName()) || - row["DATA_TYPE"].ToString().ToUpper().Equals("IMAGE") || row["DATA_TYPE"].ToString().ToUpper().Equals("TEXT") || - row["DATA_TYPE"].ToString().ToUpper().Equals("XML")) + var type = row["DATA_TYPE"].ToString(); + var col = row["COLUMN_NAME"].ToString(); + + if (col.Contains(_STRUID) || col.Contains(_STRLASTUID) || + col.Contains(_DTUDT) || col.Contains(_DTLASTUDT) || + type.ToUpper().Equals("IMAGE") || type.ToUpper().Equals("TEXT") || + type.ToUpper().Equals("XML")) continue; sb.AppendLine(); //for UNIQUEIDENTIFIER - if (row["DATA_TYPE"].ToString().ToUpper().Equals("UNIQUEIDENTIFIER")) + if (type.ToUpper().Equals("UNIQUEIDENTIFIER")) { - sb.Append("\n\t IF(LEN(@" + row["COLUMN_NAME"] + ")>0)"); - sb.Append("\n\t\t SET @strSQL = @strSQL+' AND T." + row["COLUMN_NAME"] + " = '''+ @" + row["COLUMN_NAME"] + " + ''''"); + sb.Append($"IF(LEN(@{col})>0)".n1t1()); + sb.Append($"SET @strSQL = @strSQL+' AND T.{col} = '''+ @{col} + ''''".n1t2()); } //INT/NUMBER/DECIMAL/DOUBLE/FLOAT - if (row["DATA_TYPE"].ToString().ToUpper().Equals("INT") - || row["DATA_TYPE"].ToString().ToUpper().Equals("NUMBER") - || row["DATA_TYPE"].ToString().ToUpper().Equals("DECIMAL") - || row["DATA_TYPE"].ToString().ToUpper().Equals("DOUBLE") - || row["DATA_TYPE"].ToString().ToUpper().Equals("FLOAT") - || row["DATA_TYPE"].ToString().ToUpper().Equals("SMALLINT") - || row["DATA_TYPE"].ToString().ToUpper().Equals("NUMERIC") - || row["DATA_TYPE"].ToString().ToUpper().Equals("MONEY") + if (type.ToUpper().Equals("INT") || type.ToUpper().Equals("SMALLINT") + || type.ToUpper().Equals("NUMBER") || type.ToUpper().Equals("DECIMAL") + || type.ToUpper().Equals("DOUBLE") || type.ToUpper().Equals("FLOAT") + || type.ToUpper().Equals("NUMERIC") || type.ToUpper().Equals("MONEY") ) { - sb.Append("\n\t IF (@" + row["COLUMN_NAME"] + ">0)"); - sb.Append("\n\t\t SET @strSQL = @strSQL+' AND T." + row["COLUMN_NAME"] + " = '+ CAST(@" + row["COLUMN_NAME"] + " AS VARCHAR(" + row["MAX_LENGTH"] + "))"); + sb.Append($"IF (@{col}>0)".n1t1()); + sb.Append($"SET @strSQL = @strSQL+' AND T.{col} = '+ CAST(@{col} AS VARCHAR(" + row["MAX_LENGTH"] + "))".n1t2()); } //CHAR/VARCHAR/VARCHAR2/STRING - if (row["DATA_TYPE"].ToString().ToUpper().Equals("VARCHAR") - || row["DATA_TYPE"].ToString().ToUpper().Equals("VARCHAR2") - || row["DATA_TYPE"].ToString().ToUpper().Equals("CHAR") - || row["DATA_TYPE"].ToString().ToUpper().Equals("STRING") - || row["DATA_TYPE"].ToString().ToUpper().Equals("NVARCHAR")) + if (type.ToUpper().Equals("VARCHAR") || type.ToUpper().Equals("VARCHAR2") + || type.ToUpper().Equals("CHAR") || type.ToUpper().Equals("STRING") || type.ToUpper().Equals("NVARCHAR")) { - sb.Append("\n\t IF(LEN(@" + row["COLUMN_NAME"] + ")>0)"); - sb.Append("\n\t\t SET @strSQL = @strSQL+' AND UPPER(T." + row["COLUMN_NAME"] + ") = '''+ UPPER(@" + row["COLUMN_NAME"] + ") + ''''"); - //sb.Append("\n\t\t strSql := strSql || ' AND UPPER(T." + row["COLUMN_NAME"] + ") LIKE ''%' || REPLACE(UPPER(p" + row["COLUMN_NAME"] + "),'''','''''') || '%''';"); + sb.Append($"IF(LEN(@{col})>0)".n1t1()); + sb.Append($"SET @strSQL = @strSQL+' AND UPPER(T.{col}) = '''+ UPPER(@{col}) + ''''".n1t2()); + //sb.Append($"strSql := strSql || ' AND UPPER(T.{col}) LIKE ''%' || REPLACE(UPPER(p{col}),'''','''''') || '%''';".n1t2()); } //DateTime - if (row["DATA_TYPE"].ToString().ToUpper().Equals("DATETIME")) + if (type.ToUpper().Equals("DATETIME")) { - sb.Append("\n\t IF(LEN(@" + row["COLUMN_NAME"] + "From)>0 AND LEN(@" + row["COLUMN_NAME"] + "To)>0)"); - sb.Append("\n\t\t SET @strSQL = @strSQL+' AND T." + row["COLUMN_NAME"] + " BETWEEN '''+ @" + row["COLUMN_NAME"] + "From + ''' AND ''' + @" + row["COLUMN_NAME"] + "To + ''''"); - //sb.Append("\n\t ELSIF p" + row["COLUMN_NAME"] + "From IS NOT NULL AND p" + row["COLUMN_NAME"] + "To IS NULL THEN"); - //sb.Append("\n\t\t strSql := strSql || ' AND T." + row["COLUMN_NAME"] + " >= '''|| p" + row["COLUMN_NAME"] + "From|| '''' ;"); - //sb.Append("\n\t ELSIF p" + row["COLUMN_NAME"] + "From IS NULL AND p" + row["COLUMN_NAME"] + "To IS NOT NULL THEN"); - //sb.Append("\n\t\t strSql := strSql || ' AND T." + row["COLUMN_NAME"] + " <= ''' ||p" + row["COLUMN_NAME"] + "To|| '''';"); + sb.Append($"IF(LEN(@{col}From)>0 AND LEN(@{col}To)>0)".n1t1()); + sb.Append($"SET @strSQL = @strSQL+' AND T.{col} BETWEEN '''+ @{col}From + ''' AND ''' + @{col}To + ''''".n1t2()); + //sb.Append($"ELSIF p{col}From IS NOT NULL AND p{col}To IS NULL THEN".n1t1()); + //sb.Append($"strSql := strSql || ' AND T.{col} >= '''|| p{col}From|| '''' ;".n1t2()); + //sb.Append($"ELSIF p{col}From IS NULL AND p{col}To IS NOT NULL THEN".n1t1()); + //sb.Append($"strSql := strSql || ' AND T.{col} <= ''' ||p{col}To|| '''';".n1t2()); } } - sb.Append("\n\n\t -- Paging and Sorting Parameters"); - sb.Append("\n\t IF(LEN(@strSortBy)=0) set @strSortBy = '" + Util.Utility.GetPkColName() + "'"); - sb.Append("\n\t IF(LEN(@strSortType)=0) set @strSortType = 'DESC'"); + sb.Append("-- Paging and Sorting Parameters".n1t0().n1t1()); + sb.Append($"IF(LEN(@strSortBy)=0) set @strSortBy = '" + _NUMID + "'".n1t1()); + sb.Append("IF(LEN(@strSortType)=0) set @strSortType = 'DESC'".n1t1()); sb.AppendLine(); //RowCount - sb.Append("\n\t IF (@strMode = '0') -- FOR ROW COUNT "); - sb.Append("\n\t BEGIN"); - sb.Append("\n\t\t SET @strQry ='SELECT COUNT(1) TOTALROWS FROM " + strTableName + " T "); - sb.Append("\n\t\t WHERE 0 = 0 ' + @strSQL"); - sb.Append("\n\t END"); + sb.Append("IF (@strMode = '0') -- FOR ROW COUNT ".n1t1()); + sb.Append("BEGIN".n1t1()); + sb.Append($"SET @strQry ='SELECT COUNT(1) TOTALROWS FROM {strTableName} T ".n1t2()); + sb.Append("WHERE 0 = 0 ' + @strSQL".n1t2()); + sb.Append("END".n1t1()); //SelectAll - sb.Append("\n\t IF (@strMode = '1') -- GRID VIEW "); - sb.Append("\n\t BEGIN"); - sb.Append("\n\t\t SET @strQry = 'SELECT * FROM " + strTableName + " T WHERE 0=0 '+ @strSQL"); - sb.Append("\n\t END"); - sb.Append("\n\t IF (@strMode = '2') -- Join with Other Related Table"); - sb.Append("\n\t BEGIN"); - sb.Append("\n\t\t SET @strQry = 'WITH T AS (SELECT ROW_NUMBER() OVER (ORDER BY '"); - sb.Append("\n\t\t + @strSortBy + ' ' + @strSortType + ' ) AS ROWID, * FROM ( SELECT "); + sb.Append("IF (@strMode = '1') -- GRID VIEW ".n1t1()); + sb.Append("BEGIN".n1t1()); + sb.Append($"SET @strQry = 'SELECT * FROM {strTableName} T WHERE 0=0 '+ @strSQL".n1t2()); + sb.Append("END".n1t1()); + sb.Append("IF (@strMode = '2') -- Join with Other Related Table".n1t1()); + sb.Append("BEGIN".n1t1()); + sb.Append("SET @strQry = 'WITH T AS (SELECT ROW_NUMBER() OVER (ORDER BY '".n1t2()); + sb.Append("+ @strSortBy + ' ' + @strSortType + ' ) AS ROWID, * FROM ( SELECT ".n1t2()); foreach (DataRow row in dt.Rows) { - if (row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreatorColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifierColName()) || - row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreateDateColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifiedDateColName())) + var type = row["DATA_TYPE"].ToString(); + var col = row["COLUMN_NAME"].ToString(); + + if (col.Contains(_STRUID) || col.Contains(_STRLASTUID) || + col.Contains(_DTUDT) || col.Contains(_DTLASTUDT)) continue; - sb.Append("\n\t\t\t " + row["COLUMN_NAME"] + ","); + sb.Append($"{col},".n1t3()); } sb.Remove(sb.Length -1, 1); //To Remove last Comma - sb.Append("\n\t\t FROM " + strTableName + " T WHERE 0=0 '"); - sb.Append("\n\t\t + @strSQL + ') AS T) SELECT T.* FROM T WHERE 1=1 '"); + sb.Append($"FROM {strTableName} T WHERE 0=0 '".n1t2()); + sb.Append("+ @strSQL + ') AS T) SELECT T.* FROM T WHERE 1=1 '".n1t2()); - sb.Append("\n\t\t\t IF(@maximumRows > 0)"); - sb.Append("\n\t\t\t SET @strQry = @strQry + ' AND ROWID BETWEEN ' "); - sb.Append("\n\t\t\t + CAST(@startRowIndex as varchar(20)) + ' and ' "); - sb.Append("\n\t\t\t + CAST(@startRowIndex + @maximumRows -1 as varchar(20)) "); - sb.Append("\n\t END"); + sb.Append("IF(@maximumRows > 0)".n1t3()); + sb.Append("SET @strQry = @strQry + ' AND ROWID BETWEEN ' ".n1t3()); + sb.Append("+ CAST(@startRowIndex as varchar(20)) + ' and ' ".n1t3()); + sb.Append("+ CAST(@startRowIndex + @maximumRows -1 as varchar(20)) ".n1t3()); + sb.Append("END".n1t1()); - //sb.Append("\n\t END"); - sb.Append("\n\t print(@strQry)"); - sb.Append("\n\t exec (@strQry)"); - sb.Append("\n END"); + //sb.Append("END".n1t1()); + sb.Append("print(@strQry)".n1t1()); + sb.Append("exec (@strQry)".n1t1()); + sb.Append("END".n1t0()); - sb.Append("\n\n\t IF @@error <> 0 GOTO procError"); - sb.Append("\n\t\t GOTO procEnd"); + sb.Append("IF @@error <> 0 GOTO procError".n1t0().n1t1()); + sb.Append("GOTO procEnd".n1t2()); - sb.Append("\n\n procError:"); - sb.Append("\n\t SET @numErrorCode = @@error"); - sb.Append("\n\t SELECT @strErrorMsg = [description] FROM master.dbo.sysmessages WHERE error = @numErrorCode"); - sb.Append("\n\t INSERT INTO error_log (LogDate,Source,ErrMsg) VALUES (getDate(),'" + strProcName + "',@strErrorMsg)"); + sb.Append("procError:".n1t0().n1t0()); + sb.Append("SET @numErrorCode = @@error".n1t1()); + sb.Append("SELECT @strErrorMsg = [description] FROM master.dbo.sysmessages WHERE error = @numErrorCode".n1t1()); + sb.Append("INSERT INTO error_log (LogDate,Source,ErrMsg) VALUES (getDate(),'" + strProcName + "',@strErrorMsg)".n1t1()); - sb.Append("\n procEnd:"); + sb.Append("procEnd:".n1t0()); //MessageBox.Show(sb.ToString()); return sb; @@ -302,164 +327,169 @@ public StringBuilder BuildGetProcedure(string strTableName, DataTable dt) public StringBuilder BuildGetProcedureSingle(string strTableName, DataTable dt) { StringBuilder sb = new StringBuilder(); - string strProcName = "usp" + strTableName.Replace("T_", "").Replace("t_", "").Replace("tbl", "") + "Get"; + string strProcName = $"usp{strTableName.Replace("T_", "").Replace("t_", "").Replace("tbl", "")}Get"; - sb.Append("CREATE PROCEDURE " + strProcName + "("); + sb.Append($"CREATE PROCEDURE {strProcName}("); foreach (DataRow row in dt.Rows) { - if (row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreatorColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifierColName()) || - row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreateDateColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifiedDateColName()) || - row["DATA_TYPE"].ToString().ToUpper().Equals("IMAGE") || row["DATA_TYPE"].ToString().ToUpper().Equals("TEXT") || - row["DATA_TYPE"].ToString().ToUpper().Equals("XML")) + var type = row["DATA_TYPE"].ToString(); + var col = row["COLUMN_NAME"].ToString(); + + if (col.Contains(_STRUID) || col.Contains(_STRLASTUID) || + col.Contains(_DTUDT) || col.Contains(_DTLASTUDT) || + type.ToUpper().Equals("IMAGE") || type.ToUpper().Equals("TEXT") || + type.ToUpper().Equals("XML")) continue; sb.AppendLine(); //for UNIQUEIDENTIFIER - if (row["DATA_TYPE"].ToString().ToUpper().Equals("UNIQUEIDENTIFIER")) + if (type.ToUpper().Equals("UNIQUEIDENTIFIER")) { - sb.Append("\t @" + row["COLUMN_NAME"] + "\t" + "VARCHAR(50)" + ","); + sb.Append($"@{col}\tVARCHAR(50),".n0t1()); continue; } - if (row["DATA_TYPE"].ToString().ToUpper().Equals("DATETIME")) + if (type.ToUpper().Equals("DATETIME")) { - sb.Append("\t @" + row["COLUMN_NAME"] + "From\t" + row["DATA_TYPE"].ToString().ToUpper() + ","); + sb.Append($"@{col}From\t{type.ToUpper()},".n0t1()); sb.AppendLine(); - sb.Append("\t @" + row["COLUMN_NAME"] + "To\t" + row["DATA_TYPE"].ToString().ToUpper() + ","); + sb.Append($"@{col}To\t{type.ToUpper()},".n0t1()); continue; } - string strLength = row["DATA_TYPE"].ToString().ToUpper().Equals("VARCHAR") || row["DATA_TYPE"].ToString().ToUpper().Equals("CHAR") ? " (" + row["MAX_LENGTH"].ToString() + ")" : ""; - sb.Append("\t @" + row["COLUMN_NAME"] + "\t" + row["DATA_TYPE"].ToString().ToUpper() + strLength + ","); + string strLength = type.ToUpper().Equals("VARCHAR") || type.ToUpper().Equals("CHAR") ? " (" + row["MAX_LENGTH"].ToString() + ")" : ""; + sb.Append($"@{col}\t{type.ToUpper() + strLength},".n0t1()); } - sb.Append("\n\t @SortBy VARCHAR(50),"); - sb.Append("\n\t @SortOrder VARCHAR(4),"); - sb.Append("\n\t @startRowIndex INT,"); - sb.Append("\n\t @maximumRows INT, -- OPTIONAL ; 0 TO GET ALL SELECTED"); - sb.Append("\n\t @numTotalRows INT OUTPUT,"); - sb.Append("\n\t @numErrorCode INT OUTPUT,"); - sb.Append("\n\t @strErrorMsg VARCHAR(200) OUTPUT)"); - - sb.Append("\n AS"); - sb.Append("\n\t SET NOCOUNT ON"); - sb.Append("\n\t SET ANSI_NULLS OFF"); - - sb.Append("\n\t DECLARE @strSQL VARCHAR(4000);"); - sb.Append("\n\t DECLARE @strQry VARCHAR(4000);"); - sb.Append("\n\t DECLARE @sqlTotal NVARCHAR(4000);"); - sb.Append("\n\t DECLARE @ParmDefinition NVARCHAR(1000);"); - sb.Append("\n\t DECLARE @strEditCondition VARCHAR(4000);"); - - sb.Append("\n\t SET @numErrorCode = 0"); - sb.Append("\n\t SET @strErrorMsg = 'Successful'"); - - sb.Append("\n BEGIN"); - sb.Append("\n\t SET @strSQL=''"); - sb.Append("\n\t SET @strQry =''"); + sb.Append("@SortBy VARCHAR(50),".n1t1()); + sb.Append("@SortOrder VARCHAR(4),".n1t1()); + sb.Append("@startRowIndex INT,".n1t1()); + sb.Append("@maximumRows INT, -- OPTIONAL ; 0 TO GET ALL SELECTED".n1t1()); + sb.Append("@numTotalRows INT OUTPUT,".n1t1()); + sb.Append("@numErrorCode INT OUTPUT,".n1t1()); + sb.Append("@strErrorMsg VARCHAR(200) OUTPUT)".n1t1()); + + sb.Append("AS".n1t0()); + sb.Append("SET NOCOUNT ON".n1t1()); + sb.Append("SET ANSI_NULLS OFF".n1t1()); + + sb.Append("DECLARE @strSQL VARCHAR(4000);".n1t1()); + sb.Append("DECLARE @strQry VARCHAR(4000);".n1t1()); + sb.Append("DECLARE @sqlTotal NVARCHAR(4000);".n1t1()); + sb.Append("DECLARE @ParmDefinition NVARCHAR(1000);".n1t1()); + sb.Append("DECLARE @strEditCondition VARCHAR(4000);".n1t1()); + + sb.Append("SET @numErrorCode = 0".n1t1()); + sb.Append("SET @strErrorMsg = 'Successful'".n1t1()); + + sb.Append("BEGIN".n1t0()); + sb.Append("SET @strSQL=''".n1t1()); + sb.Append("SET @strQry =''".n1t1()); //Table Name - sb.Append("\n\n\t SET @strSQL = ' FROM " + strTableName + " T WHERE 0=0 '"); + sb.Append($"SET @strSQL = ' FROM {strTableName} T WHERE 0=0 '".n1t0().n1t1()); //Search Params foreach (DataRow row in dt.Rows) { - if (row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreatorColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifierColName()) || - row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreateDateColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifiedDateColName()) || - row["DATA_TYPE"].ToString().ToUpper().Equals("IMAGE") || row["DATA_TYPE"].ToString().ToUpper().Equals("TEXT") || - row["DATA_TYPE"].ToString().ToUpper().Equals("XML")) + var type = row["DATA_TYPE"].ToString(); + var col = row["COLUMN_NAME"].ToString(); + + if (col.Contains(_STRUID) || col.Contains(_STRLASTUID) || + col.Contains(_DTUDT) || col.Contains(_DTLASTUDT) || + type.ToUpper().Equals("IMAGE") || type.ToUpper().Equals("TEXT") || + type.ToUpper().Equals("XML")) continue; sb.AppendLine(); //for UNIQUEIDENTIFIER - if (row["DATA_TYPE"].ToString().ToUpper().Equals("UNIQUEIDENTIFIER")) + if (type.ToUpper().Equals("UNIQUEIDENTIFIER")) { - sb.Append("\n\t IF(LEN(@" + row["COLUMN_NAME"] + ")>0)"); - sb.Append("\n\t\t SET @strSQL = @strSQL+' AND T." + row["COLUMN_NAME"] + " = '''+ @" + row["COLUMN_NAME"] + " + ''''"); + sb.Append($"IF(LEN(@{col})>0)".n1t1()); + sb.Append($"SET @strSQL = @strSQL+' AND T.{col} = '''+ @{col} + ''''".n1t2()); } //INT/NUMBER/DECIMAL/DOUBLE/FLOAT - if (row["DATA_TYPE"].ToString().ToUpper().Equals("INT") - || row["DATA_TYPE"].ToString().ToUpper().Equals("NUMBER") - || row["DATA_TYPE"].ToString().ToUpper().Equals("DECIMAL") - || row["DATA_TYPE"].ToString().ToUpper().Equals("NUMERIC") - || row["DATA_TYPE"].ToString().ToUpper().Equals("DOUBLE") - || row["DATA_TYPE"].ToString().ToUpper().Equals("FLOAT") - || row["DATA_TYPE"].ToString().ToUpper().Equals("SMALLINT") - || row["DATA_TYPE"].ToString().ToUpper().Equals("MONEY")) + if (type.ToUpper().Equals("INT") || type.ToUpper().Equals("SMALLINT") + || type.ToUpper().Equals("NUMBER") || type.ToUpper().Equals("NUMERIC") + || type.ToUpper().Equals("DECIMAL") || type.ToUpper().Equals("DOUBLE") + || type.ToUpper().Equals("FLOAT") || type.ToUpper().Equals("MONEY")) { - sb.Append("\n\t IF (@" + row["COLUMN_NAME"] + ">0)"); - sb.Append("\n\t\t SET @strSQL = @strSQL+' AND T." + row["COLUMN_NAME"] + " = '+ CAST(@" + row["COLUMN_NAME"] + " AS VARCHAR(" + row["MAX_LENGTH"] + "))"); + sb.Append($"IF (@{col}>0)".n1t1()); + sb.Append($"SET @strSQL = @strSQL+' AND T.{col} = '+ CAST(@{col} AS VARCHAR(" + row["MAX_LENGTH"] + "))".n1t2()); } //CHAR/VARCHAR/VARCHAR2/STRING - if (row["DATA_TYPE"].ToString().ToUpper().Equals("VARCHAR") - || row["DATA_TYPE"].ToString().ToUpper().Equals("VARCHAR2") - || row["DATA_TYPE"].ToString().ToUpper().Equals("CHAR") - || row["DATA_TYPE"].ToString().ToUpper().Equals("STRING") - || row["DATA_TYPE"].ToString().ToUpper().Equals("NVARCHAR")) + if (type.ToUpper().Equals("VARCHAR") + || type.ToUpper().Equals("VARCHAR2") + || type.ToUpper().Equals("CHAR") + || type.ToUpper().Equals("STRING") + || type.ToUpper().Equals("NVARCHAR")) { - sb.Append("\n\t IF(LEN(@" + row["COLUMN_NAME"] + ")>0)"); - sb.Append("\n\t\t SET @strSQL = @strSQL+' AND UPPER(T." + row["COLUMN_NAME"] + ") = '''+ UPPER(@" + row["COLUMN_NAME"] + ") + ''''"); - //sb.Append("\n\t\t strSql := strSql || ' AND UPPER(T." + row["COLUMN_NAME"] + ") LIKE ''%' || REPLACE(UPPER(p" + row["COLUMN_NAME"] + "),'''','''''') || '%''';"); + sb.Append($"IF(LEN(@{col})>0)".n1t1()); + sb.Append($"SET @strSQL = @strSQL+' AND UPPER(T.{col}) = '''+ UPPER(@{col}) + ''''".n1t1()); + //sb.Append($"\n\t\t strSql := strSql || ' AND UPPER(T.{col}) LIKE ''%' || REPLACE(UPPER(p{col}),'''','''''') || '%''';"); } //DateTime - if (row["DATA_TYPE"].ToString().ToUpper().Equals("DATETIME")) + if (type.ToUpper().Equals("DATETIME")) { - sb.Append("\n\t IF(LEN(@" + row["COLUMN_NAME"] + "From)>0 AND LEN(@" + row["COLUMN_NAME"] + "To)>0)"); - sb.Append("\n\t\t SET @strSQL = @strSQL+' AND T." + row["COLUMN_NAME"] + " BETWEEN '''+ @" + row["COLUMN_NAME"] + "From + ''' AND ''' + @" + row["COLUMN_NAME"] + "To + ''''"); - //sb.Append("\n\t ELSIF p" + row["COLUMN_NAME"] + "From IS NOT NULL AND p" + row["COLUMN_NAME"] + "To IS NULL THEN"); - //sb.Append("\n\t\t strSql := strSql || ' AND T." + row["COLUMN_NAME"] + " >= '''|| p" + row["COLUMN_NAME"] + "From|| '''' ;"); - //sb.Append("\n\t ELSIF p" + row["COLUMN_NAME"] + "From IS NULL AND p" + row["COLUMN_NAME"] + "To IS NOT NULL THEN"); - //sb.Append("\n\t\t strSql := strSql || ' AND T." + row["COLUMN_NAME"] + " <= ''' ||p" + row["COLUMN_NAME"] + "To|| '''';"); + sb.Append($"IF(LEN(@{col}From)>0 AND LEN(@{col}To)>0)".n1t1()); + sb.Append($"SET @strSQL = @strSQL+' AND T.{col} BETWEEN '''+ @{col}From + ''' AND ''' + @{col}To + ''''".n1t2()); + //sb.Append($"\n\t ELSIF p{col}From IS NOT NULL AND p{col}To IS NULL THEN"); + //sb.Append($"\n\t\t strSql := strSql || ' AND T.{col} >= '''|| p{col}From|| '''' ;"); + //sb.Append($"\n\t ELSIF p{col}From IS NULL AND p{col}To IS NOT NULL THEN"); + //sb.Append($"\n\t\t strSql := strSql || ' AND T.{col} <= ''' ||p{col}To|| '''';"); } } - sb.Append("\n\n\t SET @ParmDefinition = '@numTotalRowsOUT numeric OUTPUT';"); - sb.Append("\n\t SET @sqlTotal = 'SELECT @numTotalRowsOUT=count(*) ' + @strSQL"); - sb.Append("\n\t EXECUTE sp_executesql @sqlTotal, @ParmDefinition, @numTotalRowsOUT = @numTotalRows OUTPUT"); + sb.Append("SET @ParmDefinition = '@numTotalRowsOUT numeric OUTPUT';".n1t0().n1t1()); + sb.Append("SET @sqlTotal = 'SELECT @numTotalRowsOUT=count(*) ' + @strSQL".n1t1()); + sb.Append("EXECUTE sp_executesql @sqlTotal, @ParmDefinition, @numTotalRowsOUT = @numTotalRows OUTPUT".n1t1()); - sb.Append("\n\n\t -- Paging and Sorting Parameters"); - sb.Append("\n\t IF(LEN(@SortBy)=0) set @SortBy = '" + Util.Utility.GetPkColName() + "'"); - sb.Append("\n\t IF(LEN(@SortOrder)=0) set @SortOrder = 'DESC'"); + sb.Append("-- Paging and Sorting Parameters".n1t0().n1t1()); + sb.Append("IF(LEN(@SortBy)=0) set @SortBy = '" + _NUMID + "'".n1t1()); + sb.Append("IF(LEN(@SortOrder)=0) set @SortOrder = 'DESC'".n1t1()); sb.AppendLine(); //Select Statement - sb.Append("\n\t\t SET @strQry = 'WITH T AS (SELECT ROW_NUMBER() OVER (ORDER BY '"); - sb.Append("\n\t\t + @SortBy + ' ' + @SortOrder + ' ) AS ROWID, * FROM ( SELECT "); + sb.Append("SET @strQry = 'WITH T AS (SELECT ROW_NUMBER() OVER (ORDER BY '".n1t2()); + sb.Append("+ @SortBy + ' ' + @SortOrder + ' ) AS ROWID, * FROM ( SELECT ".n1t2()); foreach (DataRow row in dt.Rows) { - if (row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreatorColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifierColName()) || - row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreateDateColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifiedDateColName())) + var type = row["DATA_TYPE"].ToString(); + var col = row["COLUMN_NAME"].ToString(); + + if (col.Contains(_STRUID) || col.Contains(_STRLASTUID) || + col.Contains(_DTUDT) || col.Contains(_DTLASTUDT)) continue; - sb.Append("\n\t\t\t " + row["COLUMN_NAME"] + ","); + sb.Append($"{col},".n1t3()); } sb.Remove(sb.Length - 1, 1); //To Remove last Comma sb.Append(" '"); - sb.Append("\n\t\t + @strSQL + ') AS T) SELECT T.* FROM T WHERE 1=1 '"); + sb.Append(" + @strSQL + ') AS T) SELECT T.* FROM T WHERE 1=1 '".n1t2()); - sb.Append("\n\t\t IF(@maximumRows > 0)"); - sb.Append("\n\t\t\t SET @strQry = @strQry + ' AND ROWID BETWEEN ' "); - sb.Append("\n\t\t\t + CAST(@startRowIndex as varchar(20)) + ' and ' "); - sb.Append("\n\t\t\t + CAST(@startRowIndex + @maximumRows -1 as varchar(20)) "); + sb.Append(" IF(@maximumRows > 0)".n1t2()); + sb.Append("SET @strQry = @strQry + ' AND ROWID BETWEEN ' ".n1t3()); + sb.Append("+ CAST(@startRowIndex as varchar(20)) + ' and ' ".n1t3()); + sb.Append("+ CAST(@startRowIndex + @maximumRows -1 as varchar(20)) ".n1t3()); - sb.Append("\n\t PRINT(@strQry)"); - sb.Append("\n\t EXEC (@strQry)"); - sb.Append("\n END"); + sb.Append("PRINT(@strQry)".n1t1()); + sb.Append("EXEC (@strQry)".n1t1()); + sb.Append("END".n1t0()); - sb.Append("\n\n\t IF @@error <> 0 GOTO procError"); - sb.Append("\n\t\t GOTO procEnd"); + sb.Append("IF @@error <> 0 GOTO procError".n1t0().n1t1()); + sb.Append("GOTO procEnd".n1t2()); - sb.Append("\n\n\t procError:"); - sb.Append("\n\t\t SET @numErrorCode = @@error"); - sb.Append("\n\t\t SELECT @strErrorMsg = [description] FROM master.dbo.sysmessages WHERE error = @numErrorCode"); - sb.Append("\n\t\t INSERT INTO error_log (LogDate,Source,ErrMsg) VALUES (getDate(),'" + strProcName + "',@strErrorMsg)"); + sb.Append("procError:".n1t0().n1t1()); + sb.Append("SET @numErrorCode = @@error".n1t2()); + sb.Append("SELECT @strErrorMsg = [description] FROM master.dbo.sysmessages WHERE error = @numErrorCode".n1t2()); + sb.Append("INSERT INTO error_log (LogDate,Source,ErrMsg) VALUES (getDate(),'" + strProcName + "',@strErrorMsg)".n1t2()); - sb.Append("\n procEnd:"); + sb.Append("procEnd:".n1t0()); //MessageBox.Show(sb.ToString()); return sb; @@ -468,169 +498,171 @@ public StringBuilder BuildGetProcedureSingle(string strTableName, DataTable dt) public StringBuilder BuildGetProcedureParameterized(string strTableName, DataTable dt) { StringBuilder sb = new StringBuilder(); - string strProcName = "usp" + strTableName.Replace("T_", "").Replace("t_", "").Replace("tbl", "") + "Get"; + string strProcName = $"usp{strTableName.Replace("T_", "").Replace("t_", "").Replace("tbl", "")}Get"; sb.Append("CREATE PROCEDURE " + strProcName + "("); foreach (DataRow row in dt.Rows) { - if (row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreatorColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifierColName()) || - row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreateDateColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifiedDateColName()) || - row["DATA_TYPE"].ToString().ToUpper().Equals("IMAGE") || row["DATA_TYPE"].ToString().ToUpper().Equals("TEXT") || - row["DATA_TYPE"].ToString().ToUpper().Equals("XML")) + var type = row["DATA_TYPE"].ToString(); + var col = row["COLUMN_NAME"].ToString(); + + if (col.Contains(_STRUID) || col.Contains(_STRLASTUID) || + col.Contains(_DTUDT) || col.Contains(_DTLASTUDT) || + type.ToUpper().Equals("IMAGE") || type.ToUpper().Equals("TEXT") || + type.ToUpper().Equals("XML")) continue; sb.AppendLine(); //for UNIQUEIDENTIFIER - if (row["DATA_TYPE"].ToString().ToUpper().Equals("UNIQUEIDENTIFIER")) + if (type.ToUpper().Equals("UNIQUEIDENTIFIER")) { - sb.Append("\t @" + row["COLUMN_NAME"] + "\t" + "VARCHAR(50)" + ","); + sb.Append($"@{col}\t" + "VARCHAR(50)" + ",".n0t1()); continue; } - if (row["DATA_TYPE"].ToString().ToUpper().Equals("DATETIME")) + if (type.ToUpper().Equals("DATETIME")) { - sb.Append("\t @" + row["COLUMN_NAME"] + "From\t" + "VARCHAR(20)" + ","); + sb.Append($"@{col}From\t" + "VARCHAR(20)" + ",".n0t1()); sb.AppendLine(); - sb.Append("\t @" + row["COLUMN_NAME"] + "To\t" + "VARCHAR(20)" + ","); + sb.Append($"@{col}To\t" + "VARCHAR(20)" + ",".n0t1()); continue; } - string strLength = row["DATA_TYPE"].ToString().ToUpper().Equals("VARCHAR") || row["DATA_TYPE"].ToString().ToUpper().Equals("CHAR") ? " (" + row["MAX_LENGTH"].ToString() + ")" : ""; - sb.Append("\t @" + row["COLUMN_NAME"] + "\t" + row["DATA_TYPE"].ToString().ToUpper() + strLength + ","); + string strLength = type.ToUpper().Equals("VARCHAR") || type.ToUpper().Equals("CHAR") ? " (" + row["MAX_LENGTH"].ToString() + ")" : ""; + sb.Append($"@{col}\t" + type.ToUpper() + strLength + ",".n0t1()); } //sb.Remove(sb.Length -1, 1); //To Remove last Comma - sb.Append("\n\t @strMode VARCHAR(1), --0 for total count, 1 for single detail view, 2 for grid view"); - sb.Append("\n\t @strSortBy VARCHAR(50),"); - sb.Append("\n\t @strSortType VARCHAR(4),"); - sb.Append("\n\t @startRowIndex INT,"); - sb.Append("\n\t @maximumRows INT, -- OPTIONAL ; 0 TO GET ALL SELECTED"); - sb.Append("\n\t @numErrorCode INT OUTPUT,"); - sb.Append("\n\t @strErrorMsg VARCHAR(200) OUTPUT)"); - - sb.Append("\n AS"); - sb.Append("\n\t SET NOCOUNT ON"); - sb.Append("\n\t SET ANSI_NULLS OFF"); - sb.Append("\n\t SET @numErrorCode = 0"); - sb.Append("\n\t SET @strErrorMsg = 'Successful'"); - - sb.Append("\n BEGIN"); + sb.Append("@strMode VARCHAR(1), --0 for total count, 1 for single detail view, 2 for grid view".n1t1()); + sb.Append("@strSortBy VARCHAR(50),".n1t1()); + sb.Append("@strSortType VARCHAR(4),".n1t1()); + sb.Append("@startRowIndex INT,".n1t1()); + sb.Append("@maximumRows INT, -- OPTIONAL ; 0 TO GET ALL SELECTED".n1t1()); + sb.Append("@numErrorCode INT OUTPUT,".n1t1()); + sb.Append("@strErrorMsg VARCHAR(200) OUTPUT)".n1t1()); + + sb.Append("AS".n1t0()); + sb.Append("SET NOCOUNT ON".n1t1()); + sb.Append("SET ANSI_NULLS OFF".n1t1()); + sb.Append("SET @numErrorCode = 0".n1t1()); + sb.Append("SET @strErrorMsg = 'Successful'".n1t1()); + + sb.Append("BEGIN".n1t0()); sb.AppendLine(); StringBuilder sbSearchParams = new StringBuilder(); StringBuilder sbSortParams = new StringBuilder(); foreach (DataRow row in dt.Rows) { - if (row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreatorColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifierColName()) || - row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreateDateColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifiedDateColName()) || - row["DATA_TYPE"].ToString().ToUpper().Equals("IMAGE") || row["DATA_TYPE"].ToString().ToUpper().Equals("TEXT") || - row["DATA_TYPE"].ToString().ToUpper().Equals("XML")) + var type = row["DATA_TYPE"].ToString(); + var col = row["COLUMN_NAME"].ToString(); + + if (col.Contains(_STRUID) || col.Contains(_STRLASTUID) || + col.Contains(_DTUDT) || col.Contains(_DTLASTUDT) || + type.ToUpper().Equals("IMAGE") || type.ToUpper().Equals("TEXT") || + type.ToUpper().Equals("XML")) continue; //for UNIQUEIDENTIFIER - if (row["DATA_TYPE"].ToString().ToUpper().Equals("UNIQUEIDENTIFIER")) + if (type.ToUpper().Equals("UNIQUEIDENTIFIER")) { - sb.Append("\n\t IF(LEN(@" + row["COLUMN_NAME"] + ") <> 36)"); - sb.Append("\t SET @" + row["COLUMN_NAME"] + " = NULL;"); + sb.Append($"IF(LEN(@{col}) <> 36)".n1t1()); + sb.Append($"SET @{col} = NULL;".n0t1()); - sbSearchParams.Append("\n\t\t AND ( T." + row["COLUMN_NAME"] + " = @" + row["COLUMN_NAME"] + " OR @" + row["COLUMN_NAME"] + " IS NULL )"); + sbSearchParams.Append($"AND ( T.{col} = @{col} OR @{col} IS NULL )".n1t2()); } //INT/NUMBER/DECIMAL/DOUBLE/FLOAT - if (row["DATA_TYPE"].ToString().ToUpper().Equals("INT") - || row["DATA_TYPE"].ToString().ToUpper().Equals("NUMBER") - || row["DATA_TYPE"].ToString().ToUpper().Equals("DECIMAL") - || row["DATA_TYPE"].ToString().ToUpper().Equals("DOUBLE") - || row["DATA_TYPE"].ToString().ToUpper().Equals("FLOAT") - || row["DATA_TYPE"].ToString().ToUpper().Equals("SMALLINT") - || row["DATA_TYPE"].ToString().ToUpper().Equals("NUMERIC") - || row["DATA_TYPE"].ToString().ToUpper().Equals("MONEY") + if (type.ToUpper().Equals("INT") || type.ToUpper().Equals("SMALLINT") + || type.ToUpper().Equals("NUMBER") || type.ToUpper().Equals("DECIMAL") + || type.ToUpper().Equals("DOUBLE") || type.ToUpper().Equals("FLOAT") + || type.ToUpper().Equals("NUMERIC") || type.ToUpper().Equals("MONEY") ) { - sbSearchParams.Append("\n\t\t AND ( T." + row["COLUMN_NAME"] + " = @" + row["COLUMN_NAME"] + " OR @" + row["COLUMN_NAME"] + " <= 0 )"); + sbSearchParams.Append($"AND ( T.{col} = @{col} OR @{col} <= 0 )".n1t2()); } //CHAR/VARCHAR/VARCHAR2/STRING - if (row["DATA_TYPE"].ToString().ToUpper().Equals("VARCHAR") - || row["DATA_TYPE"].ToString().ToUpper().Equals("VARCHAR2") - || row["DATA_TYPE"].ToString().ToUpper().Equals("CHAR") - || row["DATA_TYPE"].ToString().ToUpper().Equals("STRING") - || row["DATA_TYPE"].ToString().ToUpper().Equals("NVARCHAR")) + if (type.ToUpper().Equals("VARCHAR") || type.ToUpper().Equals("VARCHAR2") + || type.ToUpper().Equals("CHAR") || type.ToUpper().Equals("STRING") || type.ToUpper().Equals("NVARCHAR")) { - sb.Append("\n\t IF(LEN(@" + row["COLUMN_NAME"] + ")=0)"); - sb.Append("\t SET @" + row["COLUMN_NAME"] + " = NULL;"); + sb.Append($"\n\t IF(LEN(@{col})=0)".n1t1()); + sb.Append($"\t SET @{col} = NULL;".n0t1()); - sbSearchParams.Append("\n\t\t AND ( T." + row["COLUMN_NAME"] + " = @" + row["COLUMN_NAME"] + " OR @" + row["COLUMN_NAME"] + " IS NULL )"); + sbSearchParams.Append($"\n\t\t AND ( T.{col} = @{col} OR @{col} IS NULL )".n1t2()); } //DateTime - if (row["DATA_TYPE"].ToString().ToUpper().Equals("DATETIME")) + if (type.ToUpper().Equals("DATETIME")) { - sb.Append("\n\t IF(LEN(@" + row["COLUMN_NAME"] + "From)=0)"); - sb.Append("\t SET @" + row["COLUMN_NAME"] + "From = NULL;"); + sb.Append($"\n\t IF(LEN(@{col}From)=0)".n1t1()); + sb.Append($"\t SET @{col}From = NULL;".n0t1()); - sb.Append("\n\t IF(LEN(@" + row["COLUMN_NAME"] + "To)=0)"); - sb.Append("\t SET @" + row["COLUMN_NAME"] + "To = NULL;"); + sb.Append($"\n\t IF(LEN(@{col}To)=0)".n1t1()); + sb.Append($"\t SET @{col} To = NULL;".n0t1()); - //sbConditions.Append("\n\t\t AND T." + row["COLUMN_NAME"] + " BETWEEN '''+ @" + row["COLUMN_NAME"] + "From + ''' AND ''' + @" + row["COLUMN_NAME"] + "To + ''''"); - sbSearchParams.Append("\n\t\t AND (T." + row["COLUMN_NAME"] + " >= + @" + row["COLUMN_NAME"] + "From + OR + @" + row["COLUMN_NAME"] + "From + IS NULL)"); - sbSearchParams.Append("\n\t\t AND (T." + row["COLUMN_NAME"] + " <= + @" + row["COLUMN_NAME"] + "To + OR + @" + row["COLUMN_NAME"] + "To + IS NULL)"); + //sbConditions.Append("\n\t\t AND T." + col + " BETWEEN '''+ @" + col + "From + ''' AND ''' + @" + col + "To + ''''"); + sbSearchParams.Append($"AND (T.{col} >= + @{col}From + OR + @{col}From + IS NULL)".n1t2()); + sbSearchParams.Append($"AND (T.{col} <= + @{col}To + OR + @{col}To + IS NULL)".n1t2()); } //SortParams - sbSortParams.Append("\n\t\t CASE WHEN UPPER(@strSortBy) = '" + row["COLUMN_NAME"].ToString().ToUpper() + "' AND UPPER(@strSortType) = 'ASC' THEN " + row["COLUMN_NAME"] + " END ASC,"); - sbSortParams.Append("\n\t\t CASE WHEN UPPER(@strSortBy) = '" + row["COLUMN_NAME"].ToString().ToUpper() + "' AND UPPER(@strSortType) = 'DESC' THEN " + row["COLUMN_NAME"] + " END DESC,"); + sbSortParams.Append($"CASE WHEN UPPER(@strSortBy) = '{col.ToUpper()}' AND UPPER(@strSortType) = 'ASC' THEN {col} END ASC,".n1t2()); + sbSortParams.Append($"CASE WHEN UPPER(@strSortBy) = '{col.ToUpper()}' AND UPPER(@strSortType) = 'DESC' THEN {col} END DESC,".n1t2()); } - sbSortParams.Append("\n\t\t CASE WHEN (@strSortBy IS NULL OR @strSortBy = '') THEN " + Util.Utility.GetPkColName() + " END DESC"); + sbSortParams.Append($"CASE WHEN (@strSortBy IS NULL OR @strSortBy = '') THEN {_NUMID} END DESC".n1t2()); sb.AppendLine(); //RowCount - sb.Append("\n\t IF (@strMode = '0') -- FOR ROW COUNT "); - sb.Append("\n\t BEGIN"); - sb.Append("\n\t\t SELECT COUNT(1) TOTALROWS FROM " + strTableName + " T "); - sb.Append("\n\t\t WHERE 0 = 0 "); + sb.Append("IF (@strMode = '0') -- FOR ROW COUNT ".n1t1()); + sb.Append("BEGIN".n1t1()); + sb.Append("SELECT COUNT(1) TOTALROWS FROM " + strTableName + " T ".n1t2()); + sb.Append("WHERE 0 = 0 ".n1t2()); sb.Append(sbSearchParams.ToString()); - sb.Append("\n\t END"); + sb.Append("END".n1t1()); //SelectAll - sb.Append("\n\t IF (@strMode = '1') -- For Populate "); - sb.Append("\n\t BEGIN"); - sb.Append("\n\t\t SELECT T.* FROM " + strTableName + " T WHERE 0=0 "); + sb.Append("IF (@strMode = '1') -- For Populate ".n1t1()); + sb.Append("BEGIN".n1t1()); + sb.Append("SELECT T.* FROM " + strTableName + " T WHERE 0=0 ".n1t2()); sb.Append(sbSearchParams.ToString()); - sb.Append("\n\t END"); - sb.Append("\n\t IF (@strMode = '2') -- Grid View and Join with Other Related Table"); - sb.Append("\n\t BEGIN"); - sb.Append("\n\t\t WITH T AS (SELECT ROW_NUMBER() OVER (ORDER BY "); + sb.Append("END".n1t1()); + sb.Append("IF (@strMode = '2') -- Grid View and Join with Other Related Table".n1t1()); + sb.Append("BEGIN".n1t1()); + sb.Append("WITH T AS (SELECT ROW_NUMBER() OVER (ORDER BY ".n1t2()); sb.Append(sbSortParams.ToString()); - sb.Append("\n\t\t ) AS ROWID, * FROM ( SELECT "); + sb.Append(") AS ROWID, * FROM ( SELECT ".n1t2()); foreach (DataRow row in dt.Rows) { - if (row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreatorColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifierColName()) || - row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordCreateDateColName()) || row["COLUMN_NAME"].ToString().Contains(Util.Utility.GetRecordModifiedDateColName())) + var type = row["DATA_TYPE"].ToString(); + var col = row["COLUMN_NAME"].ToString(); + + if (col.Contains(_STRUID) || col.Contains(_STRLASTUID) || + col.Contains(_DTUDT) || col.Contains(_DTLASTUDT)) continue; - sb.Append("\n\t\t\t T." + row["COLUMN_NAME"] + ","); + sb.Append("T." + col + ",".n1t3()); } sb.Remove(sb.Length - 1, 1); //To Remove last Comma - sb.Append("\n\t\t FROM " + strTableName + " T WHERE 0=0 "); + sb.Append("FROM " + strTableName + " T WHERE 0=0 ".n1t2()); sb.Append(sbSearchParams.ToString()); - sb.Append("\n\t\t ) AS T) SELECT T.* FROM T WHERE 1=1 "); - sb.Append("\n\t\t AND (ROWID >= @startRowIndex OR @startRowIndex IS NULL OR @startRowIndex <= 0)"); - sb.Append("\n\t\t AND (ROWID <= @startRowIndex + @maximumRows - 1 OR @maximumRows IS NULL OR @maximumRows <= 0)"); - sb.Append("\n\t END"); + sb.Append(") AS T) SELECT T.* FROM T WHERE 1=1 ".n1t2()); + sb.Append("AND (ROWID >= @startRowIndex OR @startRowIndex IS NULL OR @startRowIndex <= 0)".n1t2()); + sb.Append("AND (ROWID <= @startRowIndex + @maximumRows - 1 OR @maximumRows IS NULL OR @maximumRows <= 0)".n1t2()); + sb.Append("END".n1t1()); - sb.Append("\n END"); + sb.Append("END".n1t0()); - sb.Append("\n\n\t IF @@error <> 0 GOTO procError"); - sb.Append("\n\t\t GOTO procEnd"); + sb.Append("IF @@error <> 0 GOTO procError".n1t0().n1t1()); + sb.Append("GOTO procEnd".n1t2()); - sb.Append("\n\n\t procError:"); - sb.Append("\n\t\t SET @numErrorCode = @@error"); - sb.Append("\n\t\t SELECT @strErrorMsg = [description] FROM master.dbo.sysmessages WHERE error = @numErrorCode"); - sb.Append("\n\t\t INSERT INTO error_log (LogDate,Source,ErrMsg) VALUES (getDate(),'" + strProcName + "',@strErrorMsg)"); + sb.Append("procError:".n1t0().n1t1()); + sb.Append("SET @numErrorCode = @@error".n1t2()); + sb.Append("SELECT @strErrorMsg = [description] FROM master.dbo.sysmessages WHERE error = @numErrorCode".n1t2()); + sb.Append("INSERT INTO error_log (LogDate,Source,ErrMsg) VALUES (getDate(),'" + strProcName + "',@strErrorMsg)".n1t2()); - sb.Append("\n procEnd:"); + sb.Append("procEnd:".n1t0()); //MessageBox.Show(sb.ToString()); return sb; diff --git a/CodeGenerator/Util/Utility.cs b/CodeGenerator/Util/Utility.cs index c124c91..4bead3a 100644 --- a/CodeGenerator/Util/Utility.cs +++ b/CodeGenerator/Util/Utility.cs @@ -5,7 +5,7 @@ namespace CodeGenerator.MVP.Util { - public class Utility + public static class Utility { #region Read Configuration public static string GetProjectName() @@ -158,12 +158,20 @@ public static int ParseInt(object value) return intValue; } - /// - /// Writes the content in disk file - /// - /// - /// - public static void WriteToDisk(string filePath, string content) + public static bool In(this T item, params T[] items) + { + if (items == null) + throw new ArgumentNullException("items"); + + return items.Contains(item); + } + + /// + /// Writes the content in disk file + /// + /// + /// + public static void WriteToDisk(string filePath, string content) { System.IO.StreamWriter fileWriter = null; try @@ -194,5 +202,22 @@ public static void WriteToDisk(string filePath, string content) } } } - } + + #region Constants + + public static string n0t1(this string str) { return $"\t{str}"; } + public static string n0t2(this string str) { return $"\t\t{str}"; } + public static string n0t3(this string str) { return $"\t\t\t{str}"; } + public static string n0t4(this string str) { return $"\t\t\t\t{str}"; } + public static string n0t5(this string str) { return $"\t\t\t\t\t{str}"; } + + public static string n1t0(this string str) { return $"\n{str}"; } + public static string n1t1(this string str) { return $"\n\t{str}"; } + public static string n1t2(this string str) { return $"\n\t\t{str}"; } + public static string n1t3(this string str) { return $"\n\t\t\t{str}"; } + public static string n1t4(this string str) { return $"\n\t\t\t\t{str}"; } + public static string n1t5(this string str) { return $"\n\t\t\t\t\t{str}"; } + + #endregion + } }