Skip to content

Commit

Permalink
make query constants actual constants
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Feb 1, 2019
1 parent 86e1a73 commit c5d52c7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 56 deletions.
38 changes: 19 additions & 19 deletions lib/Yancy/Backend/Mysql.pm
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,22 @@ has mojodb =>;
use constant mojodb_class => 'Mojo::mysql';
use constant mojodb_prefix => 'mysql';

use constant q_tables => <<ENDQ;
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema=?
ENDQ
use constant q_key => <<ENDQ;
SELECT * FROM information_schema.table_constraints as tc
JOIN information_schema.key_column_usage AS ccu USING ( table_name, table_schema )
WHERE tc.table_schema=? AND tc.table_name=? AND ( constraint_type = 'PRIMARY KEY' OR constraint_type = 'UNIQUE' )
AND tc.table_schema NOT IN ('information_schema','performance_schema','mysql','sys')
ENDQ
use constant q_columns => <<ENDQ;
SELECT * FROM information_schema.columns
WHERE table_schema=?
ORDER BY ORDINAL_POSITION
ENDQ

sub create {
my ( $self, $coll, $params ) = @_;
$self->normalize( $coll, $params );
Expand All @@ -166,26 +182,16 @@ sub read_schema {
my $database = $self->mojodb->db->query( 'SELECT DATABASE()' )->array->[0];

my %schema;
my $tables_q = <<ENDQ;
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema=?
ENDQ
my $tables_q = q_tables;
if ( @table_names ) {
$tables_q .= sprintf ' AND TABLE_NAME IN ( %s )', join ', ', ('?') x @table_names;
}

my $key_q = <<ENDQ;
SELECT * FROM information_schema.table_constraints as tc
JOIN information_schema.key_column_usage AS ccu USING ( table_name, table_schema )
WHERE tc.table_schema=? AND tc.table_name=? AND ( constraint_type = 'PRIMARY KEY' OR constraint_type = 'UNIQUE' )
AND tc.table_schema NOT IN ('information_schema','performance_schema','mysql','sys')
ENDQ

my $tables = $self->mojodb->db->query( $tables_q, $database, @table_names )->hashes;
for my $t ( @$tables ) {
my $table = $t->{TABLE_NAME};
# ; say "Got table $table";
my @keys = @{ $self->mojodb->db->query( $key_q, $database, $table )->hashes };
my @keys = @{ $self->mojodb->db->query( q_key, $database, $table )->hashes };
# ; say "Got keys";
# ; use Data::Dumper;
# ; say Dumper \@keys;
Expand All @@ -197,13 +203,7 @@ ENDQ
}
}

my $columns_q = <<ENDQ;
SELECT * FROM information_schema.columns
WHERE table_schema=?
ORDER BY ORDINAL_POSITION
ENDQ

my @columns = @{ $self->mojodb->db->query( $columns_q, $database )->hashes };
my @columns = @{ $self->mojodb->db->query( q_columns, $database )->hashes };
for my $c ( @columns ) {
my $table = $c->{TABLE_NAME};
my $column = $c->{COLUMN_NAME};
Expand Down
54 changes: 27 additions & 27 deletions lib/Yancy/Backend/Pg.pm
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,30 @@ has mojodb =>;
use constant mojodb_class => 'Mojo::Pg';
use constant mojodb_prefix => 'postgresql';

use constant q_tables => <<ENDQ;
SELECT * FROM information_schema.tables
WHERE table_schema=?
ENDQ
use constant q_key => <<ENDQ;
SELECT c.column_name FROM information_schema.table_constraints as tc
JOIN information_schema.constraint_column_usage AS ccu
USING (constraint_schema, constraint_name)
JOIN information_schema.columns AS c
ON tc.table_schema=c.table_schema
AND tc.table_name=c.table_name
AND ccu.column_name=c.column_name
WHERE tc.table_schema=?
AND tc.table_name=?
AND ( constraint_type = 'PRIMARY KEY'
OR constraint_type = 'UNIQUE' )
ORDER BY ordinal_position ASC
ENDQ
use constant q_columns => <<ENDQ;
SELECT * FROM information_schema.columns
WHERE table_schema=?
ORDER BY ordinal_position ASC
ENDQ

sub create {
my ( $self, $coll, $params ) = @_;
$self->normalize( $coll, $params );
Expand All @@ -160,34 +184,16 @@ sub read_schema {
my $database = $self->mojodb->db->query( 'SELECT current_schema()' )->array->[0];

my %schema;
my $tables_q = <<ENDQ;
SELECT * FROM information_schema.tables
WHERE table_schema=?
ENDQ
my $tables_q = q_tables;
if ( @table_names ) {
$tables_q .= sprintf ' AND table_name IN ( %s )', join ', ', ('?') x @table_names;
}

my $key_q = <<ENDQ;
SELECT c.column_name FROM information_schema.table_constraints as tc
JOIN information_schema.constraint_column_usage AS ccu
USING (constraint_schema, constraint_name)
JOIN information_schema.columns AS c
ON tc.table_schema=c.table_schema
AND tc.table_name=c.table_name
AND ccu.column_name=c.column_name
WHERE tc.table_schema=?
AND tc.table_name=?
AND ( constraint_type = 'PRIMARY KEY'
OR constraint_type = 'UNIQUE' )
ORDER BY ordinal_position ASC
ENDQ

my $tables = $self->mojodb->db->query( $tables_q, $database, @table_names )->hashes;
my %keys;
for my $t ( @$tables ) {
my $table = $t->{table_name};
my @keys = @{ $self->mojodb->db->query( $key_q, $database, $table )->hashes };
my @keys = @{ $self->mojodb->db->query( q_key, $database, $table )->hashes };
$keys{ $table } = \@keys;
#; use Data::Dumper;
#; say Dumper \@keys;
Expand All @@ -199,13 +205,7 @@ ENDQ
}
}

my $columns_q = <<ENDQ;
SELECT * FROM information_schema.columns
WHERE table_schema=?
ORDER BY ordinal_position ASC
ENDQ

my @columns = @{ $self->mojodb->db->query( $columns_q, $database )->hashes };
my @columns = @{ $self->mojodb->db->query( q_columns, $database )->hashes };
for my $c ( @columns ) {
my $table = $c->{table_name};
my $column = $c->{column_name};
Expand Down
16 changes: 6 additions & 10 deletions lib/Yancy/Backend/Sqlite.pm
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ has mojodb =>;
use constant mojodb_class => 'Mojo::SQLite';
use constant mojodb_prefix => 'sqlite';

use constant q_tables => q{SELECT * FROM SQLITE_MASTER WHERE type='table'};
use constant q_column => q{PRAGMA table_info(%s)};

sub create {
my ( $self, $coll, $params ) = @_;
$self->normalize( $coll, $params );
Expand All @@ -152,7 +155,8 @@ sub create {
sub read_schema {
my ( $self, @table_names ) = @_;
my %schema;
my $tables_q = q{SELECT * FROM SQLITE_MASTER WHERE type='table'};

my $tables_q = q_tables;

if ( @table_names ) {
$tables_q .= sprintf ' AND name IN ( %s )', join ', ', ('?') x @table_names;
Expand All @@ -161,19 +165,11 @@ sub read_schema {
$tables_q .= q{ AND name NOT LIKE 'sqlite_%'};
}

my $column_q = <<ENDQ;
PRAGMA table_info(%s)
ENDQ

my $seq_q = <<ENDQ;
SELECT * FROM sqlite_sequence
ENDQ

my $tables = $self->mojodb->db->query( $tables_q, @table_names )->hashes;
for my $t ( @$tables ) {
my $table = $t->{name};
# ; say "Got table $table";
my @columns = @{ $self->mojodb->db->query( sprintf $column_q, $table )->hashes };
my @columns = @{ $self->mojodb->db->query( sprintf q_column, $table )->hashes };
# ; say "Got columns";
# ; use Data::Dumper;
# ; say Dumper \@columns;
Expand Down

0 comments on commit c5d52c7

Please sign in to comment.