Commit 393bba5
Changed files (3)
lib/screens/subsettings/foreign_db_import_screen.dart
@@ -10,6 +10,7 @@ import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:sqflite/sqflite.dart';
+import 'package:sqlparser/sqlparser.dart';
/// Screen to select the columns from a database and annotate types.
///
@@ -114,7 +115,9 @@ class _ForeignDBImportScreenState extends State<ForeignDBImportScreen> {
);
case RowDataFieldType.needlePin:
try {
- final pin = MeasurementNeedlePin.fromJson(jsonDecode(row[colType.$1].toString()));
+ final json = jsonDecode(row[colType.$1].toString());
+ if (json is! Map<String, dynamic>) continue;
+ final pin = MeasurementNeedlePin.fromJson(json);
record = record.copyWith(
needlePin: pin,
);
@@ -148,34 +151,23 @@ class _ColumnImportData {
_ColumnImportData._create(this.columns);
static Future<_ColumnImportData> loadFromDB(Database db) async {
+ final engine = SqlEngine();
+
final masterTable = await db.query('sqlite_master',
- columns: ['name', 'sql'],
+ columns: ['sql'],
where: 'type = "table"',
);
final columns = <String, List<String>?>{};
for (final e in masterTable) {
- final tableName = e['name']!.toString();
final creationSql = e['sql']!.toString();
- final colNames = RegExp(r'CREATE\s+TABLE\s+[0-9\w]+\s*\(([\w\s()0-9,]+?)\)+')
- .firstMatch(creationSql)
- ?.group(1)
- ?.split(RegExp(r'[,()]'))
- .map((e) => e
- .split(' ')
- .where((e) => e.isNotEmpty)
- .whereNot((e) => ['INTEGER', 'TEXT', 'NOT', 'OR', 'PRIMARY',
- 'FOREIGN', 'DEFAULT', 'NULL', 'KEY', 'PREFERENCES', 'BLOB',]
- .contains(e),
- )
- .firstWhereOrNull((e) => e.trim().isNotEmpty),
- )
- .whereNotNull()
- .toSet() // remove duplicates
- .toList();
- print('$creationSql:\t $colNames');
- // don't show tables without columns
- if (colNames?.isNotEmpty ?? false) {
- columns[tableName] = colNames;
+ final rootNode = engine.analyze(creationSql).root;
+ if (rootNode is CreateTableStatement) {
+ final colNames = rootNode.columns
+ .map((e) => e.columnName)
+ .toSet()
+ .toList();
+
+ if (colNames.isNotEmpty) columns[rootNode.tableName] = colNames;
}
}
return _ColumnImportData._create(columns);
pubspec.lock
@@ -97,6 +97,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.0"
+ charcode:
+ dependency: transitive
+ description:
+ name: charcode
+ sha256: fb98c0f6d12c920a02ee2d998da788bca066ca5f148492b7085ee23372b12306
+ url: "https://pub.dev"
+ source: hosted
+ version: "1.3.1"
clock:
dependency: transitive
description:
@@ -329,26 +337,26 @@ packages:
dependency: transitive
description:
name: leak_tracker
- sha256: f8cdf1383f5b4672a2693d875f1f239af6bd7e4a8925a17ef7219226db932624
+ sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa"
url: "https://pub.dev"
source: hosted
- version: "10.0.1"
+ version: "10.0.0"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
- sha256: a2055640bf5bc903475e4bbdb34e04f8bf698542bee41edec47d337a5939e1ae
+ sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0
url: "https://pub.dev"
source: hosted
- version: "2.0.3"
+ version: "2.0.1"
leak_tracker_testing:
dependency: transitive
description:
name: leak_tracker_testing
- sha256: e62042d479c4c139dd774125ed4dfbde646b8f07ac228e3c1b57a3d91d6d9df4
+ sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47
url: "https://pub.dev"
source: hosted
- version: "2.0.2"
+ version: "2.0.1"
lints:
dependency: transitive
description:
@@ -658,6 +666,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.3.0"
+ sqlparser:
+ dependency: "direct main"
+ description:
+ name: sqlparser
+ sha256: "7b20045d1ccfb7bc1df7e8f9fee5ae58673fce6ff62cefbb0e0fd7214e90e5a0"
+ url: "https://pub.dev"
+ source: hosted
+ version: "0.34.1"
stack_trace:
dependency: transitive
description:
@@ -852,4 +868,4 @@ packages:
version: "3.1.2"
sdks:
dart: ">=3.2.0 <4.0.0"
- flutter: ">=3.18.0-18.0.pre.54"
+ flutter: ">=3.16.0"
pubspec.yaml
@@ -31,6 +31,7 @@ dependencies:
jsaver: ^1.2.0
restart_app: ^1.2.1
fluttertoast: ^8.2.4
+ sqlparser: ^0.34.1
dev_dependencies:
file: any