sql_formatter 0.1.0
sql_formatter: ^0.1.0 copied to clipboard
A pure-Dart SQL pretty-printer supporting standard SQL, PostgreSQL, MySQL and SQLite dialects. A faithful port of the JS sql-formatter library.
import 'package:sql_formatter/sql_formatter.dart';
void main() {
const sql =
'select id, name, count(*) from users u left join orders o on o.uid=u.id '
'where u.active=1 and o.total>100 group by id order by name limit 10';
// Default: standard dialect, UPPER-cased keywords, two-space indent.
print('--- standard, keywords upper-cased ---');
print(format(sql));
// A PostgreSQL-specific query with a `$1` placeholder and a `->>` operator.
print('\n--- postgresql dialect ---');
print(format(
r"select data->>'k' from events where id = $1",
dialect: SqlDialect.postgresql,
));
// Lower-case the keywords instead of upper-casing them.
print('\n--- keywords lower-cased ---');
print(format(
sql,
options: const FormatOptions(keywordCase: KeywordCase.lower),
));
}