dyamic tables in query #2783
-
Considering I have tables String tableNum = 42;
select(mainTable, distinct: true).join([
innerJoin(table$tableNum , table$tableNum.mainTableId.equalsExp(mainTable.id)),
])..where(table$tableNum.commonCol.equals('query') Instead of having to do this: String tableNum = 42;
if (tableNum == 1){
select(mainTable, distinct: true).join([
innerJoin(table1 , table1.mainTableId.equalsExp(mainTable.id)),
])..where(table1.commonCol.equals('query')
} else if (tableNum == 2){
select(mainTable, distinct: true).join([
innerJoin(table2 , table2.mainTableId.equalsExp(mainTable.id)),
])..where(table2.commonCol.equals('query')
} else if (...) {...} I know I can write a custom query but the query is very big already and I don't want to have to manually write it. |
Beta Was this translation helpful? Give feedback.
Answered by
simolus3
Dec 12, 2023
Replies: 1 comment 1 reply
-
Sure, drift exposes the schema at runtime and this can be used to craft dynamic queries. final table = switch (tableNum) {
1 => table1,
2 => table2,
3 => table3,
_ => throw 'todo',
};
final mainTableId = table.columnsByName['main_table_id'] as GeneratedColumn<int>;
final commonCol = table.columnsByName['common_col'] as GeneratedColumn<String>;
select(mainTable, distinct: true).join([
innerJoin(table , mainTableId.equalsExp(mainTable.id)),
])..where(commonCol.equals('query'); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
BananaMasterz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sure, drift exposes the schema at runtime and this can be used to craft dynamic queries.