using System;
using System.Data;
using System.Reflection;
using Harlinn.Common;
using Oracle.DataAccess.Client;
namespace Harlinn.DBTool.DataSources.Oracle.DB
{
public class ConstraintColumnsReader : 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 OWNER,CONSTRAINT_NAME,TABLE_NAME,COLUMN_NAME,POSITION FROM ALL_CONS_COLUMNS";
public const int OWNER = 0;
public const int CONSTRAINT_NAME = 1;
public const int TABLE_NAME = 2;
public const int COLUMN_NAME = 3;
public const int POSITION = 4;
public ConstraintColumnsReader ( )
: base( CreateReader() )
{
}
public ConstraintColumnsReader ( OracleDataReader reader )
: base( reader )
{
}
public ConstraintColumnsReader(string schemaName, string tableName, string constraintName)
: base(CreateReader(schemaName,tableName, constraintName))
{
}
private static OracleDataReader CreateReader()
{
try
{
OracleConnection connection = ConnectionManager.Connection;
OracleCommand command = connection.CreateCommand();
using (command)
{
command.CommandText = FULL_SELECT;
OracleDataReader result = command.ExecuteReader(CommandBehavior.SingleResult);
return result;
}
}
catch (Exception exc)
{
LogException(exc, MethodBase.GetCurrentMethod());
throw;
}
}
private static OracleDataReader CreateReader(string schemaName,string tableName, string constraintName)
{
try
{
OracleConnection connection = ConnectionManager.Connection;
OracleCommand command = connection.CreateCommand();
using (command)
{
command.CommandText = FULL_SELECT + " WHERE OWNER = :schemaName AND CONSTRAINT_NAME = :constraintName AND TABLE_NAME = :tableName";
command.Parameters.Add(":schemaName", OracleDbType.Varchar2).Value = schemaName;
command.Parameters.Add(":constraintName", OracleDbType.Varchar2).Value = constraintName;
command.Parameters.Add(":tableName", OracleDbType.Varchar2).Value = tableName;
OracleDataReader result = command.ExecuteReader(CommandBehavior.SingleResult);
return result;
}
}
catch (Exception exc)
{
LogException(exc, MethodBase.GetCurrentMethod());
throw;
}
}
public string Owner
{
get
{
try
{
string result = GetString(OWNER);
return result;
}
catch(Exception exc)
{
LogException(exc, MethodBase.GetCurrentMethod());
throw;
}
}
}
public string ConstraintName
{
get
{
try
{
string result = GetString(CONSTRAINT_NAME);
return result;
}
catch(Exception exc)
{
LogException(exc, MethodBase.GetCurrentMethod());
throw;
}
}
}
public string TableName
{
get
{
try
{
string result = GetString(TABLE_NAME);
return result;
}
catch(Exception exc)
{
LogException(exc, MethodBase.GetCurrentMethod());
throw;
}
}
}
public string ColumnName
{
get
{
try
{
if(IsDBNull(COLUMN_NAME) == false)
{
string result = GetString(COLUMN_NAME);
return result;
}
return null;
}
catch(Exception exc)
{
LogException(exc, MethodBase.GetCurrentMethod());
throw;
}
}
}
public decimal? Position
{
get
{
try
{
if(IsDBNull(POSITION) == false)
{
decimal? result = GetDecimal(POSITION);
return result;
}
return null;
}
catch(Exception exc)
{
LogException(exc, MethodBase.GetCurrentMethod());
throw;
}
}
}
}
}