package dbSmoke;

use strict;
use warnings;

use Exporter();

use vars qw( @ISA @EXPORT );

@ISA = qw( Exporter );

@EXPORT = qw/
   get_grade_index
   get_module_index
   get_arch_index
   get_osv_index
   get_tester_index
   /;

sub get_tester_index( $$ )
   {
   my ( $tester, $db ) = @_;
   my $tester_index = $db->selectrow_array( qq/SELECT index
		                               FROM  testers
					       WHERE email LIKE '%$tester%'/
   );
   if ( !defined $tester_index )
      {
      $db->do( qq/INSERT INTO testers( email ) values( '$tester' )/ );
      $tester_index =
         $db->selectrow_array( qq/SELECT currval( 'testers_index_seq' )/ );
      }
   return $tester_index;
   }

sub get_grade_index( $$ )
   {
   my ( $grade, $db ) = @_;
   my $grade_index = $db->selectrow_array( qq/SELECT index
					      FROM  grades
					      WHERE grade LIKE '%$grade%'/
   );
   if ( !defined $grade_index )
      {
      $db->do( qq/INSERT INTO grades( grade ) values( '$grade' )/ );
      $grade_index =
         $db->selectrow_array( qq/SELECT currval( 'grades_index_seq' )/ );
      }
   return $grade_index;
   }

sub get_module_index( $$ )
   {
   my ( $module, $db ) = @_;
   my $module_index = $db->selectrow_array( qq/SELECT index
                                               FROM  modules
					       WHERE module LIKE '%$module%'/
   );
   if ( !defined $module_index )
      {
      $db->do( qq/INSERT INTO modules( module ) values( '$module' )/ );
      $module_index =
         $db->selectrow_array( qq/SELECT currval( 'modules_index_seq' )/ );
      }
   return $module_index;
   }

sub get_arch_index( $$ )
   {
   my ( $arch, $db ) = @_;
   my $arch_index = $db->selectrow_array( qq/SELECT index
                                             FROM  archnames
					     WHERE name = '$arch'/
   );
   if ( !defined $arch_index )
      {
      $db->do( qq/INSERT INTO archnames( name ) values( '$arch' )/ );
      $arch_index =
         $db->selectrow_array( qq/SELECT currval( 'archnames_index_seq' )/ );
      }
   return $arch_index;
   }

sub get_osv_index( $$ )
   {
   my ( $osv, $db ) = @_;
   my $osv_index = $db->selectrow_array( qq/SELECT index
                                            FROM  osversions
					    WHERE version = '$osv'/
   );
   if ( !defined $osv_index )
      {
      $db->do( qq/INSERT INTO osversions( version ) values( '$osv' )/ );
      $osv_index =
         $db->selectrow_array( qq/SELECT currval( 'osversions_index_seq' )/ );
      }
   return $osv_index;
   }
