drift/sqlparser/test/parser/insert_test.dart

224 lines
6.2 KiB
Dart
Raw Normal View History

2019-08-29 06:04:39 -07:00
import 'package:sqlparser/sqlparser.dart';
2021-09-10 02:43:21 -07:00
import 'package:test/test.dart';
2019-08-29 06:04:39 -07:00
import 'utils.dart';
void main() {
test('parses insert statements', () {
testStatement(
'INSERT OR REPLACE INTO tbl (a, b, c) VALUES (d, e, f)',
InsertStatement(
mode: InsertMode.insertOrReplace,
2021-07-22 12:32:53 -07:00
table: TableReference('tbl'),
2019-08-29 06:04:39 -07:00
targetColumns: [
Reference(columnName: 'a'),
Reference(columnName: 'b'),
Reference(columnName: 'c'),
],
source: ValuesSource([
2019-09-15 02:19:32 -07:00
Tuple(expressions: [
2019-08-29 06:04:39 -07:00
Reference(columnName: 'd'),
Reference(columnName: 'e'),
Reference(columnName: 'f'),
]),
]),
),
);
});
test('insert statement with default values', () {
testStatement(
'INSERT INTO tbl DEFAULT VALUES',
InsertStatement(
mode: InsertMode.insert,
2021-07-22 12:32:53 -07:00
table: TableReference('tbl'),
2019-08-29 06:04:39 -07:00
targetColumns: const [],
source: DefaultValues(),
2019-08-29 06:04:39 -07:00
),
);
});
test('insert statement with select as source', () {
testStatement(
'REPLACE INTO tbl SELECT * FROM tbl',
InsertStatement(
mode: InsertMode.replace,
2021-07-22 12:32:53 -07:00
table: TableReference('tbl'),
2019-08-29 06:04:39 -07:00
targetColumns: const [],
source: SelectInsertSource(
SelectStatement(
columns: [StarResultColumn(null)],
2021-07-22 12:32:53 -07:00
from: TableReference('tbl'),
2019-08-29 06:04:39 -07:00
),
),
),
);
});
group('parses upsert clauses', () {
const prefix = 'INSERT INTO tbl DEFAULT VALUES ON CONFLICT';
test('without listing indexed columns', () {
testStatement(
'$prefix DO NOTHING',
InsertStatement(
table: TableReference('tbl'),
targetColumns: const [],
source: DefaultValues(),
upsert: UpsertClause([UpsertClauseEntry(action: DoNothing())]),
),
);
});
test('listing indexed columns without where clause', () {
testStatement(
'$prefix (foo, bar DESC) DO NOTHING',
InsertStatement(
table: TableReference('tbl'),
targetColumns: const [],
source: DefaultValues(),
upsert: UpsertClause(
[
UpsertClauseEntry(
onColumns: [
IndexedColumn(Reference(columnName: 'foo')),
IndexedColumn(
Reference(columnName: 'bar'),
OrderingMode.descending,
),
],
action: DoNothing(),
),
],
),
),
);
});
test('listing indexed columns and where clause', () {
testStatement(
'$prefix (foo, bar) WHERE 2 = foo DO NOTHING',
InsertStatement(
table: TableReference('tbl'),
targetColumns: const [],
source: DefaultValues(),
upsert: UpsertClause(
[
UpsertClauseEntry(
onColumns: [
IndexedColumn(Reference(columnName: 'foo')),
IndexedColumn(Reference(columnName: 'bar')),
],
where: BinaryExpression(
NumericLiteral(2),
token(TokenType.equal),
Reference(columnName: 'foo'),
),
action: DoNothing(),
2021-03-13 06:21:27 -08:00
),
],
),
),
);
});
test('having an update action without where', () {
testStatement(
'$prefix DO UPDATE SET foo = 2',
InsertStatement(
table: TableReference('tbl'),
targetColumns: const [],
source: DefaultValues(),
upsert: UpsertClause(
[
UpsertClauseEntry(
action: DoUpdate(
[
SingleColumnSetComponent(
column: Reference(columnName: 'foo'),
expression: NumericLiteral(2),
),
],
),
2021-03-13 06:21:27 -08:00
),
],
),
),
);
});
test('having an update action with where', () {
testStatement(
'$prefix DO UPDATE SET foo = 2 WHERE ?',
InsertStatement(
table: TableReference('tbl'),
targetColumns: const [],
source: DefaultValues(),
upsert: UpsertClause(
[
UpsertClauseEntry(
action: DoUpdate(
[
SingleColumnSetComponent(
column: Reference(columnName: 'foo'),
expression: NumericLiteral(2),
),
],
where: NumberedVariable(null),
),
),
],
),
2021-03-13 06:21:27 -08:00
),
);
});
test('having more than one clause', () {
testStatement(
'$prefix (foo) DO NOTHING ON CONFLICT (bar) DO UPDATE SET x = 2',
InsertStatement(
table: TableReference('tbl'),
targetColumns: const [],
source: DefaultValues(),
upsert: UpsertClause(
[
UpsertClauseEntry(
onColumns: [IndexedColumn(Reference(columnName: 'foo'))],
action: DoNothing(),
),
UpsertClauseEntry(
onColumns: [IndexedColumn(Reference(columnName: 'bar'))],
action: DoUpdate(
[
SingleColumnSetComponent(
column: Reference(columnName: 'x'),
expression: NumericLiteral(2),
),
],
),
2021-03-13 06:21:27 -08:00
),
],
),
),
);
});
});
2021-03-13 11:41:14 -08:00
test('parses RETURNING clause', () {
testStatement(
'INSERT INTO tbl DEFAULT VALUES RETURNING foo, 3, bar;',
InsertStatement(
table: TableReference('tbl'),
targetColumns: const [],
source: DefaultValues(),
returning: Returning([
ExpressionResultColumn(expression: Reference(columnName: 'foo')),
ExpressionResultColumn(
expression: NumericLiteral(3),
2021-03-13 11:41:14 -08:00
),
ExpressionResultColumn(expression: Reference(columnName: 'bar')),
]),
),
);
});
2019-08-29 06:04:39 -07:00
}