动态创建自定义名称表并插入自定义表名 [英] Create table with custom name dynamically and insert with custom table name

查看:44
本文介绍了动态创建自定义名称表并插入自定义表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用自定义名称创建表,但找不到示例代码.我注意到创建表的唯一方法是通过像 db.CreateTable() 这样的泛型类型.我可以知道是否有办法动态创建表名而不是使用别名?原因是因为有时我们希望将相同的对象类型存储到不同的表中,例如 2015_january_activity、2015_february_activity.

I want to create the table with custom name but I cannot find the sample code. I notice the only way to create table is by generic type like db.CreateTable(). May I know if there is a way to create the table name dynamically instead of using Alias? The reason is because sometime we want to store the same object type into different tables like 2015_january_activity, 2015_february_activity.

除此之外,db.Insert 也非常受限于对象类型.无论如何要通过传入表名来插入?

Apart from this, the db.Insert also very limited to object type. Is there anyway to insert by passing in the table name?

我认为这些特性非常重要,因为它在 NoSQL 解决方案中存在很长时间并且非常灵活.谢谢.

I think these features are very important as it exists in NoSQL solution for long and it's very flexible. Thanks.

推荐答案

OrmLite 主要是代码优先的 ORM,它使用类型化 POCO 创建和查询匹配 RDMBS 表的架构.它还支持使用 自定义 SQL API.

OrmLite is primarily a code-first ORM which uses typed POCO's to create and query the schema of matching RDMBS tables. It also supports executing Custom SQL using the Custom SQL API's.

使用不同表名的一个选项是在运行时更改别名,如在之前的答案中所见可以创建自定义扩展方法来修改表的名称,例如:

One option to use a different table name is to change the Alias at runtime as seen in this previous answer where you can create custom extension methods to modify the name of the table, e.g:

public static class GenericTableExtensions 
{
    static object ExecWithAlias<T>(string table, Func<object> fn)
    {
        var modelDef = typeof(T).GetModelMetadata();
        lock (modelDef) {
            var hold = modelDef.Alias;
            try {
                modelDef.Alias = table;
                return fn();
            }
            finally {
                modelDef.Alias = hold;
            }
        }
    }

    public static void DropAndCreateTable<T>(this IDbConnection db, string table) {
        ExecWithAlias<T>(table, () => { db.DropAndCreateTable<T>(); return null; });
    }

    public static long Insert<T>(this IDbConnection db, string table, T obj, bool selectIdentity = false) {
        return (long)ExecWithAlias<T>(table, () => db.Insert(obj, selectIdentity));
    }

    public static List<T> Select<T>(this IDbConnection db, string table, Func<SqlExpression<T>, SqlExpression<T>> expression) {
        return (List<T>)ExecWithAlias<T>(table, () => db.Select(expression));
    }

    public static int Update<T>(this IDbConnection db, string table, T item, Expression<Func<T, bool>> where) {
        return (int)ExecWithAlias<T>(table, () => db.Update(item, where));
    }
}

这些扩展方法提供了额外的 API,让您可以更改所使用的表的名称,例如:

These extension methods provide additional API's that let you change the name of the table used, e.g:

var tableName = "TableA"'
db.DropAndCreateTable<GenericEntity>(tableName);

db.Insert(tableName, new GenericEntity { Id = 1, ColumnA = "A" });

var rows = db.Select<GenericEntity>(tableName, q =>
    q.Where(x => x.ColumnA == "A"));

rows.PrintDump();

db.Update(tableName, new GenericEntity { ColumnA = "B" },
    where: q => q.ColumnA == "A");

rows = db.Select<GenericEntity>(tableName, q => 
    q.Where(x => x.ColumnA == "B"));

rows.PrintDump();

这个例子也可以在 GenericTableExpressions.cs 集成测试.

This example is also available in the GenericTableExpressions.cs integration test.

这篇关于动态创建自定义名称表并插入自定义表名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆