Skip to content

Instantly share code, notes, and snippets.

@AVee
Created January 27, 2015 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AVee/0cb0dc1912698fcc43df to your computer and use it in GitHub Desktop.
Save AVee/0cb0dc1912698fcc43df to your computer and use it in GitHub Desktop.
Autoquery join issues
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack;
using ServiceStack.Data;
using ServiceStack.DataAnnotations;
using ServiceStack.OrmLite;
namespace OrmLiteTest
{
class Program
{
static void Main(string[] args)
{
AppHost appHost = new AppHost();
appHost.Init().Start("http://localhost:88/");
JsonServiceClient client = new JsonServiceClient("http://localhost:88/");
// Succeeds
client.Get(new Request() { Id = 1 } );
// Succeeds
client.Get(new CustomRequest() { Id = 1 } );
// Fails, with SQL error
client.Get(new AutoQueryRequest());
// Fails, but doesn't actually join
client.Get(new AutoQueryRequest2());
// Fails, with SQL error
client.Get("autoquery2?format=json&SellerNameStartsWith=B");
Console.ReadKey();
}
}
public class MyService : Service
{
public IDbConnectionFactory dbFactory { get; set; }
public IAutoQuery AutoQuery { get; set; }
public QueryResponse<CustomPurchase> Any(AutoQueryRequest request)
{
var q = AutoQuery.CreateQuery(request, Request.GetRequestParams());
return AutoQuery.Execute(request, q);
}
public QueryResponse<CustomPurchase> Any(AutoQueryRequest2 request)
{
var q = AutoQuery.CreateQuery(request, Request.GetRequestParams());
return AutoQuery.Execute(request, q);
}
public Purchase Any(Request request)
{
IDbConnection db = dbFactory.OpenDbConnection();
return db.SingleById<Purchase>(request.Id);
}
public CustomPurchase Any(CustomRequest request)
{
IDbConnection db = dbFactory.OpenDbConnection();
return db.SingleById<CustomPurchase>(request.Id);
}
}
[Route("/autoquery")]
public class AutoQueryRequest : QueryBase<Purchase, CustomPurchase>, IJoin<Purchase, Seller>, IJoin<Purchase, Buyer>
{ }
[Route("/autoquery2")]
public class AutoQueryRequest2 : QueryBase<CustomPurchase>
{ }
[Route("/test")]
public class Request : IReturn<Purchase>
{
public int Id { get; set; }
}
[Route("/test2")]
public class CustomRequest : IReturn<CustomPurchase>
{
public int Id { get; set; }
}
[Alias("Purchase")]
public class CustomPurchase
{
public int Id { get; set; }
public string Description { get; set; }
public string SellerName { get; set; }
public string BuyerName { get; set; }
}
[Alias("Person")]
public class Buyer : Person {}
[Alias("Person")]
public class Seller : Person {}
public class Purchase
{
public int Id { get; set; }
public string Description { get; set; }
[References(typeof(Person))]
public int SellerId { get; set; }
[References(typeof(Person))]
public int BuyerId { get; set; }
[Reference]
public Person Buyer { get; set; }
[Reference]
public Person Seller { get; set; }
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
public class AppHost : AppSelfHostBase
{
public AppHost()
: base("TestApp", typeof(AppHost).Assembly)
{ }
public override void Configure(Funq.Container container)
{
container.Register<IDbConnectionFactory>(
new OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider));
using (var db = container.Resolve<IDbConnectionFactory>().Open())
{
db.DropAndCreateTable<Person>();
db.DropAndCreateTable<Purchase>();
db.InsertAll(testPersons);
db.InsertAll(testPurchases);
}
Plugins.Add(new AutoQueryFeature() { MaxLimit = null });
}
private List<Person> testPersons = new List<Person>()
{
new Person() { Id = 1, Name = "Rose Tyler" }, new Person() { Id = 2, Name = "Martha Jones" }, new Person() { Id=3, Name="Donna Noble" }
};
private List<Purchase> testPurchases = new List<Purchase>()
{
new Purchase() { Id = 1, Description = "Sonic Screwdriver", BuyerId = 1, SellerId = 2 },
new Purchase() { Id = 2, Description = "Bow Tie", BuyerId = 3, SellerId = 4 },
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment