简体   繁体   中英

Calling sqlite function via ServiceStack.ORMLite

I'm using ServiceStack.ORMLite and SQLite as database. I've created a generic repository:

public class Repository<T> : IRepository<T> where T : class, new()
{
    private ReestrContext db;

    public Repository(ReestrContext db)
    {
        this.db = db;
    }

    public long CountAll()
    {
        return db.Connection.Count<T>();
    }

    public IQueryable<T> GetAll()
    {
        return db.Connection.SelectLazy(db.Connection.From<T>().Limit()).AsQueryable();
    }

    public IQueryable<T> GetAll(int rows)
    {
        return db.Connection.SelectLazy(db.Connection.From<T>().Limit(rows)).AsQueryable();
    }

    public IQueryable<T> GetAll(int? skip, int? rows)
    {
        return db.Connection.SelectLazy(db.Connection.From<T>().Limit(skip, rows)).AsQueryable();
    }

    public T GetById(long id)
    {
        return db.Connection.LoadSingleById<T>(id);
    }

    public long CountByCondition(Expression<Func<T, bool>> predicate)
    {
        long res = db.Connection.Count<T>(predicate);
        string qry = db.Connection.GetLastSql();
        return db.Connection.Count(predicate);
    }

    public IQueryable<T> GetByCondition(Expression<Func<T, bool>> predicate)
    {
        return db.Connection.SelectLazy(db.Connection.From<T>().Where(predicate).Limit()).AsQueryable();
    }

    public IQueryable<T> GetByCondition(Expression<Func<T, bool>> predicate, int rows)
    {
        return db.Connection.SelectLazy(db.Connection.From<T>().Where(predicate).Limit(rows)).AsQueryable();
    }

    public IQueryable<T> GetByCondition(Expression<Func<T, bool>> predicate, int? skip, int? rows)
    {
        return db.Connection.SelectLazy(db.Connection.From<T>().Where(predicate).Limit(skip, rows)).AsQueryable();
    }

    public long Create(T item)
    {
        using (var trans = db.Transaction)
        {
            long res = db.Connection.Insert(item, selectIdentity: true);
            try
            {
                trans.Commit();
            }
            catch
            {
                trans.Rollback();
            }
            return res;
        }
    }

    public T Update(T item)
    {
        using (var trans = db.Transaction)
        {
            db.Connection.Update(item);
            try
            {
                trans.Commit();
            }
            catch
            {
                trans.Rollback();
            }
            return item;
        }
    }

    public long Delete(long id)
    {
        using (var trans = db.Transaction)
        {
            long res = db.Connection.Delete(id);
            try
            {
                trans.Commit();
            }
            catch
            {
                trans.Rollback();
            }
            return res;
        }
    }
}

On a client side I created filter function that returns an expression tree. But my POCO class has

[Alias("Bd")]
[DataType(DataType.Date)]
public Nullable<DateTime> BirthdaySingle { get; set; }

field which also is using in filter conditon. So, I can't find the solution to correctly create filter on this field (because expression tree doesn't handle it) and I want to know what could be another solution to achieve such filtering. Does ORMLite support calling SQLite functions? In my case it needs to be "DATE" function. Or maybe it uses System.ComponentModel.DataAnnotations namespace to set [DataType(DataType.Date)] attribute on string field. I don't know. Help me please.

It looks like your code has referenced LINQ's Expression<T> tree instead of OrmLite's SqlExpression<T> tree. They look similar, but OrmLite only has support to transform SqlExpression<T> lambdas into queries.

I would recommend playing around in the newly-released OrmLite Gistlyn sandbox to quickly test out your ORM code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM