@@ -6,21 +6,24 @@ import 'dart:mirrors';
6
6
import 'adapter.dart' ;
7
7
import 'annotations.dart' ;
8
8
import 'operations.dart' ;
9
+ import 'orm.dart' as orm;
9
10
10
11
class Model {
11
12
Table _tableDefinition;
12
- static DBAdapter _sAdapter;
13
13
14
14
Model () {
15
15
_tableDefinition = AnnotationsParser .getTableForInstance (this );
16
16
}
17
17
18
+ /// Adds adapter that will be used by models.
19
+ /// This is deprecated and will be removed in 0.2.0
20
+ /// Use [orm.addAdapter] and [orm.setDefaultAdapter] instead.
21
+ @deprecated
18
22
static void set ormAdapter (DBAdapter adapter) {
19
- _sAdapter = adapter;
23
+ orm.addAdapter ('modelAdapter' , adapter);
24
+ orm.setDefaultAdapter ('modelAdapter' );
20
25
}
21
26
22
- static DBAdapter get ormAdapter => _sAdapter;
23
-
24
27
/**
25
28
* Returns DBFieldSQL instance
26
29
* for primary key defined in this model.
@@ -61,85 +64,26 @@ class Model {
61
64
*
62
65
* Throws [Exception] if model instance has not-null primary key.
63
66
*/
64
- Future insert () async {
65
- var primaryKeyValue = getPrimaryKeyValue ();
66
- if (primaryKeyValue != null ) {
67
- throw new Exception ('insert() should not be called' +
68
- 'on instances with not-null primary key value, use update() instead.' );
69
- }
70
-
71
- Insert insert = new Insert (_tableDefinition);
72
-
73
- for (Field field in _tableDefinition.fields) {
74
- if (! field.isPrimaryKey) {
75
- insert.value (field.fieldName,
76
- AnnotationsParser .getPropertyValueForField (field, this ));
77
- }
78
- }
79
-
80
- var newRecordId = await ormAdapter.insert (insert);
81
- if (this .getPrimaryKeyField () != null ) {
82
- this .setPrimaryKeyValue (newRecordId);
83
- }
84
-
85
- return newRecordId;
86
- }
67
+ Future insert () => orm.insert (this );
87
68
88
69
/**
89
70
* Updates this model instance data on database.
90
71
* This model instance primary key should have a not null value.
91
72
*
92
73
* Throws [Exception] if this model instance is null.
93
74
*/
94
- Future update () async {
95
- var primaryKeyValue = getPrimaryKeyValue ();
96
- if (primaryKeyValue == null ) {
97
- throw new Exception ('update() should not be called' +
98
- 'on instances with null primary key value, use insert() instead.' );
99
- }
100
-
101
- Update update = new Update (_tableDefinition);
102
-
103
- for (Field field in _tableDefinition.fields) {
104
- var value = AnnotationsParser .getPropertyValueForField (field, this );
105
- if (field.isPrimaryKey) {
106
- update.where (new Equals (field.fieldName, value));
107
- } else {
108
- update.set (field.fieldName, value);
109
- }
110
- }
111
-
112
- var updateResult = await ormAdapter.update (update);
113
- return updateResult;
114
- }
75
+ Future update () => orm.update (this );
115
76
116
77
/**
117
78
* Deletes this model instance data on database.
118
79
* This model instance primary key should have a not null value.
119
80
*
120
81
* Throws [Exception] if this model instance is null.
121
82
*/
122
- Future delete () async {
123
- var primaryKeyValue = getPrimaryKeyValue ();
124
- if (primaryKeyValue == null ) {
125
- throw new Exception ('delete() should not be called' +
126
- 'on instances with null primary key value.' );
127
- }
128
-
129
- Delete delete = new Delete (_tableDefinition);
130
-
131
- Field field = getPrimaryKeyField ();
132
- if (field != null ) {
133
- var value = AnnotationsParser .getPropertyValueForField (field, this );
134
- delete.where (new Equals (field.fieldName, value));
135
- }
136
-
137
- var deleteResult = await ormAdapter.delete (delete);
138
- return deleteResult;
139
- }
83
+ Future delete () => orm.delete (this );
140
84
141
85
Future <bool > save () async {
142
- var primaryKeyValue = getPrimaryKeyValue ();
86
+ var primaryKeyValue = this . getPrimaryKeyValue ();
143
87
144
88
if (primaryKeyValue != null ) {
145
89
var updateResult = this .update ();
@@ -176,7 +120,7 @@ class FindBase extends Select {
176
120
}
177
121
}
178
122
179
- static Future < Model > _executeFindOne (Type modelType, Select sql) async {
123
+ static Future _executeFindOne (Type modelType, Select sql) async {
180
124
List <Model > foundModels = await _executeFind (modelType, sql);
181
125
if (foundModels.length > 0 ) {
182
126
return foundModels.last;
@@ -185,14 +129,13 @@ class FindBase extends Select {
185
129
}
186
130
}
187
131
188
- static Future <List <Model >> _executeFind (
189
- Type modelType, Select selectSql) async {
132
+ static Future <List > _executeFind (Type modelType, Select selectSql) async {
190
133
Table modelTable = AnnotationsParser .getTableForType (modelType);
191
134
ClassMirror modelMirror = reflectClass (modelType);
192
135
193
- List <Model > foundInstances = new List <Model >();
136
+ List <dynamic > foundInstances = new List <dynamic >();
194
137
195
- var rows = await Model .ormAdapter .select (selectSql);
138
+ var rows = await orm. getDefaultAdapter () .select (selectSql);
196
139
for (Map <String , dynamic > row in rows) {
197
140
InstanceMirror newInstance =
198
141
modelMirror.newInstance (new Symbol ('' ), [], new Map ());
@@ -212,11 +155,11 @@ class FindBase extends Select {
212
155
class Find extends FindBase {
213
156
Find (Type modelType) : super (modelType);
214
157
215
- Future <List <Model >> execute () => FindBase ._executeFind (_modelType, this );
158
+ Future <List <dynamic >> execute () => FindBase ._executeFind (_modelType, this );
216
159
}
217
160
218
161
class FindOne extends FindBase {
219
162
FindOne (Type modelType) : super (modelType);
220
163
221
- Future <Model > execute () => FindBase ._executeFindOne (_modelType, this );
164
+ Future <dynamic > execute () => FindBase ._executeFindOne (_modelType, this );
222
165
}
0 commit comments