Click here to Skip to main content
15,885,309 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 135.8K   5.1K   88  
Enhance productivity and reliability, write your own tools.
using System;
using System.Collections.Generic;
using System.Text;
using Harlinn.DBTool.CodeGenerators.Utils;
using Harlinn.DBTool.Projects;

namespace Harlinn.DBTool.CodeGenerators.ServiceImplementation
{
    public class DataProviderInterfaceGenerator : GeneratorBase
    {
        private Project project;
        private string interfaceName = "IDataProvider";
        SortedSet<string> methodNames = new SortedSet<string>();


        public DataProviderInterfaceGenerator(Project project)
        {
            this.project = project;    
        }


        Project Project
        {
            get
            {
                return project;
            }
        }

        public override string GetFilename()
        {
            
            string directory = GetDirectory();

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

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


        public void Generate()
        {
            string dataNamespace = Project.DataNamespace;

            WriteLine("using System;");
            WriteLine("using System.Collections.Generic;");
            WriteLine();
            WriteLine("namespace " + dataNamespace);
            WriteLine("{");
            WriteLine();
            WriteLine("    public interface " + interfaceName);
            WriteLine("    {");
            WriteLine();

            Process();

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


            WriteLine("}");
            WriteLine();

        }

        private void Process( )
        {
            Process(project.GetProjectTables());
            WriteLine("        Interval GetTagValueInterval( long tag );");
            WriteLine();
            /*
            WriteLine("        NodeElementData GetNodeByPath(string nodePath);");
            WriteLine("        ItemElementData GetItemByPath(string itemPath);");
            WriteLine("        TagElementData GetTagByPath(string tagPath);");
             */ 
            WriteLine();
        }

        private void Process(ProjectTables projectTables)
        {
            foreach (ProjectTable projectTable in projectTables.Children)
            {
                Process(projectTable);
                WriteLine();
            }
        }

        private void Process(ProjectTable projectTable)
        {
            WriteLine("    // =========================================================================");
            WriteLine("    // Type : " + projectTable.GetBaseName());
            WriteLine("    // Table : " + projectTable.Name);
            WriteLine("    // =========================================================================");
            GenerateGetMethods(projectTable);

            GenerateSaveMethod(projectTable);
            GenerateInsertMethod(projectTable);
            GenerateUpdateMethod(projectTable);
            GenerateDeleteMethod(projectTable);

        }


        public void GenerateGetMethods(ProjectTable projectTable)
        {
            GenerateGetAllMethod(projectTable);

            GenerateGetByPrimaryKeyMethod(projectTable);

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


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

            GenerateTagValueGetMethods(projectTable);

        }

        private void GenerateGetAllMethod(ProjectTable projectTable)
        {
            string tagArg = "";
            if (projectTable.IsTagValue)
            {
                tagArg = " long tag ";
            }
            string baseName = projectTable.GetBaseName();
            string dataClassName = projectTable.GetDataTypeClassName();
            string methodName = "GetAll" + baseName + "s";
            WriteLine("        List<" + dataClassName + "> " + methodName + "(" + tagArg + ");");
        }
        private void GenerateGetByPrimaryKeyMethod(ProjectTable projectTable)
        {
            string tagArg = "";
            if (projectTable.IsTagValue)
            {
                tagArg = " long tag,";
            }
            string dataClassName = projectTable.GetDataTypeClassName();
            ProjectTabularPrimaryKey primaryKey = projectTable.GetPrimaryKey();
            List<ProjectTabularField> fields = primaryKey.GetFieldList();
            string methodName = MethodNamesHelper.GetServiceGetByMethodName(primaryKey);
            string argDecl = ColumnHelper.GetArgumentDeclarationList(fields);

            WriteLine("        " + dataClassName + " " + methodName + "(" + tagArg + argDecl + " );");
        }

        private void GenerateGetByIndexMethod(ProjectTable projectTable, ProjectTabularIndex index)
        {
            string tagArg = "";
            if (projectTable.IsTagValue)
            {
                tagArg = " long tag,";
            }
            string dataClassName = projectTable.GetDataTypeClassName();

            List<ProjectTabularField> fields = index.GetFieldList();
            string argDecl = ColumnHelper.GetArgumentDeclarationList(fields);
            string methodName = MethodNamesHelper.GetServiceGetByMethodName(index);

            WriteLine("        " + dataClassName + " " + methodName + "(" + tagArg + argDecl + " );");

            if ((projectTable.IsTimeSeries) && (projectTable.IsTagValue == false))
            {
                methodName = MethodNamesHelper.GetServiceGetByForTimeStampMethodName(index);
                WriteLine("        " + dataClassName + " " + methodName + "(" + tagArg + argDecl + " );");
            }
        }
        private void GenerateGetByReferenceMethod(ProjectTable projectTable, ProjectTabularReference reference)
        {
            string tagArg = "";
            if (projectTable.IsTagValue)
            {
                tagArg = " long tag,";
            }
            string baseName = projectTable.GetBaseName();
            string accessorClassName = projectTable.GetDataAccessClassName();
            string dataClassName = projectTable.GetDataTypeClassName();
            string readerClassName = projectTable.GetReaderClassName();

            List<ProjectTabularField> fields = reference.GetFieldList();
            string argDecl = ColumnHelper.GetArgumentDeclarationList(fields);
            string args = ColumnHelper.GetCommaSeparatedPropertyFieldNameList(fields);
            string methodName = MethodNamesHelper.GetServiceGetByMethodName(reference);
            string accessorMethodName = MethodNamesHelper.GetManagerGetByMethodName(reference);

            WriteLine("        List<" + dataClassName + "> " + methodName + "(" + tagArg + argDecl + " );");

        }


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

                if (projectTable.IsTagValue)
                {
                    string baseName = projectTable.GetBaseName();
                    string accessorClassName = projectTable.GetDataAccessClassName();
                    string dataClassName = projectTable.GetDataTypeClassName();

                    //string accessorMethodName = "Get" + tabular.GetBaseName() + "ForTimeStamp";
                    string methodName = "Get" + tabular.GetBaseName() + "ForTimeStamp";
                    WriteLine("        " + dataClassName + " " + methodName + "( long tag, DateTime ts );");

                    methodName = "Get" + tabular.GetBaseName() + "ForLastTimeStamp";
                    WriteLine("        " + dataClassName + " " + methodName + "( long tag );");


                    //accessorMethodName = "Get" + tabular.GetBaseName() + "ForInterval";
                    methodName = "Get" + tabular.GetBaseName() + "ForInterval";

                    WriteLine("        List<" + dataClassName + "> " + methodName + "( long tag, DateTime startOfInterval, DateTime endOfInterval );");
                }
            }

        }



        private void GenerateSaveMethod(ProjectTable projectTable)
        {
            ProjectTabularField concurrencyField = projectTable.GetConcurrencyField();
            if (concurrencyField == null)
            {
                return;
            }

            string baseName = projectTable.GetBaseName();
            string dataClassName = projectTable.GetDataTypeClassName();

            string methodName = "Save" + baseName;

            WriteLine("        " + dataClassName + " " + methodName + "( Guid clientId, " + dataClassName + " element );");
        }

        private void GenerateInsertMethod(ProjectTable projectTable)
        {
            string baseName = projectTable.GetBaseName();
            string accessorClassName = projectTable.GetDataAccessClassName();
            string dataClassName = projectTable.GetDataTypeClassName();
            string readerClassName = projectTable.GetReaderClassName();

            string methodName = "Insert" + baseName;

            WriteLine("        " + dataClassName + " " + methodName + "( Guid clientId, " + dataClassName + " element );");
            WriteLine("        void " + methodName + "List( Guid clientId, List<" + dataClassName + "> elements );");
        }

        private void GenerateUpdateMethod(ProjectTable projectTable)
        {
            string baseName = projectTable.GetBaseName();
            string accessorClassName = projectTable.GetDataAccessClassName();
            string dataClassName = projectTable.GetDataTypeClassName();
            string readerClassName = projectTable.GetReaderClassName();

            string methodName = "Update" + baseName;

            WriteLine("        " + dataClassName + " " + methodName + "( Guid clientId, " + dataClassName + " element );");
        }


        private void GenerateDeleteMethod(ProjectTable projectTable)
        {
            string tagArg = "";
            if (projectTable.IsTagValue)
            {
                tagArg = " long tag,";
            }
            string baseName = projectTable.GetBaseName();
            List<ProjectTabularField> fields = projectTable.GetPrimaryKeyFieldList();
            ProjectTabularField concurrencyField = projectTable.GetConcurrencyField();
            if (concurrencyField != null)
            {
                fields.Add(concurrencyField);
            }

            string argDecl = ColumnHelper.GetArgumentDeclarationList(fields);
            string methodName = "Delete" + baseName;

            WriteLine("        int " + methodName + "( Guid clientId, " + tagArg + argDecl + " );");
        }


    }
}

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