Click here to Skip to main content
15,892,737 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 137.9K   5.1K   88  
Enhance productivity and reliability, write your own tools.
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using Harlinn.Common;
using Harlinn.DBTool.Projects;


namespace Harlinn.DBTool.CodeGenerators.Utils
{
    public class OracleSqlInsertHelper
    {
        private static readonly log4net.ILog sfLog = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private static void LogException(Exception exc, MethodBase method)
        {
            Logger.LogException(sfLog, exc, method);
        }


        public static string GenerateSQLInsertStatement(ProjectTable table, string indent)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                string result = string.Empty;

                List<ProjectTabularField> fields = table.GetFieldList();
                int fieldCount = fields.Count;

                List<ProjectTabularField> outputFields = new List<ProjectTabularField>();


                string databaseFields = ColumnHelper.GetCommaSeparatedFieldList(fields);
                sb.AppendLine("\"INSERT INTO {0}(" + databaseFields + ") \" +");

                sb.Append(indent + "\" VALUES(");

                for (int i = 0; i < fieldCount; i++)
                {
                    ProjectTabularField field = fields[i];
                    if (field.Concurrency)
                    {
                        sb.Append("0");
                    }
                    else if (string.IsNullOrWhiteSpace(field.Sequence) == false)
                    {
                        sb.Append(field.Sequence + ".NEXTVAL");
                        outputFields.Add(field);
                    }
                    else
                    {
                        sb.Append(field.DatabaseParameterName);
                    }
                    if (i < (fieldCount - 1))
                    {
                        sb.Append(", ");
                    }
                    else
                    {
                        sb.Append(")\"");
                        if (outputFields.Count > 0)
                        {
                            sb.AppendLine(" +");
                        }
                    }
                }

                if (outputFields.Count > 0)
                {
                    sb.Append(indent + "\" RETURNING ");
                    for (int i = 0; i < outputFields.Count; i++)
                    {
                        ProjectTabularField field = outputFields[i];

                        sb.Append(field.Name);

                        if (i < (outputFields.Count - 1))
                        {
                            sb.Append(", ");
                        }

                    }
                    sb.Append(" INTO ");
                    for (int i = 0; i < outputFields.Count; i++)
                    {
                        ProjectTabularField field = outputFields[i];

                        sb.Append(field.DatabaseParameterName);

                        if (i < (outputFields.Count - 1))
                        {
                            sb.Append(", ");
                        }
                        else
                        {
                            sb.Append("\"");
                        }
                    }
                }

                result = sb.ToString();

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

        public static string GetParameterDeclarationsForInsert(List<ProjectTabularField> columns, string indent)
        {
            try
            {
                bool hasNullableColumns = false;
                int columnCount = columns.Count;
                for (int i = 0; i < columnCount; i++)
                {
                    ProjectTabularField column = columns[i];
                    if (column.AllowDBNull)
                    {
                        hasNullableColumns = true;
                        break;
                    }
                }

                StringBuilder sb = new StringBuilder();

                sb.AppendLine();

                if (hasNullableColumns)
                {


                    for (int i = 0; i < columnCount; i++)
                    {
                        ProjectTabularField column = columns[i];
                        if (column.Concurrency == false)
                        {
                            bool nullableCSharpType = GeneratorTypeHelper.GetIsCSharpNullableType(column);
                            string argumentName = column.PropertyFieldName;
                            string sqlParameterName = argumentName + "Parameter";
                            string parameterConstantName = column.DatabaseParameterName;
                            string dataTypeConstantName = "OracleDbType." + column.ProviderType.ToString();

                            string elementPropertyAccessor = "element." + column.PropertyName;

                            if (column.AllowDBNull)
                            {

                                sb.AppendLine(indent + "OracleParameter " + sqlParameterName + " = oracleCommand.Parameters.Add(new OracleParameter( \"" + parameterConstantName + "\", " + dataTypeConstantName + " ));");
                                if (nullableCSharpType)
                                {
                                    sb.AppendLine(indent + "if( " + elementPropertyAccessor + ".HasValue )");
                                }
                                else
                                {
                                    sb.AppendLine(indent + "if( " + elementPropertyAccessor + " != null )");
                                }
                                sb.AppendLine(indent + "{");


                                if (string.IsNullOrWhiteSpace(column.Sequence))
                                {
                                    if (nullableCSharpType)
                                    {
                                        sb.AppendLine(indent + "    " + sqlParameterName + ".Value = " + GeneratorTypeHelper.GetCommandParameterConversion(column.PropertyType, column.ReaderType, elementPropertyAccessor + ".Value") + ";");
                                    }
                                    else
                                    {
                                        sb.AppendLine(indent + "    " + sqlParameterName + ".Value = " + GeneratorTypeHelper.GetCommandParameterConversion(column.PropertyType, column.ReaderType, elementPropertyAccessor) + ";");
                                    }
                                }
                                else
                                {
                                    sb.AppendLine(indent + "    " + sqlParameterName + ".Direction = ParameterDirection.Output;");
                                }

                                sb.AppendLine(indent + "}");
                                sb.AppendLine(indent + "else");
                                sb.AppendLine(indent + "{");
                                sb.AppendLine(indent + "    " + sqlParameterName + ".IsNullable = true;");
                                sb.AppendLine(indent + "    " + sqlParameterName + ".Value = null;");
                                sb.AppendLine(indent + "}");

                            }
                            else
                            {
                                sb.AppendLine(indent + "OracleParameter " + sqlParameterName + " = oracleCommand.Parameters.Add(new OracleParameter( \"" + parameterConstantName + "\", " + dataTypeConstantName + " ));");

                                if (string.IsNullOrWhiteSpace(column.Sequence))
                                {
                                    if (nullableCSharpType)
                                    {
                                        sb.AppendLine(indent + sqlParameterName + ".Value = " + GeneratorTypeHelper.GetCommandParameterConversion(column.PropertyType, column.ReaderType, elementPropertyAccessor + ".Value") + ";");
                                    }
                                    else
                                    {
                                        sb.AppendLine(indent + sqlParameterName + ".Value = " + GeneratorTypeHelper.GetCommandParameterConversion(column.PropertyType, column.ReaderType, elementPropertyAccessor) + ";");
                                    }
                                }
                                else
                                {
                                    sb.AppendLine(indent + sqlParameterName + ".Direction = ParameterDirection.Output;");
                                }
                            }
                            sb.AppendLine();
                        }
                    }

                }
                else
                {
                    for (int i = 0; i < columnCount; i++)
                    {
                        ProjectTabularField column = columns[i];
                        if (column.Concurrency == false)
                        {
                            bool nullableCSharpType = GeneratorTypeHelper.GetIsCSharpNullableType(column);
                            string argumentName = column.PropertyFieldName;
                            string sqlParameterName = argumentName + "Parameter";
                            string parameterConstantName = column.DatabaseParameterName;
                            string dataTypeConstantName = "OracleDbType." + column.ProviderType.ToString();
                            string elementPropertyAccessor = "element." + column.PropertyName;

                            sb.AppendLine(indent + "OracleParameter " + sqlParameterName + " = oracleCommand.Parameters.Add(new OracleParameter( \"" + parameterConstantName + "\", " + dataTypeConstantName + " ));");

                            if (string.IsNullOrWhiteSpace(column.Sequence))
                            {
                                if (nullableCSharpType)
                                {
                                    //sb.AppendLine(indent + sqlParameterName + ".Value = " + argumentName + ".Value;");
                                    sb.AppendLine(indent + sqlParameterName + ".Value = " + GeneratorTypeHelper.GetCommandParameterConversion(column.PropertyType, column.ReaderType, elementPropertyAccessor + ".Value") + ";");
                                }
                                else
                                {
                                    //sb.AppendLine(indent + sqlParameterName + ".Value = " + argumentName + ";");
                                    sb.AppendLine(indent + sqlParameterName + ".Value = " + GeneratorTypeHelper.GetCommandParameterConversion(column.PropertyType, column.ReaderType, elementPropertyAccessor) + ";");
                                }
                            }
                            else
                            {
                                sb.AppendLine(indent + sqlParameterName + ".Direction = ParameterDirection.Output;");
                            }
                            sb.AppendLine();
                        }
                    }
                }
                return sb.ToString();
            }
            catch (Exception exc)
            {
                LogException(exc, System.Reflection.MethodBase.GetCurrentMethod());
                throw;
            }
        }


    }
}

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