Permalink
Browse files

Fix problems with default schema resolution

  • Loading branch information...
1 parent 3f10a59 commit df487712496c8a4c2327e97ca1789ec251d13113 @markrendle committed Mar 15, 2012
View
26 Simple.Data.Ado/AdoAdapterQueryRunner.cs
@@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Data;
+ using System.Diagnostics;
using System.Linq;
internal class AdoAdapterQueryRunner
@@ -22,7 +23,7 @@ public AdoAdapterQueryRunner(AdoAdapter adapter)
if (query.Clauses.OfType<WithCountClause>().Any()) return RunQueryWithCount(query, out unhandledClauses);
- ICommandBuilder[] commandBuilders = GetQueryCommandBuilders(query, out unhandledClauses);
+ ICommandBuilder[] commandBuilders = GetQueryCommandBuilders(ref query, out unhandledClauses);
IDbConnection connection = _adapter.CreateConnection();
if (_adapter.ProviderSupportsCompoundStatements || commandBuilders.Length == 1)
{
@@ -101,13 +102,13 @@ public AdoAdapterQueryRunner(AdoAdapter adapter)
}
}
- private ICommandBuilder[] GetPagedQueryCommandBuilders(SimpleQuery query,
+ private ICommandBuilder[] GetPagedQueryCommandBuilders(ref SimpleQuery query,
out IEnumerable<SimpleQueryClauseBase> unhandledClauses)
{
- return GetPagedQueryCommandBuilders(query, -1, out unhandledClauses);
+ return GetPagedQueryCommandBuilders(ref query, -1, out unhandledClauses);
}
- private ICommandBuilder[] GetPagedQueryCommandBuilders(SimpleQuery query, Int32 bulkIndex,
+ private ICommandBuilder[] GetPagedQueryCommandBuilders(ref SimpleQuery query, Int32 bulkIndex,
out IEnumerable<SimpleQueryClauseBase> unhandledClauses)
{
var commandBuilders = new List<ICommandBuilder>();
@@ -120,7 +121,6 @@ public AdoAdapterQueryRunner(AdoAdapter adapter)
unhandledClausesForPagedQuery);
unhandledClausesList.AddRange(unhandledClausesForPagedQuery);
-
SkipClause skipClause = query.Clauses.OfType<SkipClause>().FirstOrDefault();
TakeClause takeClause = query.Clauses.OfType<TakeClause>().FirstOrDefault();
@@ -129,7 +129,8 @@ public AdoAdapterQueryRunner(AdoAdapter adapter)
var queryPager = _adapter.ProviderHelper.GetCustomProvider<IQueryPager>(_adapter.ConnectionProvider);
if (queryPager == null)
{
- DeferPaging(query, mainCommandBuilder, commandBuilders, unhandledClausesList);
+ Trace.TraceWarning("There is no database-specific query paging in your current Simple.Data Provider. Paging will be done in memory.");
+ DeferPaging(ref query, mainCommandBuilder, commandBuilders, unhandledClausesList);
}
else
{
@@ -139,11 +140,12 @@ public AdoAdapterQueryRunner(AdoAdapter adapter)
return commandBuilders.ToArray();
}
- private void DeferPaging(SimpleQuery query, ICommandBuilder mainCommandBuilder, List<ICommandBuilder> commandBuilders,
+ private void DeferPaging(ref SimpleQuery query, ICommandBuilder mainCommandBuilder, List<ICommandBuilder> commandBuilders,
List<SimpleQueryClauseBase> unhandledClausesList)
{
unhandledClausesList.AddRange(query.OfType<SkipClause>());
unhandledClausesList.AddRange(query.OfType<TakeClause>());
+ query = query.ClearSkip().ClearTake();
var commandBuilder = new CommandBuilder(mainCommandBuilder.Text, _adapter.GetSchema(),
mainCommandBuilder.Parameters);
commandBuilders.Add(commandBuilder);
@@ -172,23 +174,23 @@ public AdoAdapterQueryRunner(AdoAdapter adapter)
new CommandBuilder(commandText, _adapter.GetSchema(), mainCommandBuilder.Parameters)));
}
- private ICommandBuilder[] GetQueryCommandBuilders(SimpleQuery query,
+ private ICommandBuilder[] GetQueryCommandBuilders(ref SimpleQuery query,
out IEnumerable<SimpleQueryClauseBase> unhandledClauses)
{
if (query.Clauses.OfType<TakeClause>().Any() || query.Clauses.OfType<SkipClause>().Any())
{
- return GetPagedQueryCommandBuilders(query, out unhandledClauses);
+ return GetPagedQueryCommandBuilders(ref query, out unhandledClauses);
}
return new[] {new QueryBuilder(_adapter).Build(query, out unhandledClauses)};
}
- private IEnumerable<ICommandBuilder> GetQueryCommandBuilders(SimpleQuery query, Int32 bulkIndex,
+ private IEnumerable<ICommandBuilder> GetQueryCommandBuilders(ref SimpleQuery query, Int32 bulkIndex,
out IEnumerable<SimpleQueryClauseBase>
unhandledClauses)
{
if (query.Clauses.OfType<TakeClause>().Any() || query.Clauses.OfType<SkipClause>().Any())
{
- return GetPagedQueryCommandBuilders(query, bulkIndex, out unhandledClauses);
+ return GetPagedQueryCommandBuilders(ref query, bulkIndex, out unhandledClauses);
}
return new[] {new QueryBuilder(_adapter, bulkIndex).Build(query, out unhandledClauses)};
}
@@ -206,7 +208,7 @@ public AdoAdapterQueryRunner(AdoAdapter adapter)
for (int i = 0; i < queries.Length; i++)
{
IEnumerable<SimpleQueryClauseBase> unhandledClausesForThisQuery;
- commandBuilders.AddRange(GetQueryCommandBuilders(queries[i], i, out unhandledClausesForThisQuery));
+ commandBuilders.AddRange(GetQueryCommandBuilders(ref queries[i], i, out unhandledClausesForThisQuery));
unhandledClauses.Add(unhandledClausesForThisQuery);
}
IDbConnection connection = _adapter.CreateConnection();
View
13 Simple.Data.Ado/Schema/DatabaseSchema.cs
@@ -14,6 +14,7 @@ public class DatabaseSchema
private readonly ISchemaProvider _schemaProvider;
private readonly Lazy<TableCollection> _lazyTables;
private readonly Lazy<ProcedureCollection> _lazyProcedures;
+ private string _defaultSchema;
private DatabaseSchema(ISchemaProvider schemaProvider, ProviderHelper providerHelper)
{
@@ -45,6 +46,10 @@ public IEnumerable<Table> Tables
public Table FindTable(string tableName)
{
+ if (!string.IsNullOrWhiteSpace(DefaultSchema) && !(tableName.Contains(".")))
+ {
+ tableName = DefaultSchema + "." + tableName;
+ }
return _lazyTables.Value.Find(tableName);
}
@@ -63,11 +68,11 @@ public Procedure FindProcedure(ObjectName procedureName)
return _lazyProcedures.Value.Find(procedureName);
}
- private String GetDefaultSchema()
+ private string DefaultSchema
{
- return _schemaProvider.GetDefaultSchema();
+ get { return _defaultSchema ?? (_defaultSchema = _schemaProvider.GetDefaultSchema() ?? string.Empty); }
}
-
+
private TableCollection CreateTableCollection()
{
return new TableCollection(_schemaProvider.GetTables()
@@ -110,7 +115,7 @@ public static void ClearCache()
public ObjectName BuildObjectName(String text)
{
if (text == null) throw new ArgumentNullException("text");
- if (!text.Contains('.')) return new ObjectName(this.GetDefaultSchema(), text);
+ if (!text.Contains('.')) return new ObjectName(this.DefaultSchema, text);
var schemaDotTable = text.Split('.');
if (schemaDotTable.Length != 2) throw new InvalidOperationException("Could not parse table name.");
return new ObjectName(schemaDotTable[0], schemaDotTable[1]);
View
2 Simple.Data.SqlTest/BulkInsertTest.cs
@@ -21,6 +21,8 @@ public void BulkInsertUsesSchema()
var list = db.test.SchemaTable.All().ToList();
Assert.AreEqual(1000, list.Count);
+
+ db.test.SchemaTable.DeleteAll();
}
private static IEnumerable<SchemaItem> GenerateItems()

0 comments on commit df48771

Please sign in to comment.