|
using System; |
|
using System.Collections.Generic; |
|
using System.Data; |
|
using System.IO; |
|
using System.Reflection; |
|
using Mono.Data.Sqlite; |
|
|
|
namespace OurApplication.AppCore.Data.Sqlite |
|
{ |
|
public class DbProvider : DbProviderBase |
|
{ |
|
private const string RootSqlScriptPath = "OurApplication.AppCore.Data.Sqlite.SqlScripts."; |
|
private const string CheckTableExists = "SELECT name FROM sqlite_master WHERE type='table' AND name='{0}'"; |
|
private readonly IDbConnectionProvider _connectionProvider; |
|
private readonly string _sqliteDatabasePath; |
|
|
|
public DbProvider ( string databaseName ) |
|
{ |
|
DatabaseName = databaseName; |
|
_sqliteDatabasePath = "{0}.sqlite3"; |
|
_connectionProvider = new DbConnectionProvider( _sqliteDatabasePath, databaseName ); |
|
} |
|
|
|
public override sealed IDialect Dialect { get { return new SqliteDialect(); } } |
|
|
|
public override string LoadSqlFile ( string fileName ) |
|
{ |
|
var sqlStatement = string.Empty; |
|
|
|
using ( var resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream( RootSqlScriptPath + fileName ) ) |
|
{ |
|
if ( resourceStream != null ) |
|
{ |
|
sqlStatement = new StreamReader( resourceStream ).ReadToEnd(); |
|
} |
|
} |
|
|
|
return sqlStatement; |
|
} |
|
|
|
public override bool CheckIfDatabaseExists () |
|
{ |
|
return File.Exists( string.Format( _sqliteDatabasePath, DatabaseName ) ); |
|
} |
|
|
|
public override void CreateDatabase () |
|
{ |
|
Mono.Data.Sqlite.SqliteConnection.CreateFile( DatabaseName ); |
|
} |
|
|
|
public override void DropDatabase () |
|
{ |
|
File.Delete( string.Format( _sqliteDatabasePath, DatabaseName ) ); |
|
} |
|
|
|
public override bool CheckIfTableExists ( string tableName ) |
|
{ |
|
var exists = ExecuteScalar<int>( string.Format( CheckTableExists, tableName ) ) == 1; |
|
return exists; |
|
} |
|
|
|
#region ExecuteReader |
|
|
|
public override TResult ExecuteReader<TResult> ( string commandText, Func<IDbReader, TResult> readerMapper ) |
|
{ |
|
return ExecuteReader( commandText, new Dictionary<string, object>(), readerMapper ); |
|
} |
|
|
|
public override TResult ExecuteReader<TResult> ( string commandText, IDictionary<string, object> parameters, Func<IDbReader, TResult> readerMapper ) |
|
{ |
|
using ( var connection = _connectionProvider.GetOpenConnection() ) |
|
{ |
|
using ( var command = connection.CreateCommand() ) |
|
{ |
|
command.CommandType = CommandType.Text; |
|
command.CommandText = commandText; |
|
foreach ( var parameter in parameters ) |
|
{ |
|
command.Parameters.Add( new SqliteParameter( parameter.Key, parameter.Value ?? DBNull.Value ) ); |
|
} |
|
|
|
using ( var reader = command.ExecuteReader() ) |
|
{ |
|
var r = new DbReader( reader ); |
|
return readerMapper( r ); |
|
} |
|
} |
|
} |
|
} |
|
|
|
#endregion |
|
|
|
#region ExecuteNonQuery |
|
|
|
public override void ExecuteNonQuery ( string commandText ) |
|
{ |
|
ExecuteNonQuery( commandText, new Dictionary<string, object>() ); |
|
} |
|
|
|
public override void ExecuteNonQuery ( string commandText, IDictionary<string, object> parameters ) |
|
{ |
|
using ( var connection = _connectionProvider.GetOpenConnection() ) |
|
{ |
|
using ( var command = connection.CreateCommand() ) |
|
{ |
|
command.CommandType = CommandType.Text; |
|
command.CommandText = commandText; |
|
foreach ( var parameter in parameters ) |
|
{ |
|
command.Parameters.Add( new SqliteParameter( parameter.Key, parameter.Value ?? DBNull.Value ) ); |
|
} |
|
|
|
command.ExecuteNonQuery(); |
|
} |
|
} |
|
} |
|
|
|
#endregion |
|
|
|
#region ExecuteScalar |
|
|
|
public override TKey ExecuteScalar<TKey> ( string commandText ) |
|
{ |
|
return ExecuteScalar<TKey>( commandText, new Dictionary<string, object>() ); |
|
} |
|
|
|
public override TKey ExecuteScalar<TKey> ( string commandText, IDictionary<string, object> parameters ) |
|
{ |
|
using ( var connection = _connectionProvider.GetOpenConnection() ) |
|
{ |
|
using ( var command = connection.CreateCommand() ) |
|
{ |
|
command.CommandType = CommandType.Text; |
|
command.CommandText = commandText; |
|
foreach ( var parameter in parameters ) |
|
{ |
|
command.Parameters.Add( new SqliteParameter( parameter.Key, parameter.Value ?? DBNull.Value ) ); |
|
} |
|
|
|
var result = command.ExecuteScalar(); |
|
|
|
if( typeof( TKey ) == typeof( Guid ) ) |
|
return (TKey)(object)new Guid( ( byte[] )result ); |
|
if( typeof( TKey ) == typeof( int ) ) |
|
return ( TKey )( object )(result == null ? 0 : 1); |
|
|
|
return ( TKey )result; |
|
} |
|
} |
|
} |
|
|
|
#endregion |
|
} |
|
} |