Replies: 1 comment
-
I should mention that sqlparser was designed with SQLite in mind only, but it's probably resilient enough to give you decent ASTs for other dialects using a slightly different syntax as well. In your case, where the insert statements are directly matching SQLite syntax, it works perfectly fine of course: import 'package:sqlparser/sqlparser.dart';
void main(List<String> arguments) async {
final parser = SqlEngine();
final stmts = parser.parseMultiple('''
INSERT INTO public.pessoas (id, nome, codigo) VALUES (1, 'Isaque', NULL);
INSERT INTO public.pessoas (id, nome, codigo) VALUES (2, 'Cintia', NULL);
INSERT INTO public.pessoas (id, nome, codigo) VALUES (3, 'Leo', NULL);
INSERT INTO public.pessoas (id, nome, codigo) VALUES (5, NULL, NULL);
INSERT INTO public.pessoas (id, nome, codigo) VALUES (7, '', NULL);
INSERT INTO public.pessoas (id, nome, codigo) VALUES (8, 'ASa insert quebra de linha
insert
dentro de value', NULL);
''');
for (final stmt in stmts.rootNode.childNodes) {
if (stmt is InsertStatement) {
final table = stmt.table.span!.text;
final source = stmt.source;
if (source is! ValuesSource) {
print('Unknown insert statement: $source');
continue;
}
for (final tuple in source.values) {
final entries = tuple.expressions.map((e) {
return switch (e) {
StringLiteral(:final value) => value,
NullLiteral() => null,
NumericLiteral(:final value) => value,
_ => throw 'Unsupported expression $e',
};
});
// This is where you'd record the insert to transform it into the copy - all the values are here
print('Found insert to $table with entries ${entries.toList()}');
}
} else {
print('Unknown statement! $stmt');
}
}
} If your statements aren't necessarily semicolon-separated, you can also keep using your |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to write a script in dart to convert postgresql dump of insert statements into copy statements, could the sqlparser lib help me with this?
input
output
Beta Was this translation helpful? Give feedback.
All reactions