24 using System.Diagnostics.CodeAnalysis;
28 namespace Ntp.Data.Schema
38 private const string CreateSql =
"CREATE TABLE version (number INT NOT NULL PRIMARY KEY);";
39 private const string SelectSql =
"SELECT number FROM version;";
40 private const string UpdateSql =
"UPDATE version SET number=@number;";
41 private const string InsertSql =
"INSERT INTO version (number) VALUES (0);";
48 return SelectVersion();
59 log.VersionTableParamError();
63 IDbConnection connection = null;
67 connection = factory.CreateConnection();
69 var command = factory.CreateCommand();
70 command.Connection = connection;
71 command.CommandText = UpdateSql;
72 command.Parameters.Add(factory.CreateParameter(
"@number", version.
VersionNumber));
74 log.SqlExecute(command.CommandText, command.Parameters);
75 command.ExecuteNonQuery();
79 log.VersionTableUpdateError(e);
83 if (connection != null && connection.State == ConnectionState.Open)
90 [SuppressMessage(
"Microsoft.Security",
"CA2100:Review SQL queries for security vulnerabilities")]
93 IDbConnection connection = null;
97 connection = factory.CreateConnection();
99 var command = factory.CreateCommand();
100 command.Connection = connection;
101 command.CommandText = factory.PrepareCheckTableSql(
"version");
103 log.SqlExecute(command.CommandText);
104 var reader = command.ExecuteReader();
105 return reader.Read();
109 log.VersionTableCreateError(e);
114 if (connection != null && connection.State == ConnectionState.Open)
121 IDbConnection connection = null;
125 connection = factory.CreateConnection();
127 var command = factory.CreateCommand();
128 command.Connection = connection;
129 command.CommandText = CreateSql;
131 log.SqlExecute(command.CommandText);
132 command.ExecuteNonQuery();
136 log.VersionTableCreateError(e);
140 if (connection != null && connection.State == ConnectionState.Open)
144 log.VersionTableCreated();
149 IDbConnection connection = null;
153 connection = factory.CreateConnection();
155 var command = factory.CreateCommand();
156 command.Connection = connection;
157 command.CommandText = InsertSql;
159 log.SqlExecute(command.CommandText);
160 command.ExecuteNonQuery();
164 log.VersionTableInsertError(e);
168 if (connection != null && connection.State == ConnectionState.Open)
175 IDbConnection connection = null;
179 connection = factory.CreateConnection();
181 var command = factory.CreateCommand();
182 command.Connection = connection;
183 command.CommandText = SelectSql;
185 log.SqlExecute(command.CommandText);
186 var result = command.ExecuteScalar();
187 return Convert.ToInt32(result);
191 log.VersionTableFetchError(e);
196 if (connection != null && connection.State == ConnectionState.Open)
VersionController(ISqlFactory factory, LogBase log)
readonly ISqlFactory factory
void SetCurrentVersion(IVersionChanges version)