23 using System.Collections.Generic;
25 using System.Diagnostics.CodeAnalysis;
35 namespace Ntp.Analyzer.Data.Sql
49 private static readonly AutoResetEvent InsertEvent =
new AutoResetEvent(
true);
50 private readonly Dictionary<int, T> items =
new Dictionary<int, T>();
51 protected readonly
object MapperLocker =
new object();
56 protected IEnumerable<T> Content => items.Values;
58 protected IDbCommand Command {
get;
private set; }
60 protected abstract string TableName {
get; }
62 protected abstract string CreateSql {
get; }
64 protected abstract bool UseCache {
get; }
79 item = items.ContainsKey(
id) ? items[id] : null;
82 return item ?? FetchExternal(
id);
86 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
95 Command.CommandText = sql;
96 Log.SqlExecute(Command.CommandText);
97 Reader = Command.ExecuteReader();
98 exists = Reader.Read();
107 Log.TableExists(TableName);
111 Log.CreateTable(TableName);
117 Log.SqlExecute(Command.CommandText);
119 Command.ExecuteNonQuery();
130 return Content.ToList().GetEnumerator();
154 items.Add(item.Id, item);
175 protected abstract void Insert(T item);
179 InsertEvent.WaitOne();
181 if (Reader != null && !Reader.IsClosed)
188 if (connection == null)
193 if (connection.State == ConnectionState.Closed)
199 Command.Connection = connection;
212 protected abstract void ReadContent();
218 items.Remove(item.Id);
222 protected abstract void Update(T item);
abstract IDbDataParameter CreateParameter(string name, object value)
abstract IDbCommand CreateCommand()
string PrepareInsertSql(string sql)
abstract string PrepareInsertSql(string sql)
Base class for ORM mappers. Can be used for mapping object stored in files, databases, etc.
const string DatabaseCacheError
abstract string PrepareSql(string sql)
override IEnumerator< T > GetEnumerator()
Read all data from table in a sequential manner.
abstract string PrepareCreateTableSql(string sql)
static SqlDatabaseFactory Instance
abstract string PrepareCheckTableSql(string table)
abstract IDbConnection CreateConnection()
Base class for OR/M mappers. Can be used for mapping objects stored in SQL databases.
virtual string PrepareSql(string sql)
virtual T FetchExternal(int id)
IDbDataParameter CreateParameter(string name, object value)
SqlDatabaseMapper(LogBase log)