2019-09-16 13:33:36 -07:00
|
|
|
import 'package:sqlparser/sqlparser.dart';
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
2019-09-18 12:36:25 -07:00
|
|
|
import 'utils.dart';
|
|
|
|
|
2019-09-16 13:33:36 -07:00
|
|
|
const content = r'''
|
|
|
|
import 'other.dart';
|
|
|
|
import 'another.moor';
|
|
|
|
|
|
|
|
CREATE TABLE tbl (
|
|
|
|
id INT NOT NULL PRIMARY KEY AUTOINCREMENT,
|
|
|
|
-- this is a single-line comment
|
|
|
|
place VARCHAR REFERENCES other(location)
|
2020-03-21 14:02:27 -07:00
|
|
|
) AS RowName;
|
2019-09-16 13:33:36 -07:00
|
|
|
|
|
|
|
all: SELECT /* COUNT(*), */ * FROM tbl WHERE $predicate;
|
2019-12-17 13:03:59 -08:00
|
|
|
@special: SELECT * FROM tbl;
|
2019-12-25 11:24:03 -08:00
|
|
|
typeHints(:foo AS TEXT): SELECT :foo;
|
2020-04-03 09:37:33 -07:00
|
|
|
nested: SELECT foo.** FROM tbl foo;
|
2019-09-16 13:33:36 -07:00
|
|
|
''';
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
test('parses moor files', () {
|
2019-10-27 03:51:09 -07:00
|
|
|
testMoorFile(
|
|
|
|
content,
|
2019-09-16 13:33:36 -07:00
|
|
|
MoorFile([
|
|
|
|
ImportStatement('other.dart'),
|
|
|
|
ImportStatement('another.moor'),
|
|
|
|
CreateTableStatement(
|
|
|
|
tableName: 'tbl',
|
|
|
|
columns: [
|
|
|
|
ColumnDefinition(
|
|
|
|
columnName: 'id',
|
|
|
|
typeName: 'INT',
|
|
|
|
constraints: [
|
|
|
|
NotNull(null),
|
|
|
|
PrimaryKeyColumn(null, autoIncrement: true),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
ColumnDefinition(
|
|
|
|
columnName: 'place',
|
|
|
|
typeName: 'VARCHAR',
|
|
|
|
constraints: [
|
|
|
|
ForeignKeyColumnConstraint(
|
|
|
|
null,
|
|
|
|
ForeignKeyClause(
|
|
|
|
foreignTable: TableReference('other', null),
|
|
|
|
columnNames: [
|
|
|
|
Reference(columnName: 'location'),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
2019-09-17 06:23:24 -07:00
|
|
|
overriddenDataClassName: 'RowName',
|
2019-09-16 13:33:36 -07:00
|
|
|
),
|
|
|
|
DeclaredStatement(
|
2019-12-17 13:03:59 -08:00
|
|
|
SimpleName('all'),
|
2019-09-16 13:33:36 -07:00
|
|
|
SelectStatement(
|
|
|
|
columns: [StarResultColumn(null)],
|
2020-03-22 03:51:39 -07:00
|
|
|
from: TableReference('tbl', null),
|
2019-09-16 13:33:36 -07:00
|
|
|
where: DartExpressionPlaceholder(name: 'predicate'),
|
|
|
|
),
|
|
|
|
),
|
2019-12-17 13:03:59 -08:00
|
|
|
DeclaredStatement(
|
|
|
|
SpecialStatementIdentifier('special'),
|
|
|
|
SelectStatement(
|
|
|
|
columns: [StarResultColumn(null)],
|
2020-03-22 03:51:39 -07:00
|
|
|
from: TableReference('tbl', null),
|
2019-12-17 13:03:59 -08:00
|
|
|
),
|
|
|
|
),
|
2019-12-25 11:24:03 -08:00
|
|
|
DeclaredStatement(
|
|
|
|
SimpleName('typeHints'),
|
|
|
|
SelectStatement(columns: [
|
|
|
|
ExpressionResultColumn(
|
|
|
|
expression: ColonNamedVariable(
|
|
|
|
ColonVariableToken(fakeSpan(':foo'), ':foo'),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
parameters: [
|
|
|
|
VariableTypeHint(
|
|
|
|
ColonNamedVariable(
|
|
|
|
ColonVariableToken(fakeSpan(':foo'), ':foo'),
|
|
|
|
),
|
|
|
|
'TEXT',
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2020-04-03 09:37:33 -07:00
|
|
|
DeclaredStatement(
|
|
|
|
SimpleName('nested'),
|
|
|
|
SelectStatement(
|
|
|
|
columns: [NestedStarResultColumn('foo')],
|
|
|
|
from: TableReference('tbl', 'foo'),
|
|
|
|
),
|
|
|
|
),
|
2019-09-16 13:33:36 -07:00
|
|
|
]),
|
|
|
|
);
|
|
|
|
});
|
2020-01-04 08:06:45 -08:00
|
|
|
|
|
|
|
test("reports error when the statement can't be parsed", () {
|
|
|
|
// regression test for https://github.com/simolus3/moor/issues/280#issuecomment-570789454
|
2020-02-10 09:48:03 -08:00
|
|
|
final parsed = SqlEngine(EngineOptions(useMoorExtensions: true))
|
2020-01-04 08:06:45 -08:00
|
|
|
.parseMoorFile('name: NSERT INTO foo DEFAULT VALUES;');
|
|
|
|
|
|
|
|
expect(
|
|
|
|
parsed.errors,
|
|
|
|
contains(const TypeMatcher<ParsingError>().having(
|
|
|
|
(e) => e.message,
|
|
|
|
'message',
|
|
|
|
contains('Expected a sql statement here'),
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
|
|
|
|
final root = parsed.rootNode as MoorFile;
|
|
|
|
expect(
|
|
|
|
root.allDescendants,
|
|
|
|
isNot(contains(const TypeMatcher<DeclaredStatement>())),
|
|
|
|
);
|
|
|
|
});
|
2020-03-21 14:02:27 -07:00
|
|
|
|
|
|
|
test('syntax errors contain correct position', () {
|
|
|
|
final engine = SqlEngine(EngineOptions(useMoorExtensions: true));
|
|
|
|
final result = engine.parseMoorFile('''
|
|
|
|
worksByComposer:
|
2020-03-22 03:51:39 -07:00
|
|
|
SELECT DISTINCT A.* FROM works A, works B ON A.id =
|
2020-03-21 14:02:27 -07:00
|
|
|
WHERE A.composer = :id OR B.composer = :id;
|
|
|
|
''');
|
|
|
|
|
|
|
|
expect(result.errors, hasLength(1));
|
|
|
|
expect(
|
|
|
|
result.errors.single,
|
|
|
|
isA<ParsingError>()
|
2020-03-22 03:51:39 -07:00
|
|
|
.having((e) => e.token.lexeme, 'token.lexeme', 'WHERE'));
|
2020-03-21 14:02:27 -07:00
|
|
|
});
|
2019-09-16 13:33:36 -07:00
|
|
|
}
|