-
|
First, thank you for this excellent library! It has made database operations very easy to use in my apps. I need to make a SELECT statement with what potentially may be a very large WHERE..IN clause. I can easily create a temporary table with a Is there a way to operate on a table created with a |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
|
Hi, glad you're enjoying drift!
Because drift does not support the final class TemporaryTable extends Table
with TableInfo<TemporaryTable, Map<String, dynamic>> {
@override
final String actualTableName;
final String? alias;
@override
final DatabaseConnectionUser attachedDatabase;
TemporaryTable(this.attachedDatabase, this.actualTableName, [this.alias]);
late final GeneratedColumn<String> value = GeneratedColumn(
'value', actualTableName, false,
type: DriftSqlType.string);
@override
List<GeneratedColumn<Object>> get $columns => [value];
@override
TableInfo<TemporaryTable, Map<String, dynamic>> createAlias(String alias) {
return TemporaryTable(attachedDatabase, actualTableName, alias);
}
@override
FutureOr<Map<String, dynamic>> map(Map<String, dynamic> data,
{String? tablePrefix}) {
return data;
}
}After creating the table, you can use something like this to insert into it: final table = TemporaryTable(db, 'pending_values');
await db.batch((b) {
b.insert(table, RawValuesInsertable({'value': Variable('...')}));
});Similarly, you could join the |
Beta Was this translation helpful? Give feedback.
-
|
Great, this looks like just what I need! I do get a runtime error for the batch insert though: My code (path is a String): await batch((batch) {
batch.insertAll(
temporaryTable,
filePaths.map(
(path) => RawValuesInsertable({columnName: Variable(path)}),
),
);
}); |
Beta Was this translation helpful? Give feedback.
-
|
I expanded Simon's class a bit to create a general purpose single-value temporary table if anyone else needs a solution for this use case. /// Allows Drift query builder access for temporary tables created
/// with [customStatement]. Does not support nullable values.
///
final class TemporaryTable<T extends Object> extends Table
with TableInfo<TemporaryTable, Map<String, dynamic>> {
final DriftSqlType<T> type;
final GeneratedColumn<T> value;
@override
final String actualTableName;
final String? alias;
@override
final DatabaseConnectionUser attachedDatabase;
TemporaryTable(
this.attachedDatabase,
this.actualTableName, {
this.alias,
required this.type,
}) : value = GeneratedColumn(
'value',
actualTableName,
false,
type: type,
);
@override
List<GeneratedColumn<Object>> get $columns => [value];
@override
TableInfo<TemporaryTable, Map<String, dynamic>> createAlias(String alias) {
return TemporaryTable(
attachedDatabase,
actualTableName,
alias: alias,
type: type,
);
}
@override
FutureOr<Map<String, dynamic>> map(Map<String, dynamic> data,
{String? tablePrefix}) {
return data;
}
Future<void> create() {
final context = GenerationContext.fromDb(attachedDatabase);
return attachedDatabase.customStatement(
'CREATE TEMPORARY TABLE $actualTableName '
'(${value.name} ${value.type.sqlTypeName(context)} NOT NULL)',
);
}
Future<void> drop() {
return attachedDatabase.customStatement(
'DROP TABLE IF EXISTS $actualTableName',
);
}
Insertable<Map<String, dynamic>> insertable(T value) =>
RawValuesInsertable<Map<String, dynamic>>(
{this.value.name: Variable(value)},
);
} |
Beta Was this translation helpful? Give feedback.
Does it work if you add an explicit
RawValuesInsertable<Map<String, dynamic>>()type parameter?