Click here to Skip to main content
15,886,801 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 136.3K   5.1K   88  
Enhance productivity and reliability, write your own tools.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

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

namespace Harlinn.Oracle.DBTool.Generators.Types
{
    public class TypeGenerator : GeneratorBase
    {
        ProjectTabular tabular;

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


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

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

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



        public void Generate()
        {
            string baseName = tabular.GetBaseName();
            string commonNamespace = Project.Current.CommonNamespace;
            string dataNamespace = Project.Current.DataNamespace;       
            string className = tabular.GetDataTypeClassName();

            string abstract_ = "";

            if (tabular is ProjectTable)
            {
                ProjectTable projectTable = (ProjectTable)tabular;
                if (projectTable.IsTagTable)
                {
                    abstract_ = "abstract ";
                }
            }

            WriteLine("using System;");
            WriteLine("using System.ComponentModel;");
            WriteLine("using System.Collections.ObjectModel;");
            WriteLine("using System.Runtime.Serialization;");
            WriteLine();
            WriteLine("using "+dataNamespace+".Utils;");
            WriteLine();
            WriteLine("namespace " + dataNamespace);  

            WriteLine("{");
            WriteLine("    [DataContract(Namespace = Constants.Namespace)]");
            WriteLine("    [Serializable]");
            if (tabular is ProjectTable)
            {
                ProjectTable projectTable = (ProjectTable)tabular;
                if (projectTable.IsTagTable)
                {
                    List<ProjectTable> tagValueTables = Project.Current.GetProjectTagTypeList();
                    foreach (ProjectTable tagValueTable in tagValueTables)
                    {
                        string tagClassName = tagValueTable.BaseName.Replace("Value", "") + "ElementData";
                        WriteLine("    [KnownType(typeof(" + tagClassName + "))]");
                    }
                }
            }
            WriteLine("    public " + abstract_ + "class " + className + " : ElementBase");
            WriteLine("    {");
            WriteLine();

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


            if (tabular is ProjectTable)
            {
                ProjectTable projectTable = (ProjectTable)tabular;
                if (projectTable.IsTagValue)
                {
                    WriteLine("        private long tag;");
                }
            }
            for(int i = 0; i < fields.Count; i++ )
            {
                ProjectTabularField field = fields[i];

                string typeName = field.PropertyType;
                string fieldName = field.PropertyFieldName;

                WriteLine("        private " + typeName + " " + fieldName + ";");

            }

            if (tabular is ProjectTable)
            {
                ProjectTable projectTable = (ProjectTable)tabular;
                ProjectTabularField concurrencyField = projectTable.GetConcurrencyField();
                if (concurrencyField != null)
                {
                    WriteLine("        private ElementState " + projectTable.ElementStatefieldName + ";");
                    WriteLine("        private " + className + " concurrencyConflictElement;");
                }
            }


            WriteLine();
            WriteLine("        public " + className + "( )");
            WriteLine("        {");
            WriteLine("        }");
            WriteLine();

            WriteLine();
            string argumentDeclaration = ColumnHelper.GetArgumentDeclarationList(fields);
            WriteLine("        public " + className + "( " + argumentDeclaration + " )");
            WriteLine("        {");

            for (int i = 0; i < fields.Count; i++)
            {
                ProjectTabularField field = fields[i];

                string fieldName = field.PropertyFieldName;

                WriteLine("            this." + fieldName + " = " + fieldName + ";");

            }

            WriteLine("        }");
            WriteLine();

            if (baseName == "Tag")
            {
                WriteLine("        public abstract TagType TagType");
                WriteLine("        {");
                WriteLine("            get;");
                WriteLine("        }");
                WriteLine();

                string argumentDecl = ColumnHelper.GetArgumentDeclarationList(fields);
                string arguments = ColumnHelper.GetCommaSeparatedPropertyFieldNameList(fields);

                //WriteLine("        public static TagElementData Create( long id, string name, long item, long type, string storage, long optimisticLock, string units, long? enumeration, string description, string comments )");
                WriteLine("        public static TagElementData Create( " + argumentDecl + " )");
                WriteLine("        {");
                WriteLine("            TagElementData result = null;");
                WriteLine("            TagType tagType = (TagType)type;");
                WriteLine("            switch(tagType)");
                WriteLine("            {");

                List<ProjectTable> tagValueTables = Project.Current.GetProjectTagTypeList();
                foreach (ProjectTable tagValueTable in tagValueTables)
                {
                    string subClassName = tagValueTable.BaseName.Replace("Value", "") + "ElementData";
                    WriteLine("                case TagType." + tagValueTable.BaseName + ":");
                    //WriteLine("                    result = new " + subClassName + "(id, name, item, type, storage, optimisticLock, units, enumeration, description, comments);");
                    WriteLine("                    result = new " + subClassName + "(" + arguments + ");");
                    WriteLine("                    break;");
                }

                WriteLine("            }");
                WriteLine("            return result;");
                WriteLine("        }");
                WriteLine();
            }

            if (tabular is ProjectTable)
            {
                ProjectTable projectTable = (ProjectTable)tabular;
                if (projectTable.IsTagValue)
                {
                    WriteLine("        public " + className + "( long tag," + argumentDeclaration + " )");
                    WriteLine("        {");

                    WriteLine("            this.tag = tag;");
                    for (int i = 0; i < fields.Count; i++)
                    {
                        ProjectTabularField field = fields[i];

                        string fieldName = field.PropertyFieldName;

                        WriteLine("            this." + fieldName + " = " + fieldName + ";");

                    }

                    WriteLine("        }");
                    WriteLine();
                }
            }

            WriteLine("        public override ElementType ElementType");
            WriteLine("        {");
            WriteLine("            get");
            WriteLine("            {");
            WriteLine("                return ElementType." + tabular.GetBaseName()+";");
            WriteLine("            }");
            WriteLine("        }");

            WriteLine();
            WriteLine( );
            WriteLine("        public override void AssignTo(ElementBase destination)");
            WriteLine("        {");
            WriteLine("            " + className + " destinationElement = (" + className + ")destination;");
            if (tabular is ProjectTable)
            {
                ProjectTable projectTable = (ProjectTable)tabular;
                if (projectTable.IsTagValue)
                {
                    WriteLine("            destinationElement.tag = this.tag;");
                }
            }
            for (int i = 0; i < fields.Count; i++)
            {
                ProjectTabularField field = fields[i];
                string propertyType = field.PropertyType;
                string propertyName = field.PropertyName;
                string propertyFieldName = field.PropertyFieldName;

                if(propertyType == "byte[]")
                {
                    WriteLine("            if(this."+propertyFieldName+" != null)");
                    WriteLine("            {");
                    WriteLine("                destinationElement." + propertyFieldName + " = (byte[])this." + propertyFieldName + ".Clone();");
                    WriteLine("            }");
                    WriteLine("            else");
                    WriteLine("            {");
                    WriteLine("                destinationElement." + propertyFieldName + " = null;");
                    WriteLine("            }");
                }
                else
                {
                    WriteLine("            destinationElement." + propertyFieldName + " = this." + propertyFieldName + ";");
                }
            }
            

            WriteLine("        }");
            WriteLine();
            WriteLine();
            WriteLine("        public override int CompareTo(ElementBase other)");
            WriteLine("        {");
            WriteLine("            " + className + " otherElement = (" + className + ")other;");
            for (int i = 0; i < fields.Count; i++)
            {
                ProjectTabularField field = fields[i];
                string propertyType = field.PropertyType;
                string propertyName = field.PropertyName;
                string propertyFieldName = field.PropertyFieldName;

                if (i == 0)
                {
                    WriteLine("            int result = CompareHelper.Compare( otherElement." + propertyFieldName + " , this." + propertyFieldName + ");");
                }
                else
                {
                    WriteLine("            result = CompareHelper.Compare( otherElement." + propertyFieldName + " , this." + propertyFieldName + ");");
                }
                if (i < (fields.Count - 1))
                {
                    WriteLine("            if( result != 0)");
                    WriteLine("            {");
                    WriteLine("                return result;");
                    WriteLine("            }");
                }
            }
            WriteLine("            return result;");
            WriteLine("        }");
            WriteLine();
            WriteLine();

            if (tabular is ProjectTable)
            {
                ProjectTable projectTable = (ProjectTable)tabular;
                ProjectTabularField concurrencyField = projectTable.GetConcurrencyField();
                if (concurrencyField != null)
                {
                    WriteLine("        [DataMember(EmitDefaultValue=false)]");
                    WriteLine("        public ElementState " + projectTable.ElementStatePropertyName);
                    WriteLine("        {");
                    WriteLine("            get");
                    WriteLine("            {");
                    WriteLine("                return " + projectTable.ElementStatefieldName + ";");
                    WriteLine("            }");
                    WriteLine("            set");
                    WriteLine("            {");
                    WriteLine("                this." + projectTable.ElementStatefieldName + " = value;");
                    WriteLine("            }");
                    WriteLine("        }");
                    WriteLine();
                    WriteLine("        [DataMember(EmitDefaultValue=false)]");
                    WriteLine("        public " + className + " ConcurrencyConflictElement");
                    WriteLine("        {");
                    WriteLine("            get");
                    WriteLine("            {");
                    WriteLine("                return concurrencyConflictElement;");
                    WriteLine("            }");
                    WriteLine("            set");
                    WriteLine("            {");
                    WriteLine("                this.concurrencyConflictElement = value;");
                    WriteLine("            }");
                    WriteLine("        }");
                    WriteLine();
                }
                if (projectTable.IsTagValue)
                {
                    WriteLine("        [DataMember(EmitDefaultValue=false)]");
                    WriteLine("        public long Tag");
                    WriteLine("        {");
                    WriteLine("            get");
                    WriteLine("            {");
                    WriteLine("                return tag;");
                    WriteLine("            }");
                    WriteLine("            set");
                    WriteLine("            {");
                    WriteLine("                this.tag = value;");
                    WriteLine("            }");
                    WriteLine("        }");
                    WriteLine();
                }
            }
            WriteLine();

            for (int i = 0; i < fields.Count; i++)
            {
                ProjectTabularField field = fields[i];
                string propertyType = field.PropertyType;
                string propertyName = field.PropertyName;
                string propertyFieldName = field.PropertyFieldName;

                if (string.IsNullOrWhiteSpace(field.Comments) == false)
                {
                    WriteLine("        [Description(\"" + field.Comments + "\")]");
                }

                if (string.IsNullOrWhiteSpace(field.DisplayName) == false)
                {
                    WriteLine("        [DisplayName(\"" + field.DisplayName + "\")]");
                }

                WriteLine("        [DataMember(EmitDefaultValue=false)]");
                WriteLine("        public " + propertyType + " " + propertyName);
                WriteLine("        {");
                WriteLine("            get");
                WriteLine("            {");
                WriteLine("                return " + propertyFieldName + ";");
                WriteLine("            }");
                WriteLine("            set");
                WriteLine("            {");
                WriteLine("                this." + propertyFieldName + " = value;");
                WriteLine("            }");
                WriteLine("        }");
                WriteLine();
                WriteLine();

            }



            WriteLine("    }");
            WriteLine();
            if (tabular is ProjectTable)
            {
                
                ProjectTable projectTable = (ProjectTable)tabular;
                ProjectTabularPrimaryKey primaryKey = projectTable.GetPrimaryKey();
                if ((primaryKey != null) && (projectTable.IsTagValue == false))
                {
                    
                    List<ProjectTabularField> primaryKeyFields = projectTable.GetPrimaryKeyFieldList();
                    if (primaryKeyFields.Count == 1)
                    {
                        ProjectTabularField primaryKeyField = primaryKeyFields[0];
                        WriteLine();
                        string propertyType = primaryKeyField.PropertyType;
                        string propertyName = primaryKeyField.PropertyName;
                        WriteLine("    public class Keyed"+baseName+"Collection : KeyedCollection<"+propertyType+","+className+">");
                        WriteLine("    {");
                        WriteLine("        protected override "+propertyType+" GetKeyForItem("+className+" item)");
                        WriteLine("        {");
                        WriteLine("            return item." + propertyName + ";");
                        WriteLine("        }");
                        WriteLine("    }");
                    }
                }

                WriteLine();
            }

            WriteLine("}");
        }
    }

    public class MyKeyedCollection : KeyedCollection<string,Exception>
    {

        protected override string  GetKeyForItem(Exception item)
        {
 	        throw new NotImplementedException();
        }
    }
    
}

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