Skip to content
Browse files

write has_pk/hasnt_pk.

  • Loading branch information...
1 parent 40976a2 commit 27eeae25a9748a6501e9ec46690866cfa19637ef @theory committed May 7, 2012
Showing with 26 additions and 1 deletion.
  1. +26 −1 bin/pg_tapgen
View
27 bin/pg_tapgen
@@ -172,9 +172,18 @@ sub functions_are {
sub has_table {
my ($schema, $table) = @_;
- print "SELECT has_table('$schema', '$table', 'Should have table $schema.$table');\n\n";
+ print "SELECT has_table('$schema', '$table', 'Should have table $schema.$table');\n";
+ has_pk($schema, $table);
+ print $/;
columns_are($schema, $table);
}
+
+sub has_pk {
+ my ($schema, $table) = @_;
+ my $fn = _hasc($schema, $table, 'p') ? 'has_pk' : 'hasnt_pk';
+ print "select $fn('$schema', '$table', 'Table $schema.$table should have a primary key');\n";
+}
+
sub columns_are {
my ($schema, $table) = @_;
my $cols = $dbh->selectcol_arrayref(q{
@@ -195,6 +204,22 @@ sub columns_are {
"'\n]);\n\n";
}
+sub _hasc {
+ my $sth = $dbh->prepare_cached(q{
+ SELECT EXISTS(
+ SELECT true
+ FROM pg_catalog.pg_namespace n
+ JOIN pg_catalog.pg_class c ON c.relnamespace = n.oid
+ JOIN pg_catalog.pg_constraint x ON c.oid = x.conrelid
+ WHERE c.relhaspkey = true
+ AND n.nspname = ?
+ AND c.relname = ?
+ AND x.contype = ?
+ )
+ });
+ return $dbh->selectcol_arrayref($sth, undef, @_)->[0];
+}
+
__END__
=encoding utf8

0 comments on commit 27eeae2

Please sign in to comment.
Something went wrong with that request. Please try again.