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

namespace Harlinn.Oracle.DBTool.Utils.DB
{
    public class SequenceReader : Reader
    {
        private static readonly log4net.ILog sfLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

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

        public const string FULL_SELECT = "SELECT SEQUENCE_OWNER, SEQUENCE_NAME, MIN_VALUE, MAX_VALUE, INCREMENT_BY, CYCLE_FLAG, ORDER_FLAG, CACHE_SIZE, LAST_NUMBER FROM ALL_SEQUENCES";

        public const int SEQUENCE_OWNER = 0;
        public const int SEQUENCE_NAME = 1;
        public const int MIN_VALUE  = 2;
        public const int MAX_VALUE  = 3;
        public const int INCREMENT_BY  = 4;
        public const int CYCLE_FLAG  = 5;
        public const int ORDER_FLAG  = 6;
        public const int CACHE_SIZE  = 7;
        public const int LAST_NUMBER = 8;


        public SequenceReader( )
            : base(CreateReader())
        {
            
        }

        public SequenceReader(string sequenceOwner)
            : base(CreateReader(sequenceOwner))
        {

        }


        public SequenceReader(string sequenceOwner, string sequenceName)
            : base(CreateReader(sequenceOwner, sequenceName))
        {

        }


        public SequenceReader(OracleDataReader dataReader)
            : base(dataReader)
        {

        }

        private static OracleDataReader CreateReader()
        {
            try
            {
                OracleConnection connection = ConnectionManager.Connection;
                OracleCommand command = connection.CreateCommand();
                using (command)
                {
                    command.CommandText = FULL_SELECT + " ORDER BY SEQUENCE_OWNER, SEQUENCE_NAME";
                    OracleDataReader result = command.ExecuteReader(CommandBehavior.SingleResult);
                    return result;
                }
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
                throw;
            }
        }

        private static OracleDataReader CreateReader(string sequenceOwner)
        {
            try
            {
                OracleConnection connection = ConnectionManager.Connection;
                OracleCommand command = connection.CreateCommand();
                using (command)
                {
                    command.CommandText = FULL_SELECT + " WHERE SEQUENCE_OWNER = :sequenceOwner ORDER BY SEQUENCE_NAME";
                    command.Parameters.Add(":sequenceOwner", OracleDbType.Varchar2).Value = sequenceOwner;
                    OracleDataReader result = command.ExecuteReader(CommandBehavior.SingleResult);
                    return result;
                }
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
                throw;
            }
        }


        private static OracleDataReader CreateReader(string sequenceOwner, string sequenceName)
        {
            try
            {
                OracleConnection connection = ConnectionManager.Connection;
                OracleCommand command = connection.CreateCommand();
                using (command)
                {
                    command.CommandText = FULL_SELECT + " WHERE SEQUENCE_OWNER = :sequenceOwner AND SEQUENCE_NAME = :sequenceName";
                    command.Parameters.Add(":sequenceOwner", OracleDbType.Varchar2).Value = sequenceOwner;
                    command.Parameters.Add(":sequenceName", OracleDbType.Varchar2).Value = sequenceName;
                    OracleDataReader result = command.ExecuteReader(CommandBehavior.SingleResult);
                    return result;
                }
            }
            catch (Exception exc)
            {
                LogException(exc, MethodBase.GetCurrentMethod());
                throw;
            }
        }

        public string SequenceOwner
        {
            get
            {
                try
                {
                    string result = GetString(SEQUENCE_OWNER);
                    return result;
                }
                catch (Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
        }

        public string Name
        {
            get
            {
                try
                {
                    string result = GetString(SEQUENCE_NAME);
                    return result;
                }
                catch (Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
        }

        public decimal MinValue
        {
            get
            {
                try
                {
                    decimal result = GetDecimal(MIN_VALUE);
                    return result;
                }
                catch (Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
        }

        public decimal MaxValue
        {
            get
            {
                try
                {
                    decimal result = GetDecimal(MAX_VALUE);
                    return result;
                }
                catch (Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
        }

        public decimal IncrementBy
        {
            get
            {
                try
                {
                    decimal result = GetDecimal(INCREMENT_BY);
                    return result;
                }
                catch (Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
        }

        public bool Cycle
        {
            get
            {
                try
                {
                    bool result = GetString(CYCLE_FLAG) == "Y";
                    return result;
                }
                catch (Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
        }

        public bool Order
        {
            get
            {
                try
                {
                    bool result = GetString(ORDER_FLAG) == "Y";
                    return result;
                }
                catch (Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
        }

        public decimal CacheSize
        {
            get
            {
                try
                {
                    decimal result = GetDecimal(CACHE_SIZE);
                    return result;
                }
                catch (Exception exc)
                {
                    LogException(exc, MethodBase.GetCurrentMethod());
                    throw;
                }
            }
        }

        public decimal LastNumber
        {
            get
            {
                try
                {
                    
                    decimal result = GetDecimal(LAST_NUMBER);
                    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