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;
}
}
}
}
}