-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
294 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
SQL-Formatter-* | ||
/.build/ | ||
*.swp | ||
|
||
|
||
/ffi/target | ||
/ffi/_build | ||
/ffi/Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# SQL::Formatter ![static](https://github.com/uperl/SQL-Formatter/workflows/static/badge.svg) ![linux](https://github.com/uperl/SQL-Formatter/workflows/linux/badge.svg) | ||
|
||
Format SQL using the rust sqlformat library | ||
|
||
# SYNOPSIS | ||
|
||
```perl | ||
my $f = SQL::Formatter->new; | ||
say $f->format('select foo.a, foo.b, bar.c from foo join bar on foo.a = bar.c where foo.b = 2'); | ||
``` | ||
|
||
prints: | ||
|
||
``` | ||
SELECT | ||
foo.a, | ||
foo.b, | ||
bar.c | ||
FROM | ||
foo | ||
JOIN bar ON foo.a = bar.c | ||
WHERE | ||
foo.b = 2 | ||
``` | ||
|
||
# DESCRIPTION | ||
|
||
Pretty print SQL using the rust crate `sqlformat`. | ||
|
||
# ATTRIBUTES | ||
|
||
The formatting options can be specified either when the object is constructed, or later using accessors. | ||
|
||
```perl | ||
my $f = SQL::Format->new( indent => 4 ); | ||
$f->indent(4); | ||
``` | ||
|
||
- indent | ||
|
||
Controls the length of indentation to use. The default is `2`. | ||
|
||
- uppercase | ||
|
||
When set to true (the default), changes reserved keywords to ALL CAPS. | ||
|
||
- lines\_between\_queries | ||
|
||
Controls the number of line breaks after a query. The default is `1`. | ||
|
||
# METHODS | ||
|
||
## format | ||
|
||
```perl | ||
my $pretty_sql = $f->format($sql); | ||
``` | ||
|
||
Formats whitespace in a SQL string to make it easier to read. | ||
|
||
# AUTHOR | ||
|
||
Graham Ollis <plicease@cpan.org> | ||
|
||
# COPYRIGHT AND LICENSE | ||
|
||
This software is copyright (c) 2024 by Graham Ollis. | ||
|
||
This is free software; you can redistribute it and/or modify it under | ||
the same terms as the Perl 5 programming language system itself. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "sf" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
sqlformat = "0.2.6" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use std::ffi::CString; | ||
use std::ffi::CStr; | ||
use sqlformat::format; | ||
use std::os::raw::c_char; | ||
|
||
#[no_mangle] | ||
pub fn sf_format(query: *const c_char, indent: u8, uppercase: bool, between: u8) -> *mut c_char { | ||
let query = unsafe { CStr::from_ptr(query) }; | ||
let options = sqlformat::FormatOptions { | ||
indent: sqlformat::Indent::Spaces(indent), | ||
uppercase: uppercase, | ||
lines_between_queries: between, | ||
}; | ||
let query = format(query.to_str().unwrap(), &sqlformat::QueryParams::default(), options); | ||
|
||
CString::new(query).unwrap().into_raw() | ||
} | ||
|
||
#[allow(non_snake_case)] | ||
#[no_mangle] | ||
pub fn sf__free(s: *mut c_char) { | ||
if s.is_null() { | ||
} else { | ||
unsafe { drop(CString::from_raw(s)) }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use Test2::V0 -no_srand => 1; | ||
use Config; | ||
|
||
eval { require 'Test/More.pm' }; | ||
|
||
# This .t file is generated. | ||
# make changes instead to dist.ini | ||
|
||
my %modules; | ||
my $post_diag; | ||
|
||
$modules{$_} = $_ for qw( | ||
Class::Tiny | ||
ExtUtils::MakeMaker | ||
FFI::Build::File::Cargo | ||
FFI::Build::MM | ||
FFI::Platypus | ||
FFI::Platypus::Lang::Rust | ||
Test2::V0 | ||
); | ||
|
||
|
||
|
||
my @modules = sort keys %modules; | ||
|
||
sub spacer () | ||
{ | ||
diag ''; | ||
diag ''; | ||
diag ''; | ||
} | ||
|
||
pass 'okay'; | ||
|
||
my $max = 1; | ||
$max = $_ > $max ? $_ : $max for map { length $_ } @modules; | ||
our $format = "%-${max}s %s"; | ||
|
||
spacer; | ||
|
||
my @keys = sort grep /(MOJO|PERL|\A(LC|HARNESS)_|\A(SHELL|LANG)\Z)/i, keys %ENV; | ||
|
||
if(@keys > 0) | ||
{ | ||
diag "$_=$ENV{$_}" for @keys; | ||
|
||
if($ENV{PERL5LIB}) | ||
{ | ||
spacer; | ||
diag "PERL5LIB path"; | ||
diag $_ for split $Config{path_sep}, $ENV{PERL5LIB}; | ||
|
||
} | ||
elsif($ENV{PERLLIB}) | ||
{ | ||
spacer; | ||
diag "PERLLIB path"; | ||
diag $_ for split $Config{path_sep}, $ENV{PERLLIB}; | ||
} | ||
|
||
spacer; | ||
} | ||
|
||
diag sprintf $format, 'perl', "$] $^O $Config{archname}"; | ||
|
||
foreach my $module (sort @modules) | ||
{ | ||
my $pm = "$module.pm"; | ||
$pm =~ s{::}{/}g; | ||
if(eval { require $pm; 1 }) | ||
{ | ||
my $ver = eval { $module->VERSION }; | ||
$ver = 'undef' unless defined $ver; | ||
diag sprintf $format, $module, $ver; | ||
} | ||
else | ||
{ | ||
diag sprintf $format, $module, '-'; | ||
} | ||
} | ||
|
||
if($post_diag) | ||
{ | ||
spacer; | ||
$post_diag->(); | ||
} | ||
|
||
spacer; | ||
|
||
done_testing; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters