Skip to content

Commit

Permalink
Creating query defaults on providers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wagner Andrade committed Aug 22, 2012
1 parent 51076fd commit 74416dd
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 16 deletions.
10 changes: 10 additions & 0 deletions Thunderstruck.Test/Dependencies/AnotherSqlProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,15 @@ public override string FieldFormat
{
get { return "[{0}]"; }
}

public override string SelectAllQuery(string projection, string where)
{
return String.Format("SELECT {0} {1}", projection, where);
}

public override string SelectTakeQuery(string projection, string where, int count)
{
return String.Format("SELECT TOP {0} {1} {2}", count, projection, where);
}
}
}
41 changes: 25 additions & 16 deletions Thunderstruck/DataObjectQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ public DataObjectQuery(string projection) : this()

public DataContext DataContext { get; private set; }

public T First(string where = null, object queryParams = null)
public IList<T> All(string where = null, object queryParams = null)
{
return Top(1, where, queryParams).FirstOrDefault();
var context = GetDataContext();
var query = context.Provider.SelectAllQuery(ProjectionMask, where);
return Execute(context, query, queryParams);
}

public IList<T> Top(int count, string where = null, object queryParams = null)
public IList<T> Take(int count, string where = null, object queryParams = null)
{
var query = String.Format("SELECT TOP {0} {1} {2}", count, ProjectionMask, where);

return Execute(query, queryParams);
var context = GetDataContext();
var query = context.Provider.SelectTakeQuery(ProjectionMask, where, count);
return Execute(context, query, queryParams);
}

public IList<T> All(string where = null, object queryParams = null)
public T First(string where = null, object queryParams = null)
{
var query = String.Format("SELECT {0} {1}", ProjectionMask, where);

return Execute(query, queryParams);
return Take(1, where, queryParams).FirstOrDefault();
}

public DataObjectQuery<T> With(DataContext dataContext)
Expand All @@ -49,22 +49,31 @@ public DataObjectQuery<T> With(DataContext dataContext)
return this;
}

private IList<T> Execute(string query, object queryParams = null)
private IList<T> Execute(DataContext context, string query, object queryParams = null)
{
var dataContext = DataContext ?? new DataContext(Transaction.No);

var finalQuery = query.Replace(ProjectionMask, GetProjection(dataContext));
var finalQuery = ResolveProjection(query, context);

try
{
return dataContext.All<T>(finalQuery, queryParams);
return context.All<T>(finalQuery, queryParams);
}
finally
{
if (DataContext == null) dataContext.Dispose();
if (DataContext == null) context.Dispose();
}
}

private Thunderstruck.DataContext GetDataContext()
{
return DataContext ?? new DataContext(Transaction.No);
}

private string ResolveProjection(string query, DataContext context)
{
var projection = GetProjection(context);
return query.Replace(ProjectionMask, projection);
}

public string GetProjection(DataContext context)
{
if (_customProjection != null)
Expand Down
10 changes: 10 additions & 0 deletions Thunderstruck/Provider/Common/OracleProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,15 @@ public override string FieldFormat
{
get { return "\"{0}\""; }
}

public override string SelectAllQuery(string projection, string where)
{
return String.Format("SELECT {0} {1}", projection, where);
}

public override string SelectTakeQuery(string projection, string where, int count)
{
return String.Format("SELECT {0} {1} LIMIT {2}", projection, where, count);
}
}
}
10 changes: 10 additions & 0 deletions Thunderstruck/Provider/Common/SqlProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,15 @@ public override string FieldFormat
{
get { return "[{0}]"; }
}

public override string SelectAllQuery(string projection, string where)
{
return String.Format("SELECT {0} {1}", projection, where);
}

public override string SelectTakeQuery(string projection, string where, int count)
{
return String.Format("SELECT TOP {0} {1} {2}", count, projection, where);
}
}
}
4 changes: 4 additions & 0 deletions Thunderstruck/Provider/DefaultProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public abstract class DefaultProvider : IDataProvider

public abstract string FieldFormat { get; }

public abstract string SelectAllQuery(string projection, string where);

public abstract string SelectTakeQuery(string projection, string where, int count);

public void CreateConnection(ConnectionStringSettings connectionSettings, Transaction transaction)
{
TransactionMode = transaction;
Expand Down
4 changes: 4 additions & 0 deletions Thunderstruck/Provider/IDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public interface IDataProvider : IDisposable

object ExecuteGetValue(string query, object queryParams);

string SelectAllQuery(string projection, string where);

string SelectTakeQuery(string projection, string where, int count);

void Commit();
}
}

0 comments on commit 74416dd

Please sign in to comment.