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 [],
|
2020-06-15 08:45:28 -07:00
|
|
|
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
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
2020-02-03 12:43:18 -08: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 [],
|
2020-06-15 08:45:28 -07:00
|
|
|
source: DefaultValues(),
|
2021-03-13 07:07:12 -08:00
|
|
|
upsert: UpsertClause([UpsertClauseEntry(action: DoNothing())]),
|
2020-02-03 12:43:18 -08:00
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('listing indexed columns without where clause', () {
|
|
|
|
testStatement(
|
|
|
|
'$prefix (foo, bar DESC) DO NOTHING',
|
|
|
|
InsertStatement(
|
|
|
|
table: TableReference('tbl'),
|
|
|
|
targetColumns: const [],
|
2020-06-15 08:45:28 -07:00
|
|
|
source: DefaultValues(),
|
2021-03-13 07:07:12 -08:00
|
|
|
upsert: UpsertClause(
|
|
|
|
[
|
|
|
|
UpsertClauseEntry(
|
|
|
|
onColumns: [
|
|
|
|
IndexedColumn(Reference(columnName: 'foo')),
|
|
|
|
IndexedColumn(
|
|
|
|
Reference(columnName: 'bar'),
|
|
|
|
OrderingMode.descending,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
action: DoNothing(),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2020-02-03 12:43:18 -08:00
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('listing indexed columns and where clause', () {
|
|
|
|
testStatement(
|
|
|
|
'$prefix (foo, bar) WHERE 2 = foo DO NOTHING',
|
|
|
|
InsertStatement(
|
|
|
|
table: TableReference('tbl'),
|
|
|
|
targetColumns: const [],
|
2020-06-15 08:45:28 -07:00
|
|
|
source: DefaultValues(),
|
2021-03-13 07:07:12 -08:00
|
|
|
upsert: UpsertClause(
|
|
|
|
[
|
|
|
|
UpsertClauseEntry(
|
|
|
|
onColumns: [
|
|
|
|
IndexedColumn(Reference(columnName: 'foo')),
|
|
|
|
IndexedColumn(Reference(columnName: 'bar')),
|
|
|
|
],
|
|
|
|
where: BinaryExpression(
|
2022-12-30 11:46:18 -08:00
|
|
|
NumericLiteral(2),
|
2021-03-13 07:07:12 -08:00
|
|
|
token(TokenType.equal),
|
|
|
|
Reference(columnName: 'foo'),
|
|
|
|
),
|
|
|
|
action: DoNothing(),
|
2021-03-13 06:21:27 -08:00
|
|
|
),
|
2021-03-13 07:07:12 -08:00
|
|
|
],
|
|
|
|
),
|
2020-02-03 12:43:18 -08:00
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('having an update action without where', () {
|
|
|
|
testStatement(
|
|
|
|
'$prefix DO UPDATE SET foo = 2',
|
|
|
|
InsertStatement(
|
|
|
|
table: TableReference('tbl'),
|
|
|
|
targetColumns: const [],
|
2020-06-15 08:45:28 -07:00
|
|
|
source: DefaultValues(),
|
2021-03-13 07:07:12 -08:00
|
|
|
upsert: UpsertClause(
|
|
|
|
[
|
|
|
|
UpsertClauseEntry(
|
|
|
|
action: DoUpdate(
|
|
|
|
[
|
2023-11-16 16:42:39 -08:00
|
|
|
SingleColumnSetComponent(
|
2021-03-13 07:07:12 -08:00
|
|
|
column: Reference(columnName: 'foo'),
|
2022-12-30 11:46:18 -08:00
|
|
|
expression: NumericLiteral(2),
|
2021-03-13 07:07:12 -08:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2021-03-13 06:21:27 -08:00
|
|
|
),
|
2021-03-13 07:07:12 -08:00
|
|
|
],
|
|
|
|
),
|
2020-02-03 12:43:18 -08:00
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('having an update action with where', () {
|
|
|
|
testStatement(
|
|
|
|
'$prefix DO UPDATE SET foo = 2 WHERE ?',
|
|
|
|
InsertStatement(
|
|
|
|
table: TableReference('tbl'),
|
|
|
|
targetColumns: const [],
|
2020-06-15 08:45:28 -07:00
|
|
|
source: DefaultValues(),
|
2021-03-13 07:07:12 -08:00
|
|
|
upsert: UpsertClause(
|
|
|
|
[
|
|
|
|
UpsertClauseEntry(
|
|
|
|
action: DoUpdate(
|
|
|
|
[
|
2023-11-16 16:42:39 -08:00
|
|
|
SingleColumnSetComponent(
|
2021-03-13 07:07:12 -08:00
|
|
|
column: Reference(columnName: 'foo'),
|
2022-12-30 11:46:18 -08:00
|
|
|
expression: NumericLiteral(2),
|
2021-03-13 07:07:12 -08:00
|
|
|
),
|
|
|
|
],
|
2022-12-30 11:46:18 -08:00
|
|
|
where: NumberedVariable(null),
|
2020-02-03 12:43:18 -08:00
|
|
|
),
|
|
|
|
),
|
2021-03-13 07:07:12 -08:00
|
|
|
],
|
|
|
|
),
|
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(),
|
2021-03-13 07:07:12 -08:00
|
|
|
upsert: UpsertClause(
|
|
|
|
[
|
|
|
|
UpsertClauseEntry(
|
|
|
|
onColumns: [IndexedColumn(Reference(columnName: 'foo'))],
|
|
|
|
action: DoNothing(),
|
|
|
|
),
|
|
|
|
UpsertClauseEntry(
|
|
|
|
onColumns: [IndexedColumn(Reference(columnName: 'bar'))],
|
|
|
|
action: DoUpdate(
|
|
|
|
[
|
2023-11-16 16:42:39 -08:00
|
|
|
SingleColumnSetComponent(
|
2021-03-13 07:07:12 -08:00
|
|
|
column: Reference(columnName: 'x'),
|
2022-12-30 11:46:18 -08:00
|
|
|
expression: NumericLiteral(2),
|
2021-03-13 07:07:12 -08:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2021-03-13 06:21:27 -08:00
|
|
|
),
|
2021-03-13 07:07:12 -08:00
|
|
|
],
|
|
|
|
),
|
2020-02-03 12:43:18 -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(
|
2022-12-30 11:46:18 -08:00
|
|
|
expression: NumericLiteral(3),
|
2021-03-13 11:41:14 -08:00
|
|
|
),
|
|
|
|
ExpressionResultColumn(expression: Reference(columnName: 'bar')),
|
|
|
|
]),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
2019-08-29 06:04:39 -07:00
|
|
|
}
|