flutter_orm_plugin 1.0.9

  • Readme
  • Changelog
  • Example
  • Installing
  • 86

移动端orm框架性能测评 #

flutter_orm_plugin 发布以来,不少团队试用了,我发现大家对这类数据库相关的库,第一反应就是性能如何,之前确实没做太多行业对比,最近觉得还是有必要做一下性能测试,给大家一个交代的。

在ios端,业界比较常用的orm框架应该是苹果官方推出的coredata,还有就是realm了。在android端orm框架我挑了三个比较常用的,greendao,realm和activeandroid。我会用flutter_orm_plugin跟上面提到的ios和android端orm框架做对比。 下面我会分别给出测试用例,测试代码,还有最终数据比较的结果。

测试用例 #

测试用例我列了以下这些

  • 10000次插入数据
  • 使用批量接口10000次插入数据
  • 10000次读取数据
  • 10000次修改数据
  • 使用批量接口10000次修改数据
  • 10000次删除数据
  • 使用批量接口10000次删除数据

为什么会有普通插入数据和使用批量接口插入数据的区别,大部分orm框架都会对批量操作有一定的优化,所以需要对批量操作进行测试,但是在平时使用,不一定都能用上批量接口(例如多次数据操作不在同一代码块,或者在不同的模块中都要操作数据),所以我们会分别对普通操作和批量操作进行测试。

android 测试代码 #

首先我们给出flutter_orm_plugin 的测试代码,由于不想因为flutter和原生channel通讯产生误差,我们直接用Luakit来写lua代码做测试(greendao、realm、activeandroid、coredata都不涉及flutter和原生channel通讯),flutter_orm_plugin其实底层就是luakit的orm框架,这个不影响测试准确性。

循环插入 #

Luakit定义orm模型结构并做10000次插入,下面的代码是ios和android通用的。

	local Student = {
        __dbname__ = "test.db",
        __tablename__ = "Student",
        studentId = {"TextField",{primary_key = true}},
        name = {"TextField",{}},
        claName = {"TextField",{}},
        teacherName = {"TextField",{}},
        score = {"RealField",{}},
    }
     
    local params = {
        name = "Student",
        args = Student,
    }
    
    Table.addTableInfo(params,function ()
    	local studentTable = Table("Student”)
	    for i=1,10000 do
	           local s = {
	               studentId = "studentId"..i,
	               name = "name"..i,
	               claName = "claName"..i,
	               teacherName = "teacherName"..i,
	               score = 90,
	           }
	           studentTable(s):save()
	    end
 	end)

activeandroid定义orm模型结构并做10000次插入


@Table(name = "Students")
public class Student extends Base {
    @Column(name = "studentId")
    public String studentId;
    @Column(name = "name")
    public String name;
    @Column(name = "claName")
    public String claName;
    @Column(name = "teacherName")
    public String teacherName;
    @Column(name = "score")
    public float score;
    public Student() {
        super();
    }
    @Override
    public String toString() {
        return this.studentId;
    }
}

for (int i=0 ; i<10000 ;i++) {
    ActiveAndroid.beginTransaction();
    Student s = new Student();
    s.studentId = "studentId"+i;
    s.name = "name"+i;
    s.teacherName = "teacherName"+i;
    s.claName = "claName"+i;
    s.score = 90;
    s.save();
    ActiveAndroid.setTransactionSuccessful();
    ActiveAndroid.endTransaction();
}

realm android 定义orm模型结构并做10000次插入


public class StudentRealm extends RealmObject {
    @PrimaryKey
    private String studentId;
    @Required
    private String name;
    @Required
    private String teacherName;
    @Required
    private String claName;
    private float score;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getClaName() {
        return claName;
    }
    public void setClaName(String ClaName) {
        this.claName = claName;
    }
    public String getTeacherName() {
        return teacherName;
    }
    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }
    public String getStudentId() {
        return studentId;
    }
    public void setStudentId(String id) {
        this.studentId = id;
    }
    public float getScore() {
        return score;
    }
    public void setScore(float score) {
        this.score = score;
    }
}
for (int i=0 ; i<10000 ;i++) {
    realm.beginTransaction();
    StudentRealm realmStudent = realm.createObject(StudentRealm.class,"studentId"+i);
    realmStudent.setName("name"+i);
    realmStudent.setTeacherName("setTeacherName"+i);
    realmStudent.setClaName("setClaName"+i);
    realmStudent.setScore(90);
    realm.commitTransaction();
}


GreenDao定义orm模型结构并做10000次插入

@Entity()
public class Student {
    @Id
    private String studentId;
    @NotNull
    private String name;
    private String claName;
    private String teacherName;
    private float score;
    @Generated(hash = 1491230551)
    public Student(String studentId, @NotNull String name, String claName, String teacherName,
            float score) {
        this.studentId = studentId;
        this.name = name;
        this.claName = claName;
        this.teacherName = teacherName;
        this.score = score;
    }

    @Generated(hash = 1556870573)
    public Student() {
    }

    public String getStudentId() {
        return studentId;
    }
    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }
    @NotNull
    public String getName() {
        return name;
    }
    /** Not-null value; ensure this value is available before it is saved to the database. */
    public void setName(@NotNull String name) {
        this.name = name;
    }
    public String getClaName() {
        return claName;
    }
    public void setClaName(String claName) {
        this.claName = claName;
    }
    public String getTeacherName() {
        return teacherName;
    }
    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }
    public float getScore() {
        return score;
    }
    public void setScore(float score) {
        this.score = score;
    }
}

DaoSession daoSession = ((App) getApplication()).getDaoSession();

StudentDao sd = daoSession.getStudentDao();
for (int i = 0; i < 10000; i++) {
    Student s = new Student();
    s.setStudentId("StudentId"+i);
    s.setClaName("getClaName"+i);
    s.setScore(90);
    s.setName("name"+i);
    s.setTeacherName("tn"+i);
    sd.insertOrReplace(s);
}

批量插入 #

Luakit没有提供批量插入接口。

active android批量插入10000条数据。


ActiveAndroid.beginTransaction();
for (int i=0 ; i<10000 ;i++) {
    Student s = new Student();
    s.studentId = "studentId"+i;
    s.name = "name"+i;
    s.teacherName = "teacherName"+i;
    s.claName = "claName"+i;
    s.score = 90;
    s.save();
}
ActiveAndroid.setTransactionSuccessful();
ActiveAndroid.endTransaction();


realm android批量插入10000条数据。


Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
for (int i=0 ; i<10000 ;i++) {
    StudentRealm realmStudent = realm.createObject(StudentRealm.class,"studentId"+i);
    realmStudent.setName("name"+i);
    realmStudent.setTeacherName("setTeacherName"+i);
    realmStudent.setClaName("setClaName"+i);
    realmStudent.setScore(90);
}
realm.commitTransaction();

GreenDao批量插入10000条数据


DaoSession daoSession = ((App) getApplication()).getDaoSession();
StudentDao sd = daoSession.getStudentDao();
ArrayList<Student> ss = new ArrayList<Student>();
for (int i = 0; i < 10000; i++) {
    Student s = new Student();
    s.setStudentId("StudentId"+i);
    s.setClaName("getClaName"+i);
    s.setScore(90);
    s.setName("name"+i);
    s.setTeacherName("tn"+i);
    ss.add(s);
}
sd.insertOrReplaceInTx(ss);

###数据查询

Luakit做10000次查询,下面的代码是ios和android通用的。


local studentTable = Table("Student")
for i=1,10000 do
     local result = studentTable.get:where({"studentId"..i},"studentId = ?"):all()
end


active android做10000次查询。


for (int i=0 ; i<10000 ;i++) {
    List<Student> student = new Select()
            .from(Student.class)
            .where("studentId = ?", "studentId"+i)
            .execute();
}


realm android 做10000次查询。


for (int i=0 ; i<10000 ;i++) {
    RealmResults<StudentRealm> students = realm.where(StudentRealm.class).equalTo("studentId", "studentId"+i).findAll();
    List<StudentRealm> list = realm.copyFromRealm(students);
}

GreenDao 做10000次查询


DaoSession daoSession = ((App) getApplication()).getDaoSession();
StudentDao sd = daoSession.getStudentDao();
for (int i = 0; i < 10000; i++) {
    List<Student> s = sd.queryBuilder()
            .where(StudentDao.Properties.StudentId.eq("StudentId"+i))
            .list();
}

###循环更新

Luakit做10000次更新。


local studentTable = Table("Student")
for i=1,10000 do
    local result = studentTable.get:where({"studentId"..i},"studentId = ?"):update({name = "name2”})
end


active android做10000次更新。


for (int i=0 ; i<10000 ;i++) {
    ActiveAndroid.beginTransaction();
    Update update = new Update(Student.class);
    update.set("name = ?","name2")
            .where("studentId = ?", "studentId"+i)
            .execute();
    ActiveAndroid.setTransactionSuccessful();
    ActiveAndroid.endTransaction();
}


realm android做10000次更新。


for (int i=0 ; i<10000 ;i++) {
    realm.beginTransaction();
    StudentRealm student = realm.where(StudentRealm.class).equalTo("studentId", "studentId"+i).findFirst();
    student.setClaName("ClaName"+(i+1));
    realm.copyToRealmOrUpdate(student);
    realm.commitTransaction();
}


GreenDao做10000次更新。


for (int i = 0; i < 10000; i++) {
    List<Student> s = sd.queryBuilder()
            .where(StudentDao.Properties.StudentId.eq("StudentId"+i))
            .list();
    s.get(0).setName("name2");
    sd.update(s.get(0));
}

###批量更新

Luakit没有批量更新接口。

active android批量更新10000条数据。


ActiveAndroid.beginTransaction();
for (int i=0 ; i<10000 ;i++) {
    Update update = new Update(Student.class);
    update.set("name = ?","name2")
            .where("studentId = ?", "studentId"+i)
            .execute();
}
ActiveAndroid.setTransactionSuccessful();
ActiveAndroid.endTransaction();

realm android批量更新10000条数据。


realm.beginTransaction();
for (int i=0 ; i<10000 ;i++) {
    StudentRealm student = realm.where(StudentRealm.class).equalTo("studentId", "studentId"+i).findFirst();
    student.setClaName("ClaName"+(i+1));
    realm.copyToRealmOrUpdate(student);
}
realm.commitTransaction();


GreenDao批量更新10000条数据


ArrayList<Student> ss = new ArrayList<Student>();
for (int i = 0; i < 10000; i++) {
    List<Student> s = sd.queryBuilder()
            .where(StudentDao.Properties.StudentId.eq("StudentId"+i))
            .list();
    s.get(0).setName("name2");
    ss.add(s.get(0));
}
sd.updateInTx(ss);


###循环删除

Luakit做10000次删除操作。


local studentTable = Table("Student")
for i=1,10000 do
     studentTable.get:where({"studentId"..i},"studentId = ?"):delete()
end

active android做10000次删除操作。


for (int i=0 ; i<10000 ;i++) {
    ActiveAndroid.beginTransaction();
    new Delete().from(Student.class).where("studentId = ?", "studentId"+i).execute();
    ActiveAndroid.setTransactionSuccessful();
    ActiveAndroid.endTransaction();
}

realm android做10000次删除操作。


for (int i=0 ; i<10000 ;i++) {
    realm.beginTransaction();
    StudentRealm student = realm.where(StudentRealm.class).equalTo("studentId", "studentId"+i).findFirst();
    student.deleteFromRealm();
    realm.commitTransaction();
}


GreenDao做10000次删除操作。


for (int i = 0; i < 10000; i++) {
    List<Student> s = sd.queryBuilder()
            .where(StudentDao.Properties.StudentId.eq("StudentId"+i))
            .list();
    s.get(0).setName("name2");
    sd.delete(s.get(0));
}

###批量删除

Luakit没有批量删除接口。

active android批量删除10000条数据。


ActiveAndroid.beginTransaction();
for (int i=0 ; i<10000 ;i++) {
    new Delete().from(Student.class).where("studentId = ?", "studentId"+i).execute();
}
ActiveAndroid.setTransactionSuccessful();
ActiveAndroid.endTransaction();


realm android批量删除10000条数据。


realm.beginTransaction();
for (int i=0 ; i<10000 ;i++) {
    StudentRealm student = realm.where(StudentRealm.class).equalTo("studentId", "studentId"+i).findFirst();
    student.deleteFromRealm();
}
realm.commitTransaction();

GreenDao批量删除10000条数据。


ArrayList<Student> ss = new ArrayList<Student>();
for (int i = 0; i < 10000; i++) {
    List<Student> s = sd.queryBuilder()
            .where(StudentDao.Properties.StudentId.eq("StudentId"+i))
            .list();
    ss.add(s.get(0));
}
sd.deleteInTx(ss);


android 测试结果及分析 #

下面给出测试结果,表格中所有数据的单位是秒,即做10000次操作需要的秒数。

image

image

image

  • 可以看到,active android各项性能都一般。

  • 在使用批量接口的情况下GreenDao和Realm的性能比较好。

  • 在使用批量接口的情况下Realm的性能尤其好,批量插入、查询、批量更改、批量删除都是Realm的性能最好,但是Realm的非批量接口性能较差,所有可以这样总结,如果代码高内聚,可以把数据操作代码入口都统一使用,Realm性能是最好的,但这对代码质量、模块设计有要求,当操作数据的代码到处都有,不能使用批量接口时,Realm的性能是不好的。

  • Luakit没有提供批量接口,但从图中可以看出,Luakit的各项性能指标都是比较好的,而且对代码没有要求,即使操作数据的代码不内聚,也不会对性能有影响。

ios测试代码 #

Luakit是跨平台的,代码跟android一样,下面就不列了,只给出Coredata和 Realm ios

循环插入 #

Coredata 定义orm模型结构并做10000次插入


@interface Student (CoreDataProperties)

+ (NSFetchRequest<Student *> *)fetchRequest;

@property (nullable, nonatomic, copy) NSString *claName;
@property (nullable, nonatomic, copy) NSString *name;
@property (nonatomic) float score;
@property (nullable, nonatomic, copy) NSString *studentId;
@property (nullable, nonatomic, copy) NSString *teacherName;

@end

self.context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
for (int i=0; i<10000; i++) {
    Student *s = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:self.context];
    s.studentId = [NSString stringWithFormat:@"studentId%d",i];
    s.name = [NSString stringWithFormat:@"name%d",i];
    s.teacherName = [NSString stringWithFormat:@"teacherName%d",i];
    s.claName = [NSString stringWithFormat:@"claName%d",i];
    s.score = 90;
    NSError *error = nil;
    [self.context save:&error];
}


Realm ios定义orm模型结构并做10000次插入


@interface StudentRLM : RLMObject

@property NSString *studentId;
@property NSString *name;
@property NSString *teacherName;
@property NSString *claName;
@property float score;

@end



for (int i=0; i<10000; i++) {
    [realm beginWriteTransaction];
    StudentRLM *s = [[StudentRLM alloc] init];
    s.studentId = [NSString stringWithFormat:@"studentId%d",i];;
    s.name = [NSString stringWithFormat:@"name%d",i];
    s.teacherName = [NSString stringWithFormat:@"teacherName%d",i];
    s.claName = [NSString stringWithFormat:@"claName%d",i];
    s.score = 90;
    [realm addOrUpdateObject:s];
    [realm commitWriteTransaction];
    [realm beginWriteTransaction];
}

批量插入 #

Coredata 批量插入10000条数据。


for (int i=0; i<10000; i++) {
    Student *s = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:self.context];
    s.studentId = [NSString stringWithFormat:@"studentId%d",i];
    s.name = [NSString stringWithFormat:@"name%d",i];
    s.teacherName = [NSString stringWithFormat:@"teacherName%d",i];
    s.claName = [NSString stringWithFormat:@"claName%d",i];
    s.score = 90;
    
}
NSError *error = nil;
[self.context save:&error];


Realm ios批量插入10000条数据。


[realm beginWriteTransaction];
for (int i=0; i<10000; i++) {
    StudentRLM *s = [[StudentRLM alloc] init];
    s.studentId = [NSString stringWithFormat:@"studentId%d",i];;
    s.name = [NSString stringWithFormat:@"name%d",i];
    s.teacherName = [NSString stringWithFormat:@"teacherName%d",i];
    s.claName = [NSString stringWithFormat:@"claName%d",i];
    s.score = 90;
    [realm addOrUpdateObject:s];
}
[realm commitWriteTransaction];
[realm beginWriteTransaction];


查询 #

Coredata 做10000次查询。


for (int i=0; i<10000; i++) {
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"];
    request.predicate = [NSPredicate predicateWithFormat:@"studentId = 'studentId%d'"];
    NSArray *objs = [self.context executeFetchRequest:request error:&error];
}

Realm ios做10000次查询。


for (int i=0; i<10000; i++) {
    RLMResults *results  = [StudentRLM objectsWhere: [NSString stringWithFormat:@"studentId = 'studentId%d'",i]];
    StudentRLM *s = results.firstObject;
}

###循环更新

Coredata 做10000次更新。


for (int i=0; i<10000; i++) {
    NSBatchUpdateRequest *batchUpdateRequest = [[NSBatchUpdateRequest alloc] initWithEntityName:@"Student"];
    batchUpdateRequest.predicate = [NSPredicate predicateWithFormat:@"studentId = 'studentId%d'"];
    batchUpdateRequest.propertiesToUpdate = @{@"name" : @"name2"};
    batchUpdateRequest.resultType = NSUpdatedObjectsCountResultType;
    NSBatchUpdateResult *batchResult = [self.context executeRequest:batchUpdateRequest error:&error];
    NSError *error = nil;
    [self.context save:&error];
}

Realm ios做10000次更新。


for (int i=0; i<10000; i++) {
    [realm beginWriteTransaction];
    RLMResults *results  = [StudentRLM objectsWhere: [NSString stringWithFormat:@"studentId = 'studentId%d'",i]];
    NSLog(@"results %lu",(unsigned long)[results count]);
    StudentRLM *s = results.firstObject;
    [s setName:@"name"];
    [realm addOrUpdateObject:s];
    [realm commitWriteTransaction];
}

###批量更新

Coredata 批量更新10000条数据。


for (int i=0; i<10000; i++) {
    NSBatchUpdateRequest *batchUpdateRequest = [[NSBatchUpdateRequest alloc] initWithEntityName:@"Student"];
    batchUpdateRequest.predicate = [NSPredicate predicateWithFormat:@"studentId = 'studentId%d'"];
    batchUpdateRequest.propertiesToUpdate = @{@"name" : @"name2"};
    batchUpdateRequest.resultType = NSUpdatedObjectsCountResultType;
    NSBatchUpdateResult *batchResult = [self.context executeRequest:batchUpdateRequest error:&error];
}
NSError *error = nil;
[self.context save:&error];


Realm ios批量更新10000条数据。


[realm beginWriteTransaction];
for (int i=0; i<10000; i++) {
    RLMResults *results  = [StudentRLM objectsWhere: [NSString stringWithFormat:@"studentId = 'studentId%d'",i]];
    NSLog(@"results %lu",(unsigned long)[results count]);
    StudentRLM *s = results.firstObject;
    [s setName:@"name”];
    [realm addOrUpdateObject:s];
}
[realm commitWriteTransaction];

###循环删除

Coredata 做10000次删除操作。


for (int i=0; i<10000; i++) {
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"];
    request.predicate = [NSPredicate predicateWithFormat:@"studentId = 'studentId%d'"];
    NSBatchDeleteRequest *batchRequest = [[NSBatchDeleteRequest alloc] initWithFetchRequest:request];
    batchRequest.resultType = NSUpdatedObjectsCountResultType;
    NSBatchUpdateResult *batchResult = [self.context executeRequest:batchRequest error:&error];
    NSError *error = nil;
    [self.context save:&error];
}

Realm ios做10000次删除操作。


for (int i=0; i<10000; i++) {
    [realm beginWriteTransaction];
    RLMResults *results  = [StudentRLM objectsWhere: [NSString stringWithFormat:@"studentId = 'studentId%d'",i]];
    StudentRLM *s = results.firstObject;
    [s setName:@"name"];
    [realm deleteObject:s];
    [realm commitWriteTransaction];
}

###批量删除

Coredata 批量删除10000条数据。


for (int i=0; i<10000; i++) {
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"];
    request.predicate = [NSPredicate predicateWithFormat:@"studentId = 'studentId%d'"];
    NSBatchDeleteRequest *batchRequest = [[NSBatchDeleteRequest alloc] initWithFetchRequest:request];
    batchRequest.resultType = NSUpdatedObjectsCountResultType;
    NSBatchUpdateResult *batchResult = [self.context executeRequest:batchRequest error:&error];
}
NSError *error = nil;
[self.context save:&error];


Realm ios批量删除10000条数据。


[realm beginWriteTransaction];
for (int i=0; i<10000; i++) {
    RLMResults *results  = [StudentRLM objectsWhere: [NSString stringWithFormat:@"studentId = 'studentId%d'",i]];
    StudentRLM *s = results.firstObject;
    [s setName:@"name"];
    [realm deleteObject:s];
}
[realm commitWriteTransaction];

ios 测试结果及分析 #

下面给出测试结果,表格中所有数据的单位是秒,即做10000次操作需要的秒数。

image

image

image

  • 可以看到,Coredata除了批量插入性能是最好的以外,其他项性能都一般。

  • Realm ios和Realm android性能非常相似,批量操作性能优异,但是非批量操作性能一般。可以这样总结,如果代码高内聚,可以把数据操作代码入口都统一使用,Realm性能是最好的,但这对代码质量、模块设计有要求,当操作数据的代码到处都有,不能使用批量接口时,Realm的性能是不好的。

  • Luakit没有提供批量接口,但从图中可以看出,Luakit的各项性能指标都是比较好的,而且对代码没有要求,即使操作数据的代码不内聚,也不会对性能有影响。

0.0.1 #

  • TODO: Describe initial release.

example/README.md

flutter_orm_plugin

A orm database Flutter plugin.

之前发了一篇文章《手把手教你在Flutter项目优雅的使用ORM数据库》,很多人咨询使用也提了一些宝贵的意见,说不希望要写lua,这样不够优雅,也增加了学习成本。细想了一下,确实是,对flutter项目开发来讲,最好就是纯flutter版的orm框架,于是我就写了一个flutter版的 orm插件flutter_orm_plugin ,使用的demo我放到github上了,大家可以下载来玩玩。下面我介绍一下flutter_orm_plugin提供的所有api。

添加orm表 #

flutter_orm_plugin中一个orm对应一个表,例如demo中Student表,它所在的db名字叫School,表名叫Student,它包含如下四个字段:

studentId 在数据库中是Integer类型,主键,自增的。

name 在数据库中是Text类型

class 在数据库中是Text类型,是外键,关联的表是另一个表Class表

score 在数据库中是Real类型

创建这样的表的代码在demo的main.dart

	Map<String , Field> fields = new Map<String , Field>();
    fields["studentId"] = Field(FieldType.Integer, primaryKey: true , autoIncrement: true);
    fields["name"] = Field(FieldType.Text);
    fields["class"] = Field(FieldType.Text, foreignKey: true, to: "School_Class");
    fields["score"] = Field(FieldType.Real);
    FlutterOrmPlugin.createTable("School","Student",fields);

数据库中某一列的数据通过Field类定义,我们先看看Field的定义就可以知道我们的orm对象支持哪些属性了

class Field {

  final FieldType type;//类型包括Integer、Real、Blob、Char、Text、Boolean

  bool unique;//是否惟一

  int maxLength;

  bool primaryKey;//是否主键

  bool foreignKey;//是否外键

  bool autoIncrement;//是否自增

  String to;//关联外键表,以DBName_TableName命名

  bool index;//是否有索引
}

插入数据 #

单条插入

 Map m = {"name":"william", "class":"class1", "score":96.5};
 FlutterOrmPlugin.saveOrm("Student", m);

批量插入

 List orms = new List();
 for(int i = 0 ; i < 100 ; i++) {
      Map m = {"name":name, "class":className, "score":score};
      orms.add(m);
 }
 FlutterOrmPlugin.batchSaveOrms("Student", orms);
 

查询数据 #

全部查询

  Query("Student").all().then((List l) {

  });
  

查询第一条

 Query("Student").first().then((Map m) {
      
 });
  

根据主键查询

  Query("Student").primaryKey([1,3,5]).all().then((List l) {
      
  });
     

where条件查询

  Query("Student").whereByColumFilters([WhereCondiction("score", WhereCondictionType.EQ_OR_MORE_THEN, 90)]).all().then((List l) {
      
  });
     

where sql 语句查询

  Query("Student").whereBySql("class in (?,?) and score > ?", ["class1","class2",90]).all().then((List l) {
      
  });
     

where 查询并排序

  Query("Student").orderBy(["score desc",]).all().then((List l) {
      
  });
 

查询指定列

  Query("Student").needColums(["studentId","name"]).all().then((List l) {
      
  });
 

group by 、having 查询

  Query("Student").needColums(["class"]).groupBy(["class"]).havingByBindings("avg(score) > ?", [40]).orderBy(["avg(score)"]).all().then((List l) {
    
  });
 

更新数据 #

全部更新

  Query("Student").update({"name":"test all update"});
 

根据主键更新

  Query("Student").primaryKey([11]).update({"name":"test update by primary key"});
 

根据特定条件更新

  Query("Student").whereByColumFilters([WhereCondiction("studentId", WhereCondictionType.LESS_THEN, 5),WhereCondiction("score", WhereCondictionType.EQ_OR_MORE_THEN, 0)]).update({"score":100});
 

根据自定义where sql更新

  Query("Student").whereBySql("studentId <= ? and score <= ?", [5,100]).update({"score":0});
  

删除数据 #

全部删除

  Query("Student").delete();
 

根据主键删除

  Query("Student").primaryKey([1,3,5]).delete();
 

根据条件删除

  Query("Student").whereByColumFilters([WhereCondiction("studentId", WhereCondictionType.IN, [1,3,5])]).delete();


根据自定义where sql删除

  Query("Student").whereBySql("studentId in (?,?,?)", [1,3,5]).delete();


联表查询 #

inner join

  JoinCondiction c = new JoinCondiction("Match");
  c.type = JoinType.INNER;
  c.matchColumns = {"studentId": "winnerId"};
  Query("Student").join(c).all().then((List l) {
            
  });
  

left join

  JoinCondiction c = new JoinCondiction("Match");
  c.type = JoinType.LEFT;
  c.matchColumns = {"studentId": "winnerId"};
  Query("Student").join(c).all().then((List l) {
            
  });
  

利用外键联表

  JoinCondiction c = new JoinCondiction("Class");
  c.type = JoinType.INNER;
  Query("Student").join(c).all().then((List l) {

  });
  

where sql 联表

  JoinCondiction c = new JoinCondiction("Match");
  c.type = JoinType.INNER;
  c.matchColumns = {"studentId": "winnerId"};
  Query("Student").join(c).whereBySql("Student.score > ?",[60]).all().then((List l) {
           
  });
  

部分column 联表查询

  JoinCondiction c = new JoinCondiction("Match");
  c.type = JoinType.INNER;
  c.matchColumns = {"studentId": "winnerId"};
  Query("Student").join(c).needColums(["name","score"]).all().then((List l) {
            
  });
  

group by 、having 联表查询

  JoinCondiction c = new JoinCondiction("Class");
  c.type = JoinType.INNER;
  Query("Student").join(c).needColums(["class"]).groupBy(["Student.class"]).havingByBindings("avg(Student.score) > ?", [40]).all().then((List l) {
            
  }); 
   

order by 联表查询

  JoinCondiction c = new JoinCondiction("Class");
  c.type = JoinType.INNER;
  Query("Student").join(c).orderBy(["Student.score desc"]).all().then((List l) {
            
  });
   

使用介绍 #

flutter_orm_plugin 已经发布到flutter 插件仓库。只要简单配置即可使用,在yaml文件中加上flutter_orm_plugin依赖以及orm框架所需要的lua源文件,flutter_orm_plugin会对所有lua代码进行封装,最终使用只需要关心dart接口,对lua是无感的。

  flutter_orm_plugin: ^1.0.3
  
  .
  .
  .
  
  assets:
    - packages/flutter_orm_plugin/lua/DB.lua
    - packages/flutter_orm_plugin/lua/orm/model.lua
    - packages/flutter_orm_plugin/lua/orm/cache.lua
    - packages/flutter_orm_plugin/lua/orm/dbData.lua
    - packages/flutter_orm_plugin/lua/orm/tools/fields.lua
    - packages/flutter_orm_plugin/lua/orm/tools/func.lua
    - packages/flutter_orm_plugin/lua/orm/class/fields.lua
    - packages/flutter_orm_plugin/lua/orm/class/global.lua
    - packages/flutter_orm_plugin/lua/orm/class/property.lua
    - packages/flutter_orm_plugin/lua/orm/class/query.lua
    - packages/flutter_orm_plugin/lua/orm/class/query_list.lua
    - packages/flutter_orm_plugin/lua/orm/class/select.lua
    - packages/flutter_orm_plugin/lua/orm/class/table.lua
    - packages/flutter_orm_plugin/lua/orm/class/type.lua
  

在ios项目podfile加上luakit 依赖

source 'https://github.com/williamwen1986/LuakitPod.git'
source 'https://github.com/williamwen1986/curl.git'

.
.
.

pod 'curl', '~> 1.0.0'
pod 'LuakitPod', '~> 1.0.23'

在android项目app的build.gradle加上luakit依赖

repositories {

    maven { url "https://jitpack.io" }

}

.
.
.

implementation 'com.github.williamwen1986:LuakitJitpack:1.0.13'

完成配置即可使用。

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_orm_plugin: ^1.0.9

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_orm_plugin/flutter_orm_plugin.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
72
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
86
Learn more about scoring.

We analyzed this package on Oct 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health issues and suggestions

Document public APIs. (-1 points)

100 out of 100 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Format lib/flutter_orm_plugin.dart.

Run flutter format to format lib/flutter_orm_plugin.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
flutter_luakit_plugin ^1.0.16 1.0.16
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8