/
custom.dart
75 lines (62 loc) · 2.33 KB
/
custom.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
part of '../query_builder.dart';
/// A custom expression that can appear in a sql statement.
/// The [CustomExpression.content] will be written into the query without any
/// modification.
///
/// See also:
/// - [currentDate] and [currentDateAndTime], which use a [CustomExpression]
/// internally.
class CustomExpression<D extends Object> extends Expression<D> {
/// The SQL of this expression
final String content;
final Map<SqlDialect, String>? _dialectSpecificContent;
/// Additional tables that this expression is watching.
///
/// When this expression is used in a stream query, the stream will update
/// when any table in [watchedTables] changes.
/// Usually, expressions don't introduce new tables to watch. This field is
/// mainly used for subqueries used as expressions.
final Iterable<TableInfo> watchedTables;
@override
final Precedence precedence;
final UserDefinedSqlType<D>? _customSqlType;
/// Constructs a custom expression by providing the raw sql [content].
const CustomExpression(
this.content, {
this.watchedTables = const [],
this.precedence = Precedence.unknown,
UserDefinedSqlType<D>? customType,
}) : _dialectSpecificContent = null,
_customSqlType = customType;
/// Constructs a custom expression providing the raw SQL in [content] depending
/// on the SQL dialect when this expression is built.
const CustomExpression.dialectSpecific(
Map<SqlDialect, String> content, {
this.watchedTables = const [],
this.precedence = Precedence.unknown,
UserDefinedSqlType<D>? customType,
}) : _dialectSpecificContent = content,
content = '',
_customSqlType = customType;
@override
void writeInto(GenerationContext context) {
final dialectSpecific = _dialectSpecificContent;
if (dialectSpecific != null) {
final dialect = context.dialect;
context.buffer.write(dialectSpecific[dialect]);
} else {
context.buffer.write(content);
}
context.watchedTables.addAll(watchedTables);
}
@override
int get hashCode => content.hashCode * 3;
@override
BaseSqlType<D> get driftSqlType => _customSqlType ?? super.driftSqlType;
@override
bool operator ==(Object other) {
return other.runtimeType == runtimeType &&
// ignore: test_types_in_equals
(other as CustomExpression).content == content;
}
}