1
0
mirror of https://bitbucket.org/anguist/ntpa synced 2025-10-06 11:04:40 +00:00

Version 0.7.0 prerelease changes

This commit is contained in:
Carsten
2016-08-18 19:18:52 +02:00
parent 4e9d8a5c44
commit 59afc03cf4
285 changed files with 6410 additions and 9707 deletions

View File

@ -42,21 +42,41 @@ namespace Ntp.Data.Provider
"WHERE table_schema = '{0}' " +
"AND table_name = '{1}';";
public override IDbCommand CreateCommand()
{
return new MySqlCommand();
}
public override IDbConnection CreateConnection()
{
return new MySqlConnection(BuildConnectionString());
}
[SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
public override void CreateDatabase()
{
IDbConnection connection = CreateGenericConnection();
var connection = CreateGenericConnection();
connection.Open();
IDbCommand command = Instance.CreateCommand();
var command = Instance.CreateCommand();
command.Connection = connection;
command.CommandText = string.Format(CreateDatabaseSql, Config.Name);
command.Prepare();
connection.Close();
}
public override string PrepareSql(string sql)
public override IDbConnection CreateGenericConnection()
{
return sql.Replace("[", string.Empty).Replace("]", string.Empty);
return new MySqlConnection(BuildConnectionString(false));
}
public override IDbDataParameter CreateParameter()
{
return new MySqlParameter();
}
public override IDbDataParameter CreateParameter(string name, object value)
{
return new MySqlParameter(name, value);
}
public override string PrepareCheckTableSql(string table)
@ -74,50 +94,42 @@ namespace Ntp.Data.Provider
return PrepareSql(string.Format(sql, "SELECT LAST_INSERT_ID()"));
}
public override IDbConnection CreateGenericConnection()
public override string PrepareSql(string sql)
{
return new MySqlConnection(BuildConnectionString(false));
return sql.Replace("[", string.Empty).Replace("]", string.Empty);
}
public override IDbConnection CreateConnection()
private static string BuildConnectionString(bool includeName = true)
{
return new MySqlConnection(BuildConnectionString());
}
if (Config.ConnectionString != null)
return Config.ConnectionString;
private string BuildConnectionString(bool includeName = true)
{
var b = new StringBuilder();
b.AppendFormat("Server={0};", Config.Host);
b.Append($"Server={Config.Host};");
if (Config.Port != null)
b.AppendFormat("Port={0};", Config.Port);
b.Append($"Port={Config.Port};");
if (includeName)
b.AppendFormat("Database={0};", Config.Name);
b.Append($"Database={Config.Name};");
b.AppendFormat("Uid={0};", Config.User);
b.AppendFormat("Pwd={0};", Config.Pass);
b.Append($"Uid={Config.User};");
b.Append($"Pwd={Config.Pass};");
// TODO: Inlcude SSL options
//SSL Mode=Required;CertificateFile=C:\folder\client.pfx;CertificatePassword=pass;
if (Config.EnableSsl)
b.Append(@"SSL Mode=Required;");
if (Config.CertificateFile != null)
b.Append($"CertificateFile={Config.CertificateFile};");
if (Config.CertificatePassword != null)
b.Append($"CertificatePassword={Config.CertificatePassword};");
if (Config.ConnectionTimeout.HasValue)
b.Append($"Connection Timeout={Config.ConnectionTimeout.Value};");
return b.ToString();
}
public override IDbCommand CreateCommand()
{
return new MySqlCommand();
}
public override IDbDataParameter CreateParameter()
{
return new MySqlParameter();
}
public override IDbDataParameter CreateParameter(string name, object value)
{
return new MySqlParameter(name, value);
}
}
}

View File

@ -15,27 +15,65 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\bin</OutputPath>
<DefineConstants>DEBUG;MYSQL;PGSQL</DefineConstants>
<DefineConstants>TRACE;DEBUG;MYSQL;PGSQL;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\bin</OutputPath>
<DefineConstants>TRACE;MYSQL;PGSQL;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<DefineConstants>MYSQL;PGSQL;</DefineConstants>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<!-- Override when doing automated build -->
<Choose>
<When Condition=" '$(mysql)|$(pgsql)' == 'on|on' ">
<PropertyGroup>
<DefineConstants>MYSQL;PGSQL;TRACE</DefineConstants>
</PropertyGroup>
</When>
<When Condition=" '$(mysql)' == 'on' ">
<PropertyGroup>
<DefineConstants>MYSQL;TRACE</DefineConstants>
</PropertyGroup>
</When>
<When Condition=" '$(pgsql)' == 'on' ">
<PropertyGroup>
<DefineConstants>PGSQL;TRACE</DefineConstants>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup>
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
</Otherwise>
</Choose>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="MySql.Data">
<HintPath>..\libbin\MySql.Data.dll</HintPath>
</Reference>
<Reference Include="Npgsql">
<HintPath>..\libbin\Npgsql.dll</HintPath>
</Reference>
</ItemGroup>
<Choose>
<When Condition=" '$(pgsql)' == 'on' ">
<ItemGroup>
<Reference Include="Npgsql">
<HintPath>..\libbin\Npgsql.dll</HintPath>
</Reference>
</ItemGroup>
</When>
</Choose>
<Choose>
<When Condition=" '$(mysql)' == 'on' ">
<ItemGroup>
<Reference Include="MySql.Data">
<HintPath>..\libbin\MySql.Data.dll</HintPath>
</Reference>
</ItemGroup>
</When>
</Choose>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
@ -49,4 +87,4 @@
<Name>Ntp.Data</Name>
</ProjectReference>
</ItemGroup>
</Project>
</Project>

View File

@ -26,6 +26,26 @@ using System.Diagnostics.CodeAnalysis;
using System.Text;
using Npgsql;
/**
*
* Npqsql Connection String Parameters:
* Ref: http://www.npgsql.org/doc/connection-string-parameters.html
*
* Note that by default, Npgsql will verify that your servers certificate is valid.
* If youre using a self-signed certificate this will fail. You can instruct Npgsql
* to ignore this by specifying Trust Server Certificate=true in the connection string.
* To precisely control how the servers certificate is validated, you can register
* UserCertificateValidationCallback on NpgsqlConnection (this works just like on
* .NETs SSLStream).
*
* You can also have Npgsql provide client certificates to the server by registering
* the ProvideClientCertificatesCallback on NpgsqlConnection (this works just like on
* .NETs SSLStream).
*
* Ref: http://www.npgsql.org/doc/security.html
*
*/
namespace Ntp.Data.Provider
{
public sealed class PostgreSqlFactory : SqlDatabaseFactory
@ -46,13 +66,23 @@ namespace Ntp.Data.Provider
"WHERE table_catalog = lower('{0}') " +
"AND table_name = lower('{1}');";
public override IDbCommand CreateCommand()
{
return new NpgsqlCommand();
}
public override IDbConnection CreateConnection()
{
return new NpgsqlConnection(BuildConnectionString());
}
[SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
public override void CreateDatabase()
{
IDbConnection connection = CreateGenericConnection();
var connection = CreateGenericConnection();
connection.Open();
IDbCommand command = Instance.CreateCommand();
var command = Instance.CreateCommand();
command.Connection = connection;
command.CommandText = string.Format(CreateDatabaseSql1, Config.Name);
command.Prepare();
@ -68,65 +98,11 @@ namespace Ntp.Data.Provider
connection.Close();
}
public override string PrepareSql(string sql)
{
return sql.Replace('[', '\"').Replace(']', '\"');
}
public override string PrepareCheckTableSql(string table)
{
return PrepareSql(string.Format(CheckTableSql, Config.Name, table));
}
public override string PrepareCreateTableSql(string sql)
{
string sql2 =
sql.
Replace("UNIQUE KEY", "UNIQUE").
Replace("BIT(1)", "BOOL");
return PrepareSql(string.Format(sql2, "SERIAL", string.Empty));
}
public override string PrepareInsertSql(string sql)
{
return PrepareSql(string.Format(sql, "SELECT LASTVAL();"));
}
public override IDbConnection CreateGenericConnection()
{
return new NpgsqlConnection(BuildConnectionString(false));
}
public override IDbConnection CreateConnection()
{
return new NpgsqlConnection(BuildConnectionString());
}
private string BuildConnectionString(bool includeName = true)
{
var b = new StringBuilder();
b.AppendFormat("Server={0};", Config.Host);
if (Config.Port != null)
b.AppendFormat("Port={0};", Config.Port);
if (includeName)
b.AppendFormat("Database={0};", Config.Name);
else
b.Append("Database=postgres;");
b.AppendFormat("User Id={0};", Config.User);
b.AppendFormat("Password={0};", Config.Pass);
return b.ToString();
}
public override IDbCommand CreateCommand()
{
return new NpgsqlCommand();
}
public override IDbDataParameter CreateParameter()
{
return new NpgsqlParameter();
@ -136,6 +112,66 @@ namespace Ntp.Data.Provider
{
return new NpgsqlParameter(name, value);
}
public override string PrepareCheckTableSql(string table)
{
return PrepareSql(string.Format(CheckTableSql, Config.Name, table));
}
public override string PrepareCreateTableSql(string sql)
{
string sql2 = sql.
Replace("UNIQUE KEY", "UNIQUE").
Replace("BIT(1)", "BOOL");
return PrepareSql(string.Format(sql2, "SERIAL", string.Empty));
}
public override string PrepareInsertSql(string sql)
{
return PrepareSql(string.Format(sql, "SELECT LASTVAL();"));
}
public override string PrepareSql(string sql)
{
return sql.Replace('[', '\"').Replace(']', '\"');
}
private static string BuildConnectionString(bool includeName = true)
{
if (Config.ConnectionString != null)
return Config.ConnectionString;
var b = new StringBuilder();
b.Append($"Server={Config.Host};");
if (Config.Port != null)
b.Append($"Port={Config.Port};");
b.Append(includeName ? $"Database={Config.Name};" : @"Database=postgres;");
b.Append($"User Id={Config.User};");
b.Append($"Password={Config.Pass};");
if (Config.ConnectionTimeout.HasValue)
b.Append($"Timeout={Config.ConnectionTimeout.Value};");
if (Config.EnableSsl)
{
b.Append(@"SSL=true;");
b.Append(@"SslMode=Require;");
}
else
{
b.Append(@"SSL=false;");
b.Append(@"SslMode=Disable;");
}
if (Config.Protocol.HasValue)
b.Append($"Protocol={Config.Protocol.Value};");
return b.ToString();
}
}
}

View File

@ -19,6 +19,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Data;
namespace Ntp.Data.Provider
@ -37,18 +38,18 @@ namespace Ntp.Data.Provider
#if MYSQL
return new MySqlFactory();
#else
throw new ApplicationException("NTP Analyzer was not build wit MySQL support.");
throw new NotSupportedException("Data access was not built with MySQL support.");
#endif
case SqlDatabaseProvider.PostgreSql:
#if PGSQL
return new PostgreSqlFactory();
#else
throw new ApplicationException("NTP Analyzer was not build wit PostgreSQL support.");
throw new NotSupportedException("Data access was not built with PostgreSQL support.");
#endif
case SqlDatabaseProvider.Unknown:
return null;
throw new NotSupportedException("Unknown SQL data provider was configured.");
default:
return null;
throw new NotSupportedException("No SQL data provider was configured.");
}
}
}
@ -65,16 +66,17 @@ namespace Ntp.Data.Provider
public abstract IDbDataParameter CreateParameter(string name, object value);
public abstract void CreateDatabase();
public static void Initialize(IDatabaseConfiguration factoryConfiguration)
{
Config = factoryConfiguration;
}
public abstract string PrepareSql(string sql);
public abstract string PrepareCreateTableSql(string sql);
public abstract string PrepareInsertSql(string sql);
public abstract void CreateDatabase();
public abstract string PrepareSql(string sql);
}
}