Click here to Skip to main content
15,886,258 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.2K   5.1K   88  
Enhance productivity and reliability, write your own tools.
using System;
using System.Data;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using Oracle.DataAccess.Client;


using Harlinn.Oracle.DBTool.Example.Common;
using Harlinn.Oracle.DBTool.Example.Types;

namespace Harlinn.Oracle.DBTool.Example.DB
{
    public partial class FloatValueElementReader : Reader
    {

        private static readonly log4net.ILog sfLog = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        private static void LogException(Exception exc, MethodBase method)
        {
            Harlinn.Oracle.DBTool.Example.Common.Logger.LogException(sfLog, exc, method);
        }

        public const string DEFAULT_QUALIFIED_DBNAME = "HARLINNDATA.FLOATVALUES";
        public const string FULL_SELECT = "SELECT TS,FLAGS,VALUE FROM {0}";
        public const string KEY_FIELDS = "TS";

        public const int TS = 0;
        public const int FLAGS = 1;
        public const int VALUE = 2;

        private long tag;

        public FloatValueElementReader ( )
            : base( CreateReader( DEFAULT_QUALIFIED_DBNAME ) )
        {
        }

        public FloatValueElementReader ( string qualifiedDBName )
            : base( CreateReader( qualifiedDBName ) )
        {
        }

        public FloatValueElementReader ( OracleConnection oracleConnection )
            : base( CreateReader( oracleConnection ) )
        {
        }

        public FloatValueElementReader ( OracleConnection oracleConnection, string qualifiedDBName )
            : base( CreateReader( oracleConnection, qualifiedDBName ) )
        {
        }

        public FloatValueElementReader ( OracleDataReader reader )
            : base( reader )
        {
        }





        private static OracleDataReader CreateReader( string qualifiedDBName )
        {
            try
            {
                OracleConnection oracleConnection = GetConnection();
                OracleDataReader result = CreateReader(oracleConnection,qualifiedDBName);
                return result;
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
                throw;
            }
        }


        private static OracleDataReader CreateReader( OracleConnection oracleConnection )
        {
            try
            {
                OracleDataReader result = CreateReader(oracleConnection,DEFAULT_QUALIFIED_DBNAME);
                return result;
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
                throw;
            }
        }


        private static OracleDataReader CreateReader( OracleConnection oracleConnection, string qualifiedDBName )
        {
            try
            {
                string sql = string.Format(FULL_SELECT, qualifiedDBName) + " ORDER BY " + KEY_FIELDS;
                OracleCommand oracleCommand = oracleConnection.CreateCommand();
                using (oracleCommand)
                {
                    oracleCommand.CommandText = sql;
                    OracleDataReader result = oracleCommand.ExecuteReader(CommandBehavior.SingleResult);
                    return result;
                }
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
                throw;
            }
        }

        public static FloatValueElementReader CreateReaderByTimeStamp(  DateTime ts)
        {
            FloatValueElementReader result = CreateReaderByTimeStamp( GetConnection(), DEFAULT_QUALIFIED_DBNAME,ts);
            return result;
        }

        public static FloatValueElementReader CreateReaderByTimeStamp( OracleConnection oracleConnection , DateTime ts)
        {
            FloatValueElementReader result = CreateReaderByTimeStamp( oracleConnection, DEFAULT_QUALIFIED_DBNAME,ts);
            return result;
        }

        public static FloatValueElementReader CreateReaderByTimeStamp( string qualifiedDBName,  DateTime ts)
        {
            FloatValueElementReader result = CreateReaderByTimeStamp( GetConnection(), qualifiedDBName,ts);
            return result;
        }

        public static FloatValueElementReader CreateReaderByTimeStamp( OracleConnection oracleConnection ,string qualifiedDBName,  DateTime ts)
        {
            try
            {
                string fullSelect = string.Format(FULL_SELECT, qualifiedDBName);
                OracleCommand oracleCommand = oracleConnection.CreateCommand();
                using (oracleCommand)
                {
                    oracleCommand.BindByName = true;
                    string queryFilter = " WHERE TS = :ts";
                    string selectStatement = fullSelect + queryFilter;
                    oracleCommand.CommandText = selectStatement;


                    OracleParameter tsParameter = oracleCommand.Parameters.Add(new OracleParameter( ":ts", OracleDbType.Decimal ));
                    tsParameter.Value = ts.ToUniversalTime().Ticks;

                    OracleDataReader result = oracleCommand.ExecuteReader(CommandBehavior.SingleResult);
                    return new FloatValueElementReader( result );
                }
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
                throw;
            }
        }


        public static FloatValueElementReader CreateFloatValueForTimeStamp( OracleConnection oracleConnection, string qualifiedDBName, DateTime ts )
        {
            try
            {
                string fullSelect = string.Format(FULL_SELECT, qualifiedDBName);
                string queryFilter = string.Format(" WHERE TS = (SELECT MAX(TS) FROM {0} WHERE TS <= :timeStamp_ )", qualifiedDBName);
                OracleCommand oracleCommand = oracleConnection.CreateCommand();
                using (oracleCommand)
                {
                    oracleCommand.BindByName = true;
                    string selectStatement = fullSelect + queryFilter;
                    oracleCommand.CommandText = selectStatement;

                    oracleCommand.Parameters.Add(":timeStamp_", OracleDbType.Decimal).Value = ts.ToUniversalTime().Ticks;

                    OracleDataReader result = oracleCommand.ExecuteReader(CommandBehavior.SingleResult);
                    return new FloatValueElementReader( result );
                }
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
                throw;
            }
        }

        public static FloatValueElementReader CreateFloatValueForLastTimeStamp( OracleConnection oracleConnection, string qualifiedDBName )
        {
            try
            {
                string fullSelect = string.Format(FULL_SELECT, qualifiedDBName);
                string queryFilter = string.Format(" WHERE TS = ( SELECT MAX(TS) FROM {0} )", qualifiedDBName);
                OracleCommand oracleCommand = oracleConnection.CreateCommand();
                using (oracleCommand)
                {
                    oracleCommand.BindByName = true;
                    string selectStatement = fullSelect + queryFilter;
                    oracleCommand.CommandText = selectStatement;

                    OracleDataReader result = oracleCommand.ExecuteReader(CommandBehavior.SingleResult);
                    return new FloatValueElementReader( result );
                }
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
                throw;
            }
        }

        public static FloatValueElementReader CreateFloatValueForInterval( OracleConnection oracleConnection, string qualifiedDBName, DateTime startOfInterval, DateTime endOfInterval )
        {
            try
            {
                string fullSelect = string.Format(FULL_SELECT, qualifiedDBName);
                string queryFilter = string.Format(" WHERE TS BETWEEN ( SELECT MAX(TS) FROM {0} WHERE TS <= :startTimeStamp ) AND ( SELECT MAX(TS) FROM {0} WHERE TS <= :endTimeStamp )", qualifiedDBName) + " ORDER BY " + KEY_FIELDS;
                OracleCommand oracleCommand = oracleConnection.CreateCommand();
                using (oracleCommand)
                {
                    oracleCommand.BindByName = true;
                    string selectStatement = fullSelect + queryFilter;
                    oracleCommand.CommandText = selectStatement;

                    oracleCommand.Parameters.Add(":startTimeStamp", OracleDbType.Decimal).Value = startOfInterval.ToUniversalTime().Ticks;
                    oracleCommand.Parameters.Add(":endTimeStamp", OracleDbType.Decimal).Value = endOfInterval.ToUniversalTime().Ticks;

                    OracleDataReader result = oracleCommand.ExecuteReader(CommandBehavior.SingleResult);
                    return new FloatValueElementReader( result );
                }
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
                throw;
            }
        }



        public long Tag
        {
            get
            {
                return tag;
            }
            set
            {
                this.tag = value;
            }
        }



        public DateTime TimeStamp
        {
            get
            {
                try
                {
                    DateTime result = new DateTime(Convert.ToInt64(GetDecimal(TS)),DateTimeKind.Utc);
                    return result;
                }
                catch(Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
        }


        public long Flags
        {
            get
            {
                try
                {
                    long result = Convert.ToInt64(GetDecimal(FLAGS));
                    return result;
                }
                catch(Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
        }


        public float? Value
        {
            get
            {
                try
                {
                    if(IsDBNull(VALUE) == false)
                    {
                        float? result = GetFloat(VALUE);
                        return result;
                    }
                    return null;
                }
                catch(Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
        }


        public FloatValueElementData FloatValue
        {
            get
            {
                try
                {
                    FloatValueElementData result = new FloatValueElementData( Tag,TimeStamp,Flags,Value );
                    return result;
                }
                catch(Exception exc)
                {
                    LogException(exc, 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