mirror of https://github.com/AMT-Cheif/drift.git
SqlEngine: Migrate default construtor to options
This commit is contained in:
parent
04a43b36d6
commit
814e7bf61d
|
@ -33,7 +33,7 @@ manyColumns:
|
||||||
class ParseMoorFile extends BenchmarkBase {
|
class ParseMoorFile extends BenchmarkBase {
|
||||||
ParseMoorFile(ScoreEmitter emitter) : super('Moor file: Parse only', emitter);
|
ParseMoorFile(ScoreEmitter emitter) : super('Moor file: Parse only', emitter);
|
||||||
|
|
||||||
final _engine = SqlEngine.withOptions(EngineOptions(useMoorExtensions: true));
|
final _engine = SqlEngine(EngineOptions(useMoorExtensions: true));
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void exercise() {
|
void exercise() {
|
||||||
|
|
|
@ -45,7 +45,7 @@ class MoorSession {
|
||||||
enableExperimentalTypeInference: options.useExperimentalInference,
|
enableExperimentalTypeInference: options.useExperimentalInference,
|
||||||
);
|
);
|
||||||
|
|
||||||
return SqlEngine.withOptions(sqlOptions);
|
return SqlEngine(sqlOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
FileType _findFileType(String path) {
|
FileType _findFileType(String path) {
|
||||||
|
|
|
@ -36,8 +36,7 @@ class PreprocessBuilder extends Builder {
|
||||||
FutureOr<void> build(BuildStep buildStep) async {
|
FutureOr<void> build(BuildStep buildStep) async {
|
||||||
final input = buildStep.inputId;
|
final input = buildStep.inputId;
|
||||||
final moorFileContent = await buildStep.readAsString(input);
|
final moorFileContent = await buildStep.readAsString(input);
|
||||||
final engine =
|
final engine = SqlEngine(EngineOptions(useMoorExtensions: true));
|
||||||
SqlEngine.withOptions(EngineOptions(useMoorExtensions: true));
|
|
||||||
|
|
||||||
final parsed = engine.parseMoorFile(moorFileContent);
|
final parsed = engine.parseMoorFile(moorFileContent);
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ import 'package:test/test.dart';
|
||||||
import '../utils.dart';
|
import '../utils.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
final engine = SqlEngine.withOptions(EngineOptions(useMoorExtensions: true));
|
final engine = SqlEngine(EngineOptions(useMoorExtensions: true));
|
||||||
final mapper = TypeMapper();
|
final mapper = TypeMapper();
|
||||||
|
|
||||||
test('warns when a result column is unresolved', () {
|
test('warns when a result column is unresolved', () {
|
||||||
|
|
|
@ -24,7 +24,7 @@ CREATE TABLE bar (
|
||||||
|
|
||||||
Future<void> main() async {
|
Future<void> main() async {
|
||||||
final mapper = TypeMapper();
|
final mapper = TypeMapper();
|
||||||
final engine = SqlEngine.withOptions(EngineOptions(useMoorExtensions: true));
|
final engine = SqlEngine(EngineOptions(useMoorExtensions: true));
|
||||||
final step = ParseMoorStep(
|
final step = ParseMoorStep(
|
||||||
Task(null, null, null), FoundFile(Uri.parse('foo'), FileType.moor), '');
|
Task(null, null, null), FoundFile(Uri.parse('foo'), FileType.moor), '');
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ final Table table =
|
||||||
Table(name: 'todos', resolvedColumns: [_idColumn, _titleColumn]);
|
Table(name: 'todos', resolvedColumns: [_idColumn, _titleColumn]);
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
final engine = SqlEngine.withOptions(EngineOptions(useMoorExtensions: true));
|
final engine = SqlEngine(EngineOptions(useMoorExtensions: true));
|
||||||
final mapper = TypeMapper();
|
final mapper = TypeMapper();
|
||||||
|
|
||||||
test('extracts variables and sorts them by index', () {
|
test('extracts variables and sorts them by index', () {
|
||||||
|
|
|
@ -2,8 +2,6 @@ import 'dart:collection';
|
||||||
|
|
||||||
import 'package:sqlparser/sqlparser.dart';
|
import 'package:sqlparser/sqlparser.dart';
|
||||||
import 'package:sqlparser/src/analysis/types2/types.dart' as t2;
|
import 'package:sqlparser/src/analysis/types2/types.dart' as t2;
|
||||||
import 'package:sqlparser/src/engine/module/fts5.dart';
|
|
||||||
import 'package:sqlparser/src/engine/module/json1.dart';
|
|
||||||
import 'package:sqlparser/src/engine/options.dart';
|
import 'package:sqlparser/src/engine/options.dart';
|
||||||
import 'package:sqlparser/src/reader/parser/parser.dart';
|
import 'package:sqlparser/src/reader/parser/parser.dart';
|
||||||
import 'package:sqlparser/src/reader/tokenizer/scanner.dart';
|
import 'package:sqlparser/src/reader/tokenizer/scanner.dart';
|
||||||
|
@ -22,23 +20,7 @@ class SqlEngine {
|
||||||
|
|
||||||
SchemaFromCreateTable _schemaReader;
|
SchemaFromCreateTable _schemaReader;
|
||||||
|
|
||||||
SqlEngine(
|
SqlEngine([EngineOptions options]) : options = options ?? EngineOptions() {
|
||||||
{@Deprecated('Use SqlEngine.withOptions instead')
|
|
||||||
bool useMoorExtensions = false,
|
|
||||||
@Deprecated('Use SqlEngine.withOptions instead')
|
|
||||||
bool enableJson1Module = false,
|
|
||||||
@Deprecated('Use SqlEngine.withOptions instead')
|
|
||||||
bool enableFts5 = false})
|
|
||||||
: this.withOptions(_constructOptions(
|
|
||||||
// ignore: deprecated_member_use_from_same_package
|
|
||||||
moor: useMoorExtensions,
|
|
||||||
// ignore: deprecated_member_use_from_same_package
|
|
||||||
json1: enableJson1Module,
|
|
||||||
// ignore: deprecated_member_use_from_same_package
|
|
||||||
fts5: enableFts5,
|
|
||||||
));
|
|
||||||
|
|
||||||
SqlEngine.withOptions(this.options) {
|
|
||||||
for (final extension in options.enabledExtensions) {
|
for (final extension in options.enabledExtensions) {
|
||||||
extension.register(this);
|
extension.register(this);
|
||||||
}
|
}
|
||||||
|
@ -47,6 +29,9 @@ class SqlEngine {
|
||||||
registerTable(sqliteSequence);
|
registerTable(sqliteSequence);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated('Use SqlEngine(options) instead')
|
||||||
|
factory SqlEngine.withOptions(EngineOptions options) = SqlEngine;
|
||||||
|
|
||||||
/// Obtain a [SchemaFromCreateTable] instance compatible with the
|
/// Obtain a [SchemaFromCreateTable] instance compatible with the
|
||||||
/// configuration of this engine.
|
/// configuration of this engine.
|
||||||
///
|
///
|
||||||
|
@ -231,18 +216,6 @@ class SqlEngine {
|
||||||
|
|
||||||
root.scope = _constructRootScope(parent: safeScope);
|
root.scope = _constructRootScope(parent: safeScope);
|
||||||
}
|
}
|
||||||
|
|
||||||
static EngineOptions _constructOptions({bool moor, bool fts5, bool json1}) {
|
|
||||||
final extensions = [
|
|
||||||
if (fts5) const Fts5Extension(),
|
|
||||||
if (json1) const Json1Extension(),
|
|
||||||
];
|
|
||||||
|
|
||||||
return EngineOptions(
|
|
||||||
useMoorExtensions: moor,
|
|
||||||
enabledExtensions: extensions,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The result of parsing an sql query. Contains the root of the AST and all
|
/// The result of parsing an sql query. Contains the root of the AST and all
|
||||||
|
|
|
@ -63,8 +63,7 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('supports booleans when moor extensions are enabled', () {
|
test('supports booleans when moor extensions are enabled', () {
|
||||||
final engine =
|
final engine = SqlEngine(EngineOptions(useMoorExtensions: true));
|
||||||
SqlEngine.withOptions(EngineOptions(useMoorExtensions: true));
|
|
||||||
final stmt = engine.parse('''
|
final stmt = engine.parse('''
|
||||||
CREATE TABLE foo (
|
CREATE TABLE foo (
|
||||||
a BOOL, b DATETIME, c DATE, d BOOLEAN NOT NULL
|
a BOOL, b DATETIME, c DATE, d BOOLEAN NOT NULL
|
||||||
|
|
|
@ -82,7 +82,7 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('can infer types for dart placeholder', () {
|
test('can infer types for dart placeholder', () {
|
||||||
final ctx = (SqlEngine.withOptions(EngineOptions(useMoorExtensions: true))
|
final ctx = (SqlEngine(EngineOptions(useMoorExtensions: true))
|
||||||
..registerTable(demoTable))
|
..registerTable(demoTable))
|
||||||
.analyze(r'SELECT * FROM demo WHERE $expr');
|
.analyze(r'SELECT * FROM demo WHERE $expr');
|
||||||
|
|
||||||
|
@ -94,8 +94,7 @@ void main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('respects explicit types for variables', () {
|
test('respects explicit types for variables', () {
|
||||||
final ctx =
|
final ctx = SqlEngine(EngineOptions(useMoorExtensions: true)).analyze(
|
||||||
SqlEngine.withOptions(EngineOptions(useMoorExtensions: true)).analyze(
|
|
||||||
'SELECT ?',
|
'SELECT ?',
|
||||||
stmtOptions: const AnalyzeStatementOptions(indexedVariableTypes: {
|
stmtOptions: const AnalyzeStatementOptions(indexedVariableTypes: {
|
||||||
1: ResolvedType.bool(),
|
1: ResolvedType.bool(),
|
||||||
|
|
|
@ -37,8 +37,7 @@ const Map<String, ResolvedType> _types = {
|
||||||
};
|
};
|
||||||
|
|
||||||
SqlEngine _spawnEngine() {
|
SqlEngine _spawnEngine() {
|
||||||
return SqlEngine.withOptions(
|
return SqlEngine(EngineOptions(enableExperimentalTypeInference: true))
|
||||||
EngineOptions(enableExperimentalTypeInference: true))
|
|
||||||
..registerTable(demoTable)
|
..registerTable(demoTable)
|
||||||
..registerTable(anotherTable);
|
..registerTable(anotherTable);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import 'package:test/test.dart';
|
||||||
import '../data.dart';
|
import '../data.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
final engine = SqlEngine.withOptions(EngineOptions(useMoorExtensions: true))
|
final engine = SqlEngine(EngineOptions(useMoorExtensions: true))
|
||||||
..registerTable(demoTable)
|
..registerTable(demoTable)
|
||||||
..registerTable(anotherTable);
|
..registerTable(anotherTable);
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ import 'package:test/test.dart';
|
||||||
ComputedSuggestions completionsFor(String moorFile,
|
ComputedSuggestions completionsFor(String moorFile,
|
||||||
{void Function(SqlEngine) setup}) {
|
{void Function(SqlEngine) setup}) {
|
||||||
final position = moorFile.indexOf('^');
|
final position = moorFile.indexOf('^');
|
||||||
final engine = SqlEngine.withOptions(EngineOptions(useMoorExtensions: true));
|
final engine = SqlEngine(EngineOptions(useMoorExtensions: true));
|
||||||
setup?.call(engine);
|
setup?.call(engine);
|
||||||
|
|
||||||
final result = engine.parseMoorFile(moorFile.replaceFirst('^', ''));
|
final result = engine.parseMoorFile(moorFile.replaceFirst('^', ''));
|
||||||
|
|
|
@ -5,7 +5,7 @@ final _fts5Options = EngineOptions(enabledExtensions: const [Fts5Extension()]);
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
group('creating fts5 tables', () {
|
group('creating fts5 tables', () {
|
||||||
final engine = SqlEngine.withOptions(_fts5Options);
|
final engine = SqlEngine(_fts5Options);
|
||||||
|
|
||||||
test('can create fts5 tables', () {
|
test('can create fts5 tables', () {
|
||||||
final result = engine.analyze('CREATE VIRTUAL TABLE foo USING '
|
final result = engine.analyze('CREATE VIRTUAL TABLE foo USING '
|
||||||
|
@ -35,7 +35,7 @@ void main() {
|
||||||
group('type inference for function calls', () {
|
group('type inference for function calls', () {
|
||||||
SqlEngine engine;
|
SqlEngine engine;
|
||||||
setUp(() {
|
setUp(() {
|
||||||
engine = SqlEngine.withOptions(_fts5Options);
|
engine = SqlEngine(_fts5Options);
|
||||||
// add an fts5 table for the following queries
|
// add an fts5 table for the following queries
|
||||||
final fts5Result = engine.analyze('CREATE VIRTUAL TABLE foo USING '
|
final fts5Result = engine.analyze('CREATE VIRTUAL TABLE foo USING '
|
||||||
'fts5(bar, baz);');
|
'fts5(bar, baz);');
|
||||||
|
@ -79,7 +79,7 @@ void main() {
|
||||||
group('type inference for function arguments', () {
|
group('type inference for function arguments', () {
|
||||||
SqlEngine engine;
|
SqlEngine engine;
|
||||||
setUp(() {
|
setUp(() {
|
||||||
engine = SqlEngine.withOptions(_fts5Options);
|
engine = SqlEngine(_fts5Options);
|
||||||
// add an fts5 table for the following queries
|
// add an fts5 table for the following queries
|
||||||
final fts5Result = engine.analyze('CREATE VIRTUAL TABLE foo USING '
|
final fts5Result = engine.analyze('CREATE VIRTUAL TABLE foo USING '
|
||||||
'fts5(bar, baz);');
|
'fts5(bar, baz);');
|
||||||
|
@ -125,7 +125,7 @@ void main() {
|
||||||
group('error reporting', () {
|
group('error reporting', () {
|
||||||
SqlEngine engine;
|
SqlEngine engine;
|
||||||
setUp(() {
|
setUp(() {
|
||||||
engine = SqlEngine.withOptions(_fts5Options);
|
engine = SqlEngine(_fts5Options);
|
||||||
// add an fts5 table for the following queries
|
// add an fts5 table for the following queries
|
||||||
final fts5Result = engine.analyze('CREATE VIRTUAL TABLE foo USING '
|
final fts5Result = engine.analyze('CREATE VIRTUAL TABLE foo USING '
|
||||||
'fts5(bar, baz);');
|
'fts5(bar, baz);');
|
||||||
|
|
|
@ -12,7 +12,7 @@ void main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void _runTests(bool types2) {
|
void _runTests(bool types2) {
|
||||||
final engine = SqlEngine.withOptions(EngineOptions(
|
final engine = SqlEngine(EngineOptions(
|
||||||
enableExperimentalTypeInference: types2,
|
enableExperimentalTypeInference: types2,
|
||||||
enabledExtensions: const [Json1Extension()],
|
enabledExtensions: const [Json1Extension()],
|
||||||
));
|
));
|
||||||
|
|
|
@ -93,7 +93,7 @@ void main() {
|
||||||
|
|
||||||
test("reports error when the statement can't be parsed", () {
|
test("reports error when the statement can't be parsed", () {
|
||||||
// regression test for https://github.com/simolus3/moor/issues/280#issuecomment-570789454
|
// regression test for https://github.com/simolus3/moor/issues/280#issuecomment-570789454
|
||||||
final parsed = SqlEngine.withOptions(EngineOptions(useMoorExtensions: true))
|
final parsed = SqlEngine(EngineOptions(useMoorExtensions: true))
|
||||||
.parseMoorFile('name: NSERT INTO foo DEFAULT VALUES;');
|
.parseMoorFile('name: NSERT INTO foo DEFAULT VALUES;');
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
|
|
|
@ -41,8 +41,7 @@ CREATE TABLE foo (
|
||||||
});
|
});
|
||||||
|
|
||||||
test('parses trailing comma with error', () {
|
test('parses trailing comma with error', () {
|
||||||
final engine =
|
final engine = SqlEngine(EngineOptions(useMoorExtensions: true));
|
||||||
SqlEngine.withOptions(EngineOptions(useMoorExtensions: true));
|
|
||||||
|
|
||||||
final result = engine.parseMoorFile('''
|
final result = engine.parseMoorFile('''
|
||||||
CREATE TABLE foo (
|
CREATE TABLE foo (
|
||||||
|
|
|
@ -23,7 +23,7 @@ IdentifierToken identifier(String content) {
|
||||||
}
|
}
|
||||||
|
|
||||||
MoorFile parseMoor(String content) {
|
MoorFile parseMoor(String content) {
|
||||||
return SqlEngine.withOptions(EngineOptions(useMoorExtensions: true))
|
return SqlEngine(EngineOptions(useMoorExtensions: true))
|
||||||
.parseMoorFile(content)
|
.parseMoorFile(content)
|
||||||
.rootNode as MoorFile;
|
.rootNode as MoorFile;
|
||||||
}
|
}
|
||||||
|
@ -36,9 +36,7 @@ void testMoorFile(String moorFile, MoorFile expected) {
|
||||||
|
|
||||||
void testStatement(String sql, AstNode expected, {bool moorMode = false}) {
|
void testStatement(String sql, AstNode expected, {bool moorMode = false}) {
|
||||||
final parsed =
|
final parsed =
|
||||||
SqlEngine.withOptions(EngineOptions(useMoorExtensions: moorMode))
|
SqlEngine(EngineOptions(useMoorExtensions: moorMode)).parse(sql).rootNode;
|
||||||
.parse(sql)
|
|
||||||
.rootNode;
|
|
||||||
enforceHasSpan(parsed);
|
enforceHasSpan(parsed);
|
||||||
enforceEqual(parsed, expected);
|
enforceEqual(parsed, expected);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue