Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cc509c6
Showing
146 changed files
with
21,904 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
obj | ||
bin | ||
_ReSharper.* | ||
*.user | ||
*.suo | ||
*.orig | ||
*.log | ||
aspnet_client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 11.00 | ||
# Visual Studio 2010 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gribble", "Gribble\Gribble.csproj", "{8A9D3349-E67B-4299-8D91-A4E54413845D}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{FD8392BC-2701-4CF9-B1B9-B3DCFD897881}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{8A9D3349-E67B-4299-8D91-A4E54413845D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{8A9D3349-E67B-4299-8D91-A4E54413845D}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{8A9D3349-E67B-4299-8D91-A4E54413845D}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{8A9D3349-E67B-4299-8D91-A4E54413845D}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{FD8392BC-2701-4CF9-B1B9-B3DCFD897881}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{FD8392BC-2701-4CF9-B1B9-B3DCFD897881}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{FD8392BC-2701-4CF9-B1B9-B3DCFD897881}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{FD8392BC-2701-4CF9-B1B9-B3DCFD897881}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
using System.Linq; | ||
using System.Text; | ||
using Gribble.TransactSql; | ||
|
||
namespace Gribble | ||
{ | ||
public class Command | ||
{ | ||
public class SqlException : Exception | ||
{ | ||
public SqlException(Exception exception, Statement statement) : | ||
base(string.Format("Error executing sql: {0}", exception.Message), exception) | ||
{ Statement = statement; } | ||
|
||
public Statement Statement { get; private set; } | ||
} | ||
|
||
private readonly IProfiler _profiler; | ||
|
||
public Command(Statement statement, IProfiler profiler) | ||
{ | ||
Statement = statement; | ||
_profiler = profiler; | ||
} | ||
|
||
public static Command Create(Statement statement, IProfiler profiler) | ||
{ | ||
return new Command(statement, profiler); | ||
} | ||
|
||
public Statement Statement { get; private set; } | ||
|
||
public IDataReader ExecuteReader(ConnectionManager connectionManager) | ||
{ return Execute(() => CreateCommand(connectionManager).ExecuteReader()); } | ||
|
||
public T ExecuteScalar<T>(ConnectionManager connectionManager) | ||
{ return (T)ExecuteScalar(connectionManager); } | ||
|
||
public object ExecuteScalar(ConnectionManager connectionManager) | ||
{ return Execute(() => CreateCommand(connectionManager).ExecuteScalar()); } | ||
|
||
public IEnumerable<T> ExecuteEnumerable<T>(ConnectionManager connectionManager) | ||
{ | ||
return ExecuteEnumerable(connectionManager, x => (T)x[0]); | ||
} | ||
|
||
public IEnumerable<Tuple<T1, T2>> ExecuteEnumerable<T1, T2>(ConnectionManager connectionManager) | ||
{ | ||
return ExecuteEnumerable(connectionManager, x => new Tuple<T1, T2>((T1)x[0], (T2)x[1])); | ||
} | ||
|
||
public IEnumerable<Tuple<T1, T2, T3>> ExecuteEnumerable<T1, T2, T3>(ConnectionManager connectionManager) | ||
{ | ||
return ExecuteEnumerable(connectionManager, x => new Tuple<T1, T2, T3>((T1)x[0], (T2)x[1], (T3)x[2])); | ||
} | ||
|
||
public IEnumerable<Tuple<T1, T2, T3, T4>> ExecuteEnumerable<T1, T2, T3, T4>(ConnectionManager connectionManager) | ||
{ | ||
return ExecuteEnumerable(connectionManager, x => new Tuple<T1, T2, T3, T4>((T1)x[0], (T2)x[1], (T3)x[2], (T4)x[3])); | ||
} | ||
|
||
public IEnumerable<Tuple<T1, T2, T3, T4, T5>> ExecuteEnumerable<T1, T2, T3, T4, T5>(ConnectionManager connectionManager) | ||
{ | ||
return ExecuteEnumerable(connectionManager, x => new Tuple<T1, T2, T3, T4, T5>((T1)x[0], (T2)x[1], (T3)x[2], (T4)x[3], (T5)x[4])); | ||
} | ||
|
||
public IEnumerable<Tuple<T1, T2, T3, T4, T5, T6>> ExecuteEnumerable<T1, T2, T3, T4, T5, T6>(ConnectionManager connectionManager) | ||
{ | ||
return ExecuteEnumerable(connectionManager, x => new Tuple<T1, T2, T3, T4, T5, T6>((T1)x[0], (T2)x[1], (T3)x[2], (T4)x[3], (T5)x[4], (T6)x[5])); | ||
} | ||
|
||
public IEnumerable<Tuple<T1, T2, T3, T4, T5, T6, T7>> ExecuteEnumerable<T1, T2, T3, T4, T5, T6, T7>(ConnectionManager connectionManager) | ||
{ | ||
return ExecuteEnumerable(connectionManager, x => new Tuple<T1, T2, T3, T4, T5, T6, T7>((T1)x[0], (T2)x[1], (T3)x[2], (T4)x[3], (T5)x[4], (T6)x[5], (T7)x[6])); | ||
} | ||
|
||
public IEnumerable<Tuple<T1, T2, T3, T4, T5, T6, T7, Tuple<T8>>> ExecuteEnumerable<T1, T2, T3, T4, T5, T6, T7, T8>(ConnectionManager connectionManager) | ||
{ | ||
return ExecuteEnumerable(connectionManager, x => new Tuple<T1, T2, T3, T4, T5, T6, T7, Tuple<T8>>((T1) x[0], (T2) x[1], (T3) x[2], (T4) x[3], (T5) x[4], (T6) x[5], (T7) x[6], new Tuple<T8>((T8) x[7]))); | ||
} | ||
|
||
public void ExecuteNonQuery(ConnectionManager connectionManager) | ||
{ Execute(() => CreateCommand(connectionManager).ExecuteNonQuery()); } | ||
|
||
private IDbCommand CreateCommand(ConnectionManager connectionManager) | ||
{ | ||
var command = connectionManager.CreateCommand(); | ||
command.CommandText = Statement.Text; | ||
command.CommandType = Statement.Type == Statement.StatementType.Text ? CommandType.Text : CommandType.StoredProcedure; | ||
Statement.Parameters.Select(x => new SqlParameter(x.Key, x.Value)).Run(y => command.Parameters.Add(y)); | ||
return command; | ||
} | ||
|
||
private IEnumerable<T> ExecuteEnumerable<T>(ConnectionManager connectionManager, Func<IDataReader, T> createItem) | ||
{ | ||
return Execute(() => | ||
{ | ||
var values = new List<T>(); | ||
using (var reader = CreateCommand(connectionManager).ExecuteReader()) | ||
while (reader.Read()) values.Add(createItem(reader)); | ||
return values; | ||
}); | ||
} | ||
|
||
private T Execute<T>(Func<T> command) | ||
{ | ||
//try | ||
//{ | ||
if (_profiler != null) ProfileCommand(Statement, _profiler); | ||
return command(); | ||
//} | ||
//catch (Exception exception) | ||
//{ | ||
// throw new SqlException(exception, Statement); | ||
// } | ||
} | ||
|
||
private static void ProfileCommand(Statement statement, IProfiler profiler) | ||
{ | ||
var message = new StringBuilder(); | ||
message.Append(statement.Text); | ||
if (statement.Type == Statement.StatementType.StoredProcedure && statement.Parameters.Count > 0) | ||
message.Append(" " + statement.Parameters.Select(x => x.Key).Aggregate((a, i) => string.Format("{0}, {1}", a, i))); | ||
profiler.Write(message.ToString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
|
||
namespace Gribble | ||
{ | ||
public class ConnectionManager : IDisposable | ||
{ | ||
private readonly Lazy<SqlConnection> _connection; | ||
private readonly TimeSpan _commandTimeout; | ||
|
||
public ConnectionManager(SqlConnection connection, TimeSpan commandTimeout) | ||
{ | ||
_connection = new Lazy<SqlConnection>(() => connection); | ||
_commandTimeout = commandTimeout; | ||
} | ||
|
||
public ConnectionManager(string connectionString, TimeSpan commandTimeout) | ||
{ | ||
_connection = new Lazy<SqlConnection>(() => | ||
{ | ||
var connection = new SqlConnection(connectionString); | ||
connection.Open(); | ||
return connection; | ||
}); | ||
_commandTimeout = commandTimeout; | ||
} | ||
|
||
public SqlConnection Connection { get { return _connection.Value; } } | ||
|
||
public IDbCommand CreateCommand() | ||
{ | ||
return new SqlCommand | ||
{ | ||
Connection = _connection.Value, | ||
CommandTimeout = (int)_commandTimeout.TotalSeconds | ||
}; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_connection.IsValueCreated) _connection.Value.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
|
||
namespace Gribble | ||
{ | ||
public class ConsoleProfiler : IProfiler | ||
{ | ||
public void Write(string format, params object[] args) | ||
{ | ||
Console.WriteLine("DHibernate ({0:hh:mm:ss.fffffff}): {1}", DateTime.Now, string.Format(format, args)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data.SqlClient; | ||
using Gribble.Mapping; | ||
using Gribble.Statements; | ||
using Gribble.TransactSql; | ||
|
||
namespace Gribble | ||
{ | ||
public class Database : IDatabase | ||
{ | ||
private readonly ConnectionManager _connectionManager; | ||
private readonly IProfiler _profiler; | ||
private readonly EntityMappingCollection _mappingCollection; | ||
|
||
public Database(SqlConnection connection, TimeSpan commandTimeout, EntityMappingCollection mappingCollection) : | ||
this(new ConnectionManager(connection, commandTimeout), mappingCollection, null) { } | ||
|
||
public Database(SqlConnection connection, TimeSpan commandTimeout, EntityMappingCollection mappingCollection, IProfiler profiler) : | ||
this(new ConnectionManager(connection, commandTimeout), mappingCollection, profiler) { } | ||
|
||
public Database(ConnectionManager connectionManager, EntityMappingCollection mappingCollection) : | ||
this(connectionManager, mappingCollection, null) { } | ||
|
||
public Database(ConnectionManager connectionManager, EntityMappingCollection mappingCollection, IProfiler profiler) | ||
{ | ||
_connectionManager = connectionManager; | ||
_profiler = profiler; | ||
_mappingCollection = mappingCollection; | ||
} | ||
|
||
public void CallProcedure(string name) | ||
{ Command.Create(StoredProcedureWriter.CreateStatement(name, Statement.ResultType.None), _profiler).ExecuteNonQuery(_connectionManager); } | ||
|
||
public void CallProcedure(string name, Dictionary<string, object> parameters) | ||
{ Command.Create(StoredProcedureWriter.CreateStatement(name, parameters, Statement.ResultType.None), _profiler).ExecuteNonQuery(_connectionManager); } | ||
|
||
public T CallProcedureScalar<T>(string name) | ||
{ return Command.Create(StoredProcedureWriter.CreateStatement(name, Statement.ResultType.Scalar), _profiler).ExecuteScalar<T>(_connectionManager); } | ||
|
||
public T CallProcedureScalar<T>(string name, Dictionary<string, object> parameters) | ||
{ return Command.Create(StoredProcedureWriter.CreateStatement(name, parameters, Statement.ResultType.Scalar), _profiler).ExecuteScalar<T>(_connectionManager); } | ||
|
||
public TEntity CallProcedureSingle<TEntity>(string name) | ||
{ return Load<TEntity, TEntity>(Command.Create(StoredProcedureWriter.CreateStatement(name, Statement.ResultType.Single), _profiler)); } | ||
|
||
public TEntity CallProcedureSingle<TEntity>(string name, Dictionary<string, object> parameters) | ||
{ return Load<TEntity, TEntity>(Command.Create(StoredProcedureWriter.CreateStatement(name, parameters, Statement.ResultType.Single), _profiler)); } | ||
|
||
public TEntity CallProcedureSingleOrNone<TEntity>(string name) | ||
{ return Load<TEntity, TEntity>(Command.Create(StoredProcedureWriter.CreateStatement(name, Statement.ResultType.SingleOrNone), _profiler)); } | ||
|
||
public TEntity CallProcedureSingleOrNone<TEntity>(string name, Dictionary<string, object> parameters) | ||
{ return Load<TEntity, TEntity>(Command.Create(StoredProcedureWriter.CreateStatement(name, parameters, Statement.ResultType.SingleOrNone), _profiler)); } | ||
|
||
public IEnumerable<TEntity> CallProcedureMany<TEntity>(string name) | ||
{ return Load<TEntity, IEnumerable<TEntity>>(Command.Create(StoredProcedureWriter.CreateStatement(name, Statement.ResultType.Multiple), _profiler)); } | ||
|
||
public IEnumerable<TEntity> CallProcedureMany<TEntity>(string name, Dictionary<string, object> parameters) | ||
{ return Load<TEntity, IEnumerable<TEntity>>(Command.Create(StoredProcedureWriter.CreateStatement(name, parameters, Statement.ResultType.Multiple), _profiler)); } | ||
|
||
private TResult Load<TEntity, TResult>(Command command) | ||
{ return (TResult)new Loader<TEntity>(command, _mappingCollection.GetEntityMapping<TEntity>()).Execute(_connectionManager); } | ||
|
||
public void CreateTable(string tableName, params Column[] columns) | ||
{ Command.Create(SchemaWriter.CreateTableCreateStatement(tableName, columns), _profiler).ExecuteNonQuery(_connectionManager); } | ||
|
||
public void DeleteTable(string tableName) | ||
{ Command.Create(SchemaWriter.CreateDeleteTableStatement(tableName), _profiler).ExecuteNonQuery(_connectionManager); } | ||
|
||
public void AddColumn(string tableName, Column column) | ||
{ Command.Create(SchemaWriter.CreateAddColumnStatement(tableName, column), _profiler).ExecuteNonQuery(_connectionManager); } | ||
|
||
public void RemoveColumn(string tableName, string columnName) | ||
{ Command.Create(SchemaWriter.CreateRemoveColumnStatement(tableName, columnName), _profiler).ExecuteNonQuery(_connectionManager); } | ||
|
||
public void AddNonClusteredIndex(string tableName, string indexName, params string[] columnNames) | ||
{ Command.Create(SchemaWriter.CreateAddNonClusteredIndexStatement(tableName, indexName, columnNames), _profiler).ExecuteNonQuery(_connectionManager); } | ||
|
||
public void RemoveNonClusteredIndex(string tableName, string indexName) | ||
{ Command.Create(SchemaWriter.CreateRemoveNonClusteredIndexStatement(tableName, indexName), _profiler).ExecuteNonQuery(_connectionManager); } | ||
} | ||
} |
Oops, something went wrong.