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