{ # Based on the change to Class::DBI in OpenILS::Application::Storage.  This will
  # allow us to use TSearch2 via a simple cdbi "search" interface.
	#-------------------------------------------------------------------------------
	use Class::DBI;
	package Class::DBI;

	sub search_fti {
		my $self = shift;
		my @args = @_;
		if (ref($args[-1]) eq 'HASH') {
			$args[-1]->{_placeholder} = "to_tsquery('default',?)";
		} else {
			push @args, {_placeholder => "to_tsquery('default',?)"};
		}
		$self->_do_search("@@"  => @args);
	}
}

{ # Every driver needs to provide a 'compile()' method to OpenILS::Application::Storage::FTS.
  # If that driver wants to support FTI, that is...
	#-------------------------------------------------------------------------------
	package OpenILS::Application::Storage::FTS;
	use OpenSRF::Utils::Logger qw/:level/;
	my $log = 'OpenSRF::Utils::Logger';

	sub compile {
		my $self = shift;
		my $term = shift;

		$self = ref($self) || $self;
		$self = bless {} => $self;

		$self->decompose($term);

		my $newterm = join('&', $self->words);

		if ($self->nots) {
			$newterm = '('.$newterm.')&('. join('|', $self->nots) . ')';
		}

		$newterm = OpenILS::Application::Storage->driver->quote($newterm);

		$self->{fts_query} = ["to_tsquery('default',$newterm)"];
		$self->{fts_query_nots} = [];
		$self->{fts_op} = '@@';

		return $self;
	}
}


{ # The driver package itself just needs a db_Main method for Class::DBI to call.
  # any other fixups can go in here too... Also, the drivers should subclass the
  # DBI driver that they are wrapping, or provide a 'quote()' method that calls
  # the DBD::xxx::quote() method on FTI's behalf.
  #
  # The dirver MUST be a subclass of Class::DBI.
	#-------------------------------------------------------------------------------
	package OpenILS::Application::Storage::Driver::Pg;
	use base qw/Class::DBI OpenILS::Application::Storage/;
	use DBI;
	use DBD::Pg;
	use OpenSRF::Utils::Logger qw/:level/;

	__PACKAGE__->set_sql( retrieve_limited => 'SELECT * FROM __TABLE__ ORDER BY id LIMIT ?' );

	my $_db_params;
	sub child_init {
		my $self = shift;
		$_db_params = shift;
	}

	my $_dbh;
	sub db_Main {	
		my $self = shift;

		return $_dbh if (defined $_dbh and $_dbh->ping);

		my %attrs = (	%{$self->_default_attributes},
				RootClass => 'DBIx::ContextualFetch',
				ShowErrorStatement => 1,
				RaiseError => 1,
				AutoCommit => 1,
				PrintError => 1,
				Taint => 1,
				pg_enable_utf8 => 1,
				FetchHashKeyName => 'NAME_lc',
				ChopBlanks => 1,
		);

		$log->debug(" Default attributes for this DB connection are:\n\t".join("\n\t",map { "$_\t==> $attrs{$_}" } keys %attrs), INTERNAL);

		$_dbh = DBI->connect( "dbi:Pg:host=$_db_params{host};dbname=$_db_params{database}",$_db_params{user},$_db_params{pw}, \%attrs );
		$_dbh->do("SET CLIENT_ENCODING TO 'SQL_ASCII';");

		return $_dbh;
	}

	sub quote {
		return $_dbh->quote(@_)
	}
}

{
	#---------------------------------------------------------------------
	package asset::call_number;
	
	__PACKAGE__->table( 'asset.call_number' );
	__PACKAGE__->sequence( 'asset.call_number_id_seq' );
	
	#---------------------------------------------------------------------
	package asset::copy;
	
	__PACKAGE__->table( 'asset.copy' );
	__PACKAGE__->sequence( 'asset.copy_id_seq' );
	
	#---------------------------------------------------------------------
	package biblio::record_entry;
	
	__PACKAGE__->table( 'biblio.record_entry' );
	__PACKAGE__->sequence( 'biblio.record_entry_id_seq' );
	
	#---------------------------------------------------------------------
	package biblio::record_node;
	
	__PACKAGE__->table( 'biblio.record_data' );
	__PACKAGE__->sequence( 'biblio.record_data_id_seq' );
	
	#---------------------------------------------------------------------
	package biblio::record_note;
	
	__PACKAGE__->table( 'biblio.record_note' );
	__PACKAGE__->sequence( 'biblio.record_note_id_seq' );
	
	#---------------------------------------------------------------------
	package actor::usr;
	
	__PACKAGE__->table( 'actor.usr' );
	__PACKAGE__->sequence( 'actor.usr_id_seq' );
	
	#---------------------------------------------------------------------
	package actor::org_unit_type;
	
	__PACKAGE__->table( 'actor.org_unit_type' );
	__PACKAGE__->sequence( 'actor.org_unit_type_id_seq' );
	
	#---------------------------------------------------------------------
	
}

1;
