177 lines
5.4 KiB
C#
177 lines
5.4 KiB
C#
using System;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.App;
|
|
|
|
namespace Sarand.Net.Standard.Base.Dbas
|
|
{
|
|
//=======================================================================
|
|
public abstract class DbResultSet : SaErrorStateItem, IDbResultSet
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
protected DbResultSet ()
|
|
{
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Navigation.
|
|
public abstract bool NextRecord ();
|
|
public abstract void Close ();
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
public abstract int FieldCount { get; }
|
|
public bool IsValidColumn (int columnIdx)
|
|
{
|
|
return (columnIdx >= 0 && columnIdx < FieldCount);
|
|
}
|
|
public abstract bool HasRows { get; }
|
|
|
|
//-------------------------------------------------------------------
|
|
// Field Values.
|
|
public abstract int GetOrdinal (String columnName);
|
|
public abstract String GetDataTypeName (int columnIdx);
|
|
public abstract String GetName (int columnIdx);
|
|
public abstract bool IsDbNull (int columnIdx);
|
|
public abstract Object GetValue (int columnIdx);
|
|
public abstract bool GetBoolean (int columnIdx);
|
|
public abstract byte GetByte (int columnIdx);
|
|
public abstract short GetInt16 (int columnIdx);
|
|
public abstract int GetInt32 (int columnIdx);
|
|
public abstract long GetInt64 (int columnIdx);
|
|
public abstract float GetFloat (int columnIdx);
|
|
public abstract double GetDouble (int columnIdx);
|
|
public abstract decimal GetDecimal (int columnIdx);
|
|
public abstract String GetString (int columnIdx);
|
|
public abstract DateTime GetDateTime (int columnIdx);
|
|
|
|
//-------------------------------------------------------------------
|
|
// Get Field Value.
|
|
public DataValue GetFieldValue (String columnName)
|
|
{
|
|
String fn = GetType().Name+".GetFieldValue('" + columnName + "')";
|
|
DataValue dataValue = null;
|
|
try
|
|
{
|
|
dataValue = GetFieldValue(GetOrdinal(columnName));
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SaLogger.Warn(fn + " Exception: " + exc.Message);
|
|
}
|
|
return dataValue;
|
|
}
|
|
#if DEBUG
|
|
private static bool handlingGetFieldValueException = false;
|
|
#endif
|
|
public DataValue GetFieldValue (int columnIdx)
|
|
{
|
|
String fn = GetType().Name+".GetFieldValue(" + columnIdx + ")";
|
|
if (IsDbNull(columnIdx)) return null;
|
|
String columnDataType = "";
|
|
DataValue dataValue = null;
|
|
try
|
|
{
|
|
columnDataType = GetDataTypeName(columnIdx);
|
|
switch (columnDataType)
|
|
{
|
|
// Catering for c# (DataTableReader) and sql (DbDataReader) type names
|
|
case "bit":
|
|
case "Bool":
|
|
dataValue = new DataValue(GetBoolean(columnIdx));
|
|
break;
|
|
case "tinyint":
|
|
dataValue = new DataValue((long)GetByte(columnIdx));
|
|
break;
|
|
case "smallint":
|
|
case "Int16":
|
|
dataValue = new DataValue((long)GetInt16(columnIdx));
|
|
break;
|
|
case "int":
|
|
case "int identity":
|
|
case "Int32":
|
|
dataValue = new DataValue((long)GetInt32(columnIdx));
|
|
break;
|
|
case "bigint":
|
|
case "Int64":
|
|
dataValue = new DataValue((long)GetInt64(columnIdx));
|
|
break;
|
|
case "real":
|
|
case "float":
|
|
case "Float":
|
|
case "Single":
|
|
case "Double":
|
|
dataValue = new DataValue(GetDouble(columnIdx));
|
|
break;
|
|
case "decimal":
|
|
case "Decimal":
|
|
case "numeric":
|
|
case "Numeric":
|
|
dataValue = new DataValue(GetDecimal(columnIdx));
|
|
break;
|
|
case "char":
|
|
case "varchar":
|
|
case "nvarchar":
|
|
case "String":
|
|
dataValue = new DataValue(GetString(columnIdx));
|
|
break;
|
|
case "datetime":
|
|
case "DateTime":
|
|
case "smalldatetime":
|
|
case "SmallDateTime":
|
|
dataValue = new DataValue(GetDateTime(columnIdx));
|
|
break;
|
|
default:
|
|
SaLogger.Warn(fn + " Unrecognised data type '" + columnDataType + "' for column '" + GetName(columnIdx) + "'");
|
|
break;
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SaLogger.Warn(fn + " Data type: "+ columnDataType + ". Exception: " + exc.Message /*+ "\n" + exc.ToString()*/);
|
|
#if DEBUG
|
|
if (!handlingGetFieldValueException)
|
|
{
|
|
// Reinvoke for debugging.
|
|
handlingGetFieldValueException = true;
|
|
DataValue value = GetFieldValue(columnIdx);
|
|
handlingGetFieldValueException = false;
|
|
}
|
|
#endif
|
|
}
|
|
return dataValue;
|
|
}
|
|
// Get Record Values.
|
|
public NamedValue.Map GetRecordValues ()
|
|
{
|
|
NamedValue.Map values = null;
|
|
values = new NamedValue.Map();
|
|
for (int columnIdx = 0; columnIdx < FieldCount; columnIdx++)
|
|
{
|
|
String columnName = GetName(columnIdx);
|
|
DataValue dataValue = GetFieldValue(columnIdx);
|
|
if (dataValue != null)
|
|
values.AddValue(columnName, dataValue);
|
|
}
|
|
return values;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Logging.
|
|
public static SaMessageLevel LoggingLevel { get; set; } = SaMessageLevel.None;
|
|
public static String LoggingPrefix { get; set; } = " ~~~ ";
|
|
protected static void Log (String fn, String message, SaMessageLevel level = SaMessageLevel.Debug)
|
|
{
|
|
if ((int)level > (int)LoggingLevel) return;
|
|
SaLogger.Log((fn + "\n\t" + message).Trim(),level);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|