import './provider.dart'; import 'dart:async'; import 'package:sqflite/sqflite.dart'; class BaseModel{ Database db; final String table = ''; var query; BaseModel(this.db){ query = db.query; } } class Sql extends BaseModel { final String tableName; Sql.setTable(String name) : tableName = name, super(Provider.db); // sdf Future get() async{ return await this.query(tableName); } String getTableName () { return tableName; } Future delete(String value,String key) async{ return await this.db.delete(tableName,where:'$key = ?',whereArgs:[value]); } Future getByCondition({Map conditions}) async { if (conditions == null || conditions.isEmpty) { return this.get(); } String stringConditions = ''; int index = 0; conditions.forEach((key, value) { if (value == null) { return ; } if (value.runtimeType == String) { stringConditions = '$stringConditions $key = "$value"'; } if (value.runtimeType == int) { stringConditions = '$stringConditions $key = $value'; } if (index >= 0 && index < conditions.length -1) { stringConditions = '$stringConditions and'; } index++; }); // print("this is string condition for sql > $stringConditions"); return await this.query(tableName, where: stringConditions); } Future> insert(Map json) async { var id = await this.db.insert(tableName, json); json['id'] = id; return json; } /// /// 搜索 /// @param Object condition /// @mods [And, Or] default is Or /// search({'name': "hanxu', 'id': 1}; /// Future search({Map conditions, String Mods = 'Or'}) async { if (conditions == null || conditions.isEmpty) { return this.get(); } String stringConditions = ''; int index = 0; conditions.forEach((key, value) { if (value == null) { return ; } if (value.runtimeType == String) { stringConditions = '$stringConditions $key like "%$value%"'; } if (value.runtimeType == int) { stringConditions = '$stringConditions $key = "%$value%"'; } if (index >= 0 && index < conditions.length -1) { stringConditions = '$stringConditions $Mods'; } index++; }); return await this.query(tableName, where: stringConditions); } }