├── LICENSE ├── CHANGELOG.md ├── lib ├── dao.dart ├── yun_dao.dart ├── builder.dart ├── entity.dart ├── db_manager.dart ├── query.dart ├── template.dart └── entity_generator.dart ├── android ├── local.properties └── app │ └── src │ └── main │ └── java │ └── io │ └── flutter │ └── plugins │ └── GeneratedPluginRegistrant.java ├── .gitignore ├── .flutter-plugins ├── .metadata ├── test └── yun_dao_test.dart ├── yun_dao.iml ├── pubspec.yaml ├── README_CH.md ├── README.md └── pubspec.lock /LICENSE: -------------------------------------------------------------------------------- 1 | TODO: Add your license here. 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## [0.0.1] - TODO: Add release date. 2 | 3 | * TODO: Describe initial release. 4 | -------------------------------------------------------------------------------- /lib/dao.dart: -------------------------------------------------------------------------------- 1 | ///dao的抽象类 2 | abstract class Dao { 3 | String getTableName(); 4 | 5 | E formMap(Map map); 6 | 7 | Map toMap(E entity); 8 | } 9 | -------------------------------------------------------------------------------- /android/local.properties: -------------------------------------------------------------------------------- 1 | sdk.dir=C:\\Users\\Administrator\\AppData\\Local\\Android\\Sdk 2 | flutter.sdk=D:\\flutter\\flutter_windows_v1.2.1-stable\\flutter -------------------------------------------------------------------------------- /lib/yun_dao.dart: -------------------------------------------------------------------------------- 1 | library yun_dao; 2 | 3 | /// A Calculator. 4 | class Calculator { 5 | /// Returns [value] plus 1. 6 | int addOne(int value) => value + 1; 7 | } 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .dart_tool/ 3 | 4 | .packages 5 | .pub/ 6 | 7 | build/ 8 | ios/.generated/ 9 | ios/Flutter/Generated.xcconfig 10 | ios/Runner/GeneratedPluginRegistrant.* 11 | -------------------------------------------------------------------------------- /.flutter-plugins: -------------------------------------------------------------------------------- 1 | path_provider=D:\\flutter\\flutter_windows_v1.2.1-stable\\flutter\\.pub-cache\\hosted\\pub.flutter-io.cn\\path_provider-0.5.0+1\\ 2 | sqflite=D:\\flutter\\flutter_windows_v1.2.1-stable\\flutter\\.pub-cache\\hosted\\pub.flutter-io.cn\\sqflite-1.1.5\\ 3 | -------------------------------------------------------------------------------- /.metadata: -------------------------------------------------------------------------------- 1 | # This file tracks properties of this Flutter project. 2 | # Used by Flutter tool to assess capabilities and perform upgrades etc. 3 | # 4 | # This file should be version controlled and should not be manually edited. 5 | 6 | version: 7 | revision: 8661d8aecd626f7f57ccbcb735553edc05a2e713 8 | channel: stable 9 | 10 | project_type: package 11 | -------------------------------------------------------------------------------- /lib/builder.dart: -------------------------------------------------------------------------------- 1 | import 'package:build/build.dart'; 2 | import 'package:source_gen/source_gen.dart'; 3 | 4 | import 'package:yun_dao/entity_generator.dart'; 5 | 6 | Builder yunEntityGenerator(BuilderOptions options) => 7 | LibraryBuilder(EntityGenerator(options), generatedExtension: '.dao.dart'); 8 | 9 | ///flutter packages pub run build_runner build 10 | -------------------------------------------------------------------------------- /test/yun_dao_test.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter_test/flutter_test.dart'; 2 | 3 | import 'package:yun_dao/yun_dao.dart'; 4 | 5 | void main() { 6 | test('adds one to input values', () { 7 | final calculator = Calculator(); 8 | expect(calculator.addOne(2), 3); 9 | expect(calculator.addOne(-7), -6); 10 | expect(calculator.addOne(0), 1); 11 | expect(() => calculator.addOne(null), throwsNoSuchMethodError); 12 | }); 13 | } 14 | -------------------------------------------------------------------------------- /android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java: -------------------------------------------------------------------------------- 1 | package io.flutter.plugins; 2 | 3 | import io.flutter.plugin.common.PluginRegistry; 4 | import io.flutter.plugins.pathprovider.PathProviderPlugin; 5 | import com.tekartik.sqflite.SqflitePlugin; 6 | 7 | /** 8 | * Generated file. Do not edit. 9 | */ 10 | public final class GeneratedPluginRegistrant { 11 | public static void registerWith(PluginRegistry registry) { 12 | if (alreadyRegisteredWith(registry)) { 13 | return; 14 | } 15 | PathProviderPlugin.registerWith(registry.registrarFor("io.flutter.plugins.pathprovider.PathProviderPlugin")); 16 | SqflitePlugin.registerWith(registry.registrarFor("com.tekartik.sqflite.SqflitePlugin")); 17 | } 18 | 19 | private static boolean alreadyRegisteredWith(PluginRegistry registry) { 20 | final String key = GeneratedPluginRegistrant.class.getCanonicalName(); 21 | if (registry.hasPlugin(key)) { 22 | return true; 23 | } 24 | registry.registrarFor(key); 25 | return false; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /yun_dao.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /lib/entity.dart: -------------------------------------------------------------------------------- 1 | ///数据库表的注解 2 | class Entity { 3 | ///表名注解 4 | final String nameInDb; 5 | final List propertyList; 6 | 7 | const Entity({this.nameInDb, this.propertyList}); 8 | 9 | @override 10 | String toString() => 'Entity'; 11 | } 12 | 13 | ///属性model 14 | class Property { 15 | ///属性名 16 | final String name; 17 | 18 | ///属性类型 19 | final PropertyType type; 20 | 21 | ///是否是主键 22 | final bool isPrimary; 23 | 24 | const Property({this.name, this.type, this.isPrimary}); 25 | 26 | @override 27 | String toString() => "Property"; 28 | 29 | factory Property.fromJson(Map json) { 30 | return Property( 31 | name: json['name'] as String, 32 | type: PropertyType.fromJson(json['type'] as Map), 33 | isPrimary: json['isPrimary'] as bool); 34 | } 35 | 36 | Map toJson() => 37 | {'name': name, 'type': type, 'isPrimary': isPrimary}; 38 | } 39 | 40 | ///数据类型 41 | class PropertyType { 42 | final String value; 43 | 44 | const PropertyType({this.value}); 45 | 46 | @override 47 | String toString() => "PropertyType"; 48 | 49 | factory PropertyType.fromJson(Map json) { 50 | return PropertyType(value: json['value']); 51 | } 52 | 53 | Map toJson() => {'value': value}; 54 | 55 | static const PropertyType INT = PropertyType(value: "INT"), 56 | STRING = PropertyType(value: "TEXT"), 57 | DOUBLE = PropertyType(value: "REAL"); 58 | } 59 | -------------------------------------------------------------------------------- /lib/db_manager.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | 3 | import 'package:path/path.dart'; 4 | import 'package:sqflite/sqflite.dart'; 5 | 6 | ///数据库管理类 7 | class DBManager { 8 | ///数据库连接 9 | Database _db; 10 | 11 | Database get db => _db; 12 | 13 | ///单例 14 | static DBManager _instance; 15 | 16 | static DBManager _getInstance() { 17 | if (_instance == null) { 18 | _instance = new DBManager._internal(); 19 | } 20 | return _instance; 21 | } 22 | 23 | DBManager._internal() {} 24 | 25 | ///创建单例的数据库管理 26 | factory DBManager() => _getInstance(); 27 | 28 | ///数据库版本号 29 | int _version; 30 | 31 | int get version => _version; 32 | 33 | ///数据库文件路径 34 | String _dbPath; 35 | 36 | String get dbPath => _dbPath; 37 | 38 | ///数据库名称 39 | String _dbName; 40 | 41 | String get dbName => _dbName; 42 | 43 | ///初始化数据库 44 | void initByPath(int version, String dbPath, String dbName) async { 45 | this._version = version; 46 | this._dbPath = dbPath; 47 | this._dbName = dbName; 48 | var databasesPath = await _createNewDb(dbName, dbPath); 49 | _db = await openDatabase(databasesPath, version: version); 50 | } 51 | 52 | void init(int version, String dbName) async { 53 | var dbPath = await getDatabasesPath(); 54 | initByPath(version, dbPath, dbName); 55 | } 56 | 57 | Future _createNewDb(String dbName, String dbPath) async { 58 | String path = join(dbPath, dbName); 59 | if (await new Directory(dirname(path)).exists()) { 60 | } else { 61 | try { 62 | await new Directory(dirname(path)).create(recursive: true); 63 | } catch (e) { 64 | print(e); 65 | } 66 | } 67 | return path; 68 | } 69 | 70 | void close() { 71 | db.close(); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /lib/query.dart: -------------------------------------------------------------------------------- 1 | import 'package:yun_dao/dao.dart'; 2 | import 'package:yun_dao/db_manager.dart'; 3 | 4 | ///sqllite查询器 5 | class Query { 6 | ///表名 7 | dynamic dao; 8 | List _queryConditionList = List(); 9 | String _orderBy; 10 | String _groupBy; 11 | 12 | Query(dynamic dao) { 13 | this.dao = dao; 14 | } 15 | 16 | ///增加查询条件 17 | Query where(QueryCondition queryCondition) { 18 | _queryConditionList.add(queryCondition); 19 | return this; 20 | } 21 | 22 | ///排序 23 | Query orderBy(QueryCondition queryCondition) { 24 | _orderBy = queryCondition.key; 25 | return this; 26 | } 27 | 28 | Query groupBy(QueryCondition queryCondition) { 29 | _groupBy = queryCondition.key; 30 | return this; 31 | } 32 | 33 | ///查询 34 | Future list() async { 35 | DBManager dbManager = DBManager(); 36 | String whereKey = ""; 37 | List whereValue = List(); 38 | for (QueryCondition queryCondition in _queryConditionList) { 39 | if (queryCondition == 40 | _queryConditionList[_queryConditionList.length - 1]) { 41 | whereKey = whereKey + queryCondition.key + " = ?"; 42 | } else { 43 | whereKey = whereKey + queryCondition.key + " = ? and "; 44 | } 45 | 46 | whereValue.add(queryCondition.value); 47 | } 48 | List maps = await dbManager.db.query(dao.getTableName(), 49 | where: whereKey, 50 | whereArgs: whereValue, 51 | orderBy: _orderBy, 52 | groupBy: _groupBy); 53 | List list = List(); 54 | for (Map map in maps) { 55 | list.add(dao.formMap(map)); 56 | } 57 | return list; 58 | } 59 | } 60 | 61 | ///查询条件 62 | class QueryCondition { 63 | String key; 64 | dynamic value; 65 | 66 | QueryCondition({this.value, this.key}); 67 | } 68 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: yun_dao 2 | description: flutter ORM Lib. 3 | version: 0.0.3 4 | author: 'yunye<550752356@qq.com>' 5 | homepage: "https://github.com/yeyunHZ/yun_dao.git" 6 | 7 | environment: 8 | sdk: ">=2.1.0 <3.0.0" 9 | 10 | dependencies: 11 | flutter: 12 | sdk: flutter 13 | sqflite: ^1.1.0 14 | source_gen: '^0.8.0' 15 | build_config: '^0.3.0' 16 | mustache4dart: ^3.0.0-dev.0.0 17 | build: '>=0.12.0 <2.0.0' 18 | path_provider: ^0.5.0 19 | 20 | 21 | dev_dependencies: 22 | flutter_test: 23 | sdk: flutter 24 | build_runner: '^0.9.1' 25 | test: any 26 | 27 | # For information on the generic Dart part of this file, see the 28 | # following page: https://www.dartlang.org/tools/pub/pubspec 29 | 30 | # The following section is specific to Flutter. 31 | flutter: 32 | 33 | 34 | # To add assets to your package, add an assets section, like this: 35 | # assets: 36 | # - images/a_dot_burr.jpeg 37 | # - images/a_dot_ham.jpeg 38 | # 39 | # For details regarding assets in packages, see 40 | # https://flutter.io/assets-and-images/#from-packages 41 | # 42 | # An image asset can refer to one or more resolution-specific "variants", see 43 | # https://flutter.io/assets-and-images/#resolution-aware. 44 | 45 | # To add custom fonts to your package, add a fonts section here, 46 | # in this "flutter" section. Each entry in this list should have a 47 | # "family" key with the font family name, and a "fonts" key with a 48 | # list giving the asset and other descriptors for the font. For 49 | # example: 50 | # fonts: 51 | # - family: Schyler 52 | # fonts: 53 | # - asset: fonts/Schyler-Regular.ttf 54 | # - asset: fonts/Schyler-Italic.ttf 55 | # style: italic 56 | # - family: Trajan Pro 57 | # fonts: 58 | # - asset: fonts/TrajanPro.ttf 59 | # - asset: fonts/TrajanPro_Bold.ttf 60 | # weight: 700 61 | # 62 | # For details regarding fonts in packages, see 63 | # https://flutter.io/custom-fonts/#from-packages 64 | -------------------------------------------------------------------------------- /README_CH.md: -------------------------------------------------------------------------------- 1 | 简介 2 | === 3 | 4 | 一个以注解方式实现的ORM数据库解决方案,基于 source_gen 5 | 6 | 使用 7 | === 8 | 1、创建你的实体类并以*_entity.dart作为文件名,编译后生成的数据库操作文件将以*.entity.dao.dart命名 例: 9 | ```Dart 10 | 你的实体类文件: student_entity.dart 11 | 编译后生成的数据库操作类文件: student_entity.dao.dart 12 | ``` 13 | 14 | 15 | 2、使用 **@Entity** 注解你的 **实体类** ,并且 **nameInDb**属性来定义其在数据库中的表名。 16 | **propertyList** 的类型为List,通过他来定义表中的字段 17 | **注意**表中字段名,必须与实体类中的属性名一一对应 18 | 19 | 例: 20 | ```Dart 21 | @Entity(nameInDb:'student',propertyList:[Property(name:'name',type:PropertyType.STRING)]) 22 | class StudentEntity{ 23 | String name; 24 | } 25 | ``` 26 | 27 | 3、实体类必须拥有主键,并在 **@Property** 注解中通过 **isPrimary=true** 来声明这个属性为主键 28 | ```Dart 29 | @Entity(nameInDb:'student', 30 | propertyList:[ 31 | Property(name:'name',type:PropertyType.STRING), 32 | Property(name:'id',type:PropertyType.INT,isPrimary:true), 33 | ]) 34 | class StudentEntity{ 35 | String name; 36 | int id; 37 | } 38 | ``` 39 | 40 | 4、通过命令来生成数据库操作文件,在命令行中输入下面的命令 41 | flutter packages pub run build_runner build 42 | 43 | 44 | 5、编译后生成的数据库操作文件中包含当前表的创建、增删改查等方法,在项目中使用需要先进行数据库的初始化 45 | ```Dart 46 | ///导入数据库管理类 47 | import 'package:yun_dao/db_manager.dart'; 48 | 49 | ///传入数据库版本、数据库路径以及数据库名称来初始化数据库,DBManager为单例,每次创建拿到的都是同一个 50 | DBManager dBManager = DBManager(); 51 | dBManager.initByPath(1,“dbPath”,"dbName"); 52 | ///你也可以使用默认路径来初始化数据库 默认的路径为 getDatabasesPath() 53 | dBManager.init(1,"dbName"); 54 | ``` 55 | 56 | 57 | 6、在项目中调用生成的数据库操作文件的 **init()** 方法来创建表, **init()** 方法中会做相应的判断不会重复创建表格 58 | ```Dart 59 | StudentEntityDao.init(); 60 | ``` 61 | 62 | 63 | 7、然后就可以在项目中方便的进行数据库的增删改查操作了,所有的数据库操作方法都是静态 64 | ```Dart 65 | StudentEntityTable.queryAll(); 66 | StudentEntityTable.insert(StudentEntity()); 67 | ``` 68 | 69 | 8、你也可以通过构造查询器来查询数据 70 | ```Dart 71 | List list = await StudentEntityDao.queryBuild() 72 | .where(StudentEntityDao.NAME.equal("李四")) 73 | .where(StudentEntityDao.AGE.equal(2)) 74 | .list(); 75 | ``` 76 | 77 | 78 | 79 | 安装 80 | === 81 | 82 | dev_dependencies: 83 | yun_dao: 0.0.4 84 | 85 | 86 | [示例代码](https://github.com/yeyunHZ/yun_dao_test) 87 | === 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | [中文版](https://github.com/yeyunHZ/yun_dao/blob/master/README_CH.md) 3 | === 4 | 5 | Introduction 6 | === 7 | 8 | It is an ORM database solution by annotation method which is based on source_gen 9 | 10 | Usage 11 | === 12 | 1.Create entity file which names `*_entity.dart`,then will generate database operation file which names `*.entity.dao.dart` after compilation. 13 | 14 | ```Dart 15 | entity file: student_entity.dart 16 | generated databse file : student_entity.dao.dart 17 | ``` 18 | 19 | 2.Use `@Entity` to annotate **entity class**; use `nameInDb` property to define the database table name of entity class; use `propertyList` to define the columns name in table which must be correspond to the property name of the entity class.And the type of `propertyList` is `List` 20 | 21 | 22 | ```Dart 23 | @Entity(nameInDb:'student',propertyList:[Property(name:'name',type:PropertyType.STRING)]) 24 | class StudentEntity{ 25 | String name; 26 | } 27 | ``` 28 | 29 | 3.Entity class must has primary key, and define the property as primary key by the code `isPrimary=true` in `@Property` annotation 30 | 31 | ```Dart 32 | @Entity(nameInDb:'student', 33 | propertyList:[ 34 | Property(name:'name',type:PropertyType.STRING), 35 | Property(name:'id',type:PropertyType.INT,isPrimary:true), 36 | ]) 37 | class StudentEntity{ 38 | String name; 39 | int id; 40 | } 41 | ``` 42 | 43 | 4.Using the command to create database operation file in the command-line: 44 | 45 | ``` 46 | flutter packages pub run build_runner build 47 | ``` 48 | 49 | 5.The gennereated databse operation file includes the CURD methods after compilation.And you should init database when use in project 50 | 51 | ```Dart 52 | ///import database manager class 53 | import 'package:yun_dao/db_manager.dart'; 54 | 55 | ///pass the params include vesion,path,name to init databse. DBManager is a singleton 56 | DBManager dBManager = DBManager(); 57 | dBManager.initByPath(1,“dbPath”,"dbName"); 58 | 59 | ///you can also use default path to init database.The method to get default path is `getDatabasesPath()` 60 | dBManager.init(1,"dbName"); 61 | ``` 62 | 63 | 6.Call the method `init()` of genearated database operation file to create a table in project.However,the method would judge whether create a new table.Then you can don't be afraid of creating repeated tables. 64 | 65 | ```Dart 66 | StudentEntityDao.init(); 67 | ``` 68 | 69 | 7.Next,it's convenient to execute CURD operation in databse.And the all operation methods is static. 70 | 71 | ```Dart 72 | StudentEntityTable.queryAll(); 73 | StudentEntityTable.insert(StudentEntity()); 74 | ``` 75 | 76 | 8.Also you can select data by query builder 77 | 78 | ```Dart 79 | List list = await StudentEntityDao.queryBuild() 80 | .where(StudentEntityDao.NAME.equal("李四")) 81 | .where(StudentEntityDao.AGE.equal(2)) 82 | .list(); 83 | ``` 84 | 85 | 86 | 87 | Install 88 | === 89 | 90 | dev_dependencies: 91 | yun_dao: 0.0.4 92 | 93 | 94 | [Example](https://github.com/yeyunHZ/yun_dao_test) 95 | === 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /lib/template.dart: -------------------------------------------------------------------------------- 1 | const String clazzTpl = """ 2 | ///{{{tableName}}}表 3 | import 'package:yun_dao/db_manager.dart'; 4 | import 'package:yun_dao/entity.dart'; 5 | import 'package:{{{source}}}'; 6 | import 'package:yun_dao/Dao.dart'; 7 | import 'package:yun_dao/query.dart'; 8 | 9 | class {{{className}}} extends Dao<{{{entityName}}}>{ 10 | static List propertyMapList = {{{propertyList}}}; 11 | 12 | ///初始化数据库 13 | static Future init() async{ 14 | DBManager dbManager = DBManager(); 15 | List maps = await await dbManager.db.query("sqlite_master", where: " name = '{{{tableName}}}'"); 16 | if(maps == null ||maps.length ==0){ 17 | List propertyList = List(); 18 | for(Map map in propertyMapList){ 19 | propertyList.add(Property.fromJson(map)); 20 | } 21 | dbManager.db.execute("CREATE TABLE {{{tableName}}}({{{createSql}}})"); 22 | 23 | } 24 | return true; 25 | 26 | } 27 | 28 | 29 | ///查询表中所有数据 30 | static Future> queryAll() async{ 31 | DBManager dbManager = DBManager(); 32 | List<{{{entityName}}}> entityList = List(); 33 | {{{className}}} entityDao = {{{className}}}(); 34 | List maps = await dbManager.db.query("{{{tableName}}}") ; 35 | for(Map map in maps){ 36 | entityList.add(entityDao.formMap(map)); 37 | } 38 | return entityList; 39 | } 40 | 41 | 42 | ///增加一条数据 43 | static Future insert({{{entityName}}} entity) async{ 44 | DBManager dbManager = DBManager(); 45 | {{{className}}} entityDao = {{{className}}}(); 46 | await dbManager.db.insert("{{{tableName}}}", entityDao.toMap(entity)); 47 | return true; 48 | } 49 | 50 | ///增加多条条数据 51 | static Future insertList(List<{{{entityName}}}> entityList) async{ 52 | DBManager dbManager = DBManager(); 53 | List maps = List(); 54 | {{{className}}} entityDao = {{{className}}}(); 55 | for({{{entityName}}} entity in entityList){ 56 | maps.add(entityDao.toMap(entity)); 57 | } 58 | await dbManager.db.rawInsert("{{{tableName}}}", maps); 59 | return true; 60 | } 61 | 62 | ///更新数据 63 | static Future update({{{entityName}}} entity) async { 64 | DBManager dbManager = DBManager(); 65 | {{{className}}} entityDao = {{{className}}}(); 66 | return await dbManager.db.update("{{{tableName}}}", entityDao.toMap(entity), 67 | where: '{{{primary}}} = ?', whereArgs: [entity.{{{primary}}}]); 68 | } 69 | 70 | 71 | ///删除数据 72 | static Future delete({{{entityName}}} entity) async { 73 | DBManager dbManager = DBManager(); 74 | return await dbManager.db.delete("{{{tableName}}}", where: '{{{primary}}} = ?', whereArgs: [entity.{{{primary}}}]); 75 | } 76 | 77 | 78 | ///map转为entity 79 | @override 80 | {{{entityName}}} formMap(Map map){ 81 | {{{formMap}}} 82 | } 83 | 84 | ///entity转为map 85 | @override 86 | Map toMap({{{entityName}}} entity){ 87 | {{{toMap}}} 88 | 89 | } 90 | 91 | @override 92 | String getTableName(){ 93 | return "{{{tableName}}}"; 94 | } 95 | 96 | {{{propertyClass}}} 97 | 98 | static Query queryBuild(){ 99 | Query query = Query({{{className}}}()); 100 | return query; 101 | } 102 | } 103 | 104 | ///查询条件生成 105 | class QueryProperty{ 106 | String name; 107 | QueryProperty({this.name}); 108 | QueryCondition equal(dynamic queryValue){ 109 | QueryCondition queryCondition = QueryCondition(); 110 | queryCondition.key= name; 111 | queryCondition.value = queryValue; 112 | return queryCondition; 113 | } 114 | } 115 | 116 | 117 | """; 118 | 119 | const String instanceCreatedTpl = """ 120 | """; 121 | -------------------------------------------------------------------------------- /lib/entity_generator.dart: -------------------------------------------------------------------------------- 1 | import 'dart:convert'; 2 | import 'package:build/build.dart'; 3 | import 'package:analyzer/dart/constant/value.dart'; 4 | import 'package:analyzer/dart/element/element.dart'; 5 | import 'package:build/src/builder/build_step.dart'; 6 | import 'package:mustache4dart/mustache4dart.dart'; 7 | import 'package:source_gen/source_gen.dart'; 8 | import 'package:yun_dao/entity.dart'; 9 | 10 | import 'package:yun_dao/template.dart'; 11 | 12 | ///代码生成 13 | class EntityGenerator extends GeneratorForAnnotation { 14 | BuilderOptions options; 15 | 16 | EntityGenerator(this.options) : super(); 17 | 18 | @override 19 | generateForAnnotatedElement( 20 | Element element, ConstantReader annotation, BuildStep buildStep) { 21 | // TODO: implement generateForAnnotatedElement 22 | String className = element.name + "Dao"; 23 | ConstantReader propertyListConstantReader = annotation.peek('propertyList'); 24 | List dartList = propertyListConstantReader.listValue; 25 | List propertyList = List(); 26 | final Function addProperty = (DartObject propertyDartObject) { 27 | final ConstantReader propertyConstantReader = 28 | ConstantReader(propertyDartObject); 29 | String name = propertyConstantReader.peek("name")?.stringValue; 30 | ConstantReader typeConstantReader = 31 | ConstantReader(propertyConstantReader.peek("type").objectValue); 32 | PropertyType type = 33 | PropertyType(value: typeConstantReader.peek("value").stringValue); 34 | bool isPrimary = false; 35 | if (propertyConstantReader.peek("isPrimary") != null) { 36 | isPrimary = propertyConstantReader.peek("isPrimary").boolValue; 37 | } 38 | Property property = 39 | Property(name: name, type: type, isPrimary: isPrimary); 40 | propertyList.add(property); 41 | }; 42 | dartList.forEach(addProperty); 43 | String jsonStr = json.encode(propertyList); 44 | String entityName = element.name; 45 | String formMap = "$entityName entity = $entityName();"; 46 | String toMap = " var map = Map();\n"; 47 | String primary = null; 48 | String propertyClass = ""; 49 | for (Property property in propertyList) { 50 | String propertyName = property.name; 51 | propertyClass = propertyClass + 52 | "static QueryProperty " + 53 | propertyName.toUpperCase() + 54 | " = QueryProperty(name: '$propertyName');\n"; 55 | 56 | toMap = toMap + "map['$propertyName'] = entity.$propertyName;\n"; 57 | formMap = formMap + "entity.$propertyName = map['$propertyName'];\n"; 58 | if (property.isPrimary) { 59 | if (primary == null) { 60 | primary = propertyName; 61 | } else { 62 | throw '$entityName不能拥有两个主键!'; 63 | } 64 | } 65 | } 66 | toMap = toMap + "return map;"; 67 | formMap = formMap + "return entity;"; 68 | 69 | String createSql = ""; 70 | for (Property property in propertyList) { 71 | if (property == propertyList[propertyList.length - 1]) { 72 | createSql = createSql + 73 | property.name + 74 | " " + 75 | property.type.value + 76 | (property.isPrimary ? " PRIMARY KEY" : ""); 77 | } else { 78 | createSql = createSql + 79 | property.name + 80 | " " + 81 | property.type.value + 82 | (property.isPrimary ? " PRIMARY KEY" : "") + 83 | ","; 84 | } 85 | } 86 | if (primary == null) { 87 | throw '$entityName必须设置主键!'; 88 | } 89 | 90 | return render(clazzTpl, { 91 | 'className': className, 92 | 'entityName': entityName, 93 | 'tableName': annotation.peek("nameInDb")?.stringValue, 94 | "propertyList": "$jsonStr", 95 | "source": _getSource(element.source.fullName.substring(1)), 96 | "toMap": toMap, 97 | "formMap": formMap, 98 | "createSql": createSql, 99 | "primary": primary, 100 | "propertyClass": propertyClass 101 | }); 102 | } 103 | 104 | String wK(String key) { 105 | return "'${key}'"; 106 | } 107 | 108 | ///获取要生成的文件的路径 109 | String _getSource(String fullName) { 110 | String source = fullName.replaceAll("/lib", ""); 111 | return source; 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /pubspec.lock: -------------------------------------------------------------------------------- 1 | # Generated by pub 2 | # See https://www.dartlang.org/tools/pub/glossary#lockfile 3 | packages: 4 | analyzer: 5 | dependency: transitive 6 | description: 7 | name: analyzer 8 | url: "https://pub.flutter-io.cn" 9 | source: hosted 10 | version: "0.32.6" 11 | args: 12 | dependency: transitive 13 | description: 14 | name: args 15 | url: "https://pub.flutter-io.cn" 16 | source: hosted 17 | version: "1.5.1" 18 | async: 19 | dependency: transitive 20 | description: 21 | name: async 22 | url: "https://pub.flutter-io.cn" 23 | source: hosted 24 | version: "2.0.8" 25 | boolean_selector: 26 | dependency: transitive 27 | description: 28 | name: boolean_selector 29 | url: "https://pub.flutter-io.cn" 30 | source: hosted 31 | version: "1.0.4" 32 | build: 33 | dependency: "direct main" 34 | description: 35 | name: build 36 | url: "https://pub.flutter-io.cn" 37 | source: hosted 38 | version: "0.12.7+3" 39 | build_config: 40 | dependency: "direct main" 41 | description: 42 | name: build_config 43 | url: "https://pub.flutter-io.cn" 44 | source: hosted 45 | version: "0.3.2" 46 | build_resolvers: 47 | dependency: transitive 48 | description: 49 | name: build_resolvers 50 | url: "https://pub.flutter-io.cn" 51 | source: hosted 52 | version: "0.2.2+7" 53 | build_runner: 54 | dependency: "direct dev" 55 | description: 56 | name: build_runner 57 | url: "https://pub.flutter-io.cn" 58 | source: hosted 59 | version: "0.9.2" 60 | build_runner_core: 61 | dependency: transitive 62 | description: 63 | name: build_runner_core 64 | url: "https://pub.flutter-io.cn" 65 | source: hosted 66 | version: "0.2.2+2" 67 | built_collection: 68 | dependency: transitive 69 | description: 70 | name: built_collection 71 | url: "https://pub.flutter-io.cn" 72 | source: hosted 73 | version: "4.2.2" 74 | built_value: 75 | dependency: transitive 76 | description: 77 | name: built_value 78 | url: "https://pub.flutter-io.cn" 79 | source: hosted 80 | version: "6.5.0" 81 | charcode: 82 | dependency: transitive 83 | description: 84 | name: charcode 85 | url: "https://pub.flutter-io.cn" 86 | source: hosted 87 | version: "1.1.2" 88 | cli_util: 89 | dependency: transitive 90 | description: 91 | name: cli_util 92 | url: "https://pub.flutter-io.cn" 93 | source: hosted 94 | version: "0.1.3+2" 95 | code_builder: 96 | dependency: transitive 97 | description: 98 | name: code_builder 99 | url: "https://pub.flutter-io.cn" 100 | source: hosted 101 | version: "3.2.0" 102 | collection: 103 | dependency: transitive 104 | description: 105 | name: collection 106 | url: "https://pub.flutter-io.cn" 107 | source: hosted 108 | version: "1.14.11" 109 | convert: 110 | dependency: transitive 111 | description: 112 | name: convert 113 | url: "https://pub.flutter-io.cn" 114 | source: hosted 115 | version: "2.1.1" 116 | crypto: 117 | dependency: transitive 118 | description: 119 | name: crypto 120 | url: "https://pub.flutter-io.cn" 121 | source: hosted 122 | version: "2.0.6" 123 | csslib: 124 | dependency: transitive 125 | description: 126 | name: csslib 127 | url: "https://pub.flutter-io.cn" 128 | source: hosted 129 | version: "0.16.0" 130 | dart_style: 131 | dependency: transitive 132 | description: 133 | name: dart_style 134 | url: "https://pub.flutter-io.cn" 135 | source: hosted 136 | version: "1.1.3" 137 | fixnum: 138 | dependency: transitive 139 | description: 140 | name: fixnum 141 | url: "https://pub.flutter-io.cn" 142 | source: hosted 143 | version: "0.10.9" 144 | flutter: 145 | dependency: "direct main" 146 | description: flutter 147 | source: sdk 148 | version: "0.0.0" 149 | flutter_test: 150 | dependency: "direct dev" 151 | description: flutter 152 | source: sdk 153 | version: "0.0.0" 154 | front_end: 155 | dependency: transitive 156 | description: 157 | name: front_end 158 | url: "https://pub.flutter-io.cn" 159 | source: hosted 160 | version: "0.1.4+2" 161 | glob: 162 | dependency: transitive 163 | description: 164 | name: glob 165 | url: "https://pub.flutter-io.cn" 166 | source: hosted 167 | version: "1.1.7" 168 | graphs: 169 | dependency: transitive 170 | description: 171 | name: graphs 172 | url: "https://pub.flutter-io.cn" 173 | source: hosted 174 | version: "0.1.3+1" 175 | html: 176 | dependency: transitive 177 | description: 178 | name: html 179 | url: "https://pub.flutter-io.cn" 180 | source: hosted 181 | version: "0.14.0+2" 182 | http: 183 | dependency: transitive 184 | description: 185 | name: http 186 | url: "https://pub.flutter-io.cn" 187 | source: hosted 188 | version: "0.12.0+2" 189 | http_multi_server: 190 | dependency: transitive 191 | description: 192 | name: http_multi_server 193 | url: "https://pub.flutter-io.cn" 194 | source: hosted 195 | version: "2.0.6" 196 | http_parser: 197 | dependency: transitive 198 | description: 199 | name: http_parser 200 | url: "https://pub.flutter-io.cn" 201 | source: hosted 202 | version: "3.1.3" 203 | io: 204 | dependency: transitive 205 | description: 206 | name: io 207 | url: "https://pub.flutter-io.cn" 208 | source: hosted 209 | version: "0.3.3" 210 | js: 211 | dependency: transitive 212 | description: 213 | name: js 214 | url: "https://pub.flutter-io.cn" 215 | source: hosted 216 | version: "0.6.1+1" 217 | json_annotation: 218 | dependency: transitive 219 | description: 220 | name: json_annotation 221 | url: "https://pub.flutter-io.cn" 222 | source: hosted 223 | version: "1.2.0" 224 | json_rpc_2: 225 | dependency: transitive 226 | description: 227 | name: json_rpc_2 228 | url: "https://pub.flutter-io.cn" 229 | source: hosted 230 | version: "2.1.0" 231 | kernel: 232 | dependency: transitive 233 | description: 234 | name: kernel 235 | url: "https://pub.flutter-io.cn" 236 | source: hosted 237 | version: "0.3.4+2" 238 | logging: 239 | dependency: transitive 240 | description: 241 | name: logging 242 | url: "https://pub.flutter-io.cn" 243 | source: hosted 244 | version: "0.11.3+2" 245 | matcher: 246 | dependency: transitive 247 | description: 248 | name: matcher 249 | url: "https://pub.flutter-io.cn" 250 | source: hosted 251 | version: "0.12.3+1" 252 | meta: 253 | dependency: transitive 254 | description: 255 | name: meta 256 | url: "https://pub.flutter-io.cn" 257 | source: hosted 258 | version: "1.1.6" 259 | mime: 260 | dependency: transitive 261 | description: 262 | name: mime 263 | url: "https://pub.flutter-io.cn" 264 | source: hosted 265 | version: "0.9.6+2" 266 | multi_server_socket: 267 | dependency: transitive 268 | description: 269 | name: multi_server_socket 270 | url: "https://pub.flutter-io.cn" 271 | source: hosted 272 | version: "1.0.2" 273 | mustache4dart: 274 | dependency: "direct main" 275 | description: 276 | name: mustache4dart 277 | url: "https://pub.flutter-io.cn" 278 | source: hosted 279 | version: "3.0.0-dev.0.0" 280 | node_preamble: 281 | dependency: transitive 282 | description: 283 | name: node_preamble 284 | url: "https://pub.flutter-io.cn" 285 | source: hosted 286 | version: "1.4.4" 287 | package_config: 288 | dependency: transitive 289 | description: 290 | name: package_config 291 | url: "https://pub.flutter-io.cn" 292 | source: hosted 293 | version: "1.0.5" 294 | package_resolver: 295 | dependency: transitive 296 | description: 297 | name: package_resolver 298 | url: "https://pub.flutter-io.cn" 299 | source: hosted 300 | version: "1.0.10" 301 | path: 302 | dependency: transitive 303 | description: 304 | name: path 305 | url: "https://pub.flutter-io.cn" 306 | source: hosted 307 | version: "1.6.2" 308 | path_provider: 309 | dependency: "direct main" 310 | description: 311 | name: path_provider 312 | url: "https://pub.flutter-io.cn" 313 | source: hosted 314 | version: "0.5.0+1" 315 | pedantic: 316 | dependency: transitive 317 | description: 318 | name: pedantic 319 | url: "https://pub.flutter-io.cn" 320 | source: hosted 321 | version: "1.4.0" 322 | plugin: 323 | dependency: transitive 324 | description: 325 | name: plugin 326 | url: "https://pub.flutter-io.cn" 327 | source: hosted 328 | version: "0.2.0+3" 329 | pool: 330 | dependency: transitive 331 | description: 332 | name: pool 333 | url: "https://pub.flutter-io.cn" 334 | source: hosted 335 | version: "1.4.0" 336 | pub_semver: 337 | dependency: transitive 338 | description: 339 | name: pub_semver 340 | url: "https://pub.flutter-io.cn" 341 | source: hosted 342 | version: "1.4.2" 343 | pubspec_parse: 344 | dependency: transitive 345 | description: 346 | name: pubspec_parse 347 | url: "https://pub.flutter-io.cn" 348 | source: hosted 349 | version: "0.1.4" 350 | quiver: 351 | dependency: transitive 352 | description: 353 | name: quiver 354 | url: "https://pub.flutter-io.cn" 355 | source: hosted 356 | version: "2.0.1" 357 | shelf: 358 | dependency: transitive 359 | description: 360 | name: shelf 361 | url: "https://pub.flutter-io.cn" 362 | source: hosted 363 | version: "0.7.5" 364 | shelf_packages_handler: 365 | dependency: transitive 366 | description: 367 | name: shelf_packages_handler 368 | url: "https://pub.flutter-io.cn" 369 | source: hosted 370 | version: "1.0.4" 371 | shelf_static: 372 | dependency: transitive 373 | description: 374 | name: shelf_static 375 | url: "https://pub.flutter-io.cn" 376 | source: hosted 377 | version: "0.2.8" 378 | shelf_web_socket: 379 | dependency: transitive 380 | description: 381 | name: shelf_web_socket 382 | url: "https://pub.flutter-io.cn" 383 | source: hosted 384 | version: "0.2.3" 385 | sky_engine: 386 | dependency: transitive 387 | description: flutter 388 | source: sdk 389 | version: "0.0.99" 390 | source_gen: 391 | dependency: "direct main" 392 | description: 393 | name: source_gen 394 | url: "https://pub.flutter-io.cn" 395 | source: hosted 396 | version: "0.8.3+1" 397 | source_map_stack_trace: 398 | dependency: transitive 399 | description: 400 | name: source_map_stack_trace 401 | url: "https://pub.flutter-io.cn" 402 | source: hosted 403 | version: "1.1.5" 404 | source_maps: 405 | dependency: transitive 406 | description: 407 | name: source_maps 408 | url: "https://pub.flutter-io.cn" 409 | source: hosted 410 | version: "0.10.8" 411 | source_span: 412 | dependency: transitive 413 | description: 414 | name: source_span 415 | url: "https://pub.flutter-io.cn" 416 | source: hosted 417 | version: "1.5.4" 418 | sqflite: 419 | dependency: "direct main" 420 | description: 421 | name: sqflite 422 | url: "https://pub.flutter-io.cn" 423 | source: hosted 424 | version: "1.1.5" 425 | stack_trace: 426 | dependency: transitive 427 | description: 428 | name: stack_trace 429 | url: "https://pub.flutter-io.cn" 430 | source: hosted 431 | version: "1.9.3" 432 | stream_channel: 433 | dependency: transitive 434 | description: 435 | name: stream_channel 436 | url: "https://pub.flutter-io.cn" 437 | source: hosted 438 | version: "1.6.8" 439 | stream_transform: 440 | dependency: transitive 441 | description: 442 | name: stream_transform 443 | url: "https://pub.flutter-io.cn" 444 | source: hosted 445 | version: "0.0.16+1" 446 | string_scanner: 447 | dependency: transitive 448 | description: 449 | name: string_scanner 450 | url: "https://pub.flutter-io.cn" 451 | source: hosted 452 | version: "1.0.4" 453 | synchronized: 454 | dependency: transitive 455 | description: 456 | name: synchronized 457 | url: "https://pub.flutter-io.cn" 458 | source: hosted 459 | version: "2.1.0" 460 | term_glyph: 461 | dependency: transitive 462 | description: 463 | name: term_glyph 464 | url: "https://pub.flutter-io.cn" 465 | source: hosted 466 | version: "1.1.0" 467 | test: 468 | dependency: "direct dev" 469 | description: 470 | name: test 471 | url: "https://pub.flutter-io.cn" 472 | source: hosted 473 | version: "1.5.3" 474 | test_api: 475 | dependency: transitive 476 | description: 477 | name: test_api 478 | url: "https://pub.flutter-io.cn" 479 | source: hosted 480 | version: "0.2.2" 481 | test_core: 482 | dependency: transitive 483 | description: 484 | name: test_core 485 | url: "https://pub.flutter-io.cn" 486 | source: hosted 487 | version: "0.2.1+1" 488 | typed_data: 489 | dependency: transitive 490 | description: 491 | name: typed_data 492 | url: "https://pub.flutter-io.cn" 493 | source: hosted 494 | version: "1.1.6" 495 | vector_math: 496 | dependency: transitive 497 | description: 498 | name: vector_math 499 | url: "https://pub.flutter-io.cn" 500 | source: hosted 501 | version: "2.0.8" 502 | vm_service_client: 503 | dependency: transitive 504 | description: 505 | name: vm_service_client 506 | url: "https://pub.flutter-io.cn" 507 | source: hosted 508 | version: "0.2.6+2" 509 | watcher: 510 | dependency: transitive 511 | description: 512 | name: watcher 513 | url: "https://pub.flutter-io.cn" 514 | source: hosted 515 | version: "0.9.7+10" 516 | web_socket_channel: 517 | dependency: transitive 518 | description: 519 | name: web_socket_channel 520 | url: "https://pub.flutter-io.cn" 521 | source: hosted 522 | version: "1.0.12" 523 | yaml: 524 | dependency: transitive 525 | description: 526 | name: yaml 527 | url: "https://pub.flutter-io.cn" 528 | source: hosted 529 | version: "2.1.15" 530 | sdks: 531 | dart: ">=2.1.0 <3.0.0" 532 | flutter: ">=1.2.1 <2.0.0" 533 | --------------------------------------------------------------------------------