Click here to Skip to main content
15,897,334 members
Articles / Desktop Programming / WPF

DBTool for Oracle - Part 1

Rate me:
Please Sign up or sign in to vote.
4.92/5 (45 votes)
13 Apr 2014CPOL18 min read 138.5K   5.1K   88  
Enhance productivity and reliability, write your own tools.
using System;
using System.Collections.Generic;
using System.Text;

using Harlinn.Oracle.DBTool.Types.Projects;
using Harlinn.Oracle.DBTool.Generators.Utils;
using Harlinn.Oracle.DBTool.Types.CSharp;

namespace Harlinn.Oracle.DBTool.Generators.Manager
{
    public class ManagerGenerator : GeneratorBase
    {
        ProjectTabular tabular;

        SortedSet<string> getByMethodMethods = new SortedSet<string>();

        public ManagerGenerator(ProjectTabular tabular)
        {
            this.tabular = tabular;    
        }

        public override string GetFilename()
        {
            string className = tabular.GetDataAccessClassName();
            string directory = GetDirectory();

            string result = directory + "\\" + className + ".cs";
            return result;
        }

        public override string GetDirectory()
        {
            string namespace_ = Project.Current.DatabaseNamespace;
            string directory = Project.Current.NamespaceToDirectory(namespace_);
            return directory;
        }



        public void Generate()
        {
            string commonNamespace = Project.Current.CommonNamespace;
            string dataNamespace = Project.Current.DataNamespace;
            string databaseNamespace = Project.Current.DatabaseNamespace;
            string className = tabular.GetDataAccessClassName();
            string dataClassName = tabular.GetDataTypeClassName();
            string baseName = tabular.GetBaseName();
            string readerClassName = tabular.GetReaderClassName();
            string baseClassName = "Accessor";

            if (tabular is ProjectTable)
            {
                ProjectTable projectTable = (ProjectTable)tabular;
                if (projectTable.IsTagValue)
                {
                    baseClassName = "TagAccessor";
                }
            }

            List<ProjectTabularField> primaryKeyFields = null;
            ProjectTabularPrimaryKey primaryKey = tabular.GetPrimaryKey();
            if (primaryKey != null)
            {
                primaryKeyFields = primaryKey.GetFieldList();
            }


            WriteLine("using System;");
            WriteLine("using System.Collections.Generic;");
            WriteLine("using System.ComponentModel;");
            WriteLine("using System.Data;");
            WriteLine("using System.Reflection;");

            WriteLine("using Oracle.DataAccess.Client;");
            WriteLine("using Oracle.DataAccess.Types;");
            WriteLine();
            WriteLine();
            WriteLine("using " + commonNamespace + ";");
            WriteLine("using " + dataNamespace + ";");

            WriteLine();
            WriteLine("namespace " + databaseNamespace);
            WriteLine("{");

            WriteLine("    [DataObject]");
            WriteLine("    public partial class " + className + " : " + baseClassName );
            WriteLine("    {");
            WriteLine();
            WriteLine("        private static readonly log4net.ILog sfLog = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);");
            WriteLine();
            WriteLine("        private static void LogException(Exception exc, MethodBase method)");
            WriteLine("        {");
            WriteLine("            Logger.LogException(sfLog, exc, method);");
            WriteLine("        }");
            WriteLine();

            WriteLine();
            WriteLine();

            if (tabular is ProjectSchemaElement)
            {
                ProjectSchemaElement projectSchemaElement = (ProjectSchemaElement)tabular;

                if (projectSchemaElement is ProjectTable)
                {
                    ProjectTable projectTable = (ProjectTable)projectSchemaElement;
                    if (projectTable.IsTagValue == false)
                    {
                        WriteLine("        public const string SCHEMA = \"" + projectSchemaElement.SchemaName + "\";");
                    }
                }
                else
                {
                    WriteLine("        public const string SCHEMA = \"" + projectSchemaElement.SchemaName + "\";");
                }
                if (tabular is ProjectTable)
                {
                    WriteLine("        public const string TABLE = \"" + projectSchemaElement.ElementName + "\";");
                    WriteLine("        public const string DB_QUALIFIED_NAME = \"" + projectSchemaElement.SchemaName + "." + projectSchemaElement.ElementName + "\";");

                }
                else if (tabular is ProjectView)
                {
                    WriteLine("        public const string VIEW = \"" + projectSchemaElement.ElementName + "\";");
                    WriteLine("        public const string DB_QUALIFIED_NAME = \"" + projectSchemaElement.SchemaName + "." + projectSchemaElement.ElementName + "\";");
                }
            }
            WriteLine();
            WriteLine();

            GenerateCreateDataTable();

            WriteLine();
            List<ProjectTabularField> fields = tabular.GetFieldList();

            GenerateGetMethods();

            if (tabular is ProjectTable)
            {
                ProjectTable projectTable = (ProjectTable)tabular;
                ProjectTabularField concurrencyField = projectTable.GetConcurrencyField();

                WriteLine();
                WriteLine("        public static int Insert( " + dataClassName + " element )");
                WriteLine("        {");
                WriteLine("            try");
                WriteLine("            {");
                WriteLine("                " + dataClassName + " result = null;");
                WriteLine("                int recordsInserted = Insert( GetConnection(), element, out result );");
                WriteLine("                return recordsInserted;");
                WriteLine("            }");
                WriteLine("            catch (Exception exc)");
                WriteLine("            {");
                WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                WriteLine("                throw;");
                WriteLine("            }");
                WriteLine("        }");
                WriteLine();

                WriteLine();
                WriteLine("        public static int Insert( " + dataClassName + " element, out " + dataClassName + " result )");
                WriteLine("        {");
                WriteLine("            try");
                WriteLine("            {");
                WriteLine("                int recordsInserted = Insert( GetConnection(), element, out result );");
                WriteLine("                return recordsInserted;");
                WriteLine("            }");
                WriteLine("            catch (Exception exc)");
                WriteLine("            {");
                WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                WriteLine("                throw;");
                WriteLine("            }");
                WriteLine("        }");
                WriteLine();

                WriteLine();
                WriteLine("        public static int Insert( OracleConnection oracleConnection, " + dataClassName + " element, out " + dataClassName + " result )");
                WriteLine("        {");
                WriteLine("            try");
                WriteLine("            {");
                if (tabular.IsTagValue)
                {
                    WriteLine("                string qualifiedDBName = GetQualfiedTagValueTablename( element.Tag );");
                }
                else
                {
                    WriteLine("                string qualifiedDBName = DB_QUALIFIED_NAME;");
                }
                WriteLine("                int recordsInserted = 0;");
                WriteLine("                result = null;");
                WriteLine();
                WriteLine("                OracleCommand oracleCommand = oracleConnection.CreateCommand();");
                WriteLine("                using (oracleCommand)");
                WriteLine("                {");
                WriteLine("                    oracleCommand.BindByName = true;");
                string insertSQL = OracleSqlInsertHelper.GenerateSQLInsertStatement(projectTable, "                    ");
                WriteLine();
                WriteLine("                    string insertSQLStatement = " + insertSQL +";");
                WriteLine("                    string finalInsertSQLStatement = string.Format(insertSQLStatement,qualifiedDBName);");
                WriteLine();
                WriteLine("                    oracleCommand.CommandText = finalInsertSQLStatement;");
                WriteLine();
                string initializeParametersCode = OracleSqlInsertHelper.GetParameterDeclarationsForInsert(fields, "                    ");
                WriteLine(initializeParametersCode);
                WriteLine();

                if (projectTable.IsTagValue)
                {
                    WriteLine("                    bool tryUpdate = false;");
                    WriteLine("                    try");
                    WriteLine("                    {");
                    WriteLine("                        recordsInserted = oracleCommand.ExecuteNonQuery();");
                    WriteLine("                        if( recordsInserted != 0)");
                    WriteLine("                        {");
                    WriteLine("                            result = element;");
                    for (int i = 0; i < fields.Count; i++)
                    {
                        ProjectTabularField column = fields[i];
                        if ((string.IsNullOrWhiteSpace(column.Sequence) == false) && (column.Concurrency == false))
                        {
                            string sqlParameterName = column.PropertyFieldName + "Parameter";
                            WriteLine("                            result." + column.PropertyName + " = " + TypeHelper.GetCommandParameterOutConversion(column.ReaderType, column.PropertyType, sqlParameterName + ".Value") + ";");
                        }
                    }
                    WriteLine("                        }");
                    WriteLine("                    }");
                    WriteLine("                    catch (OracleException oexc)");
                    WriteLine("                    {");
                    WriteLine("                        if (oexc.Number == 1)");
                    WriteLine("                        {");
                    WriteLine("                            tryUpdate = true;");
                    WriteLine("                        }");
                    WriteLine("                        else");
                    WriteLine("                        {");
                    WriteLine("                            throw;");
                    WriteLine("                        }");
                    WriteLine("                    }");
                    WriteLine("                    if (tryUpdate)");
                    WriteLine("                    {");
                    WriteLine("                        recordsInserted = Update(oracleConnection, element, out result);");
                    WriteLine("                    }");
                }
                else
                {
                    
                    WriteLine("                    recordsInserted = oracleCommand.ExecuteNonQuery();");
                    WriteLine("                    if( recordsInserted != 0)");
                    WriteLine("                    {");
                    WriteLine("                        result = element;");
                    for (int i = 0; i < fields.Count; i++)
                    {
                        ProjectTabularField column = fields[i];
                        if ((string.IsNullOrWhiteSpace(column.Sequence) == false) && (column.Concurrency == false))
                        {
                            string sqlParameterName = column.PropertyFieldName + "Parameter";
                            WriteLine("                        result." + column.PropertyName + " = " + TypeHelper.GetCommandParameterOutConversion(column.ReaderType, column.PropertyType, sqlParameterName + ".Value") + ";");
                        }
                    }
                    if (concurrencyField != null)
                    {
                        WriteLine("                        result." + projectTable.ElementStatePropertyName + " = ElementState.Stored;");
                    }

                    if (projectTable.IsTagTable)
                    {
                        WriteLine();
                        WriteLine("                        string tagValueTable = TagAccessor.GetTagValueTablename(result.Id);");
                        WriteLine("                        TagType tagType = (TagType)result.Type;");
                        WriteLine("                        TagAccessor.CreateTagValueTable(tagType, TagAccessor.SCHEMA, tagValueTable);");
                        WriteLine();
                    }

                    WriteLine("                    }");
                }
                WriteLine("                }");
                WriteLine();
                WriteLine("                return recordsInserted;");
                WriteLine("            }");
                WriteLine("            catch (Exception exc)");
                WriteLine("            {");
                WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                WriteLine("                throw;");
                WriteLine("            }");
                WriteLine("        }");
                WriteLine();
                WriteLine();


                WriteLine();
                WriteLine("        public static void Insert( List<" + dataClassName + "> elements )");
                WriteLine("        {");
                WriteLine("            try");
                WriteLine("            {");
                WriteLine("                Insert( GetConnection(), elements);");
                WriteLine("            }");
                WriteLine("            catch (Exception exc)");
                WriteLine("            {");
                WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                WriteLine("                throw;");
                WriteLine("            }");
                WriteLine("        }");
                WriteLine();

                WriteLine();
                WriteLine("        public static void Insert( OracleConnection oracleConnection, List<" + dataClassName + "> elements )");
                WriteLine("        {");
                WriteLine("            try");
                WriteLine("            {");
                if (tabular.IsTagValue)
                {
                    WriteLine("                if(elements.Count == 0)");
                    WriteLine("                {");
                    WriteLine("                    return;");
                    WriteLine("                }");
                    WriteLine("                string qualifiedDBName = GetQualfiedTagValueTablename( elements[0].Tag );");
                }
                else
                {
                    WriteLine("                string qualifiedDBName = DB_QUALIFIED_NAME;");
                }
                WriteLine("                DataTable dataTable = CreateDataTable( elements );");
                WriteLine("                using (dataTable)");
                WriteLine("                {");
                WriteLine("                    WriteToServer(oracleConnection,qualifiedDBName,dataTable);");
                WriteLine("                }");
                WriteLine("            }");
                WriteLine("            catch (Exception exc)");
                WriteLine("            {");
                WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                WriteLine("                throw;");
                WriteLine("            }");
                WriteLine("        }");
                WriteLine();
                WriteLine();


                WriteLine("        public static int Update( " + dataClassName + " element )");
                WriteLine("        {");
                WriteLine("            try");
                WriteLine("            {");
                WriteLine("                " + dataClassName + " result = null;");
                WriteLine("                int recordsUpdated = Update(GetConnection(), element, out result);");
                WriteLine("                return recordsUpdated;");
                WriteLine("            }");
                WriteLine("            catch (Exception exc)");
                WriteLine("            {");
                WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                WriteLine("                throw;");
                WriteLine("            }");
                WriteLine("        }");
                WriteLine();
                WriteLine("        public static int Update( " + dataClassName + " element, out " + dataClassName + " result )");
                WriteLine("        {");
                WriteLine("            try");
                WriteLine("            {");
                WriteLine("                int recordsUpdated = Update(GetConnection(), element, out result);");
                WriteLine("                return recordsUpdated;");
                WriteLine("            }");
                WriteLine("            catch (Exception exc)");
                WriteLine("            {");
                WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                WriteLine("                throw;");
                WriteLine("            }");
                WriteLine("        }");
                WriteLine();

                WriteLine("        public static int Update( OracleConnection oracleConnection, " + dataClassName + " element, out " + dataClassName + " result )");
                WriteLine("        {");
                WriteLine("            try");
                WriteLine("            {");
                if (tabular.IsTagValue)
                {
                    WriteLine("                string qualifiedDBName = GetQualfiedTagValueTablename( element.Tag );");
                }
                else
                {
                    WriteLine("                string qualifiedDBName = DB_QUALIFIED_NAME;");
                }
                WriteLine("                int recordsUpdated = 0;");
                WriteLine("                result = null;");
                WriteLine();
                WriteLine("                OracleCommand oracleCommand = oracleConnection.CreateCommand();");
                WriteLine("                using (oracleCommand)");
                WriteLine("                {");
                WriteLine("                    oracleCommand.BindByName = true;");
                string updateSQL = OracleSqlUpdateHelper.GenerateSQLUpdateStatement(projectTable, "                    ");
                WriteLine("                    string updateSQLStatement = " + updateSQL);
                WriteLine();
                WriteLine("                    string finalUpdateSQLStatement = string.Format(updateSQLStatement,qualifiedDBName);");
                WriteLine();
                WriteLine("                    oracleCommand.CommandText = finalUpdateSQLStatement;");
                WriteLine();
                string initializeUpdateParametersCode = OracleSqlUpdateHelper.GetParameterDeclarationsForUpdate(fields, "                    ");
                WriteLine(initializeUpdateParametersCode);
                WriteLine();
                WriteLine();
                WriteLine("                    recordsUpdated = oracleCommand.ExecuteNonQuery();");
                WriteLine("                    if( recordsUpdated != 0)");
                WriteLine("                    {");
                WriteLine("                        result = element;");
                for (int i = 0; i < fields.Count; i++)
                {
                    ProjectTabularField column = fields[i];
                    if (column.Concurrency)
                    {
                        string sqlParameterName = column.PropertyFieldName + "Parameter";

                        WriteLine("                        result." + column.PropertyName + " = " + TypeHelper.GetCommandParameterOutConversion(column.ReaderType, column.PropertyType, sqlParameterName + ".Value") + ";");
                    }
                }
                if (concurrencyField != null)
                {
                    WriteLine("                        result." + projectTable.ElementStatePropertyName + " = ElementState.Stored;");
                }
                WriteLine("                    }");


                WriteLine("                    else");
                WriteLine("                    {");
                string createReaderByPrimaryKeyMethodName = MethodNamesHelper.GetCreateReaderMethodName(primaryKey);
                string createReaderByPrimaryKeyMethodArmuments = ColumnHelper.GetCommaSeparatedPropertyNameList("element", primaryKeyFields);
                WriteLine("                        " + readerClassName + " reader =  " + readerClassName + "." + createReaderByPrimaryKeyMethodName + "(oracleConnection, qualifiedDBName," + createReaderByPrimaryKeyMethodArmuments + ");");
                WriteLine("                        using (reader)");
                WriteLine("                        {");
                WriteLine("                            result = element;");
                if (concurrencyField != null)
                {
                    WriteLine("                            if (reader.Read())");
                    WriteLine("                            {");
                    WriteLine("                                result.ElementState = ElementState.ConcurrencyConflict;");
                    WriteLine("                                result.ConcurrencyConflictElement = reader."+baseName+";");
                    WriteLine("                            }");
                    WriteLine("                            else");
                    WriteLine("                            {");
                    WriteLine("                                result.ElementState = ElementState.Deleted;");
                    WriteLine("                            }");
                }
                WriteLine("                        }");
                WriteLine("                    }");


                WriteLine("                }");
                WriteLine();
                WriteLine("                return recordsUpdated;");
                WriteLine("            }");
                WriteLine("            catch (Exception exc)");
                WriteLine("            {");
                WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                WriteLine("                throw;");
                WriteLine("            }");
                WriteLine("        }");
                WriteLine();
                WriteLine();

                if (concurrencyField != null)
                {
                    WriteLine("        public static int Save( " + dataClassName + " element )");
                    WriteLine("        {");
                    WriteLine("            try");
                    WriteLine("            {");
                    WriteLine("                " + dataClassName + " result = null;");
                    WriteLine("                int recordsSaved = Save(GetConnection(), element, out result);");
                    WriteLine("                return recordsSaved;");
                    WriteLine("            }");
                    WriteLine("            catch (Exception exc)");
                    WriteLine("            {");
                    WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                    WriteLine("                throw;");
                    WriteLine("            }");
                    WriteLine("        }");
                    WriteLine();
                    WriteLine("        public static int Save( " + dataClassName + " element, out " + dataClassName + " result )");
                    WriteLine("        {");
                    WriteLine("            try");
                    WriteLine("            {");
                    WriteLine("                int recordsSaved = Save(GetConnection(), element, out result);");
                    WriteLine("                return recordsSaved;");
                    WriteLine("            }");
                    WriteLine("            catch (Exception exc)");
                    WriteLine("            {");
                    WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                    WriteLine("                throw;");
                    WriteLine("            }");
                    WriteLine("        }");
                    WriteLine();
                    WriteLine();
                
                    WriteLine("        public static int Save( OracleConnection oracleConnection, " + dataClassName + " element, out " + dataClassName + " result )");
                    WriteLine("        {");
                    WriteLine("            try");
                    WriteLine("            {");
                    WriteLine("                int recordsSaved = 0;");
                    WriteLine("                result = null;");
                    WriteLine();
                    WriteLine("                if (element." + projectTable.ElementStatePropertyName + " == ElementState.New)");
                    WriteLine("                {");
                    WriteLine("                    recordsSaved = Insert(oracleConnection, element, out result);");
                    WriteLine("                }");
                    WriteLine("                else if (element." + projectTable.ElementStatePropertyName + " == ElementState.Changed)");
                    WriteLine("                {");
                    WriteLine("                    recordsSaved = Update(oracleConnection, element, out result);");
                    WriteLine("                }");
                    WriteLine();
                    WriteLine("                return recordsSaved;");
                    WriteLine("            }");
                    WriteLine("            catch (Exception exc)");
                    WriteLine("            {");
                    WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                    WriteLine("                throw;");
                    WriteLine("            }");
                    WriteLine("        }");
                    WriteLine();
                    WriteLine();


                    WriteLine("        public static List<" + dataClassName + "> Save( List<" + dataClassName + "> elements )");
                    WriteLine("        {");
                    WriteLine("            try");
                    WriteLine("            {");
                    WriteLine("                List<" + dataClassName + "> result = Save(GetConnection(), elements);");
                    WriteLine("                return result;");
                    WriteLine("            }");
                    WriteLine("            catch (Exception exc)");
                    WriteLine("            {");
                    WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                    WriteLine("                throw;");
                    WriteLine("            }");
                    WriteLine("        }");
                    WriteLine();
                    WriteLine();

                    WriteLine("        public static List<" + dataClassName + "> Save( OracleConnection oracleConnection, List<" + dataClassName + "> elements )");
                    WriteLine("        {");
                    WriteLine("            try");
                    WriteLine("            {");
                    WriteLine("                //string qualifiedDBName = DB_QUALIFIED_NAME;");
                    WriteLine("                List<" + dataClassName + "> result = null;");
                    WriteLine();
                    WriteLine("                OracleCommand oracleCommand = oracleConnection.CreateCommand();");
                    WriteLine("                using (oracleCommand)");
                    WriteLine("                {");

                    WriteLine("                }");
                    WriteLine();
                    WriteLine("                return result;");
                    WriteLine("            }");
                    WriteLine("            catch (Exception exc)");
                    WriteLine("            {");
                    WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                    WriteLine("                throw;");
                    WriteLine("            }");
                    WriteLine("        }");
                    WriteLine();
                }

                WriteLine();
                if ((primaryKeyFields != null) && (primaryKeyFields.Count > 0))
                {
                    ProjectTabularField primaryKeyField = primaryKeyFields[0];
                    string primaryKeyFieldName = primaryKeyField.Name;
                    string primaryKeyVariableName = primaryKeyField.PropertyFieldName;
                    string primaryKeyVariableType = primaryKeyField.PropertyType;

                    string tagArgDecl = string.Empty;
                    string tagArg = string.Empty;
                    if (tabular.IsTagValue)
                    {
                        tagArgDecl = "long tag, ";
                        tagArg = "tag, ";
                    }

                    if (concurrencyField != null)
                    {
                        string optimisticLockFieldName = concurrencyField.Name;
                        string optimisticLockVariableName = concurrencyField.PropertyFieldName;
                        string optimisticLockVariableType = concurrencyField.PropertyType;

                        
                        WriteLine("        public static int Delete( "+ tagArgDecl + primaryKeyVariableType + " " + primaryKeyVariableName + ", " + optimisticLockVariableType + " optimisticLock )");
                        WriteLine("        {");
                        WriteLine("            try");
                        WriteLine("            {");
                        WriteLine("                int result = Delete(GetConnection(), " + tagArg + primaryKeyVariableName + ", optimisticLock);");
                        WriteLine("                return result;");
                        WriteLine("            }");
                        WriteLine("            catch (Exception exc)");
                        WriteLine("            {");
                        WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                        WriteLine("                throw;");
                        WriteLine("            }");
                        WriteLine("        }");
                        WriteLine();

                        WriteLine("        private const string DELETE_STATEMENT = \"DELETE FROM {0} WHERE " + primaryKeyFieldName + " = :" + primaryKeyVariableName + " AND " + optimisticLockFieldName + " = :" + optimisticLockVariableName + "\";");
                        WriteLine("        public static int Delete( OracleConnection oracleConnection, " + tagArgDecl + primaryKeyVariableType + " " + primaryKeyVariableName + ", " + optimisticLockVariableType + " optimisticLock )");
                        WriteLine("        {");
                        WriteLine("            try");
                        WriteLine("            {");
                        if (tabular.IsTagValue)
                        {
                            WriteLine("                string qualifiedDBName = GetQualfiedTagValueTablename( tag );");
                        }
                        else
                        {
                            WriteLine("                string qualifiedDBName = DB_QUALIFIED_NAME;");
                        }
                        WriteLine("                int result = 0;");
                        WriteLine();
                        WriteLine("                OracleCommand oracleCommand = oracleConnection.CreateCommand();");
                        WriteLine("                using (oracleCommand)");
                        WriteLine("                {");
                        WriteLine("                    string deleteStatement = string.Format(DELETE_STATEMENT, qualifiedDBName);");
                        WriteLine("                    oracleCommand.CommandText = deleteStatement;");
                        WriteLine();
                        WriteLine(OracleSqlSelectHelper.GetParameterDeclarationsForWhereCriteriaStatement(primaryKeyFields, "                    "));
                        WriteLine();
                        WriteLine("                    result = oracleCommand.ExecuteNonQuery();");
                        WriteLine("                }");
                        WriteLine();
                        WriteLine("                return result;");
                        WriteLine("            }");
                        WriteLine("            catch (Exception exc)");
                        WriteLine("            {");
                        WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                        WriteLine("                throw;");
                        WriteLine("            }");
                        WriteLine("        }");
                        WriteLine();
                    }
                    else
                    {
                        WriteLine("        public static int Delete( " + tagArgDecl+ primaryKeyVariableType + " " + primaryKeyVariableName + " )");
                        WriteLine("        {");
                        WriteLine("            try");
                        WriteLine("            {");
                        WriteLine("                int result = Delete( GetConnection(), " + tagArg + primaryKeyVariableName + " );");
                        WriteLine("                return result;");
                        WriteLine("            }");
                        WriteLine("            catch (Exception exc)");
                        WriteLine("            {");
                        WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                        WriteLine("                throw;");
                        WriteLine("            }");
                        WriteLine("        }");
                        WriteLine();

                        WriteLine("        private const string DELETE_STATEMENT = \"DELETE FROM {0} WHERE " + primaryKeyFieldName + " = :" + primaryKeyVariableName + "\";");
                        WriteLine("        public static int Delete( OracleConnection oracleConnection, " + tagArgDecl + primaryKeyVariableType + " " + primaryKeyVariableName + " )");
                        WriteLine("        {");
                        WriteLine("            try");
                        WriteLine("            {");
                        if (tabular.IsTagValue)
                        {
                            WriteLine("                string qualifiedDBName = GetQualfiedTagValueTablename( tag );");
                        }
                        else
                        {
                            WriteLine("                string qualifiedDBName = DB_QUALIFIED_NAME;");
                        }
                        WriteLine("                int result = 0;");
                        WriteLine();
                        WriteLine("                OracleCommand oracleCommand = oracleConnection.CreateCommand();");
                        WriteLine("                using (oracleCommand)");
                        WriteLine("                {");
                        WriteLine("                    string deleteStatement = string.Format(DELETE_STATEMENT, qualifiedDBName);");
                        WriteLine("                    oracleCommand.CommandText = deleteStatement;");
                        WriteLine();
                        WriteLine(OracleSqlSelectHelper.GetParameterDeclarationsForWhereCriteriaStatement(primaryKeyFields, "                    "));
                        WriteLine();
                        WriteLine("                    result = oracleCommand.ExecuteNonQuery();");
                        WriteLine("                }");
                        WriteLine();
                        WriteLine("                return result;");
                        WriteLine("            }");
                        WriteLine("            catch (Exception exc)");
                        WriteLine("            {");
                        WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                        WriteLine("                throw;");
                        WriteLine("            }");
                        WriteLine("        }");
                        WriteLine();
                    
                    }
                    WriteLine();
                }
            }

            WriteLine("    }");

            WriteLine("}");


        }



        public void GenerateGetMethods()
        {
            GenerateGetAllMethod();

            GenerateGetByPrimaryKeyMethod();



            ProjectTabularIndexes indexes = tabular.GetIndexes();
            foreach (ProjectTabularIndex index in indexes.Children)
            {
                GenerateGetByIndexMethod(index);
            }


            ProjectTabularReferences references = tabular.GetReferences();
            foreach (ProjectTabularReference reference in references.Children)
            {
                GenerateGetByReferenceMethod(reference);
            }

            GenerateTagValueGetMethods();

        }
        private void GenerateGetAllMethod()
        {
            string readerClassName = tabular.GetReaderClassName();
            string dataClassName = tabular.GetDataTypeClassName();
            string baseName = tabular.GetBaseName();

            WriteLine("        [DataObjectMethod(DataObjectMethodType.Select,true)]");
            if (tabular.IsTagValue)
            {
                WriteLine("        public static List<" + dataClassName + "> GetAll( long tag )");
            }
            else
            {
                WriteLine("        public static List<" + dataClassName + "> GetAll( )");
            }
            WriteLine("        {");
            WriteLine("            try");
            WriteLine("            {");
            if (tabular.IsTagValue)
            {
                WriteLine("                string qualifiedDBName = GetQualfiedTagValueTablename( tag );");
            }
            else
            {
                WriteLine("                string qualifiedDBName = DB_QUALIFIED_NAME;");
            }
            WriteLine("                List<" + dataClassName + "> result = new List<" + dataClassName + ">( );");
            WriteLine("                " + readerClassName + " elementReader = new " + readerClassName + "( qualifiedDBName );");
            WriteLine("                using( elementReader )");
            WriteLine("                {");
            if (tabular.IsTagValue)
            {
                WriteLine("                    elementReader.Tag = tag;");
            }
            WriteLine("                    while( elementReader.Read( ) )");
            WriteLine("                    {");
            WriteLine("                        " + dataClassName + " element = elementReader." + baseName+ ";");
            WriteLine("                        result.Add(element);");
            WriteLine("                    }");
            WriteLine("                }");
            WriteLine("                return result;");
            WriteLine("            }");
            WriteLine("            catch (Exception exc)");
            WriteLine("            {");
            WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
            WriteLine("                throw;");
            WriteLine("            }");
            WriteLine("        }");
            WriteLine();
            WriteLine("        [DataObjectMethod(DataObjectMethodType.Select,false)]");
            if (tabular.IsTagValue)
            {
                WriteLine("        public static List<" + dataClassName + "> GetAll( OracleConnection oracleConnection, long tag )");
            }
            else
            {
                WriteLine("        public static List<" + dataClassName + "> GetAll( OracleConnection oracleConnection )");
            }
            WriteLine("        {");
            WriteLine("            try");
            WriteLine("            {");
            if (tabular.IsTagValue)
            {
                WriteLine("                string qualifiedDBName = GetQualfiedTagValueTablename( tag );");
            }
            else
            {
                WriteLine("                string qualifiedDBName = DB_QUALIFIED_NAME;");
            }
            WriteLine("                List<" + dataClassName + "> result = new List<" + dataClassName + ">( );");
            WriteLine("                " + readerClassName + " elementReader = new " + readerClassName + "( oracleConnection, qualifiedDBName );");
            WriteLine("                using( elementReader )");
            WriteLine("                {");
            if (tabular.IsTagValue)
            {
                WriteLine("                    elementReader.Tag = tag;");
            }
            WriteLine("                    while( elementReader.Read( ) )");
            WriteLine("                    {");
            WriteLine("                        " + dataClassName + " element = elementReader." + baseName + ";");
            WriteLine("                        result.Add(element);");
            WriteLine("                    }");
            WriteLine("                }");
            WriteLine("                return result;");
            WriteLine("            }");
            WriteLine("            catch (Exception exc)");
            WriteLine("            {");
            WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
            WriteLine("                throw;");
            WriteLine("            }");
            WriteLine("        }");
            WriteLine();
            if (tabular is ProjectTable)
            {
                ProjectTable projectTable = (ProjectTable)tabular;
                List<ProjectTabularField> primaryKeyFields = projectTable.GetPrimaryKeyFieldList();
                if ((projectTable.IsTagValue == false) && (primaryKeyFields.Count == 1))
                {
                    WriteLine("        public static Keyed" + baseName + "Collection GetKeyedCollection( )");
                    WriteLine("        {");
                    WriteLine("            try");
                    WriteLine("            {");
                    WriteLine("                string qualifiedDBName = DB_QUALIFIED_NAME;");
                    WriteLine("                Keyed" + baseName + "Collection result = new Keyed" + baseName + "Collection( );");
                    WriteLine("                " + readerClassName + " elementReader = new " + readerClassName + "( qualifiedDBName );");
                    WriteLine("                using( elementReader )");
                    WriteLine("                {");
                    WriteLine("                    while( elementReader.Read( ) )");
                    WriteLine("                    {");
                    WriteLine("                        " + dataClassName + " element = elementReader." + baseName + ";");
                    WriteLine("                        result.Add(element);");
                    WriteLine("                    }");
                    WriteLine("                }");
                    WriteLine("                return result;");
                    WriteLine("            }");
                    WriteLine("            catch (Exception exc)");
                    WriteLine("            {");
                    WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                    WriteLine("                throw;");
                    WriteLine("            }");
                    WriteLine("        }");
                    WriteLine();
                    WriteLine();
                }
            }

        }
        private void GenerateGetByPrimaryKeyMethod()
        {
            string readerClassName = tabular.GetReaderClassName();
            string dataClassName = tabular.GetDataTypeClassName();
            string dataPropertyName = tabular.GetBaseName();

            ProjectTabularPrimaryKey primaryKey = tabular.GetPrimaryKey();
            if (primaryKey != null)
            {
                string methodName = MethodNamesHelper.GetManagerGetByMethodName(primaryKey);
                if (getByMethodMethods.Contains(methodName))
                {
                    return;
                }
                getByMethodMethods.Add(methodName);
                List<ProjectTabularField> argumentFields = primaryKey.GetFieldList();

                string createReaderMethodName = MethodNamesHelper.GetCreateReaderMethodName(primaryKey);

                string arguments = ColumnHelper.GetCommaSeparatedPropertyFieldNameList(argumentFields);
                string argumentDeclaration = ColumnHelper.GetArgumentDeclarationList(argumentFields);

                WriteLine("        [DataObjectMethod(DataObjectMethodType.Select,false)]");
                if (tabular.IsTagValue)
                {
                    WriteLine("        public static " + dataClassName + " " + methodName + "( long tag, " + argumentDeclaration + " )");
                }
                else
                {
                    WriteLine("        public static " + dataClassName + " " + methodName + "( " + argumentDeclaration + " )");
                }
                WriteLine("        {");
                WriteLine("            try");
                WriteLine("            {");
                if (tabular.IsTagValue)
                {
                    WriteLine("                string qualifiedDBName = GetQualfiedTagValueTablename( tag );");
                }
                else
                {
                    WriteLine("                string qualifiedDBName = DB_QUALIFIED_NAME;");
                }
                WriteLine("                " + dataClassName + " result = null;");
                WriteLine("                " + readerClassName + " elementReader = " + readerClassName + "." + createReaderMethodName + "( qualifiedDBName," + arguments + " );");
                WriteLine("                using( elementReader )");
                WriteLine("                {");
                if (tabular.IsTagValue)
                {
                    WriteLine("                    elementReader.Tag = tag;");
                }
                WriteLine("                    if( elementReader.Read( ) )");
                WriteLine("                    {");
                WriteLine("                        " + dataClassName + " element = elementReader." + dataPropertyName + ";");
                WriteLine("                        result = element;");
                WriteLine("                    }");
                WriteLine("                }");
                WriteLine("                return result;");
                WriteLine("            }");
                WriteLine("            catch (Exception exc)");
                WriteLine("            {");
                WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                WriteLine("                throw;");
                WriteLine("            }");
                WriteLine("        }");
                WriteLine();

                WriteLine("        [DataObjectMethod(DataObjectMethodType.Select,false)]");
                if (tabular.IsTagValue)
                {
                    WriteLine("        public static " + dataClassName + " " + methodName + "( OracleConnection oracleConnection, long tag, " + argumentDeclaration + " )");
                }
                else
                {
                    WriteLine("        public static " + dataClassName + " " + methodName + "( OracleConnection oracleConnection, " + argumentDeclaration + " )");
                }
                WriteLine("        {");
                WriteLine("            try");
                WriteLine("            {");
                if (tabular.IsTagValue)
                {
                    WriteLine("                string qualifiedDBName = GetQualfiedTagValueTablename( tag );");
                }
                else
                {
                    WriteLine("                string qualifiedDBName = DB_QUALIFIED_NAME;");
                }
                WriteLine("                " + dataClassName + " result = null;");
                WriteLine("                " + readerClassName + " elementReader = " + readerClassName + "." + createReaderMethodName + "( oracleConnection ,qualifiedDBName," + arguments + " );");
                WriteLine("                using( elementReader )");
                WriteLine("                {");
                if (tabular.IsTagValue)
                {
                    WriteLine("                    elementReader.Tag = tag;");
                }
                WriteLine("                    if( elementReader.Read( ) )");
                WriteLine("                    {");
                WriteLine("                        " + dataClassName + " element = elementReader." + dataPropertyName + ";");
                WriteLine("                        result = element;");
                WriteLine("                    }");
                WriteLine("                }");
                WriteLine("                return result;");
                WriteLine("            }");
                WriteLine("            catch (Exception exc)");
                WriteLine("            {");
                WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                WriteLine("                throw;");
                WriteLine("            }");
                WriteLine("        }");
                WriteLine();

            }



        }
        private void GenerateGetByIndexMethod(ProjectTabularIndex index)
        {
            string readerClassName = tabular.GetReaderClassName();
            string dataClassName = tabular.GetDataTypeClassName();
            string dataPropertyName = tabular.GetBaseName();

            string methodName = MethodNamesHelper.GetManagerGetByMethodName(index);
            if (getByMethodMethods.Contains(methodName))
            {
                return;
            }
            getByMethodMethods.Add(methodName);
            List<ProjectTabularField> argumentFields = index.GetFieldList();

            string createReaderMethodName = MethodNamesHelper.GetCreateReaderMethodName(index);

            string arguments = ColumnHelper.GetCommaSeparatedPropertyFieldNameList(argumentFields);
            string argumentDeclaration = ColumnHelper.GetArgumentDeclarationList(argumentFields);

            WriteLine("        [DataObjectMethod(DataObjectMethodType.Select,false)]");
            if (tabular.IsTagValue)
            {
                WriteLine("        public static " + dataClassName + " " + methodName + "( long tag," + argumentDeclaration + " )");
            }
            else
            {
                WriteLine("        public static " + dataClassName + " " + methodName + "( " + argumentDeclaration + " )");
            }
            WriteLine("        {");
            WriteLine("            try");
            WriteLine("            {");
            if (tabular.IsTagValue)
            {
                WriteLine("                string qualifiedDBName = GetQualfiedTagValueTablename( tag );");
            }
            else
            {
                WriteLine("                string qualifiedDBName = DB_QUALIFIED_NAME;");
            }
            WriteLine("                " + dataClassName + " result = null;");
            WriteLine("                " + readerClassName + " elementReader = " + readerClassName + "." + createReaderMethodName + "( qualifiedDBName," + arguments + " );");
            WriteLine("                using( elementReader )");
            WriteLine("                {");
            if (tabular.IsTagValue)
            {
                WriteLine("                    elementReader.Tag = tag;");
            }
            WriteLine("                    if( elementReader.Read( ) )");
            WriteLine("                    {");
            WriteLine("                        " + dataClassName + " element = elementReader." + dataPropertyName + ";");
            WriteLine("                        result = element;");
            WriteLine("                    }");
            WriteLine("                }");
            WriteLine("                return result;");
            WriteLine("            }");
            WriteLine("            catch (Exception exc)");
            WriteLine("            {");
            WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
            WriteLine("                throw;");
            WriteLine("            }");
            WriteLine("        }");
            WriteLine();
            WriteLine("        [DataObjectMethod(DataObjectMethodType.Select,false)]");
            if (tabular.IsTagValue)
            {
                WriteLine("        public static " + dataClassName + " " + methodName + "( OracleConnection oracleConnection, long tag," + argumentDeclaration + " )");
            }
            else
            {
                WriteLine("        public static " + dataClassName + " " + methodName + "( OracleConnection oracleConnection, " + argumentDeclaration + " )");
            }
            WriteLine("        {");
            WriteLine("            try");
            WriteLine("            {");
            if (tabular.IsTagValue)
            {
                WriteLine("                string qualifiedDBName = GetQualfiedTagValueTablename( tag );");
            }
            else
            {
                WriteLine("                string qualifiedDBName = DB_QUALIFIED_NAME;");
            }
            WriteLine("                " + dataClassName + " result = null;");
            WriteLine("                " + readerClassName + " elementReader = " + readerClassName + "." + createReaderMethodName + "( oracleConnection ,qualifiedDBName," + arguments + " );");
            WriteLine("                using( elementReader )");
            WriteLine("                {");
            if (tabular.IsTagValue)
            {
                WriteLine("                    elementReader.Tag = tag;");
            }
            WriteLine("                    if( elementReader.Read( ) )");
            WriteLine("                    {");
            WriteLine("                        " + dataClassName + " element = elementReader." + dataPropertyName + ";");
            WriteLine("                        result = element;");
            WriteLine("                    }");
            WriteLine("                }");
            WriteLine("                return result;");
            WriteLine("            }");
            WriteLine("            catch (Exception exc)");
            WriteLine("            {");
            WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
            WriteLine("                throw;");
            WriteLine("            }");
            WriteLine("        }");
            WriteLine();
            if (tabular is ProjectTable)
            {
                ProjectTable projectTable = (ProjectTable)tabular;
                if ((projectTable.IsTimeSeries) && (projectTable.IsTagValue == false))
                {
                    methodName = MethodNamesHelper.GetManagerGetByForTimeStampMethodName(index);
                    if (getByMethodMethods.Contains(methodName))
                    {
                        return;
                    }
                    getByMethodMethods.Add(methodName);

                    createReaderMethodName = MethodNamesHelper.GetCreateReaderForTimeStampMethodName(index);

                    WriteLine("        [DataObjectMethod(DataObjectMethodType.Select,false)]");
                    WriteLine("        public static " + dataClassName + " " + methodName + "("+ argumentDeclaration + " )");
                    WriteLine("        {");
                    WriteLine("            try");
                    WriteLine("            {");
                    WriteLine("                string qualifiedDBName = DB_QUALIFIED_NAME;");
                    WriteLine("                " + dataClassName + " result = null;");
                    WriteLine("                " + readerClassName + " elementReader = " + readerClassName + "." + createReaderMethodName + "( qualifiedDBName," + arguments + " );");
                    WriteLine("                using( elementReader )");
                    WriteLine("                {");
                    WriteLine("                    if( elementReader.Read( ) )");
                    WriteLine("                    {");
                    WriteLine("                        " + dataClassName + " element = elementReader." + dataPropertyName + ";");
                    WriteLine("                        result = element;");
                    WriteLine("                    }");
                    WriteLine("                }");
                    WriteLine("                return result;");
                    WriteLine("            }");
                    WriteLine("            catch (Exception exc)");
                    WriteLine("            {");
                    WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                    WriteLine("                throw;");
                    WriteLine("            }");
                    WriteLine("        }");

                    WriteLine();
                    WriteLine("        [DataObjectMethod(DataObjectMethodType.Select,false)]");
                    WriteLine("        public static " + dataClassName + " " + methodName + "( OracleConnection oracleConnection, " + argumentDeclaration + " )");
                    WriteLine("        {");
                    WriteLine("            try");
                    WriteLine("            {");
                    WriteLine("                string qualifiedDBName = DB_QUALIFIED_NAME;");
                    WriteLine("                " + dataClassName + " result = null;");
                    WriteLine("                " + readerClassName + " elementReader = " + readerClassName + "." + createReaderMethodName + "( oracleConnection, qualifiedDBName," + arguments + " );");
                    WriteLine("                using( elementReader )");
                    WriteLine("                {");
                    WriteLine("                    if( elementReader.Read( ) )");
                    WriteLine("                    {");
                    WriteLine("                        " + dataClassName + " element = elementReader." + dataPropertyName + ";");
                    WriteLine("                        result = element;");
                    WriteLine("                    }");
                    WriteLine("                }");
                    WriteLine("                return result;");
                    WriteLine("            }");
                    WriteLine("            catch (Exception exc)");
                    WriteLine("            {");
                    WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                    WriteLine("                throw;");
                    WriteLine("            }");
                    WriteLine("        }");

                    WriteLine();
                }
            }


        }
        private void GenerateGetByReferenceMethod(ProjectTabularReference reference)
        {
            string readerClassName = tabular.GetReaderClassName();
            string dataClassName = tabular.GetDataTypeClassName();
            string dataPropertyName = tabular.GetBaseName();

            string methodName = MethodNamesHelper.GetManagerGetByMethodName(reference);
            if (getByMethodMethods.Contains(methodName))
            {
                return;
            }
            getByMethodMethods.Add(methodName);
            List<ProjectTabularField> argumentFields = reference.GetFieldList();

            string createReaderMethodName = MethodNamesHelper.GetCreateReaderMethodName(reference);

            string arguments = ColumnHelper.GetCommaSeparatedPropertyFieldNameList(argumentFields);
            string argumentDeclaration = ColumnHelper.GetArgumentDeclarationList(argumentFields);

            WriteLine("        [DataObjectMethod(DataObjectMethodType.Select,false)]");
            if (tabular.IsTagValue)
            {
                WriteLine("        public static List<" + dataClassName + "> " + methodName + "( long tag, " + argumentDeclaration + " )");
            }
            else
            {
                WriteLine("        public static List<" + dataClassName + "> " + methodName + "( " + argumentDeclaration + " )");
            }
            WriteLine("        {");
            WriteLine("            try");
            WriteLine("            {");
            if (tabular.IsTagValue)
            {
                WriteLine("                string qualifiedDBName = GetQualfiedTagValueTablename( tag );");
            }
            else
            {
                WriteLine("                string qualifiedDBName = DB_QUALIFIED_NAME;");
            }
            WriteLine("                List<" + dataClassName + "> result = new List<" + dataClassName + ">( );");
            WriteLine("                " + readerClassName + " elementReader = " + readerClassName + "." + createReaderMethodName + "( qualifiedDBName," + arguments + " );");
            WriteLine("                using( elementReader )");
            WriteLine("                {");
            WriteLine("                    while( elementReader.Read( ) )");
            WriteLine("                    {");
            WriteLine("                        " + dataClassName + " element = elementReader." + dataPropertyName + ";");
            WriteLine("                        result.Add( element );");
            WriteLine("                    }");
            WriteLine("                }");
            WriteLine("                return result;");
            WriteLine("            }");
            WriteLine("            catch (Exception exc)");
            WriteLine("            {");
            WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
            WriteLine("                throw;");
            WriteLine("            }");
            WriteLine("        }");
            WriteLine();
        }


        private void GenerateTagValueGetMethods()
        {
            if (tabular is ProjectTable)
            {
                ProjectTable projectTable = (ProjectTable)tabular;

                if (projectTable.IsTagValue)
                {
                    string readerClassName = tabular.GetReaderClassName();
                    string dataClassName = tabular.GetDataTypeClassName();
                    string dataPropertyName = tabular.GetBaseName();

                    string readerMethodName = "Create" + tabular.GetBaseName() + "ForTimeStamp";
                    string methodName = "Get" + tabular.GetBaseName() + "ForTimeStamp";
                    WriteLine();
                    WriteLine("        public static " + dataClassName + " " + methodName + "( long tag, DateTime ts )");
                    WriteLine("        {");
                    WriteLine("            try");
                    WriteLine("            {");
                    WriteLine("                " + dataClassName + " result = "+methodName+"( GetConnection(), tag, ts );");
                    WriteLine("                return result;");
                    WriteLine("            }");
                    WriteLine("            catch (Exception exc)");
                    WriteLine("            {");
                    WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                    WriteLine("                throw;");
                    WriteLine("            }");
                    WriteLine("        }");
                    WriteLine();
                    WriteLine("        public static " + dataClassName + " " + methodName + "( OracleConnection oracleConnection, long tag, DateTime ts )");
                    WriteLine("        {");
                    WriteLine("            try");
                    WriteLine("            {");
                    WriteLine("                string qualifiedDBName = GetQualfiedTagValueTablename( tag );");
                    WriteLine("                " + dataClassName + " result = null;");
                    WriteLine("                " + readerClassName + " elementReader = " + readerClassName + "." + readerMethodName + "( oracleConnection, qualifiedDBName, ts );");
                    WriteLine("                using( elementReader )");
                    WriteLine("                {");
                    WriteLine("                    elementReader.Tag = tag;");
                    WriteLine("                    if( elementReader.Read( ) )");
                    WriteLine("                    {");
                    WriteLine("                        " + dataClassName + " element = elementReader." + dataPropertyName + ";");
                    WriteLine("                        result = element;");
                    WriteLine("                    }");
                    WriteLine("                }");
                    WriteLine("                return result;");
                    WriteLine("            }");
                    WriteLine("            catch (Exception exc)");
                    WriteLine("            {");
                    WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                    WriteLine("                throw;");
                    WriteLine("            }");
                    WriteLine("        }");
                    WriteLine();


                    readerMethodName = "Create" + tabular.GetBaseName() + "ForLastTimeStamp";
                    methodName = "Get" + tabular.GetBaseName() + "ForLastTimeStamp";
                    WriteLine();
                    WriteLine("        public static " + dataClassName + " " + methodName + "( long tag )");
                    WriteLine("        {");
                    WriteLine("            try");
                    WriteLine("            {");
                    WriteLine("                " + dataClassName + " result = " + methodName + "( GetConnection(), tag );");
                    WriteLine("                return result;");
                    WriteLine("            }");
                    WriteLine("            catch (Exception exc)");
                    WriteLine("            {");
                    WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                    WriteLine("                throw;");
                    WriteLine("            }");
                    WriteLine("        }");
                    WriteLine();
                    WriteLine("        public static " + dataClassName + " " + methodName + "( OracleConnection oracleConnection, long tag )");
                    WriteLine("        {");
                    WriteLine("            try");
                    WriteLine("            {");
                    WriteLine("                string qualifiedDBName = GetQualfiedTagValueTablename( tag );");
                    WriteLine("                " + dataClassName + " result = null;");
                    WriteLine("                " + readerClassName + " elementReader = " + readerClassName + "." + readerMethodName + "( oracleConnection, qualifiedDBName );");
                    WriteLine("                using( elementReader )");
                    WriteLine("                {");
                    WriteLine("                    elementReader.Tag = tag;");
                    WriteLine("                    if( elementReader.Read( ) )");
                    WriteLine("                    {");
                    WriteLine("                        " + dataClassName + " element = elementReader." + dataPropertyName + ";");
                    WriteLine("                        result = element;");
                    WriteLine("                    }");
                    WriteLine("                }");
                    WriteLine("                return result;");
                    WriteLine("            }");
                    WriteLine("            catch (Exception exc)");
                    WriteLine("            {");
                    WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                    WriteLine("                throw;");
                    WriteLine("            }");
                    WriteLine("        }");
                    WriteLine();


                    readerMethodName = "Create" + tabular.GetBaseName() + "ForInterval";
                    methodName = "Get" + tabular.GetBaseName() + "ForInterval";
                    WriteLine("        public static List<" + dataClassName + "> " + methodName + "( long tag, DateTime startOfInterval, DateTime endOfInterval )");
                    WriteLine("        {");
                    WriteLine("            try");
                    WriteLine("            {");
                    WriteLine("                List<" + dataClassName + "> result = " + methodName + "(GetConnection(), tag, startOfInterval, endOfInterval );");
                    WriteLine("                return result;");
                    WriteLine("            }");
                    WriteLine("            catch (Exception exc)");
                    WriteLine("            {");
                    WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                    WriteLine("                throw;");
                    WriteLine("            }");
                    WriteLine("        }");
                    WriteLine();
                    WriteLine("        public static List<" + dataClassName + "> " + methodName + "( OracleConnection oracleConnection, long tag, DateTime startOfInterval, DateTime endOfInterval )");
                    WriteLine("        {");
                    WriteLine("            try");
                    WriteLine("            {");
                    WriteLine("                string qualifiedDBName = GetQualfiedTagValueTablename( tag );");
                    WriteLine("                List<" + dataClassName + "> result = new List<" + dataClassName + ">( );");
                    WriteLine("                " + readerClassName + " elementReader = " + readerClassName + "." + readerMethodName + "( oracleConnection, qualifiedDBName, startOfInterval, endOfInterval );");
                    WriteLine("                using( elementReader )");
                    WriteLine("                {");
                    WriteLine("                    elementReader.Tag = tag;");
                    WriteLine("                    while( elementReader.Read( ) )");
                    WriteLine("                    {");
                    WriteLine("                        " + dataClassName + " element = elementReader." + dataPropertyName + ";");
                    WriteLine("                        result.Add( element );");
                    WriteLine("                    }");
                    WriteLine("                }");
                    WriteLine("                return result;");
                    WriteLine("            }");
                    WriteLine("            catch (Exception exc)");
                    WriteLine("            {");
                    WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                    WriteLine("                throw;");
                    WriteLine("            }");
                    WriteLine("        }");
                    WriteLine();

                    WriteLine();

                }
            }

        }


        private void GenerateCreateDataTable()
        {
            if (tabular is ProjectTable)
            {
                ProjectTable projectTable = (ProjectTable)tabular;
                string dataClassName = projectTable.DataClassName;
                List<ProjectTabularField> fields = projectTable.GetFieldList();
                List<ProjectTabularField> primaryKeyFields = projectTable.GetPrimaryKeyFieldList();


                WriteLine("        public static DataTable CreateDataTable()");
                WriteLine("        {");
                WriteLine("            try");
                WriteLine("            {");
                WriteLine("                DataTable result = new DataTable();");
                WriteLine();
                for (int i = 0; i < fields.Count; i++)
                {
                    ProjectTabularField field = fields[i];
                    string propertyFieldName = field.PropertyFieldName;
                    string propertyDatabaseType = field.ReaderType.TrimEnd('?'); 
                    string dataColumnName = propertyFieldName + "DataColumn";
                    if ((field.AllowDBNull) && (TypeHelper.GetIsCSharpNullableType(field)))
                    {
                        if (propertyDatabaseType == "byte[]")
                        {
                            WriteLine("                DataColumn " + dataColumnName + " = new DataColumn( \"" + field.Name + "\", typeof(byte[]) );");
                        }
                        else if (field.PropertyType == "bool?")
                        {
                            WriteLine("                DataColumn " + dataColumnName + " = new DataColumn( \"" + field.Name + "\", typeof(string) );");
                            WriteLine("                " + dataColumnName + ".MaxLength = 1;");
                        }
                        else
                        {
                            WriteLine("                DataColumn " + dataColumnName + " = new DataColumn( \"" + field.Name + "\", typeof(" + propertyDatabaseType + ") );");
                        }
                    }
                    else
                    {
                        if (field.PropertyType == "bool")
                        {
                            WriteLine("                DataColumn " + dataColumnName + " = new DataColumn( \"" + field.Name + "\", typeof(string) );");
                            WriteLine("                " + dataColumnName + ".MaxLength = 1;");
                        }
                        else
                        {
                            WriteLine("                DataColumn " + dataColumnName + " = new DataColumn( \"" + field.Name + "\", typeof(" + propertyDatabaseType + ") );");
                        }
                    }
                    WriteLine("                " + dataColumnName + ".AllowDBNull = " + (field.AllowDBNull ? "true;" : "false;"));
                    WriteLine("                result.Columns.Add(" + dataColumnName + ");");

                }
                WriteLine();
                WriteLine("                DataColumn[] keys = new DataColumn[" + primaryKeyFields.Count + "];");
                for (int i = 0; i < primaryKeyFields.Count; i++)
                {
                    ProjectTabularField field = primaryKeyFields[i];
                    string propertyFieldName = field.PropertyFieldName;
                    string dataColumnName = propertyFieldName + "DataColumn";
                    WriteLine("                keys[" + i + "] = " + dataColumnName + ";");
                }
                WriteLine("                result.PrimaryKey = keys;");
                WriteLine();
                WriteLine("                return result;");
                WriteLine("            }");
                WriteLine("            catch (Exception exc)");
                WriteLine("            {");
                WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                WriteLine("                throw;");
                WriteLine("            }");
                WriteLine("        }");
                WriteLine();
                WriteLine("        public static DataTable CreateDataTable( List<"+dataClassName+"> elements )");
                WriteLine("        {");
                WriteLine("            try");
                WriteLine("            {");
                WriteLine("                DataTable result = CreateDataTable();");
                WriteLine("                foreach(" + dataClassName + " element in elements)");
                WriteLine("                {");
                for (int i = 0; i < fields.Count; i++)
                {
                    ProjectTabularField field = fields[i];
                    string propertyFieldName = field.PropertyFieldName;
                    string propertyName = field.PropertyName;
                    string propertyDatabaseType = field.ReaderType.TrimEnd('?');

                    

                    if (field.AllowDBNull)
                    {

                        WriteLine("                    object " + propertyFieldName + ";");
                        bool nullableCSharpType = TypeHelper.GetIsCSharpNullableType(field);
                        if (nullableCSharpType)
                        {
                            WriteLine("                    if( element."+ propertyName + ".HasValue )");
                            WriteLine("                    {");
                            if (propertyDatabaseType == "byte[]")
                            {
                                if (field.PropertyType == "sbyte?")
                                {
                                    WriteLine("                        " + propertyFieldName + " = new byte[] { unchecked( (byte)element." + propertyName + ".Value) };");
                                }
                                else if(field.PropertyType == "byte?")
                                {
                                    WriteLine("                        " + propertyFieldName + " = new byte[] { element." + propertyName + ".Value };");
                                }
                            }
                            else if (field.PropertyType == "bool?")
                            {
                                WriteLine("                        " + propertyFieldName + " = element." + propertyName + ".Value ? \"T\":\"F\";");
                            }
                            else
                            {
                                WriteLine("                        " + propertyFieldName + " = " + TypeHelper.GetCommandParameterConversion(field.PropertyType.TrimEnd('?'), field.ReaderType, "element." + propertyName + ".Value") + ";");
                            }
                            WriteLine("                    }");
                            WriteLine("                    else");
                            WriteLine("                    {");
                            WriteLine("                        " + propertyFieldName + " = null;");
                            WriteLine("                    }");
                        }
                        else
                        {
                            WriteLine("                    if( element." + propertyName + " != null )");
                            WriteLine("                    {");
                            WriteLine("                        " + propertyFieldName + " = " + TypeHelper.GetCommandParameterConversion(field.PropertyType, field.ReaderType, "element." + propertyName) + ";");
                            WriteLine("                    }");
                            WriteLine("                    else");
                            WriteLine("                    {");
                            WriteLine("                        " + propertyFieldName + " = null;");
                            WriteLine("                    }");
                        }
                    }
                    else
                    {
                        if (field.PropertyType == "bool")
                        {
                            WriteLine("                        object " + propertyFieldName + " = element." + propertyName + " ? \"T\":\"F\";");
                        }
                        else
                        {
                            WriteLine("                    object " + propertyFieldName + " = " + TypeHelper.GetCommandParameterConversion(field.PropertyType, field.ReaderType, "element." + propertyName) + ";");
                        }
                    }


                }
                string args = ColumnHelper.GetCommaSeparatedPropertyFieldNameList(fields);
                WriteLine("                    result.Rows.Add(" + args + " );");
                WriteLine("                }");
                WriteLine();
                WriteLine("                return result;");
                WriteLine("            }");
                WriteLine("            catch (Exception exc)");
                WriteLine("            {");
                WriteLine("                LogException(exc, MethodBase.GetCurrentMethod());");
                WriteLine("                throw;");
                WriteLine("            }");
                WriteLine("        }");
                WriteLine();
                
                WriteLine("        public static void WriteToServer( OracleConnection oracleConnection, string qualifiedDBName, DataTable dataTable )");
                WriteLine("        {");
                WriteLine("            try");
                WriteLine("            {");
                WriteLine("                using ( OracleBulkCopy bulkCopy = new OracleBulkCopy( oracleConnection ) )");
                WriteLine("                {");
                WriteLine("                    bulkCopy.DestinationTableName = qualifiedDBName;");
                WriteLine("                    bulkCopy.WriteToServer( dataTable );");
                WriteLine("                }");
                WriteLine("            }");
                WriteLine("            catch ( Exception exc )");
                WriteLine("            {");
                WriteLine("                LogException( exc, MethodBase.GetCurrentMethod( ) );");
                WriteLine("                throw;");
                WriteLine("            }");
                WriteLine("        }");
                WriteLine();

            }
        }



    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Sea Surveillance AS
Norway Norway
Chief Architect - Sea Surveillance AS.

Specializing in integrated operations and high performance computing solutions.

I’ve been fooling around with computers since the early eighties, I’ve even done work on CP/M and MP/M.

Wrote my first “real” program on a BBC micro model B based on a series in a magazine at that time. It was fun and I got hooked on this thing called programming ...

A few Highlights:

  • High performance application server development
  • Model Driven Architecture and Code generators
  • Real-Time Distributed Solutions
  • C, C++, C#, Java, TSQL, PL/SQL, Delphi, ActionScript, Perl, Rexx
  • Microsoft SQL Server, Oracle RDBMS, IBM DB2, PostGreSQL
  • AMQP, Apache qpid, RabbitMQ, Microsoft Message Queuing, IBM WebSphereMQ, Oracle TuxidoMQ
  • Oracle WebLogic, IBM WebSphere
  • Corba, COM, DCE, WCF
  • AspenTech InfoPlus.21(IP21), OsiSoft PI


More information about what I do for a living can be found at: harlinn.com or LinkedIn

You can contact me at espen@harlinn.no

Comments and Discussions