NTP Analyzer  0.8.2
Analyze the operation of time servers
Ntp.Data.Schema.DatabaseUpdater Class Reference
Inheritance diagram for Ntp.Data.Schema.DatabaseUpdater:
Collaboration diagram for Ntp.Data.Schema.DatabaseUpdater:

Public Member Functions

 DatabaseUpdater (ISqlFactory factory, LogBase log)
 
void Add (IVersionChanges item)
 
void ApplyChanges ()
 
bool CheckLatestVersion ()
 
IEnumerator< IVersionChangesGetEnumerator ()
 

Private Member Functions

IEnumerator IEnumerable. GetEnumerator ()
 

Private Attributes

readonly List< IVersionChangeschanges
 
readonly ISqlFactory factory
 
readonly LogBase log
 

Detailed Description

Definition at line 33 of file DatabaseUpdater.cs.

Constructor & Destructor Documentation

Ntp.Data.Schema.DatabaseUpdater.DatabaseUpdater ( ISqlFactory  factory,
LogBase  log 
)
inline

Definition at line 35 of file DatabaseUpdater.cs.

References factory.

36  {
37  this.factory = factory;
38  this.log = log;
39  changes = new List<IVersionChanges>();
40  }
readonly ISqlFactory factory
readonly List< IVersionChanges > changes

Member Function Documentation

void Ntp.Data.Schema.DatabaseUpdater.Add ( IVersionChanges  item)
inline

Definition at line 56 of file DatabaseUpdater.cs.

57  {
58  changes.Add(item);
59  }
readonly List< IVersionChanges > changes
void Ntp.Data.Schema.DatabaseUpdater.ApplyChanges ( )
inline

Definition at line 62 of file DatabaseUpdater.cs.

References c, e, and version.

63  {
64  var controller = new VersionController(factory, log);
65  int databaseVersion = controller.FindCurrentVersion();
66 
67  if (databaseVersion <= 0)
68  {
69  var last = changes.OrderBy(c => c.VersionNumber).LastOrDefault();
70  if (last != null)
71  controller.SetCurrentVersion(last);
72 
73  return;
74  }
75 
76  IVersionChanges lastVersion = null;
77  var versionChanges = changes.
78  OrderBy(c => c.VersionNumber).
79  Where(c => c.VersionNumber > databaseVersion);
80 
81  var changeList = versionChanges.ToList();
82  if (changeList.Count == 0)
83  {
84  log.SchemaUpToDate();
85  return;
86  }
87 
88  log.ApplySchemaChanges();
89 
90  var connection = factory.CreateConnection();
91  connection.Open();
92 
93  foreach (var version in changeList)
94  {
95  var transaction = connection.BeginTransaction();
96  try
97  {
98  foreach (var change in version)
99  {
100  var command = factory.CreateCommand();
101  command.Connection = connection;
102  command.CommandText = change.Sql;
103  command.Transaction = transaction;
104  command.Prepare();
105  log.SqlExecute(command.CommandText);
106  command.ExecuteNonQuery();
107  }
108  transaction.Commit();
109  }
110  catch (Exception e)
111  {
112  log.SchemaUpdateError(e);
113  transaction.Rollback();
114  return;
115  }
116 
117  lastVersion = version;
118  log.SchemaChangesApplied(lastVersion.VersionText);
119  }
120 
121  if (connection.State == ConnectionState.Open)
122  connection.Close();
123 
124  controller.SetCurrentVersion(lastVersion);
125  }
abstract void Close()
var e
Definition: bootstrap.min.js:6
var c
Definition: bootstrap.min.js:6
IDbConnection CreateConnection()
readonly ISqlFactory factory
var version
Definition: bootstrap.js:13
IDbCommand CreateCommand()
readonly List< IVersionChanges > changes
bool Ntp.Data.Schema.DatabaseUpdater.CheckLatestVersion ( )
inline

Definition at line 127 of file DatabaseUpdater.cs.

References c.

128  {
129  var controller = new VersionController(factory, log);
130  int databaseVersion = controller.FindCurrentVersion();
131  var latest = changes.OrderBy(c => c.VersionNumber).Last();
132 
133  if (databaseVersion == -1)
134  {
135  controller.SetCurrentVersion(latest);
136  return true;
137  }
138 
139  if (databaseVersion == latest.VersionNumber)
140  return true;
141 
142  log.SchemaVersionError(databaseVersion, latest.VersionNumber);
143  return false;
144  }
var c
Definition: bootstrap.min.js:6
readonly ISqlFactory factory
readonly List< IVersionChanges > changes
IEnumerator IEnumerable. Ntp.Data.Schema.DatabaseUpdater.GetEnumerator ( )
inlineprivate

Definition at line 46 of file DatabaseUpdater.cs.

47  {
48  return changes.GetEnumerator();
49  }
readonly List< IVersionChanges > changes
IEnumerator<IVersionChanges> Ntp.Data.Schema.DatabaseUpdater.GetEnumerator ( )
inline

Definition at line 51 of file DatabaseUpdater.cs.

52  {
53  return changes.GetEnumerator();
54  }
readonly List< IVersionChanges > changes

Member Data Documentation

readonly List<IVersionChanges> Ntp.Data.Schema.DatabaseUpdater.changes
private

Definition at line 42 of file DatabaseUpdater.cs.

readonly ISqlFactory Ntp.Data.Schema.DatabaseUpdater.factory
private

Definition at line 43 of file DatabaseUpdater.cs.

readonly LogBase Ntp.Data.Schema.DatabaseUpdater.log
private

Definition at line 44 of file DatabaseUpdater.cs.


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