discourse-legacysite-perl/site/slowtwitch.com/cgi-bin/articles/GT/SQL/Search/MSSQL/Indexer.pm
2024-06-17 21:49:12 +10:00

99 lines
3.8 KiB
Perl

# ==================================================================
# Gossamer Threads Module Library - http://gossamer-threads.com/
#
# GT::Search::MSSQL::Indexer
# Author: Alex Krohn
# CVS Info : 087,071,086,086,085
# $Id: Indexer.pm,v 1.6 2004/08/28 03:53:48 jagerman Exp $
#
# Copyright (c) 2004 Gossamer Threads Inc. All Rights Reserved.
# ==================================================================
#
# Description:
# Supports MS SQL full text indexer on MS SQL 2000 only.
#
package GT::SQL::Search::MSSQL::Indexer;
#--------------------------------------------------------------------------------
use strict;
use vars qw/@ISA $VERSION $DEBUG $ERRORS $ERROR_MESSAGE/;
use GT::SQL::Search::Base::Indexer;
@ISA = qw/ GT::SQL::Search::Base::Indexer /;
$DEBUG = 0;
$VERSION = sprintf "%d.%03d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/;
$ERRORS = {
NOTFROMWEB => 'There are far too many records in table %s for create/destroy of this indexing scheme from the web. Please use alternative method.',
MSSQLNONSUPPORT => 'You must be using MS SQL 2000 in order to use full text indexing. Current Database: %s',
CREATEINDEX => 'Problem Creating Full Text Index: %s'
};
$ERROR_MESSAGE = 'GT::SQL';
sub load {
my $class = shift;
return $class->new(@_);
}
sub ok {
#--------------------------------------------------------------------------------
my ($class, $tbl) = @_;
unless (uc $tbl->{connect}->{driver} eq 'ODBC') {
return $class->error ('MSSQLNONSUPPORT', 'WARN', $tbl->{connect}->{driver});
}
return 1;
}
sub drop_search_driver {
#--------------------------------------------------------------------------------
my $self = shift;
my $table = $self->{table};
my $name = $table->name;
my $cat = $name . '_ctlg';
my $res = eval {
$table->do_query(" sp_fulltext_table '$name', 'drop' ");
$table->do_query(" sp_fulltext_catalog '$cat', 'drop' ");
1;
};
$res ? return 1 : return;
}
sub add_search_driver {
#--------------------------------------------------------------------------------
my $self = shift;
my $table = $self->{table};
my $name = $table->name;
my $cat = $name . '_ctlg';
my %weights = $table->weight;
my ($pk) = $table->pk;
# Enable a database for full text indexing
$table->do_query(" sp_fulltext_database 'enable' ") or $self->error('CREATEINDEX', 'FATAL', $GT::SQL::error);
# Create a full text catalog to store the data.
$table->do_query(" sp_fulltext_catalog '$cat', 'create' ") or $self->error('CREATEINDEX', 'WARN', $GT::SQL::error);
# Make a unique index on primary key (not sure why it isn't by default.
$table->do_query(" create unique index PK_$name on $name ($pk) ");
# Mark this table as using the full text catalog created
$table->do_query(" sp_fulltext_table '$name', 'create', '$cat', 'PK_$name' ") or $self->error('CREATEINDEX', 'WARN', $GT::SQL::error);
# Specify which columns are to be indexed
foreach my $col (keys %weights) {
if ($weights{$col}) {
$table->do_query(" sp_fulltext_column '$name', '$col', 'add' ") or $self->error('CREATEINDEX', 'WARN', $GT::SQL::error);
}
}
# Must have a timestamp field.
$table->do_query(" alter table $name add timestamp ");
# Build the index.
$table->do_query(" sp_fulltext_table '$name', 'start_change_tracking' ") or $self->error('CREATEINDEX', 'WARN', $GT::SQL::error);
$table->do_query(" sp_fulltext_table '$name', 'start_background_updateindex' ") or $self->error('CREATEINDEX', 'WARN', $GT::SQL::error);
return 1;
}
sub post_create_table {
#--------------------------------------------------------------------------------
shift->add_search_driver(@_);
}
1;