PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/CXGN/Tools/PgCatalog.pm

https://github.com/solgenomics/cxgn-corelibs
Perl | 160 lines | 139 code | 15 blank | 6 comment | 4 complexity | 14238c52e672df19cb68439888bdbc77 MD5 | raw file
  1. package CXGN::Tools::PgCatalog;
  2. use strict;
  3. use warnings;
  4. use Carp;
  5. BEGIN {
  6. our @EXPORT_OK = qw/ table_info /;
  7. }
  8. our @EXPORT_OK;
  9. use base qw/Exporter/;
  10. =head1 NAME
  11. CXGN::Tools::PgCatalog - tools for getting information out of the
  12. Postgres pg_catalog schema.
  13. =head1 DESCRIPTION
  14. Tools for getting information from the Postgres pg_catalog schema,
  15. which holds everything you ever wanted to know about the structure
  16. of the database itself.
  17. =head1 FUNCTIONS
  18. All functions listed below are EXPORT_OK.
  19. =head2 table_info
  20. Usage: my %info = table_info($dbc,'genomic','blast_hit');
  21. Desc :
  22. Ret : hash-style list as:
  23. ( primary => ['primary key col','primary key col',...],
  24. columns => ['column name', 'column name', ...],
  25. sequence => 'genomic.my_crazy_seq',
  26. )
  27. Args : L<CXGN::DB::Connection> object, schema name, table name
  28. Side Effects:
  29. Example:
  30. =cut
  31. #adapted by Rob from set_up_table in Class::DBI::Pg
  32. sub table_info {
  33. my ( $dbh, $schema, $table ) = @_;
  34. print "getting info for $schema.$table\n";
  35. #convert the schema into the base table schema name
  36. my $schema_bt = $dbh->qualify_schema($schema,1);
  37. # find primary key
  38. my $sth = $dbh->prepare_cached(<<SQL);
  39. SELECT indkey
  40. FROM pg_catalog.pg_index
  41. WHERE indisprimary=true
  42. AND indrelid=( SELECT c.oid
  43. FROM pg_catalog.pg_class as c
  44. JOIN pg_catalog.pg_namespace as n ON (c.relnamespace=n.oid)
  45. WHERE n.nspname = ?
  46. AND c.relname = ?
  47. )
  48. SQL
  49. $sth->execute($schema_bt,$table);
  50. my %prinum = map { $_ => 1 } split ' ', ($sth->fetchrow_array || (''));
  51. $sth->finish;
  52. # find all columns
  53. $sth = $dbh->prepare_cached(<<SQL);
  54. SELECT a.attname,
  55. a.attnum
  56. FROM pg_catalog.pg_class as c,
  57. pg_catalog.pg_attribute as a,
  58. pg_catalog.pg_namespace as n
  59. WHERE n.nspname = ?
  60. AND a.attnum > 0
  61. AND a.attrelid = c.oid
  62. AND n.oid = c.relnamespace
  63. AND c.relname = ?
  64. ORDER BY a.attnum
  65. SQL
  66. $sth->execute($schema, $table);
  67. my $columns = $sth->fetchall_arrayref;
  68. $sth->finish;
  69. # find SERIAL type.
  70. # nextval('"table_id_seq"'::text)
  71. $sth = $dbh->prepare_cached(<<SQL);
  72. SELECT adsrc
  73. FROM pg_catalog.pg_attrdef
  74. WHERE adrelid=( SELECT c.oid
  75. FROM pg_catalog.pg_class as c
  76. JOIN pg_catalog.pg_namespace as n ON (c.relnamespace=n.oid)
  77. WHERE n.nspname = ?
  78. AND c.relname = ?
  79. )
  80. SQL
  81. $sth->execute($schema_bt, $table);
  82. my ($nextval_str) = $sth->fetchrow_array;
  83. $sth->finish;
  84. my ($sequence) =
  85. $nextval_str ? $nextval_str =~ m/^nextval\('"?([^"']+)"?'::text\)/ : '';
  86. # ($sequence) = (split /\./,$sequence)[-1]; #un-qualify the sequence name
  87. my ( @cols, @primary );
  88. foreach my $col (@$columns) {
  89. # skip dropped column.
  90. next if $col->[0] =~ /^\.+pg\.dropped\.\d+\.+$/;
  91. push @cols, $col->[0];
  92. next unless $prinum{ $col->[1] };
  93. push @primary, $col->[0];
  94. }
  95. warn("$schema.$table has no primary key") unless @primary;
  96. warn("$schema.$table has a composite primary key") if @primary > 1;
  97. return ( primary => \@primary,
  98. columns => \@cols,
  99. sequence => $sequence,
  100. );
  101. }
  102. =head2 is_valid_column
  103. #Example
  104. unless(CXGN::DB::Tools::is_valid_column($dbh,$table_name,$column_name))
  105. {
  106. CXGN::Apache::Error::notify('found invalid parameter',"Someone sent in '$column_name' as a parameter. Wacky.");
  107. $sortby='';
  108. }
  109. =cut
  110. sub is_valid_column
  111. {
  112. my($dbh,$table_name,$column_name)=@_;
  113. my $test=$dbh->prepare
  114. ("
  115. select
  116. count(*)
  117. from
  118. pg_class
  119. inner join pg_attribute on (pg_attribute.attrelid=pg_class.oid)
  120. where
  121. relname=?
  122. and attname=?
  123. and relkind='r'
  124. ");
  125. $test->execute($table_name,$column_name);
  126. my($found)=$test->fetchrow_array();
  127. return $found;
  128. }
  129. =head1 AUTHOR
  130. Robert Buels and John Binns <zombieite@gmail.com>
  131. =cut
  132. ###
  133. 1;#do not remove
  134. ###