手把手教你在Flutter專案優雅的使用ORM資料庫--下篇

williamwen1986發表於2019-04-15

A orm database Flutter plugin.

之前發了一篇文章《手把手教你在Flutter專案優雅的使用ORM資料庫》,很多人諮詢使用也提了一些寶貴的意見,說不希望要寫lua,這樣不夠優雅,也增加了學習成本。細想了一下,確實是,對flutter專案開發來講,最好就是純flutter版的orm框架,於是我就寫了一個flutter版的 orm外掛flutter_orm_plugin ,使用的demo我放到github上了,大家可以下載來玩玩。下面我介紹一下flutter_orm_plugin提供的所有api。

image

image

新增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.0
  
  .
  .
  .
  
  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.17'

複製程式碼

在android專案app的build.gradle加上luakit依賴

repositories {

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

}

.
.
.

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

複製程式碼

完成配置即可使用。

相關文章