71 lines
2.4 KiB
C#
71 lines
2.4 KiB
C#
using Sarand.Net.Standard.Base.Dbas;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using Sarand.Net.Standard.Dbas.Data;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Sarand.BusinessMgr.Net.Standard.Dbas.Data
|
|
{
|
|
//=======================================================================
|
|
public abstract class SbmDbDataItem : DbDataItem
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Definitions.
|
|
/// <summary>
|
|
/// Database information - object supplied by main application.
|
|
/// <para>NB Application may have multiple databases with schema differences.</para>
|
|
/// <para>Each instance of a DbDataItem must be database specific.</para>
|
|
/// </summary>
|
|
public class SbmDatabaseInfo : IDatabaseInfo
|
|
{
|
|
// Data members.
|
|
private readonly List<DbDfnTable> tables;
|
|
private static readonly String defaultSchemaName = "dbo";
|
|
// Construction.
|
|
public SbmDatabaseInfo(String databaseName, List<DbDfnTable> tables)
|
|
{
|
|
this.DatabaseName = databaseName;
|
|
this.tables = tables;
|
|
}
|
|
// Properties.
|
|
public String DatabaseName { get; }
|
|
public String Context { get { return StringUtil.OnEmpty(DatabaseName, "UnknownDb"); } }
|
|
// Database schema.
|
|
public DbDfnTable GetTableDefinition(String schemaName, String tableName)
|
|
{
|
|
foreach (DbDfnTable table in tables.FindAll(item => item.Name.EqualsCI(tableName)))
|
|
{
|
|
if (StringUtil.OnEmpty(table.Schema, defaultSchemaName).EqualsCI(StringUtil.OnEmpty(schemaName, defaultSchemaName)))
|
|
return table;
|
|
}
|
|
return null;
|
|
}
|
|
public bool TableExists(String schemaName, String tableName)
|
|
{
|
|
return GetTableDefinition(schemaName, tableName) != null;
|
|
}
|
|
public bool ColumnExists(String schemaName, String tableName, String columnName)
|
|
{
|
|
DbDfnTable table = GetTableDefinition(schemaName, tableName);
|
|
if (table == null) return false;
|
|
return table.HasColumnByName(columnName);
|
|
}
|
|
public List<DbDfnColumn> GetColumnsForTable(String schemaName, String tableName)
|
|
{
|
|
DbDfnTable table = GetTableDefinition(schemaName, tableName);
|
|
if (table != null) return table.Columns;
|
|
return new List<DbDfnColumn>();
|
|
}
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
protected SbmDbDataItem(IDatabaseInfo dbInfo) : base(dbInfo)
|
|
{
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|