File: dbi2omega

package info (click to toggle)
xapian-omega 1.0.7-3+lenny2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,424 kB
  • ctags: 744
  • sloc: sh: 9,112; cpp: 7,954; makefile: 245; perl: 119
file content (63 lines) | stat: -rwxr-xr-x 2,187 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/perl -w
# dbi2omega - dump an SQL database into a form suitable for indexing
# into a Xapian database using scriptindex.  This script requires the perl DBI
# interface to be installed (on Debian systems, this is provided by the
# libdbi-perl package).
#
# Copyright (c) 2002,2006 Olly Betts
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
# USA

use strict;
use DBI;

$#ARGV >= 1 or die "Syntax: $0 DATABASE TABLE [FIELD...]\n";

my $database = shift @ARGV;
my $table = shift @ARGV;
my $fields = join ",", @ARGV;
my $username = $ENV{'DBUSER'} || $ENV{USER} || $ENV{LOGNAME} || '';
my $password = $ENV{'DBPASSWORD'} || '';
# 0.9.4 and earlier were hardwired to use mysql here.  DBI defaults
# to DBIDRIVER if you specify a datasource of "DBI::$database", so
# it's an appropriate environment variable to check.
my $driver = $ENV{'DBIDRIVER'} || 'mysql';

length $fields or $fields = "*";

my $dbh = DBI->connect("DBI:$driver:$database", $username, $password)
  or die "Couldn't connect to database: " . DBI->errstr;

my $sth = $dbh->prepare("SELECT $fields FROM $table")
  or die "Couldn't prepare statement: " . $dbh->errstr;

$sth->execute()
  or die "Couldn't execute statement: " . $sth->errstr;

my $data;
while (defined($data = $sth->fetchrow_arrayref())) {
  for my $i (0 .. $sth->{NUM_OF_FIELDS} - 1) {
    my $v = $$data[$i];
    if (defined($v)) {
      $v =~ s/\n/\n=/g;
      print "${$sth->{NAME_lc}}[$i]=$v\n";
    }
  }
  print "\n";
}
$sth->err and die "Couldn't fetch row: " . $sth->errstr;

$dbh->disconnect;