NTP Analyzer  0.8.2
Analyze the operation of time servers
Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T > Class Template Referenceabstract

Base class for OR/M mappers. Can be used for mapping objects stored in SQL databases. More...

Inheritance diagram for Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >:
Collaboration diagram for Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >:

Public Member Functions

void CheckTable ()
 
override IEnumerator< T > GetEnumerator ()
 Read all data from table in a sequential manner. More...
 
void Save (T item)
 

Protected Member Functions

 SqlDatabaseMapper (LogBase log)
 
void AddItem (T item)
 
void Close ()
 
IDbDataParameter CreateParameter (string name, object value)
 
virtual T FetchExternal (int id)
 
abstract void Insert (T item)
 
void Open ()
 
string PrepareInsertSql (string sql)
 
virtual string PrepareSql (string sql)
 
abstract void ReadContent ()
 
void RemoveItem (T item)
 
abstract void Update (T item)
 
- Protected Member Functions inherited from Ntp.Data.DataMapper< T >
 DataMapper (LogBase log)
 

Protected Attributes

IEnumerable< T > Content => items.Values
 
readonly object MapperLocker = new object()
 
IDataReader Reader
 

Properties

IDbCommand Command [get, private set]
 
abstract string CreateSql [get]
 
abstract string TableName [get]
 
this[int id] [get]
 
abstract bool UseCache [get]
 
- Properties inherited from Ntp.Data.DataMapper< T >
LogBase Log [get]
 

Private Member Functions

void FetchTable ()
 

Private Attributes

IDbConnection connection
 
readonly Dictionary< int, T > items = new Dictionary<int, T>()
 
bool tableFetched
 

Static Private Attributes

static readonly AutoResetEvent InsertEvent = new AutoResetEvent(true)
 

Detailed Description

Base class for OR/M mappers. Can be used for mapping objects stored in SQL databases.

Type Constraints
T :PersistentObject 

Definition at line 40 of file SqlDatabaseMapper.cs.

Constructor & Destructor Documentation

Definition at line 43 of file SqlDatabaseMapper.cs.

44  : base(log)
45  {
46  }

Member Function Documentation

void Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.AddItem ( item)
inlineprotected

Definition at line 150 of file SqlDatabaseMapper.cs.

151  {
152  lock (items)
153  {
154  items.Add(item.Id, item);
155  }
156  }
readonly Dictionary< int, T > items
void Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.CheckTable ( )
inline

Implements Ntp.Data.ITableInitializer.

Definition at line 87 of file SqlDatabaseMapper.cs.

Referenced by Ntp.Analyzer.Data.DatabaseInitializer.CreateTables().

88  {
90  bool exists;
91 
92  try
93  {
94  Open();
95  Command.CommandText = sql;
96  Log.SqlExecute(Command.CommandText);
97  Reader = Command.ExecuteReader();
98  exists = Reader.Read();
99  }
100  finally
101  {
102  Close();
103  }
104 
105  if (exists)
106  {
107  Log.TableExists(TableName);
108  return;
109  }
110 
111  Log.CreateTable(TableName);
112 
113  try
114  {
115  Open();
117  Log.SqlExecute(Command.CommandText);
118  Command.Prepare();
119  Command.ExecuteNonQuery();
120  }
121  finally
122  {
123  Close();
124  }
125  }
abstract string PrepareCreateTableSql(string sql)
static SqlDatabaseFactory Instance
abstract string PrepareCheckTableSql(string table)

Here is the caller graph for this function:

void Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.Close ( )
inlineprotected

Definition at line 158 of file SqlDatabaseMapper.cs.

159  {
160  connection?.Close();
161  connection = null;
162  InsertEvent.Set();
163  }
static readonly AutoResetEvent InsertEvent
IDbDataParameter Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.CreateParameter ( string  name,
object  value 
)
inlineprotected

Definition at line 165 of file SqlDatabaseMapper.cs.

166  {
167  return SqlDatabaseFactory.Instance.CreateParameter(name, value ?? DBNull.Value);
168  }
abstract IDbDataParameter CreateParameter(string name, object value)
static SqlDatabaseFactory Instance
virtual T Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.FetchExternal ( int  id)
inlineprotectedvirtual

Reimplemented in Ntp.Analyzer.Data.Sql.TimeServerDatabaseMapper.

Definition at line 170 of file SqlDatabaseMapper.cs.

171  {
172  return null;
173  }
void Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.FetchTable ( )
inlineprivate

Definition at line 224 of file SqlDatabaseMapper.cs.

225  {
226  lock (MapperLocker)
227  {
228  if (tableFetched)
229  return;
230 
231  ReadContent();
232  tableFetched = true;
233  }
234  }
override IEnumerator<T> Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.GetEnumerator ( )
inlinevirtual

Read all data from table in a sequential manner.

Returns
The enumerator.

Implements Ntp.Data.DataMapper< T >.

Definition at line 127 of file SqlDatabaseMapper.cs.

128  {
129  FetchTable();
130  return Content.ToList().GetEnumerator();
131  }
abstract void Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.Insert ( item)
protectedpure virtual
void Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.Open ( )
inlineprotected

Definition at line 177 of file SqlDatabaseMapper.cs.

178  {
179  InsertEvent.WaitOne();
180 
181  if (Reader != null && !Reader.IsClosed)
182  {
183  Reader.Close();
184  Reader = null;
185  }
186 
187  // TODO: Implement monitoring of database link
188  if (connection == null)
189  {
191  }
192 
193  if (connection.State == ConnectionState.Closed)
194  {
195  connection.Open();
196  }
197 
199  Command.Connection = connection;
200  }
abstract IDbCommand CreateCommand()
static SqlDatabaseFactory Instance
abstract IDbConnection CreateConnection()
static readonly AutoResetEvent InsertEvent
string Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.PrepareInsertSql ( string  sql)
inlineprotected

Definition at line 202 of file SqlDatabaseMapper.cs.

virtual string Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.PrepareSql ( string  sql)
inlineprotectedvirtual
void Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.RemoveItem ( item)
inlineprotected

Definition at line 214 of file SqlDatabaseMapper.cs.

215  {
216  lock (items)
217  {
218  items.Remove(item.Id);
219  }
220  }
readonly Dictionary< int, T > items
void Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.Save ( item)
inline

Definition at line 133 of file SqlDatabaseMapper.cs.

134  {
135  if (item.NewObject)
136  {
137  Insert(item);
138 
139  if (UseCache)
140  {
141  AddItem(item);
142  }
143  }
144  else
145  {
146  Update(item);
147  }
148  }
abstract void Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.Update ( item)
protectedpure virtual

Member Data Documentation

IDbConnection Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.connection
private

Definition at line 52 of file SqlDatabaseMapper.cs.

IEnumerable<T> Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.Content => items.Values
protected

Definition at line 56 of file SqlDatabaseMapper.cs.

readonly AutoResetEvent Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.InsertEvent = new AutoResetEvent(true)
staticprivate

Definition at line 49 of file SqlDatabaseMapper.cs.

readonly Dictionary<int, T> Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.items = new Dictionary<int, T>()
private

Definition at line 50 of file SqlDatabaseMapper.cs.

readonly object Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.MapperLocker = new object()
protected

Definition at line 51 of file SqlDatabaseMapper.cs.

IDataReader Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.Reader
protected

Definition at line 53 of file SqlDatabaseMapper.cs.

bool Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.tableFetched
private

Definition at line 54 of file SqlDatabaseMapper.cs.

Property Documentation

IDbCommand Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.Command
getprivate setprotected

Definition at line 58 of file SqlDatabaseMapper.cs.

abstract string Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.CreateSql
getprotected

Definition at line 62 of file SqlDatabaseMapper.cs.

abstract string Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.TableName
getprotected

Definition at line 60 of file SqlDatabaseMapper.cs.

T Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.this[int id]
get

Definition at line 67 of file SqlDatabaseMapper.cs.

abstract bool Ntp.Analyzer.Data.Sql.SqlDatabaseMapper< T >.UseCache
getprotected

Definition at line 64 of file SqlDatabaseMapper.cs.


The documentation for this class was generated from the following file: