BusinessMgr 3.0.0.1
This commit is contained in:
@@ -170,6 +170,16 @@ namespace Sarand.Net.Standard.Dbas.Data
|
||||
public ColumnDataDateTimeNullable(String name, bool identity = false) : base(name, identity) { }
|
||||
public DateTime? Value { get; set; } = null;
|
||||
}
|
||||
public class ColumnDataGuid : ColumnData
|
||||
{
|
||||
public ColumnDataGuid(String name, bool identity = false) : base(name, identity) { }
|
||||
public Guid Value { get; set; } = default(Guid);
|
||||
}
|
||||
public class ColumnDataGuidNullable : ColumnData
|
||||
{
|
||||
public ColumnDataGuidNullable(String name, bool identity = false) : base(name, identity) { }
|
||||
public Guid Value { get; set; } = default(Guid);
|
||||
}
|
||||
public class ColumnDataByteArray : ColumnData
|
||||
{
|
||||
public ColumnDataByteArray(String name, bool identity = false) : base(name, identity) { }
|
||||
@@ -527,6 +537,23 @@ namespace Sarand.Net.Standard.Dbas.Data
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public Guid GetFieldGuid(DbDataReader dbDataReader, String columnName, Guid defaultValue)
|
||||
{
|
||||
int columnIdx = GetColumnOrdinal(dbDataReader, columnName);
|
||||
if (columnIdx < 0) return defaultValue;
|
||||
// Check for null value (to avoid throwing exception).
|
||||
if (!dbDataReader.IsDBNull(columnIdx))
|
||||
{
|
||||
Type dataType = dbDataReader.GetFieldType(columnIdx);
|
||||
// Expected data type.
|
||||
object data = dbDataReader[columnIdx];
|
||||
if (data is Guid)
|
||||
return (Guid)data;
|
||||
LogAlternativeTypeColumn(columnName, typeof(Byte[]), dataType);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public byte[] GetFieldByteArray(DbDataReader dbDataReader, String columnName, byte[] defaultValue)
|
||||
{
|
||||
int columnIdx = GetColumnOrdinal(dbDataReader, columnName);
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace Sarand.Net.Standard.Dbas.Data
|
||||
private class DataMembers
|
||||
{
|
||||
// Construction.
|
||||
public DataMembers (ISacsDatabase database, String query)
|
||||
public DataMembers(ISacsDatabase database, String query)
|
||||
{
|
||||
Database = database;
|
||||
Query = query;
|
||||
@@ -31,7 +31,7 @@ namespace Sarand.Net.Standard.Dbas.Data
|
||||
// Result set schema - to avoid keeping connection open.
|
||||
public String[] ColumnNames { get; set; }
|
||||
public String[] ColumnTypeNames { get; set; }
|
||||
public void PopulateColumns ()
|
||||
public void PopulateColumns()
|
||||
{
|
||||
int fieldCount = DbDataReader.FieldCount;
|
||||
ColumnNames = new String[fieldCount];
|
||||
@@ -48,7 +48,7 @@ namespace Sarand.Net.Standard.Dbas.Data
|
||||
//-------------------------------------------------------------------
|
||||
// Construction.
|
||||
[Obsolete("Constructor is deprecated. Use ExecuteQuery[Async] instead.")]
|
||||
public DbResultSet_DataReader (SacsDatabase database, String query)
|
||||
public DbResultSet_DataReader(SacsDatabase database, String query)
|
||||
{
|
||||
data = new DataMembers(database, query);
|
||||
String fn = GetType().Name + ".CTR";
|
||||
@@ -64,18 +64,18 @@ namespace Sarand.Net.Standard.Dbas.Data
|
||||
}
|
||||
}
|
||||
/// <summary>Private construction - created from ExecuteQuery{Async].</summary>
|
||||
private DbResultSet_DataReader (DataMembers data)
|
||||
private DbResultSet_DataReader(DataMembers data)
|
||||
{
|
||||
this.data = data;
|
||||
}
|
||||
protected override void Dispose (bool disposing)
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing) Close();
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Execute query.
|
||||
public static DbResultSet_DataReader ExecuteQuery (ISacsDatabase database, String query)
|
||||
public static DbResultSet_DataReader ExecuteQuery(ISacsDatabase database, String query)
|
||||
{
|
||||
DataMembers data = new DataMembers(database, query);
|
||||
String errorState = "";
|
||||
@@ -89,11 +89,11 @@ namespace Sarand.Net.Standard.Dbas.Data
|
||||
errorState = ExceptionUtil.GetErrorStateMessage(exc);
|
||||
SaLogger.Warn(SysUtil.Fn + " " + ExceptionUtil.GetLogMessage(exc, 6) + "\n\t Query: " + query, true);
|
||||
}
|
||||
DbResultSet_DataReader resultSet = new DbResultSet_DataReader (data);
|
||||
DbResultSet_DataReader resultSet = new DbResultSet_DataReader(data);
|
||||
resultSet.SetErrorState(errorState);
|
||||
return resultSet;
|
||||
}
|
||||
public static async Task<DbResultSet_DataReader> ExecuteQueryAsync (ISacsDatabase database, String query)
|
||||
public static async Task<DbResultSet_DataReader> ExecuteQueryAsync(ISacsDatabase database, String query)
|
||||
{
|
||||
DataMembers data = new DataMembers(database, query);
|
||||
String errorState = "";
|
||||
@@ -112,14 +112,14 @@ namespace Sarand.Net.Standard.Dbas.Data
|
||||
errorState = ExceptionUtil.GetErrorStateMessage(exc);
|
||||
SaLogger.Warn(SysUtil.Fn + " " + ExceptionUtil.GetLogMessage(exc, 6) + "\n\t Query: " + query, true);
|
||||
}
|
||||
DbResultSet_DataReader resultSet = new DbResultSet_DataReader (data);
|
||||
DbResultSet_DataReader resultSet = new DbResultSet_DataReader(data);
|
||||
resultSet.SetErrorState(errorState);
|
||||
return resultSet;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Close.
|
||||
public override void Close ()
|
||||
public override void Close()
|
||||
{
|
||||
// Dispose of database objects.
|
||||
if (data.DbDataReader != null && !data.DbDataReader.IsClosed) data.DbDataReader.Close();
|
||||
@@ -132,7 +132,7 @@ namespace Sarand.Net.Standard.Dbas.Data
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Execute query.
|
||||
private static void ExecuteQuery_Internal (DataMembers data)
|
||||
private static void ExecuteQuery_Internal(DataMembers data)
|
||||
{
|
||||
data.DbConnection = data.Database.CreateConnection();
|
||||
data.DbCommand = data.DbConnection.CreateCommand();
|
||||
@@ -144,7 +144,7 @@ namespace Sarand.Net.Standard.Dbas.Data
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Navigation.
|
||||
public override bool NextRecord ()
|
||||
public override bool NextRecord()
|
||||
{
|
||||
// Move to next result.
|
||||
bool nextRecord = false;
|
||||
@@ -179,97 +179,103 @@ namespace Sarand.Net.Standard.Dbas.Data
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public override int GetOrdinal (String columnName)
|
||||
public override int GetOrdinal(String columnName)
|
||||
{
|
||||
if (data.DbDataReader != null)
|
||||
return data.DbDataReader.GetOrdinal(columnName);
|
||||
return -1;
|
||||
}
|
||||
public override String GetDataTypeName (int columnIdx)
|
||||
public override String GetDataTypeName(int columnIdx)
|
||||
{
|
||||
if (data.ColumnTypeNames != null)
|
||||
return data.ColumnTypeNames[columnIdx];
|
||||
return String.Empty;
|
||||
}
|
||||
public override String GetName (int columnIdx)
|
||||
public override String GetName(int columnIdx)
|
||||
{
|
||||
if (data.ColumnNames != null)
|
||||
return data.ColumnNames[columnIdx];
|
||||
return String.Empty;
|
||||
}
|
||||
public override bool IsDbNull (int columnIdx)
|
||||
public override bool IsDbNull(int columnIdx)
|
||||
{
|
||||
if (data.DbDataReader != null && data.DbDataReader.IsDBNull(columnIdx))
|
||||
return true;
|
||||
object value = GetValue(columnIdx);
|
||||
return (value is System.DBNull);
|
||||
}
|
||||
public override object GetValue (int columnIdx)
|
||||
public override object GetValue(int columnIdx)
|
||||
{
|
||||
if (data.DbDataReader != null)
|
||||
return data.DbDataReader.GetValue(columnIdx);
|
||||
return false;
|
||||
}
|
||||
public override bool GetBoolean (int columnIdx)
|
||||
public override bool GetBoolean(int columnIdx)
|
||||
{
|
||||
if (data.DbDataReader != null)
|
||||
return data.DbDataReader.GetBoolean(columnIdx);
|
||||
return false;
|
||||
}
|
||||
public override byte GetByte (int columnIdx)
|
||||
public override byte GetByte(int columnIdx)
|
||||
{
|
||||
if (data.DbDataReader != null)
|
||||
return data.DbDataReader.GetByte(columnIdx);
|
||||
return 0;
|
||||
}
|
||||
public override short GetInt16 (int columnIdx)
|
||||
public override short GetInt16(int columnIdx)
|
||||
{
|
||||
if (data.DbDataReader != null)
|
||||
return data.DbDataReader.GetInt16(columnIdx);
|
||||
return 0;
|
||||
}
|
||||
public override int GetInt32 (int columnIdx)
|
||||
public override int GetInt32(int columnIdx)
|
||||
{
|
||||
if (data.DbDataReader != null)
|
||||
return data.DbDataReader.GetInt32(columnIdx);
|
||||
return 0;
|
||||
}
|
||||
public override long GetInt64 (int columnIdx)
|
||||
public override long GetInt64(int columnIdx)
|
||||
{
|
||||
if (data.DbDataReader != null)
|
||||
return data.DbDataReader.GetInt64(columnIdx);
|
||||
return 0;
|
||||
}
|
||||
public override float GetFloat (int columnIdx)
|
||||
public override float GetFloat(int columnIdx)
|
||||
{
|
||||
if (data.DbDataReader != null)
|
||||
return data.DbDataReader.GetFloat(columnIdx);
|
||||
return 0.0f;
|
||||
}
|
||||
public override double GetDouble (int columnIdx)
|
||||
public override double GetDouble(int columnIdx)
|
||||
{
|
||||
if (data.DbDataReader != null)
|
||||
return data.DbDataReader.GetDouble(columnIdx);
|
||||
return 0.0;
|
||||
}
|
||||
public override decimal GetDecimal (int columnIdx)
|
||||
public override decimal GetDecimal(int columnIdx)
|
||||
{
|
||||
if (data.DbDataReader != null)
|
||||
return data.DbDataReader.GetDecimal(columnIdx);
|
||||
return 0.0m;
|
||||
}
|
||||
public override String GetString (int columnIdx)
|
||||
public override String GetString(int columnIdx)
|
||||
{
|
||||
if (data.DbDataReader != null)
|
||||
return data.DbDataReader.GetString(columnIdx);
|
||||
return String.Empty;
|
||||
}
|
||||
public override DateTime GetDateTime (int columnIdx)
|
||||
public override DateTime GetDateTime(int columnIdx)
|
||||
{
|
||||
if (data.DbDataReader != null)
|
||||
return data.DbDataReader.GetDateTime(columnIdx);
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
public override Guid GetGuid(int columnIdx)
|
||||
{
|
||||
if (data.DbDataReader != null)
|
||||
return data.DbDataReader.GetGuid(columnIdx);
|
||||
return default(Guid);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace Sarand.Net.Standard.Dbas.Data
|
||||
private DataTableReader dataTableReader = null;
|
||||
//-------------------------------------------------------------------
|
||||
// Construction.
|
||||
public DbResultSet_DataTableReader (SacsDatabase database, String query)
|
||||
public DbResultSet_DataTableReader(SacsDatabase database, String query)
|
||||
{
|
||||
this.database = database;
|
||||
this.query = query;
|
||||
@@ -30,7 +30,7 @@ namespace Sarand.Net.Standard.Dbas.Data
|
||||
}
|
||||
//-------------------------------------------------------------------
|
||||
// Execute query.
|
||||
private void ExecuteQuery ()
|
||||
private void ExecuteQuery()
|
||||
{
|
||||
ClearErrorState();
|
||||
Close();
|
||||
@@ -53,11 +53,11 @@ namespace Sarand.Net.Standard.Dbas.Data
|
||||
}
|
||||
}
|
||||
//-------------------------------------------------------------------
|
||||
protected override void Dispose (bool disposing)
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
public override void Close ()
|
||||
public override void Close()
|
||||
{
|
||||
// Dispose of database objects.
|
||||
if (dataTableReader != null) dataTableReader.Close();
|
||||
@@ -72,7 +72,7 @@ namespace Sarand.Net.Standard.Dbas.Data
|
||||
}
|
||||
//-------------------------------------------------------------------
|
||||
// Navigation.
|
||||
public override bool NextRecord ()
|
||||
public override bool NextRecord()
|
||||
{
|
||||
// Move to next result.
|
||||
bool nextRecord = false;
|
||||
@@ -106,94 +106,101 @@ namespace Sarand.Net.Standard.Dbas.Data
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public override int GetOrdinal (String columnName)
|
||||
public override int GetOrdinal(String columnName)
|
||||
{
|
||||
if (dataTableReader != null)
|
||||
return dataTableReader.GetOrdinal(columnName);
|
||||
return -1;
|
||||
}
|
||||
public override String GetDataTypeName (int columnIdx)
|
||||
public override String GetDataTypeName(int columnIdx)
|
||||
{
|
||||
if (dataTableReader != null)
|
||||
return dataTableReader.GetDataTypeName(columnIdx);
|
||||
return String.Empty;
|
||||
}
|
||||
public override String GetName (int columnIdx)
|
||||
public override String GetName(int columnIdx)
|
||||
{
|
||||
if (dataTableReader != null)
|
||||
return dataTableReader.GetName(columnIdx);
|
||||
return String.Empty;
|
||||
}
|
||||
public override bool IsDbNull (int columnIdx)
|
||||
public override bool IsDbNull(int columnIdx)
|
||||
{
|
||||
return (GetValue(columnIdx) is System.DBNull);
|
||||
}
|
||||
public override object GetValue (int columnIdx)
|
||||
public override object GetValue(int columnIdx)
|
||||
{
|
||||
if (dataTableReader != null)
|
||||
return dataTableReader.GetValue(columnIdx);
|
||||
return false;
|
||||
}
|
||||
public override bool GetBoolean (int columnIdx)
|
||||
public override bool GetBoolean(int columnIdx)
|
||||
{
|
||||
if (dataTableReader != null)
|
||||
return dataTableReader.GetBoolean(columnIdx);
|
||||
return false;
|
||||
}
|
||||
public override byte GetByte (int columnIdx)
|
||||
public override byte GetByte(int columnIdx)
|
||||
{
|
||||
if (dataTableReader != null)
|
||||
return dataTableReader.GetByte(columnIdx);
|
||||
return 0;
|
||||
}
|
||||
public override short GetInt16 (int columnIdx)
|
||||
public override short GetInt16(int columnIdx)
|
||||
{
|
||||
if (dataTableReader != null)
|
||||
return dataTableReader.GetInt16(columnIdx);
|
||||
return 0;
|
||||
}
|
||||
public override int GetInt32 (int columnIdx)
|
||||
public override int GetInt32(int columnIdx)
|
||||
{
|
||||
if (dataTableReader != null)
|
||||
return dataTableReader.GetInt32(columnIdx);
|
||||
return 0;
|
||||
}
|
||||
public override long GetInt64 (int columnIdx)
|
||||
public override long GetInt64(int columnIdx)
|
||||
{
|
||||
if (dataTableReader != null)
|
||||
return dataTableReader.GetInt64(columnIdx);
|
||||
return 0;
|
||||
}
|
||||
public override float GetFloat (int columnIdx)
|
||||
public override float GetFloat(int columnIdx)
|
||||
{
|
||||
if (dataTableReader != null)
|
||||
return dataTableReader.GetFloat(columnIdx);
|
||||
return 0.0f;
|
||||
}
|
||||
public override double GetDouble (int columnIdx)
|
||||
public override double GetDouble(int columnIdx)
|
||||
{
|
||||
if (dataTableReader != null)
|
||||
return dataTableReader.GetDouble(columnIdx);
|
||||
return 0.0;
|
||||
}
|
||||
public override decimal GetDecimal (int columnIdx)
|
||||
public override decimal GetDecimal(int columnIdx)
|
||||
{
|
||||
if (dataTableReader != null)
|
||||
return dataTableReader.GetDecimal(columnIdx);
|
||||
return 0.0m;
|
||||
}
|
||||
public override String GetString (int columnIdx)
|
||||
public override String GetString(int columnIdx)
|
||||
{
|
||||
if (dataTableReader != null)
|
||||
return dataTableReader.GetString(columnIdx);
|
||||
return String.Empty;
|
||||
}
|
||||
public override DateTime GetDateTime (int columnIdx)
|
||||
public override DateTime GetDateTime(int columnIdx)
|
||||
{
|
||||
if (dataTableReader != null)
|
||||
return dataTableReader.GetDateTime(columnIdx);
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
public override Guid GetGuid(int columnIdx)
|
||||
{
|
||||
if (dataTableReader != null)
|
||||
return dataTableReader.GetGuid(columnIdx);
|
||||
return default(Guid);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
}
|
||||
//=======================================================================
|
||||
|
||||
@@ -21,7 +21,8 @@ namespace Sarand.Net.Standard.Dbas.Data
|
||||
//-------------------------------------------------------------------
|
||||
// Data members.
|
||||
private String sqlConnectionString = "";
|
||||
private readonly List<DbDfnTable> tables = new List<DbDfnTable>();
|
||||
private readonly Dictionary<String, DbDfnTable> tableDfns = new Dictionary<String, DbDfnTable>();
|
||||
private bool tableDfnsPopulated = false;
|
||||
public static readonly String DbCol_CreateOn = "CreateOn";
|
||||
public static readonly String DbCol_CreateBy = "CreateBy";
|
||||
public static readonly String DbCol_UpdateOn = "UpdateOn";
|
||||
@@ -350,38 +351,43 @@ namespace Sarand.Net.Standard.Dbas.Data
|
||||
//-------------------------------------------------------------------
|
||||
// NB Always use ODBC connection for querying schema.
|
||||
// eg. SQL Connection GetSchema('Indices') doesn't return index columns.
|
||||
public override List<String> GetTableNames(bool refresh = false)
|
||||
public override HashSet<String> GetTableNames(bool refresh = false)
|
||||
{
|
||||
lock (tables)
|
||||
lock (tableDfns)
|
||||
{
|
||||
// Only populate table names if not previously done.
|
||||
if (tables.Count < 1 || refresh)
|
||||
if (!tableDfnsPopulated || refresh)
|
||||
{
|
||||
tables.Clear();
|
||||
tableDfns.Clear();
|
||||
using (DbConnection dbConnection = CreateConnection(true))
|
||||
{
|
||||
dbConnection.Open();
|
||||
PopulateTableNames(dbConnection);
|
||||
tableDfns.AddRange(GetTableDefinitions(dbConnection).ToDictionary(table => table.Name, table => table));
|
||||
}
|
||||
}
|
||||
return tables.GetNames();
|
||||
return tableDfns.Values.GetNames().ToHashSet(StringComparer.InvariantCultureIgnoreCase);
|
||||
}
|
||||
}
|
||||
public override List<DbDfnTable> GetTableDfns(bool refresh = false)
|
||||
{
|
||||
lock (tables)
|
||||
lock (tableDfns)
|
||||
{
|
||||
if (refresh) ClearTableDfns();
|
||||
// Only populate table names and/or table schemas where not previously done.
|
||||
if (tables.Count < 1 || tables.Exists(item => !item.HasColumns()))
|
||||
if (!tableDfnsPopulated || tableDfns.Count < 1 || tableDfns.Values.Any(table => !table.HasColumns()))
|
||||
{
|
||||
// Use a single connection for table and schema population.
|
||||
using (DbConnection dbConnection = CreateConnection(true))
|
||||
{
|
||||
dbConnection.Open();
|
||||
if (tables.Count < 1)
|
||||
PopulateTableNames(dbConnection);
|
||||
foreach (DbDfnTable table in tables.ToList())
|
||||
if (!tableDfnsPopulated || tableDfns.Count < 1)
|
||||
{
|
||||
tableDfns.Clear();
|
||||
tableDfns.AddRange(GetTableDefinitions(dbConnection).ToDictionary(table => table.Name, table => table));
|
||||
}
|
||||
tableDfnsPopulated = true;
|
||||
// Populate details for each table.
|
||||
foreach (DbDfnTable table in tableDfns.Values)
|
||||
{
|
||||
if (!table.HasColumns())
|
||||
{
|
||||
@@ -391,32 +397,36 @@ namespace Sarand.Net.Standard.Dbas.Data
|
||||
}
|
||||
}
|
||||
}
|
||||
return tables.ToList();
|
||||
return new List<DbDfnTable>(tableDfns.Values);
|
||||
}
|
||||
}
|
||||
public void ClearTableDfns()
|
||||
{
|
||||
lock (tables)
|
||||
tables.Clear();
|
||||
lock (tableDfns)
|
||||
{
|
||||
tableDfns.Clear();
|
||||
tableDfnsPopulated = false;
|
||||
}
|
||||
}
|
||||
public override DbDfnTable GetTableDfn(String tableName, bool refresh = false)
|
||||
{
|
||||
lock (tables)
|
||||
lock (tableDfns)
|
||||
{
|
||||
if (!tables.GetNames().Contains(tableName) || refresh)
|
||||
if (!tableDfns.Keys.Contains(tableName) || refresh)
|
||||
{
|
||||
// Remove current table definition.
|
||||
tables.RemoveAll(item => item.Name.EqualsCI(tableName));
|
||||
tableDfns.Remove(tableName);
|
||||
}
|
||||
// Only populate the specified table if required.
|
||||
if (!tables.Exists(item => item.Name.EqualsCI(tableName) && item.Columns.Count > 0))
|
||||
DbDfnTable table = tableDfns.GetValueOrDefault(tableName);
|
||||
if (table == null || !table.HasColumns())
|
||||
{
|
||||
using (DbConnection dbConnection = CreateConnection(true))
|
||||
{
|
||||
dbConnection.Open();
|
||||
if (!tables.Exists(item => item.Name.EqualsCI(tableName)))
|
||||
PopulateTableNames(dbConnection, tableName);
|
||||
DbDfnTable table = tables.GetTableByName(tableName);
|
||||
if (!tableDfns.ContainsKey(tableName))
|
||||
tableDfns.AddRange(GetTableDefinitions(dbConnection, tableName).ToDictionary(table => table.Name, table => table));
|
||||
table = tableDfns.GetValueOrDefault(tableName);
|
||||
if (table != null && (!table.HasColumns() || refresh))
|
||||
{
|
||||
PopulateTableColumns(dbConnection, table);
|
||||
@@ -424,75 +434,69 @@ namespace Sarand.Net.Standard.Dbas.Data
|
||||
}
|
||||
}
|
||||
}
|
||||
return tables.GetTableByName(tableName);
|
||||
return tableDfns.GetValueOrDefault(tableName);
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
private void PopulateTableNames(DbConnection dbConnection, String tableNameRestriction = null)
|
||||
private static List<DbDfnTable> GetTableDefinitions(DbConnection dbConnection, String tableNameRestriction = null)
|
||||
{
|
||||
lock (tables)
|
||||
List<DbDfnTable> tables = new List<DbDfnTable>();
|
||||
if (SacsSystem.DebugMode) SaLogger.Debug(SysUtil.Fn + (!StringUtil.IsEmpty(tableNameRestriction) ? " Table: " + tableNameRestriction : " "));
|
||||
String[] restrictions = new String[] { null, null, tableNameRestriction }; // database, owner, table name, type
|
||||
using (DataTable tableSchema = dbConnection.GetSchema(OdbcMetaDataCollectionNames.Tables, restrictions))
|
||||
{
|
||||
if (StringUtil.IsEmpty(tableNameRestriction) && tables.Count > 0) return;
|
||||
if (!StringUtil.IsEmpty(tableNameRestriction) && tables.Exists(item => item.Name.Equals(tableNameRestriction))) return;
|
||||
if (SacsSystem.DebugMode) SaLogger.Debug(SysUtil.Fn + (!StringUtil.IsEmpty(tableNameRestriction) ? " Table: " + tableNameRestriction : " "));
|
||||
String[] restrictions = new String[] { null, null, tableNameRestriction }; // database, owner, table name, type
|
||||
using (DataTable tableSchema = dbConnection.GetSchema(OdbcMetaDataCollectionNames.Tables, restrictions))
|
||||
//DataSet set = tableSchema.DataSet;
|
||||
foreach (DataRow row in tableSchema.Rows)
|
||||
{
|
||||
//DataSet set = tableSchema.DataSet;
|
||||
foreach (DataRow row in tableSchema.Rows)
|
||||
//-----------------------------------------------------------
|
||||
// Columns are: Name Size Data Type Values
|
||||
// 1. TABLE_CAT 255 12 VarChar
|
||||
// 2. TABLE_SCHEM 128 12 VarChar
|
||||
// 3. TABLE_NAME 128 12 VarChar
|
||||
// 4. TABLE_TYPE 128 12 VarChar 'SYSTEM TABLE' 'TABLE' 'VIEW'
|
||||
// 'GLOBAL TEMPORARY' 'LOCAL TEMPORARY'
|
||||
// 'ALIAS' 'SYNONYM'
|
||||
// 5. REMARKS 254 12 VarChar
|
||||
// TABLE_CATALOG is the SQL alternative to TABLE_CAT
|
||||
// TABLE_QUALIFIER is the ODBC 2.0 alternative to TABLE_CAT (ODBC 3.0).
|
||||
// TABLE_SCHEMA is the SQL alternative to TABLE_SCHEM
|
||||
// TABLE_OWNER is the ODBC 2.0 alternative to TABLE_SCHEM (ODBC 3.0).
|
||||
// Variations:
|
||||
// Sage Line 50: TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, TABLE_TYPE, REMARKS.
|
||||
// Sybase Advantage: QUALIFIERNAME, OWNERNAME, TABLENAME, TYPENAME, REMARKS
|
||||
//-----------------------------------------------------------
|
||||
String database = String.Empty, schema = String.Empty, tableName = String.Empty, type = String.Empty, remarks = String.Empty;
|
||||
foreach (DataColumn column in tableSchema.Columns)
|
||||
{
|
||||
String colName = column.ColumnName;
|
||||
String value = row[colName].ToString();
|
||||
if (colName.EqualsCI("TABLE_CAT") || colName.EqualsCI("TABLE_CATALOG") || colName.EqualsCI("TABLE_QUALIFIER") || colName.EqualsCI("QualifierName"))
|
||||
database = value;
|
||||
else if (colName.EqualsCI("TABLE_SCHEM") || colName.EqualsCI("TABLE_SCHEMA") || colName.EqualsCI("TABLE_OWNER") || colName.EqualsCI("OwnerName"))
|
||||
schema = value;
|
||||
else if (colName.EqualsCI("TABLE_NAME") || colName.EqualsCI("TableName"))
|
||||
tableName = value;
|
||||
else if (colName.EqualsCI("TABLE_TYPE") || colName.EqualsCI("TypeName"))
|
||||
type = value;
|
||||
else if (colName.EqualsCI("REMARKS"))
|
||||
remarks = value;
|
||||
else
|
||||
SaLogger.Warn($"{SysUtil.Fn} Unrecognised column name: '{colName}'");
|
||||
}
|
||||
// TODO Why is Type sometimes "BASE TABLE"?
|
||||
if (!StringUtil.IsEmpty(tableName) && (type.EqualsCI("Table") || type.EqualsCI("Base Table")) && (!schema.EqualsCI("sys") || !tableName.StartsWithCI("trace_xe_")))
|
||||
{
|
||||
//-----------------------------------------------------------
|
||||
// Columns are: Name Size Data Type Values
|
||||
// 1. TABLE_CAT 255 12 VarChar
|
||||
// 2. TABLE_SCHEM 128 12 VarChar
|
||||
// 3. TABLE_NAME 128 12 VarChar
|
||||
// 4. TABLE_TYPE 128 12 VarChar 'SYSTEM TABLE' 'TABLE' 'VIEW'
|
||||
// 'GLOBAL TEMPORARY' 'LOCAL TEMPORARY'
|
||||
// 'ALIAS' 'SYNONYM'
|
||||
// 5. REMARKS 254 12 VarChar
|
||||
// TABLE_CATALOG is the SQL alternative to TABLE_CAT
|
||||
// TABLE_QUALIFIER is the ODBC 2.0 alternative to TABLE_CAT (ODBC 3.0).
|
||||
// TABLE_SCHEMA is the SQL alternative to TABLE_SCHEM
|
||||
// TABLE_OWNER is the ODBC 2.0 alternative to TABLE_SCHEM (ODBC 3.0).
|
||||
// Variations:
|
||||
// Sage Line 50: TABLE_QUALIFIER, TABLE_OWNER, TABLE_NAME, TABLE_TYPE, REMARKS.
|
||||
// Sybase Advantage: QUALIFIERNAME, OWNERNAME, TABLENAME, TYPENAME, REMARKS
|
||||
//-----------------------------------------------------------
|
||||
String database = String.Empty, schema = String.Empty, tableName = String.Empty, type = String.Empty, remarks = String.Empty;
|
||||
foreach (DataColumn column in tableSchema.Columns)
|
||||
{
|
||||
String colName = column.ColumnName;
|
||||
String value = row[colName].ToString();
|
||||
if (colName.EqualsCI("TABLE_CAT") || colName.EqualsCI("TABLE_CATALOG") || colName.EqualsCI("TABLE_QUALIFIER") || colName.EqualsCI("QualifierName"))
|
||||
database = value;
|
||||
else if (colName.EqualsCI("TABLE_SCHEM") || colName.EqualsCI("TABLE_SCHEMA") || colName.EqualsCI("TABLE_OWNER") || colName.EqualsCI("OwnerName"))
|
||||
schema = value;
|
||||
else if (colName.EqualsCI("TABLE_NAME") || colName.EqualsCI("TableName"))
|
||||
tableName = value;
|
||||
else if (colName.EqualsCI("TABLE_TYPE") || colName.EqualsCI("TypeName"))
|
||||
type = value;
|
||||
else if (colName.EqualsCI("REMARKS"))
|
||||
remarks = value;
|
||||
else
|
||||
SaLogger.Warn($"{SysUtil.Fn} Unrecognised column name: '{colName}'");
|
||||
}
|
||||
// TODO Why is Type sometimes "BASE TABLE"?
|
||||
if (!StringUtil.IsEmpty(tableName) && (type.EqualsCI("Table") || type.EqualsCI("Base Table")) && (!schema.EqualsCI("sys") || !tableName.Left(9).EqualsCI("trace_xe_")))
|
||||
{
|
||||
#if DEBUG
|
||||
//SaLogger.Trace($"{SysUtil.Fn} Database={database}, Schema={schema}, Table={tableName}, Type={type}");
|
||||
//SaLogger.Trace($"{SysUtil.Fn} Database={database}, Schema={schema}, Table={tableName}, Type={type}");
|
||||
#endif
|
||||
tables.Add(new DbDfnTable(schema, tableName, remarks));
|
||||
}
|
||||
tables.Add(new DbDfnTable(schema, tableName, remarks));
|
||||
}
|
||||
}
|
||||
List<DbDfnTable> tablesUnsorted = tables.ToList();
|
||||
if (SacsSystem.DebugMode && tablesUnsorted.Exists(item => item == null))
|
||||
Debugger.Break();
|
||||
tables.Clear();
|
||||
tables.AddRange(tablesUnsorted.OrderBy(item => item.Name).ToList());
|
||||
}
|
||||
if (SacsSystem.DebugMode && tables.Exists(item => item == null))
|
||||
Debugger.Break();
|
||||
return tables.OrderBy(item => item.Name).ToList();
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user