Permalink
Browse files

Creating query defaults on providers.

  • Loading branch information...
Wagner Andrade
Wagner Andrade committed Aug 22, 2012
1 parent 51076fd commit 74416dd1bd9f8273604e4823d2e2f7ee5c8afa1b
@@ -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);
+ }
}
}
@@ -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)
@@ -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)
@@ -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);
+ }
}
}
@@ -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);
+ }
}
}
@@ -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;
@@ -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.