-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0bb98f1
commit 9cd7f92
Showing
8 changed files
with
158 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
abstract class SupportedType { | ||
static const STRING = 'String'; | ||
static const BOOL = 'bool'; | ||
static const INT = 'int'; | ||
static const DOUBLE = 'double'; | ||
} | ||
|
||
abstract class Annotation { | ||
static const ENTITY = 'Entity'; | ||
static const DATABASE = 'Database'; | ||
static const COLUMN_INFO = 'ColumnInfo'; | ||
static const PRIMARY_KEY = 'PrimaryKey'; | ||
static const QUERY = 'Query'; | ||
} | ||
|
||
abstract class AnnotationField { | ||
static const QUERY_VALUE = 'value'; | ||
static const COLUMN_INFO_AUTO_GENERATE = 'autoGenerate'; | ||
} | ||
|
||
abstract class SqlConstants { | ||
static const INTEGER = 'INTEGER'; | ||
static const TEXT = 'TEXT'; | ||
static const REAL = 'REAL'; | ||
|
||
static const PRIMARY_KEY = 'PRIMARY KEY'; | ||
static const AUTOINCREMENT = 'AUTOINCREMNT'; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,60 @@ | ||
import 'package:analyzer/dart/element/element.dart'; | ||
import 'package:floor_generator/misc/constants.dart'; | ||
import 'package:floor_generator/misc/type_utils.dart'; | ||
import 'package:source_gen/source_gen.dart'; | ||
|
||
/// Represents a table column. | ||
class Column { | ||
final String name; | ||
final String type; | ||
final bool isPrimaryKey; | ||
final bool autoGenerate; | ||
final FieldElement field; | ||
|
||
Column(this.field); | ||
|
||
String get name => field.displayName; | ||
|
||
String get type { | ||
final type = field.type; | ||
if (isInt(type)) { | ||
return SqlConstants.INTEGER; | ||
} else if (isString(type)) { | ||
return SqlConstants.INTEGER; | ||
} else if (isBool(type)) { | ||
return SqlConstants.INTEGER; | ||
} else if (isDouble(type)) { | ||
return SqlConstants.REAL; | ||
} | ||
throw InvalidGenerationSourceError( | ||
'Column type is not supported for $type.', | ||
element: field, | ||
); | ||
} | ||
|
||
bool get isPrimaryKey => field.metadata.any(isPrimaryKeyAnnotation); | ||
|
||
bool get autoGenerate { | ||
if (!isPrimaryKey) { | ||
return null; | ||
} | ||
return field.metadata | ||
.firstWhere(isPrimaryKeyAnnotation) | ||
.computeConstantValue() | ||
.getField(AnnotationField.COLUMN_INFO_AUTO_GENERATE) | ||
.toBoolValue(); | ||
} | ||
|
||
/// Primary key and auto increment. | ||
String get additionals { | ||
String add = ''; | ||
|
||
if (isPrimaryKey) { | ||
add += ' ${SqlConstants.PRIMARY_KEY}'; | ||
if (autoGenerate) { | ||
add += ' ${SqlConstants.AUTOINCREMENT}'; | ||
} | ||
} | ||
|
||
const Column(this.name, this.type, this.isPrimaryKey, this.autoGenerate); | ||
if (add.isEmpty) { | ||
return null; | ||
} | ||
return add; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import 'package:analyzer/dart/element/element.dart'; | ||
import 'package:floor_generator/misc/type_utils.dart'; | ||
import 'package:floor_generator/model/entity.dart'; | ||
import 'package:floor_generator/model/query_method.dart'; | ||
import 'package:source_gen/source_gen.dart'; | ||
|
||
class Database { | ||
final ClassElement clazz; | ||
|
||
Database(this.clazz); | ||
|
||
String get name => clazz.displayName; | ||
|
||
List<MethodElement> get methods => clazz.methods; | ||
|
||
List<QueryMethod> get queryMethods { | ||
return methods | ||
.where((method) => method.metadata.any(isQueryAnnotation)) | ||
.map((method) => QueryMethod(method)) | ||
.toList(); | ||
} | ||
|
||
List<Entity> getEntities(LibraryReader library) { | ||
return library.classes | ||
.where((clazz) => | ||
!clazz.isAbstract && clazz.metadata.any(isEntityAnnotation)) | ||
.map((entity) => Entity(entity)) | ||
.toList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'package:analyzer/dart/element/element.dart'; | ||
import 'package:floor_generator/model/column.dart'; | ||
|
||
class Entity { | ||
final ClassElement clazz; | ||
|
||
Entity(this.clazz); | ||
|
||
String get name => clazz.displayName; | ||
|
||
List<FieldElement> get fields => clazz.fields; | ||
|
||
List<Column> get columns { | ||
return fields.map((field) => Column(field)).toList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters