mirror of https://github.com/AMT-Cheif/drift.git
Fix writing type converters
This commit is contained in:
parent
fe3566429f
commit
c2b319bc8e
|
@ -50,7 +50,9 @@ class $NoIdsTable extends Table with TableInfo<$NoIdsTable, NoIdRow> {
|
|||
final VerificationMeta _payloadMeta = const VerificationMeta('payload');
|
||||
late final GeneratedColumn<Uint8List> payload = GeneratedColumn<Uint8List>(
|
||||
'payload', aliasedName, false,
|
||||
type: DriftSqlType.blob, requiredDuringInsert: true);
|
||||
type: DriftSqlType.blob,
|
||||
requiredDuringInsert: true,
|
||||
defaultConstraints: 'PRIMARY KEY');
|
||||
@override
|
||||
List<GeneratedColumn> get $columns => [payload];
|
||||
@override
|
||||
|
@ -72,7 +74,7 @@ class $NoIdsTable extends Table with TableInfo<$NoIdsTable, NoIdRow> {
|
|||
}
|
||||
|
||||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => const <GeneratedColumn>{};
|
||||
Set<GeneratedColumn> get $primaryKey => {payload};
|
||||
@override
|
||||
NoIdRow map(Map<String, dynamic> data, {String? tablePrefix}) {
|
||||
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : '';
|
||||
|
@ -89,6 +91,8 @@ class $NoIdsTable extends Table with TableInfo<$NoIdsTable, NoIdRow> {
|
|||
|
||||
@override
|
||||
bool get withoutRowId => true;
|
||||
@override
|
||||
bool get dontWriteConstraints => true;
|
||||
}
|
||||
|
||||
class WithDefault extends DataClass implements Insertable<WithDefault> {
|
||||
|
@ -219,11 +223,15 @@ class $WithDefaultsTable extends Table
|
|||
final VerificationMeta _aMeta = const VerificationMeta('a');
|
||||
late final GeneratedColumn<String> a = GeneratedColumn<String>(
|
||||
'a', aliasedName, true,
|
||||
type: DriftSqlType.string, requiredDuringInsert: false);
|
||||
type: DriftSqlType.string,
|
||||
requiredDuringInsert: false,
|
||||
defaultValue: const CustomExpression('\'something\''));
|
||||
final VerificationMeta _bMeta = const VerificationMeta('b');
|
||||
late final GeneratedColumn<int> b = GeneratedColumn<int>(
|
||||
'b', aliasedName, true,
|
||||
type: DriftSqlType.int, requiredDuringInsert: false);
|
||||
type: DriftSqlType.int,
|
||||
requiredDuringInsert: false,
|
||||
defaultConstraints: 'UNIQUE');
|
||||
@override
|
||||
List<GeneratedColumn> get $columns => [a, b];
|
||||
@override
|
||||
|
@ -261,6 +269,9 @@ class $WithDefaultsTable extends Table
|
|||
$WithDefaultsTable createAlias(String alias) {
|
||||
return $WithDefaultsTable(attachedDatabase, alias);
|
||||
}
|
||||
|
||||
@override
|
||||
bool get dontWriteConstraints => true;
|
||||
}
|
||||
|
||||
class WithConstraint extends DataClass implements Insertable<WithConstraint> {
|
||||
|
@ -466,6 +477,9 @@ class $WithConstraintsTable extends Table
|
|||
$WithConstraintsTable createAlias(String alias) {
|
||||
return $WithConstraintsTable(attachedDatabase, alias);
|
||||
}
|
||||
|
||||
@override
|
||||
bool get dontWriteConstraints => true;
|
||||
}
|
||||
|
||||
class Config extends DataClass implements Insertable<Config> {
|
||||
|
@ -486,11 +500,11 @@ class Config extends DataClass implements Insertable<Config> {
|
|||
map['config_value'] = Variable<String>(configValue);
|
||||
}
|
||||
if (!nullToAbsent || syncState != null) {
|
||||
final converter = $ConfigTable.$convertersyncState;
|
||||
final converter = $ConfigTable.$convertersyncStaten;
|
||||
map['sync_state'] = Variable<int>(converter.toSql(syncState));
|
||||
}
|
||||
if (!nullToAbsent || syncStateImplicit != null) {
|
||||
final converter = $ConfigTable.$convertersyncStateImplicit;
|
||||
final converter = $ConfigTable.$convertersyncStateImplicitn;
|
||||
map['sync_state_implicit'] =
|
||||
Variable<int>(converter.toSql(syncStateImplicit));
|
||||
}
|
||||
|
@ -629,11 +643,11 @@ class ConfigCompanion extends UpdateCompanion<Config> {
|
|||
map['config_value'] = Variable<String>(configValue.value);
|
||||
}
|
||||
if (syncState.present) {
|
||||
final converter = $ConfigTable.$convertersyncState;
|
||||
final converter = $ConfigTable.$convertersyncStaten;
|
||||
map['sync_state'] = Variable<int>(converter.toSql(syncState.value));
|
||||
}
|
||||
if (syncStateImplicit.present) {
|
||||
final converter = $ConfigTable.$convertersyncStateImplicit;
|
||||
final converter = $ConfigTable.$convertersyncStateImplicitn;
|
||||
map['sync_state_implicit'] =
|
||||
Variable<int>(converter.toSql(syncStateImplicit.value));
|
||||
}
|
||||
|
@ -660,7 +674,9 @@ class $ConfigTable extends Table with TableInfo<$ConfigTable, Config> {
|
|||
final VerificationMeta _configKeyMeta = const VerificationMeta('configKey');
|
||||
late final GeneratedColumn<String> configKey = GeneratedColumn<String>(
|
||||
'config_key', aliasedName, false,
|
||||
type: DriftSqlType.string, requiredDuringInsert: true);
|
||||
type: DriftSqlType.string,
|
||||
requiredDuringInsert: true,
|
||||
defaultConstraints: 'PRIMARY KEY');
|
||||
final VerificationMeta _configValueMeta =
|
||||
const VerificationMeta('configValue');
|
||||
late final GeneratedColumn<String> configValue = GeneratedColumn<String>(
|
||||
|
@ -670,14 +686,14 @@ class $ConfigTable extends Table with TableInfo<$ConfigTable, Config> {
|
|||
late final GeneratedColumnWithTypeConverter<SyncType?, int> syncState =
|
||||
GeneratedColumn<int>('sync_state', aliasedName, true,
|
||||
type: DriftSqlType.int, requiredDuringInsert: false)
|
||||
.withConverter<SyncType?>($ConfigTable.$convertersyncState);
|
||||
.withConverter<SyncType?>($ConfigTable.$convertersyncStaten);
|
||||
final VerificationMeta _syncStateImplicitMeta =
|
||||
const VerificationMeta('syncStateImplicit');
|
||||
late final GeneratedColumnWithTypeConverter<SyncType?, int>
|
||||
syncStateImplicit = GeneratedColumn<int>(
|
||||
'sync_state_implicit', aliasedName, true,
|
||||
type: DriftSqlType.int, requiredDuringInsert: false)
|
||||
.withConverter<SyncType?>($ConfigTable.$convertersyncStateImplicit);
|
||||
.withConverter<SyncType?>($ConfigTable.$convertersyncStateImplicitn);
|
||||
@override
|
||||
List<GeneratedColumn> get $columns =>
|
||||
[configKey, configValue, syncState, syncStateImplicit];
|
||||
|
@ -708,7 +724,7 @@ class $ConfigTable extends Table with TableInfo<$ConfigTable, Config> {
|
|||
}
|
||||
|
||||
@override
|
||||
Set<GeneratedColumn> get $primaryKey => const <GeneratedColumn>{};
|
||||
Set<GeneratedColumn> get $primaryKey => {configKey};
|
||||
@override
|
||||
Config map(Map<String, dynamic> data, {String? tablePrefix}) {
|
||||
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : '';
|
||||
|
@ -717,10 +733,10 @@ class $ConfigTable extends Table with TableInfo<$ConfigTable, Config> {
|
|||
.read(DriftSqlType.string, data['${effectivePrefix}config_key'])!,
|
||||
configValue: attachedDatabase.options.types
|
||||
.read(DriftSqlType.string, data['${effectivePrefix}config_value']),
|
||||
syncState: $ConfigTable.$convertersyncState.fromSql(attachedDatabase
|
||||
syncState: $ConfigTable.$convertersyncStaten.fromSql(attachedDatabase
|
||||
.options.types
|
||||
.read(DriftSqlType.int, data['${effectivePrefix}sync_state'])),
|
||||
syncStateImplicit: $ConfigTable.$convertersyncStateImplicit.fromSql(
|
||||
syncStateImplicit: $ConfigTable.$convertersyncStateImplicitn.fromSql(
|
||||
attachedDatabase.options.types.read(
|
||||
DriftSqlType.int, data['${effectivePrefix}sync_state_implicit'])),
|
||||
);
|
||||
|
@ -733,14 +749,16 @@ class $ConfigTable extends Table with TableInfo<$ConfigTable, Config> {
|
|||
|
||||
static TypeConverter<SyncType, int> $convertersyncState =
|
||||
const SyncTypeConverter();
|
||||
static TypeConverter<SyncType?, int> $convertersyncStaten =
|
||||
static TypeConverter<SyncType?, int?> $convertersyncStaten =
|
||||
NullAwareTypeConverter.wrap($convertersyncState);
|
||||
static TypeConverter<SyncType, int> $convertersyncStateImplicit =
|
||||
const EnumIndexConverter<SyncType>(SyncType.values);
|
||||
static TypeConverter<SyncType?, int> $convertersyncStateImplicitn =
|
||||
static TypeConverter<SyncType?, int?> $convertersyncStateImplicitn =
|
||||
NullAwareTypeConverter.wrap($convertersyncStateImplicit);
|
||||
@override
|
||||
bool get isStrict => true;
|
||||
@override
|
||||
bool get dontWriteConstraints => true;
|
||||
}
|
||||
|
||||
class MytableData extends DataClass implements Insertable<MytableData> {
|
||||
|
@ -994,6 +1012,9 @@ class $MytableTable extends Table with TableInfo<$MytableTable, MytableData> {
|
|||
$MytableTable createAlias(String alias) {
|
||||
return $MytableTable(attachedDatabase, alias);
|
||||
}
|
||||
|
||||
@override
|
||||
bool get dontWriteConstraints => true;
|
||||
}
|
||||
|
||||
class EMail extends DataClass implements Insertable<EMail> {
|
||||
|
@ -1200,6 +1221,8 @@ class $EmailTable extends Table
|
|||
return $EmailTable(attachedDatabase, alias);
|
||||
}
|
||||
|
||||
@override
|
||||
bool get dontWriteConstraints => true;
|
||||
@override
|
||||
String get moduleAndArgs => 'fts5(sender, title, body)';
|
||||
}
|
||||
|
@ -1377,6 +1400,9 @@ class $WeirdTableTable extends Table
|
|||
$WeirdTableTable createAlias(String alias) {
|
||||
return $WeirdTableTable(attachedDatabase, alias);
|
||||
}
|
||||
|
||||
@override
|
||||
bool get dontWriteConstraints => true;
|
||||
}
|
||||
|
||||
class MyViewData extends DataClass {
|
||||
|
@ -1478,10 +1504,10 @@ class MyView extends ViewInfo<MyView, MyViewData> implements HasResultSet {
|
|||
.read(DriftSqlType.string, data['${effectivePrefix}config_key'])!,
|
||||
configValue: attachedDatabase.options.types
|
||||
.read(DriftSqlType.string, data['${effectivePrefix}config_value']),
|
||||
syncState: MyView.$convertersyncState.fromSql(attachedDatabase
|
||||
syncState: $ConfigTable.$convertersyncStaten.fromSql(attachedDatabase
|
||||
.options.types
|
||||
.read(DriftSqlType.int, data['${effectivePrefix}sync_state'])),
|
||||
syncStateImplicit: MyView.$convertersyncStateImplicit.fromSql(
|
||||
syncStateImplicit: $ConfigTable.$convertersyncStateImplicitn.fromSql(
|
||||
attachedDatabase.options.types.read(
|
||||
DriftSqlType.int, data['${effectivePrefix}sync_state_implicit'])),
|
||||
);
|
||||
|
@ -1496,12 +1522,12 @@ class MyView extends ViewInfo<MyView, MyViewData> implements HasResultSet {
|
|||
late final GeneratedColumnWithTypeConverter<SyncType?, int> syncState =
|
||||
GeneratedColumn<int>('sync_state', aliasedName, true,
|
||||
type: DriftSqlType.int)
|
||||
.withConverter<SyncType?>(MyView.$convertersyncState);
|
||||
.withConverter<SyncType?>($ConfigTable.$convertersyncStaten);
|
||||
late final GeneratedColumnWithTypeConverter<SyncType?, int>
|
||||
syncStateImplicit = GeneratedColumn<int>(
|
||||
'sync_state_implicit', aliasedName, true,
|
||||
type: DriftSqlType.int)
|
||||
.withConverter<SyncType?>(MyView.$convertersyncStateImplicit);
|
||||
.withConverter<SyncType?>($ConfigTable.$convertersyncStateImplicitn);
|
||||
@override
|
||||
MyView createAlias(String alias) {
|
||||
return MyView(attachedDatabase, alias);
|
||||
|
@ -1534,8 +1560,7 @@ abstract class _$CustomTablesDb extends GeneratedDatabase {
|
|||
return customInsert(
|
||||
'REPLACE INTO config (config_key, config_value) VALUES (?1, ?2)',
|
||||
variables: [Variable<String>(key), Variable<String>(value)],
|
||||
updates: {},
|
||||
updateKind: UpdateKind.delete,
|
||||
updates: {config},
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -1605,11 +1630,11 @@ abstract class _$CustomTablesDb extends GeneratedDatabase {
|
|||
'SELECT config_key FROM config WHERE ${generatedpred.sql} AND(sync_state = ?1 OR sync_state_implicit IN ($expandedvar2))',
|
||||
variables: [
|
||||
Variable<int>(NullAwareTypeConverter.wrapToSql(
|
||||
$ConfigTable.$convertersyncStaten, var1)),
|
||||
$ConfigTable.$convertersyncState, var1)),
|
||||
...generatedpred.introducedVariables,
|
||||
for (var $ in var2)
|
||||
Variable<int>(NullAwareTypeConverter.wrapToSql(
|
||||
$ConfigTable.$convertersyncStateImplicitn, $))
|
||||
$ConfigTable.$convertersyncStateImplicit, $))
|
||||
],
|
||||
readsFrom: {
|
||||
config,
|
||||
|
@ -1703,30 +1728,19 @@ abstract class _$CustomTablesDb extends GeneratedDatabase {
|
|||
configKey: row.read<String>('config_key'),
|
||||
configValue: row.readNullable<String>('config_value'),
|
||||
syncState: NullAwareTypeConverter.wrapFromSql(
|
||||
$ConfigTable.$convertersyncStaten,
|
||||
$ConfigTable.$convertersyncState,
|
||||
row.readNullable<int>('sync_state')),
|
||||
syncStateImplicit: NullAwareTypeConverter.wrapFromSql(
|
||||
$ConfigTable.$convertersyncStateImplicitn,
|
||||
$ConfigTable.$convertersyncStateImplicit,
|
||||
row.readNullable<int>('sync_state_implicit')),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Selectable<ReadViewResult> readView() {
|
||||
Selectable<MyViewData> readView() {
|
||||
return customSelect('SELECT * FROM my_view', variables: [], readsFrom: {
|
||||
config,
|
||||
}).map((QueryRow row) {
|
||||
return ReadViewResult(
|
||||
row: row,
|
||||
configKey: row.read<String>('config_key'),
|
||||
configValue: row.readNullable<String>('config_value'),
|
||||
syncState: NullAwareTypeConverter.wrapFromSql(
|
||||
MyView.$convertersyncStaten, row.readNullable<int>('sync_state')),
|
||||
syncStateImplicit: NullAwareTypeConverter.wrapFromSql(
|
||||
MyView.$convertersyncStateImplicitn,
|
||||
row.readNullable<int>('sync_state_implicit')),
|
||||
);
|
||||
});
|
||||
}).asyncMap(myView.mapFromRow);
|
||||
}
|
||||
|
||||
Selectable<int> cfeTest() {
|
||||
|
@ -1797,22 +1811,8 @@ abstract class _$CustomTablesDb extends GeneratedDatabase {
|
|||
weirdTable,
|
||||
myTrigger,
|
||||
myView,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
OnCreateQuery(
|
||||
'INSERT INTO config (config_key, config_value) VALUES (\'key\', \'values\')'),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null
|
||||
'INSERT INTO config (config_key, config_value) VALUES (\'key\', \'values\')')
|
||||
];
|
||||
@override
|
||||
DriftDatabaseOptions get options =>
|
||||
|
@ -1949,41 +1949,6 @@ class ReadRowIdResult extends CustomResultSet {
|
|||
|
||||
typedef ReadRowId$expr = Expression<int> Function($ConfigTable config);
|
||||
|
||||
class ReadViewResult extends CustomResultSet {
|
||||
final String configKey;
|
||||
final String? configValue;
|
||||
final SyncType? syncState;
|
||||
final SyncType? syncStateImplicit;
|
||||
ReadViewResult({
|
||||
required QueryRow row,
|
||||
required this.configKey,
|
||||
this.configValue,
|
||||
this.syncState,
|
||||
this.syncStateImplicit,
|
||||
}) : super(row);
|
||||
@override
|
||||
int get hashCode =>
|
||||
Object.hash(configKey, configValue, syncState, syncStateImplicit);
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
(other is ReadViewResult &&
|
||||
other.configKey == this.configKey &&
|
||||
other.configValue == this.configValue &&
|
||||
other.syncState == this.syncState &&
|
||||
other.syncStateImplicit == this.syncStateImplicit);
|
||||
@override
|
||||
String toString() {
|
||||
return (StringBuffer('ReadViewResult(')
|
||||
..write('configKey: $configKey, ')
|
||||
..write('configValue: $configValue, ')
|
||||
..write('syncState: $syncState, ')
|
||||
..write('syncStateImplicit: $syncStateImplicit')
|
||||
..write(')'))
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
|
||||
class NestedResult extends CustomResultSet {
|
||||
final WithDefault defaults;
|
||||
final List<WithConstraint> nestedQuery0;
|
||||
|
|
|
@ -325,7 +325,7 @@ class Category extends DataClass implements Insertable<Category> {
|
|||
map['id'] = Variable<int>(id);
|
||||
map['desc'] = Variable<String>(description);
|
||||
{
|
||||
final converter = $CategoriesTable.$converterpriorityn;
|
||||
final converter = $CategoriesTable.$converterpriority;
|
||||
map['priority'] = Variable<int>(converter.toSql(priority));
|
||||
}
|
||||
return map;
|
||||
|
@ -450,7 +450,7 @@ class CategoriesCompanion extends UpdateCompanion<Category> {
|
|||
map['desc'] = Variable<String>(description.value);
|
||||
}
|
||||
if (priority.present) {
|
||||
final converter = $CategoriesTable.$converterpriorityn;
|
||||
final converter = $CategoriesTable.$converterpriority;
|
||||
map['priority'] = Variable<int>(converter.toSql(priority.value));
|
||||
}
|
||||
return map;
|
||||
|
@ -495,8 +495,7 @@ class $CategoriesTable extends Categories
|
|||
type: DriftSqlType.int,
|
||||
requiredDuringInsert: false,
|
||||
defaultValue: const Constant(0))
|
||||
.withConverter<CategoryPriority>(
|
||||
$CategoriesTable.$converterpriorityn);
|
||||
.withConverter<CategoryPriority>($CategoriesTable.$converterpriority);
|
||||
final VerificationMeta _descriptionInUpperCaseMeta =
|
||||
const VerificationMeta('descriptionInUpperCase');
|
||||
@override
|
||||
|
@ -546,7 +545,7 @@ class $CategoriesTable extends Categories
|
|||
.read(DriftSqlType.int, data['${effectivePrefix}id'])!,
|
||||
description: attachedDatabase.options.types
|
||||
.read(DriftSqlType.string, data['${effectivePrefix}desc'])!,
|
||||
priority: $CategoriesTable.$converterpriorityn.fromSql(attachedDatabase
|
||||
priority: $CategoriesTable.$converterpriority.fromSql(attachedDatabase
|
||||
.options.types
|
||||
.read(DriftSqlType.int, data['${effectivePrefix}priority'])!),
|
||||
descriptionInUpperCase: attachedDatabase.options.types.read(
|
||||
|
@ -1095,7 +1094,7 @@ class TableWithoutPKCompanion extends UpdateCompanion<CustomRowClass> {
|
|||
map['web_safe_int'] = Variable<BigInt>(webSafeInt.value);
|
||||
}
|
||||
if (custom.present) {
|
||||
final converter = $TableWithoutPKTable.$convertercustomn;
|
||||
final converter = $TableWithoutPKTable.$convertercustom;
|
||||
map['custom'] = Variable<String>(converter.toSql(custom.value));
|
||||
}
|
||||
return map;
|
||||
|
@ -1121,6 +1120,8 @@ class _$CustomRowClassInsertable implements Insertable<CustomRowClass> {
|
|||
@override
|
||||
Map<String, Expression> toColumns(bool nullToAbsent) {
|
||||
return TableWithoutPKCompanion(
|
||||
notReallyAnId: Value(_object.notReallyAnId),
|
||||
someFloat: Value(_object.someFloat),
|
||||
custom: Value(_object.custom),
|
||||
webSafeInt: Value(_object.webSafeInt),
|
||||
).toColumns(false);
|
||||
|
@ -1162,8 +1163,7 @@ class $TableWithoutPKTable extends TableWithoutPK
|
|||
type: DriftSqlType.string,
|
||||
requiredDuringInsert: false,
|
||||
clientDefault: _uuid.v4)
|
||||
.withConverter<MyCustomObject>(
|
||||
$TableWithoutPKTable.$convertercustomn);
|
||||
.withConverter<MyCustomObject>($TableWithoutPKTable.$convertercustom);
|
||||
@override
|
||||
List<GeneratedColumn> get $columns =>
|
||||
[notReallyAnId, someFloat, webSafeInt, custom];
|
||||
|
@ -1210,7 +1210,7 @@ class $TableWithoutPKTable extends TableWithoutPK
|
|||
.read(DriftSqlType.int, data['${effectivePrefix}not_really_an_id'])!,
|
||||
attachedDatabase.options.types
|
||||
.read(DriftSqlType.double, data['${effectivePrefix}some_float'])!,
|
||||
custom: $TableWithoutPKTable.$convertercustomn.fromSql(attachedDatabase
|
||||
custom: $TableWithoutPKTable.$convertercustom.fromSql(attachedDatabase
|
||||
.options.types
|
||||
.read(DriftSqlType.string, data['${effectivePrefix}custom'])!),
|
||||
webSafeInt: attachedDatabase.options.types
|
||||
|
@ -1234,7 +1234,7 @@ class PureDefault extends DataClass implements Insertable<PureDefault> {
|
|||
Map<String, Expression> toColumns(bool nullToAbsent) {
|
||||
final map = <String, Expression>{};
|
||||
if (!nullToAbsent || txt != null) {
|
||||
final converter = $PureDefaultsTable.$convertertxt;
|
||||
final converter = $PureDefaultsTable.$convertertxtn;
|
||||
map['insert'] = Variable<String>(converter.toSql(txt));
|
||||
}
|
||||
return map;
|
||||
|
@ -1250,7 +1250,7 @@ class PureDefault extends DataClass implements Insertable<PureDefault> {
|
|||
{ValueSerializer? serializer}) {
|
||||
serializer ??= driftRuntimeOptions.defaultSerializer;
|
||||
return PureDefault(
|
||||
txt: $PureDefaultsTable.$convertertxt
|
||||
txt: $PureDefaultsTable.$convertertxtn
|
||||
.fromJson(serializer.fromJson<Map<dynamic, dynamic>>(json['txt'])),
|
||||
);
|
||||
}
|
||||
|
@ -1264,7 +1264,7 @@ class PureDefault extends DataClass implements Insertable<PureDefault> {
|
|||
serializer ??= driftRuntimeOptions.defaultSerializer;
|
||||
return <String, dynamic>{
|
||||
'txt': serializer.toJson<Map<dynamic, dynamic>?>(
|
||||
$PureDefaultsTable.$convertertxt.toJson(txt)),
|
||||
$PureDefaultsTable.$convertertxtn.toJson(txt)),
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -1313,7 +1313,7 @@ class PureDefaultsCompanion extends UpdateCompanion<PureDefault> {
|
|||
Map<String, Expression> toColumns(bool nullToAbsent) {
|
||||
final map = <String, Expression>{};
|
||||
if (txt.present) {
|
||||
final converter = $PureDefaultsTable.$convertertxt;
|
||||
final converter = $PureDefaultsTable.$convertertxtn;
|
||||
map['insert'] = Variable<String>(converter.toSql(txt.value));
|
||||
}
|
||||
return map;
|
||||
|
@ -1339,7 +1339,7 @@ class $PureDefaultsTable extends PureDefaults
|
|||
late final GeneratedColumnWithTypeConverter<MyCustomObject?, String> txt =
|
||||
GeneratedColumn<String>('insert', aliasedName, true,
|
||||
type: DriftSqlType.string, requiredDuringInsert: false)
|
||||
.withConverter<MyCustomObject?>($PureDefaultsTable.$convertertxt);
|
||||
.withConverter<MyCustomObject?>($PureDefaultsTable.$convertertxtn);
|
||||
@override
|
||||
List<GeneratedColumn> get $columns => [txt];
|
||||
@override
|
||||
|
@ -1361,7 +1361,7 @@ class $PureDefaultsTable extends PureDefaults
|
|||
PureDefault map(Map<String, dynamic> data, {String? tablePrefix}) {
|
||||
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : '';
|
||||
return PureDefault(
|
||||
txt: $PureDefaultsTable.$convertertxt.fromSql(attachedDatabase
|
||||
txt: $PureDefaultsTable.$convertertxtn.fromSql(attachedDatabase
|
||||
.options.types
|
||||
.read(DriftSqlType.string, data['${effectivePrefix}insert'])),
|
||||
);
|
||||
|
@ -1372,10 +1372,10 @@ class $PureDefaultsTable extends PureDefaults
|
|||
return $PureDefaultsTable(attachedDatabase, alias);
|
||||
}
|
||||
|
||||
static JsonTypeConverter<MyCustomObject, String> $convertertxt =
|
||||
const CustomJsonConverter();
|
||||
static JsonTypeConverter<MyCustomObject?, String> $convertertxtn =
|
||||
JsonTypeConverter2.asNullable($convertertxt);
|
||||
static JsonTypeConverter2<MyCustomObject, String, Map<dynamic, dynamic>>
|
||||
$convertertxt = const CustomJsonConverter();
|
||||
static JsonTypeConverter2<MyCustomObject?, String?, Map<dynamic, dynamic>?>
|
||||
$convertertxtn = JsonTypeConverter2.asNullable($convertertxt);
|
||||
}
|
||||
|
||||
class CategoryTodoCountViewData extends DataClass {
|
||||
|
@ -1664,7 +1664,7 @@ abstract class _$TodoDb extends GeneratedDatabase {
|
|||
variables: [],
|
||||
readsFrom: {
|
||||
tableWithoutPK,
|
||||
}).map((QueryRow row) => $TableWithoutPKTable.$convertercustomn
|
||||
}).map((QueryRow row) => $TableWithoutPKTable.$convertercustom
|
||||
.fromSql(row.read<String>('custom')));
|
||||
}
|
||||
|
||||
|
@ -1741,6 +1741,7 @@ mixin _$SomeDaoMixin on DatabaseAccessor<TodoDb> {
|
|||
$TodosTableTable get todosTable => attachedDatabase.todosTable;
|
||||
$TodoWithCategoryViewView get todoWithCategoryView =>
|
||||
attachedDatabase.todoWithCategoryView;
|
||||
$CategoriesTable get categories => attachedDatabase.categories;
|
||||
Selectable<TodoEntry> todosForUser({required int user}) {
|
||||
return customSelect(
|
||||
'SELECT t.* FROM todos AS t INNER JOIN shared_todos AS st ON st.todo = t.id INNER JOIN users AS u ON u.id = st.user WHERE u.id = ?1',
|
||||
|
|
|
@ -324,6 +324,7 @@ class DriftTableResolver extends LocalElementResolver<DiscoveredDriftTable> {
|
|||
strict: table.isStrict,
|
||||
tableConstraints: tableConstraints,
|
||||
virtualTableData: virtualTableData,
|
||||
writeDefaultConstraints: false,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -42,6 +42,7 @@ class DriftViewResolver extends DriftElementResolver<DiscoveredDriftView> {
|
|||
declaration: DriftDeclaration.driftFile(stmt, file.ownUri),
|
||||
nullable: type?.nullable == true,
|
||||
typeConverter: converter,
|
||||
foreignConverter: true,
|
||||
);
|
||||
|
||||
columns.add(driftColumn);
|
||||
|
|
|
@ -156,7 +156,7 @@ class ElementSerializer {
|
|||
'nameInDart': column.nameInDart,
|
||||
'declaration': column.declaration.toJson(),
|
||||
'typeConverter': column.typeConverter != null
|
||||
? _serializeTypeConverter(column.typeConverter!)
|
||||
? _serializeTypeConverter(column, column.typeConverter!)
|
||||
: null,
|
||||
'clientDefaultCode': column.clientDefaultCode?.toJson(),
|
||||
'defaultArgument': column.defaultArgument?.toJson(),
|
||||
|
@ -254,7 +254,8 @@ class ElementSerializer {
|
|||
return action?.name;
|
||||
}
|
||||
|
||||
Map<String, Object?> _serializeTypeConverter(AppliedTypeConverter converter) {
|
||||
Map<String, Object?> _serializeTypeConverter(
|
||||
DriftColumn appliedTo, AppliedTypeConverter converter) {
|
||||
return {
|
||||
'expression': converter.expression.toJson(),
|
||||
'dart_type': converter.dartType.accept(const _DartTypeSerializer()),
|
||||
|
@ -262,6 +263,8 @@ class ElementSerializer {
|
|||
'sql_type': converter.sqlType.name,
|
||||
'dart_type_is_nullable': converter.dartTypeIsNullable,
|
||||
'sql_type_is_nullable': converter.sqlTypeIsNullable,
|
||||
if (converter.owningColumn != appliedTo)
|
||||
'owner': _serializeColumnReference(converter.owningColumn),
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -345,6 +348,7 @@ class _DartTypeSerializer extends TypeVisitor<Map<String, Object?>> {
|
|||
|
||||
class ElementDeserializer {
|
||||
final Map<Uri, LibraryElement> _loadedLibraries = {};
|
||||
final List<DriftElementId> _currentlyReading = [];
|
||||
|
||||
final DriftAnalysisDriver driver;
|
||||
|
||||
|
@ -400,15 +404,23 @@ class ElementDeserializer {
|
|||
'Analysis data for ${id..libraryUri} not found');
|
||||
}
|
||||
|
||||
assert(!_currentlyReading.contains(id));
|
||||
try {
|
||||
_currentlyReading.add(id);
|
||||
|
||||
final result = await _readDriftElement(data[id.name] as Map);
|
||||
state
|
||||
..result = result
|
||||
..isUpToDate = true;
|
||||
return result;
|
||||
} catch (e, s) {
|
||||
if (e is CouldNotDeserializeException) rethrow;
|
||||
|
||||
throw CouldNotDeserializeException(
|
||||
'Internal error while deserializing $id: $e at \n$s');
|
||||
} finally {
|
||||
final lastId = _currentlyReading.removeLast();
|
||||
assert(lastId == id);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -460,8 +472,8 @@ class ElementDeserializer {
|
|||
}
|
||||
|
||||
virtualTableData = VirtualTableData(
|
||||
json['module'] as String,
|
||||
(json['arguments'] as List).cast(),
|
||||
data['module'] as String,
|
||||
(data['arguments'] as List).cast(),
|
||||
recognizedModule,
|
||||
);
|
||||
}
|
||||
|
@ -635,15 +647,17 @@ class ElementDeserializer {
|
|||
}
|
||||
|
||||
Future<DriftColumn> _readColumn(Map json) async {
|
||||
final rawConverter = json['typeConverter'] as Map?;
|
||||
|
||||
return DriftColumn(
|
||||
sqlType: DriftSqlType.values.byName(json['sqlType'] as String),
|
||||
nullable: json['nullable'] as bool,
|
||||
nameInSql: json['nameInSql'] as String,
|
||||
nameInDart: json['nameInDart'] as String,
|
||||
declaration: DriftDeclaration.fromJson(json['declaration'] as Map),
|
||||
typeConverter: json['typeConverter'] != null
|
||||
? await _readTypeConverter(json['typeConverter'] as Map)
|
||||
: null,
|
||||
typeConverter:
|
||||
rawConverter != null ? await _readTypeConverter(rawConverter) : null,
|
||||
foreignConverter: rawConverter != null && rawConverter['owner'] != null,
|
||||
clientDefaultCode: json['clientDefaultCode'] != null
|
||||
? AnnotatedDartCode.fromJson(json['clientDefaultCode'] as Map)
|
||||
: null,
|
||||
|
@ -661,7 +675,13 @@ class ElementDeserializer {
|
|||
}
|
||||
|
||||
Future<AppliedTypeConverter> _readTypeConverter(Map json) async {
|
||||
return AppliedTypeConverter(
|
||||
final owner = json['owner'];
|
||||
DriftColumn? readOwner;
|
||||
if (owner != null) {
|
||||
readOwner = await _readDriftColumnReference(owner as Map);
|
||||
}
|
||||
|
||||
final converter = AppliedTypeConverter(
|
||||
expression: AnnotatedDartCode.fromJson(json['expression'] as Map),
|
||||
dartType: await _readDartType(json['dart_type'] as Map),
|
||||
jsonType: json['json_type'] != null
|
||||
|
@ -671,6 +691,10 @@ class ElementDeserializer {
|
|||
dartTypeIsNullable: json['dart_type_is_nullable'] as bool,
|
||||
sqlTypeIsNullable: json['sql_type_is_nullable'] as bool,
|
||||
);
|
||||
|
||||
if (readOwner != null) converter.owningColumn = readOwner;
|
||||
|
||||
return converter;
|
||||
}
|
||||
|
||||
ReferenceAction? _readAction(String? value) {
|
||||
|
|
|
@ -142,7 +142,8 @@ class DatabaseWriter {
|
|||
..write('=> [');
|
||||
|
||||
schemaScope
|
||||
..write(elements.map((e) {
|
||||
..write(elements
|
||||
.map((e) {
|
||||
if (e is DefinedSqlQuery && e.mode == QueryMode.atCreate) {
|
||||
final resolved = input.importedQueries[e]!;
|
||||
final sql = schemaScope.sqlCode(resolved.root!);
|
||||
|
@ -151,7 +152,9 @@ class DatabaseWriter {
|
|||
}
|
||||
|
||||
return entityGetters[e];
|
||||
}).join(', '))
|
||||
})
|
||||
.whereType<String>()
|
||||
.join(', '))
|
||||
// close list literal and allSchemaEntities getter
|
||||
..write('];\n');
|
||||
|
||||
|
|
|
@ -59,7 +59,7 @@ class ResultSetWriter {
|
|||
}
|
||||
|
||||
into
|
||||
..write(modifier)
|
||||
..write('$modifier ')
|
||||
..writeDart(nested.resultRowType(scope))
|
||||
..writeln('$fieldName;');
|
||||
|
||||
|
|
|
@ -131,7 +131,9 @@ class DataClassWriter {
|
|||
|
||||
final typeConverter = column.typeConverter;
|
||||
if (typeConverter != null && typeConverter.alsoAppliesToJsonConversion) {
|
||||
final type = typeConverter.jsonType;
|
||||
final type =
|
||||
_emitter.dartCode(AnnotatedDartCode.type(typeConverter.jsonType!));
|
||||
|
||||
final fromConverter = "serializer.fromJson<$type>(json['$jsonKey'])";
|
||||
final converterField = _converter(column);
|
||||
deserialized = '$converterField.fromJson($fromConverter)';
|
||||
|
|
|
@ -239,10 +239,10 @@ class UpdateCompanionWriter {
|
|||
'Map<String, Expression> toColumns(bool nullToAbsent) {\n'
|
||||
'return $_companionClass(\n');
|
||||
|
||||
final fields = info.positionalColumns.followedBy(info.namedColumns.keys);
|
||||
for (final field in fields) {
|
||||
final columns = info.positionalColumns.followedBy(info.namedColumns.values);
|
||||
for (final columnName in columns) {
|
||||
final column =
|
||||
table.columns.firstWhereOrNull((e) => e.nameInDart == field);
|
||||
table.columns.firstWhereOrNull((e) => e.nameInSql == columnName);
|
||||
|
||||
if (column != null && !column.isGenerated) {
|
||||
final dartName = column.nameInDart;
|
||||
|
|
|
@ -69,8 +69,8 @@ abstract class _NodeOrWriter {
|
|||
AnnotatedDartCode readConverter(AppliedTypeConverter converter,
|
||||
{bool forNullable = false}) {
|
||||
final fieldName =
|
||||
forNullable ? converter.fieldName : converter.nullableFieldName;
|
||||
final table = converter.owningColumn.owner as DriftElementWithResultSet;
|
||||
forNullable ? converter.nullableFieldName : converter.fieldName;
|
||||
final table = converter.owningColumn.owner;
|
||||
|
||||
return AnnotatedDartCode([
|
||||
DartTopLevelSymbol(table.entityInfoName, table.id.libraryUri),
|
||||
|
@ -81,21 +81,31 @@ abstract class _NodeOrWriter {
|
|||
/// A suitable typename to store an instance of the type converter used here.
|
||||
AnnotatedDartCode converterType(AppliedTypeConverter converter,
|
||||
{bool makeNullable = false}) {
|
||||
// Write something like `TypeConverter<MyFancyObject, String>`
|
||||
return AnnotatedDartCode.build((b) {
|
||||
var sqlDartType = dartTypeNames[converter.sqlType]!;
|
||||
final className = converter.alsoAppliesToJsonConversion
|
||||
? 'JsonTypeConverter'
|
||||
? 'JsonTypeConverter2'
|
||||
: 'TypeConverter';
|
||||
|
||||
// Write something like `TypeConverter<MyFancyObject, String>`
|
||||
return AnnotatedDartCode([
|
||||
DartTopLevelSymbol.drift(className),
|
||||
'<',
|
||||
...AnnotatedDartCode.type(converter.dartType).elements,
|
||||
if (makeNullable) '?',
|
||||
',',
|
||||
sqlDartType,
|
||||
'>',
|
||||
]);
|
||||
b
|
||||
..addSymbol(className, AnnotatedDartCode.drift)
|
||||
..addText('<')
|
||||
..addDartType(converter.dartType)
|
||||
..questionMarkIfNullable(makeNullable)
|
||||
..addText(',')
|
||||
..addTopLevel(sqlDartType)
|
||||
..questionMarkIfNullable(makeNullable);
|
||||
|
||||
if (converter.alsoAppliesToJsonConversion) {
|
||||
b
|
||||
..addText(',')
|
||||
..addDartType(converter.jsonType!)
|
||||
..questionMarkIfNullable(makeNullable);
|
||||
}
|
||||
|
||||
b.addText('>');
|
||||
});
|
||||
}
|
||||
|
||||
AnnotatedDartCode dartType(HasType hasType) {
|
||||
|
@ -306,3 +316,9 @@ String thisIfNeeded(String getter, Set<String> locals) {
|
|||
|
||||
return getter;
|
||||
}
|
||||
|
||||
extension on AnnotatedDartCodeBuilder {
|
||||
void questionMarkIfNullable(bool nullable) {
|
||||
if (nullable) addText('?');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue