手把手教你在Flutter項目優雅的使用ORM數據庫--下篇

An orm database Flutter plugin.

以前發了一篇文章《手把手教你在Flutter項目優雅的使用ORM數據庫》,不少人諮詢使用也提了一些寶貴的意見,說不但願要寫lua,這樣不夠優雅,也增長了學習成本。細想了一下,確實是,對flutter項目開發來說,最好就是純flutter版的orm框架,因而我就寫了一個flutter版的 orm插件flutter_orm_plugin ,使用的demo我放到github上了,你們能夠下載來玩玩。下面我介紹一下flutter_orm_plugin提供的全部api。android

image

image

添加orm表

flutter_orm_plugin中一個orm對應一個表,例如demo中Student表,它所在的db名字叫School,表名叫Student,它包含以下四個字段:ios

studentId 在數據庫中是Integer類型,主鍵,自增的。git

name 在數據庫中是Text類型github

class 在數據庫中是Text類型,是外鍵,關聯的表是另外一個表Class表sql

score 在數據庫中是Real類型數據庫

建立這樣的表的代碼在demo的main.dartapi

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對象支持哪些屬性了bash

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;//是否有索引
}

複製代碼

插入數據

單條插入app

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.9
  
  .
  .
  .
  
  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'

複製代碼

android項目混淆keep規則

-keep class org.chromium.** {*; }
-keep class com.common.luakit.** {*; }
複製代碼

完成配置便可使用。

相關文章
相關標籤/搜索