Skip to content

Commit

Permalink
ST harness and one passing test for querying by integer
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Oct 20, 2015
1 parent d89cb54 commit 09fb992
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 60 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,5 @@ FakesAssemblies/

# Visual Studio 6 workspace options file
*.opt

artifacts
2 changes: 1 addition & 1 deletion rakefile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

task :ci => [:default, :pack]

task :default => [:test]
task :default => [:test, :storyteller]

desc "Prepares the working directory for a new build"
task :clean do
Expand Down
8 changes: 4 additions & 4 deletions src/CommonAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[assembly: AssemblyDescription("Postgresql as a Document Db and Event Store for .Net Development")]
[assembly: AssemblyProduct("StructureMap")]
[assembly: AssemblyCopyright("Copyright 2015 Jeremy D. Miller et al. All rights reserved.")]
[assembly: AssemblyTrademark("80a3c19a62d3ff5dd33b0f59217b9fa41295000d")]
[assembly: AssemblyVersion("0.0.1.51125")]
[assembly: AssemblyFileVersion("0.0.1.51125")]
[assembly: AssemblyInformationalVersion("0.0.1.51125")]
[assembly: AssemblyTrademark("d89cb548ce4c7baa0842b7148aa26f35015408c2")]
[assembly: AssemblyVersion("0.0.1.51234")]
[assembly: AssemblyFileVersion("0.0.1.51234")]
[assembly: AssemblyInformationalVersion("0.0.1.51234")]
9 changes: 7 additions & 2 deletions src/Marten.Testing/ConnectionSource.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using FubuCore;
using Marten.Generation;
using Marten.Testing.Documents;
Expand All @@ -11,8 +12,12 @@ public class ConnectionSource : IConnectionFactory
{
private readonly static Lazy<string> _connectionString = new Lazy<string>(() =>
{
var path =
AppDomain.CurrentDomain.BaseDirectory.ParentDirectory().ParentDirectory().AppendPath("connection.txt");
var path = AppDomain.CurrentDomain.BaseDirectory.AppendPath("connection.txt");
if (!File.Exists(path))
{
path = AppDomain.CurrentDomain.BaseDirectory.ParentDirectory().ParentDirectory().AppendPath("connection.txt");
}


return new FileSystem().ReadStringFromFile(path);
});
Expand Down
25 changes: 15 additions & 10 deletions src/Marten.Testing/Fixtures/LinqFixture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using FubuCore;
Expand All @@ -21,6 +22,15 @@ public class LinqFixture : Fixture
public LinqFixture()
{
Title = "Linq Support";

// set the expressions
expression(x => x.Number == 1);
expression(x => x.Number > 3);
expression(x => x.Number < 3);
expression(x => x.Number <= 3);
expression(x => x.Number >= 3);

AddSelectionValues("Expressions", _wheres.Keys.ToArray());
}

public override void SetUp()
Expand All @@ -31,19 +41,13 @@ public override void SetUp()
_container = Container.For<DevelopmentModeRegistry>();
_session = _container.GetInstance<IDocumentSession>();

// set the expressions
expression(x => x.Number == 1);
expression(x => x.Number > 3);
expression(x => x.Number < 3);
expression(x => x.Number <= 3);
expression(x => x.Number >= 3);

AddSelectionValues("Expressions", _wheres.Keys.ToArray());
}

private void expression(Expression<Func<Target, bool>> where)
{
_wheres.Add(where.ToString(), where);
var key = @where.As<LambdaExpression>().Body.ToString().TrimStart('(').TrimEnd(')');
_wheres.Add(key, where);
}

public override void TearDown()
Expand All @@ -68,12 +72,13 @@ public IGrammar TheDocumentsAre()
}

[ExposeAsTable("Executing queries")]
public void ExecutingQuery([SelectionList("Expressions")]string WhereClause, out string Sql, out ResultSet Results)
public void ExecutingQuery([SelectionList("Expressions")]string WhereClause, out ResultSet Results)
{
var expression = _wheres[WhereClause];
var queryable = _session.Query<Target>().Where(expression);

Sql = queryable.As<MartenQueryable<Target>>().ToCommand().CommandText;
var sql = _session.As<DocumentSession>().BuildCommand<Target>(queryable).CommandText;
Debug.WriteLine(sql);

Results = new ResultSet(queryable.ToArray().Select(x => _idToName[x.Id]).ToArray());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Spec id="deb452b4-e9ce-49d9-ab54-77868463162c" maxRetries="0" tags="" lifecycle="Regression" name="Basic comparisons on a single property" lastUpdated="Tuesday, October 20, 2015" expirationPeriod="0">
<Linq id="2124a9c3-d8eb-444b-b94f-4c572b62545f">
<TheDocumentsAre>
<Rows id="cbbb10d5-daac-4b27-99ef-386a029dd2fe">
<TheDocumentsAre-row Name="A" Number="1" />
<TheDocumentsAre-row Name="B" Number="2" />
<TheDocumentsAre-row Name="C" Number="3" />
<TheDocumentsAre-row Name="D" Number="4" />
<TheDocumentsAre-row Name="E" Number="5" />
<TheDocumentsAre-row Name="F" Number="6" />
</Rows>
</TheDocumentsAre>
<ExecutingQuery>
<table id="724c8058-0089-4510-9f5c-bff8ff85b720">
<ExecutingQuery-row WhereClause="x.Number == 1" Sql="select data from mt_doc_Target where data -&gt;&gt; 'Number' = :arg0" Results="A" />
</table>
</ExecutingQuery>
</Linq>
</Spec>
66 changes: 35 additions & 31 deletions src/Marten/DocumentSession.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using FubuCore;
using Marten.Linq;
using Marten.Schema;
Expand All @@ -24,20 +22,50 @@ public class DocumentSession : IDocumentSession, IDocumentExecutor
private readonly IList<NpgsqlCommand> _deletes = new List<NpgsqlCommand>();
private readonly IConnectionFactory _factory;
private readonly IQueryParser _parser;
private readonly QueryParser _queryParser;
private readonly IDocumentSchema _schema;
private readonly ISerializer _serializer;

private readonly IList<object> _updates = new List<object>();

public DocumentSession(IDocumentSchema schema, ISerializer serializer, IConnectionFactory factory, IQueryParser parser)
public DocumentSession(IDocumentSchema schema, ISerializer serializer, IConnectionFactory factory,
IQueryParser parser)
{
_schema = schema;
_serializer = serializer;
_factory = factory;
_parser = parser;
}

T IQueryExecutor.ExecuteScalar<T>(QueryModel queryModel)
{
throw new NotImplementedException();
}

T IQueryExecutor.ExecuteSingle<T>(QueryModel queryModel, bool returnDefaultWhenEmpty)
{
throw new NotImplementedException();
}

IEnumerable<T> IQueryExecutor.ExecuteCollection<T>(QueryModel queryModel)
{
var command = BuildCommand<T>(queryModel);

return query<T>(command);
}

public NpgsqlCommand BuildCommand<T>(QueryModel queryModel)
{
var query = new DocumentQuery<T>(_schema.StorageFor(typeof (T)).TableName);
var @where = queryModel.BodyClauses.OfType<WhereClause>().FirstOrDefault();
if (@where != null)
{
query.Where = MartenExpressionParser.ParseWhereFragment(@where.Predicate);
}

var command = query.ToCommand();
return command;
}

public void Dispose()
{
}
Expand Down Expand Up @@ -182,34 +210,10 @@ private IEnumerable<T> query<T>(NpgsqlCommand cmd)
}
}

T IQueryExecutor.ExecuteScalar<T>(QueryModel queryModel)
{
throw new NotImplementedException();
}

T IQueryExecutor.ExecuteSingle<T>(QueryModel queryModel, bool returnDefaultWhenEmpty)
{
throw new NotImplementedException();
}

IEnumerable<T> IQueryExecutor.ExecuteCollection<T>(QueryModel queryModel)
{
var command = BuildCommand<T>(queryModel);

return query<T>(command);
}

public NpgsqlCommand BuildCommand<T>(QueryModel queryModel)
public NpgsqlCommand BuildCommand<T>(IQueryable<T> queryable)
{
var query = new DocumentQuery<T>(_schema.StorageFor(typeof (T)).TableName);
var @where = queryModel.BodyClauses.OfType<WhereClause>().FirstOrDefault();
if (@where != null)
{
query.Where = MartenExpressionParser.ParseWhereFragment(@where.Predicate);
}

var command = query.ToCommand();
return command;
var model = _parser.GetParsedQuery(queryable.Expression);
return BuildCommand<T>(model);
}
}
}
23 changes: 21 additions & 2 deletions src/Marten/Linq/MartenExpressionParser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Linq.Expressions;
using System.Reflection;
using FubuCore;
using Marten.Util;

namespace Marten.Linq
{
Expand All @@ -21,8 +23,12 @@ public static IWhereFragment GetWhereFragment(BinaryExpression binary)
switch (binary.NodeType)
{
case ExpressionType.Equal:
// TODO -- handle NULL differently I'd imagine
var value = Value(binary.Right);
var sql = "{0} = ?".ToFormat(JsonLocator(binary.Left));
return new WhereFragment(sql, Value(binary.Right));


return new WhereFragment(sql, value);
}

throw new NotSupportedException();
Expand All @@ -32,6 +38,8 @@ public static object Value(Expression expression)
{
if (expression is ConstantExpression)
{
// TODO -- handle nulls
// TODO -- check out more types here.
return expression.As<ConstantExpression>().Value;
}

Expand All @@ -42,7 +50,18 @@ public static string JsonLocator(Expression expression)
{
if (expression is MemberExpression)
{
return "data ->> '{0}'".ToFormat(expression.As<MemberExpression>().Member.Name);
var member = expression.As<MemberExpression>().Member;


var locator = "data ->> '{0}'".ToFormat(member.Name);
var memberType = member.GetMemberType();
if (memberType == typeof (int))
{
return "CAST({0} as integer)".ToFormat(locator);
}


return locator;
}

throw new NotSupportedException();
Expand Down
11 changes: 1 addition & 10 deletions src/Marten/Linq/MartenQueryable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ namespace Marten.Linq
{
public class MartenQueryable<T> : QueryableBase<T>
{
private readonly IDocumentExecutor _executor;
private readonly IQueryParser _queryParser;

public MartenQueryable(IQueryParser queryParser, IDocumentExecutor executor) : base(queryParser, executor)
{
_queryParser = queryParser;
_executor = executor;

}

public MartenQueryable(IQueryProvider provider) : base(provider)
Expand All @@ -24,10 +20,5 @@ public MartenQueryable(IQueryProvider provider) : base(provider)
public MartenQueryable(IQueryProvider provider, Expression expression) : base(provider, expression)
{
}

public NpgsqlCommand ToCommand()
{
return _executor.BuildCommand<T>(_queryParser.GetParsedQuery(Expression));
}
}
}
1 change: 1 addition & 0 deletions src/Marten/Marten.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<Compile Include="Schema\IDocumentStorage.cs" />
<Compile Include="Schema\SchemaClasses.cs" />
<Compile Include="Util\CommandExtensions.cs" />
<Compile Include="Util\MemberInfoExtensions.cs" />
<Compile Include="Util\StringExtensions.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
17 changes: 17 additions & 0 deletions src/Marten/Util/MemberInfoExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Reflection;
using FubuCore;

namespace Marten.Util
{
public static class MemberInfoExtensions
{
public static Type GetMemberType(this MemberInfo member)
{
if (member is FieldInfo) return member.As<FieldInfo>().FieldType;
if (member is PropertyInfo) return member.As<PropertyInfo>().PropertyType;

throw new NotSupportedException();
}
}
}

0 comments on commit 09fb992

Please sign in to comment.