在ServiceStack AutoQuery中多次连接同一个表

时间:2015-01-28 10:30:43

标签: c# servicestack ormlite-servicestack

我试图对表A使用ServiceStack的Auto Query功能,表A多次引用另一个表B,但无法使其工作。

这似乎是AutoQuery生成连接而没有对连接表进行别名,导致列不明确。它对SQL Server和Sqlite都有类似的错误。我还没有找到办法解决这个问题。

这基本上就是我所做的:

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; }
}

[Route("/autoquery")]
public class AutoQueryRequest : QueryBase<Purchase, CustomPurchase>, IJoin<Purchase, Seller>, IJoin<Purchase, Buyer>
{ }

完整的代码可以在这里找到: https://gist.github.com/AVee/0cb0dc1912698fcc43df

1 个答案:

答案 0 :(得分:2)

OrmLite(以及扩展名为AutoQuery)不支持Table JOIN的自定义别名,因此您无法通过AutoQuery查询单个表。

虽然正常用例:

[Route("/purchase/query")]
public class QueryPurchase : QueryBase<Purchase>
{
    public int Id { get; set; }
}

client.Get(new QueryPurchase { Id = 1 }).PrintDump();

将加入并填充multiple self references

{
        Offset: 0,
        Total: 1,
        Results:
        [
                {
                        Id: 1,
                        Description: Sonic Screwdriver,
                        SellerId: 2,
                        BuyerId: 1,
                        Buyer:
                        {
                                Id: 1,
                                Name: Rose Tyler
                        },
                        Seller:
                        {
                                Id: 2,
                                Name: Martha Jones
                        }
                }
        ]
}

OrmLite中还有一个新的CustomJoin API,可让您指定自己的Aliases on JOINS

使用自定义AutoQuery实现

由于您无法查询多个自引用表,因此一种方法是使用自定义AutoQuery实现,您可以在自定义AutoQuery实现中查询子表ID。

为此,我们将创建一个额外的“支持”AutoQuery DTO QueryPurchase,其中包含AutoQuery可以处理的属性,而CustomPurchase AutoQuery仍然是您要为此服务公开的面向公众的API ,例如:

[Alias("Purchase")]
public class CustomPurchase : QueryBase<Purchase>
{
    public int? Id { get; set; }
    public string Description { get; set; }
    public string SellerName { get; set; }
    public string BuyerName { get; set; }
}

public class QueryPurchase : QueryBase<Purchase>
{
    public int? Id { get; set; }
    public string Description { get; set; }
    public List<int> BuyerIds { get; set; }
    public List<int> SellerIds { get; set; }
}

在您的自定义AutoQuery实现中,您可以获取相应的买方和卖方ID并将其添加到支持QueryPurchase查询中,例如:

public QueryResponse<Purchase> Any(CustomPurchase request)
{
    var qPurchase = request.ConvertTo<QueryPurchase>();

    if (request.BuyerName != null)
    {
        qPurchase.BuyerIds = Db.Column<int>(Db.From<Person>()
            .Where(x => x.Name == request.BuyerName)
            .Select(x => x.Id));
    }

    if (request.SellerName != null)
    {
        qPurchase.SellerIds = Db.Column<int>(Db.From<Person>()
            .Where(x => x.Name == request.SellerName)
            .Select(x => x.Id));
    }

    var q = AutoQuery.CreateQuery(qPurchase, Request.GetRequestParams());

    return AutoQuery.Execute(qPurchase, q);
}

AutoQuery了解BuyerIds等集合属性,并在BuyerId上执行相应的查询。

所以现在调用此服务时:

client.Get(new CustomPurchase { BuyerName = "Rose Tyler" }).PrintDump();

它将打印所需的:

{
        Offset: 0,
        Total: 1,
        Results:
        [
                {
                        Id: 1,
                        Description: Sonic Screwdriver,
                        SellerId: 2,
                        BuyerId: 1,
                        Buyer:
                        {
                                Id: 1,
                                Name: Rose Tyler
                        },
                        Seller:
                        {
                                Id: 2,
                                Name: Martha Jones
                        }
                }
        ]
}

附加自定义条件

与此类似,另一种方法是为AutoQuery本身添加自定义条件,为此我们不再需要BuyerIdsSellerIds属性,例如:

public class QueryPurchase : QueryBase<Purchase>
{
    public int? Id { get; set; }
    public string Description { get; set; }
}

QueryPurchase DTO拥有AutoQuery可以处理的Purchase表属性减去需要特殊的SellerNameBuyerName,例如:

public QueryResponse<Purchase> Any(CustomPurchase request)
{
    //Copy only the properties that AutoQuery can handle
    var qPurchase = request.ConvertTo<QueryPurchase>();
    var q = AutoQuery.CreateQuery(qPurchase, Request.GetRequestParams());

    //Add Custom SQL Conditions for each Custom Query
    if (request.BuyerName != null)
        q.UnsafeWhere("BuyerId IN (SELECT Id FROM Person WHERE Name = {0})", 
            request.BuyerName);

    if (request.SellerName != null)
        q.UnsafeWhere("SellerId IN (SELECT Id FROM Person WHERE Name = {0})", 
            request.SellerName);

    return AutoQuery.Execute(qPurchase, q);
}

这与上面的自定义AutoQuery实现类似,只是它被添加到单个AutoQuery查询中。

同样调用此服务:

csharp client.Get(new CustomPurchase { BuyerName = "Rose Tyler" }).PrintDump();

还打印所需的:

{
        Offset: 0,
        Total: 1,
        Results:
        [
                {
                        Id: 1,
                        Description: Sonic Screwdriver,
                        SellerId: 2,
                        BuyerId: 1,
                        Buyer:
                        {
                                Id: 1,
                                Name: Rose Tyler
                        },
                        Seller:
                        {
                                Id: 2,
                                Name: Martha Jones
                        }
                }
        ]
}

为了支持这一点,OrmLite的UnsafeWhere有一个新的SqlExpression API,以便能够像上面添加的子SELECT一样添加未经验证的原始SQL。参数仍然被转义,这将阻止任何SQL注入。此更改可从 v4.0.37 + 获得,现在为available on MyGet