summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlperl/lib/CPAN/SQLite/DBI.pm
blob: 52318924531c3c893479098f5056f8c74f362e30 (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package CPAN::SQLite::DBI;
use strict;
use warnings;
require File::Spec;
use DBI;
our $VERSION = '0.199';

use base qw(Exporter);
our ($dbh, $tables, @EXPORT_OK);
@EXPORT_OK = qw($dbh $tables);

sub rlike {
  my ($x, $y) = @_;
  return ($x =~ /$y/i) ? 1 : 0;
}

$tables = {
	   mods => {
		    primary => {mod_id => q{INTEGER NOT NULL PRIMARY KEY}},
		    other => {
			      mod_name => q{VARCHAR(100) NOT NULL},
			      dist_id => q{INTEGER NOT NULL},
			      mod_abs => q{TEXT},
			      mod_vers => q{VARCHAR(10)},
			      dslip => q{VARCHAR(5)},
			      chapterid => q{INTEGER},
			     },
		    key => [qw/dist_id mod_name/],
		    name => 'mod_name',
		    id => 'mod_id',
		    has_a => {dists => 'dist_id'},
		   },
	   dists => {
		     primary => {dist_id => q{INTEGER NOT NULL PRIMARY KEY}},
		     other => {
			       dist_name => q{VARCHAR(90) NOT NULL},
			       auth_id => q{INTEGER NOT NULL},
			       dist_file => q{VARCHAR(110) NOT NULL},
			       dist_vers => q{VARCHAR(20)},
			       dist_abs => q{TEXT},
                               dist_dslip => q{VARCHAR(5)},
			      },
		     key => [qw/auth_id dist_name/],
		     name => 'dist_name',
		     id => 'dist_id',
		     has_a => {auths => 'auth_id'},
		     has_many => {mods => 'dist_id',
				  chaps => 'dist_id',
				 },
		    },
	   auths => {
		     primary => {auth_id => q{INTEGER NOT NULL PRIMARY KEY}},
		     other => {
			       cpanid => q{VARCHAR(20) NOT NULL},
			       fullname => q{VARCHAR(40) NOT NULL},
			       email => q{TEXT},
			      },
		     key => [qw/cpanid/],
		     has_many => {dists => 'dist_id'},
		     name => 'cpanid',
		     id => 'auth_id',
		    },
	   chaps => {
		     primary => {chap_id => q{INTEGER NOT NULL PRIMARY KEY}},
		     other => {
			       dist_id => q{INTEGER NOT NULL},
			       chapterid => q{INTEGER},
			       subchapter => q{TEXT},
			      },
		     key => [qw/dist_id/],
		     id => 'chap_id',
		     name => 'chapterid',
		     has_a => {dists => 'dist_id'},
		    },
          };

sub new {
  my ($class, %args) = @_;
  my $db_dir = $args{db_dir} || $args{CPAN};
  my $db = File::Spec->catfile($db_dir, $args{db_name});
  $dbh ||= DBI->connect("DBI:SQLite:$db", '', '',
                        {RaiseError => 1, AutoCommit => 0})
    or die "Cannot connect to $db";
  $dbh->{AutoCommit} = 0;
  $dbh->func('RLIKE', 2, \&rlike, 'create_function');

  my $objs;
  foreach my $table (keys %$tables) {
    my $cl = $class . '::' . $table;
    $objs->{$table} = $cl->make(table => $table);
  }

  for my $table (keys %$tables) {
    foreach my $type (qw(primary other)) {
      foreach my $column (keys %{$tables->{$table}->{$type}}) {
	push @{$tables->{$table}->{columns}}, $column;
      }
    }
  }

  bless {objs => $objs}, $class;
}

sub make {
  my ($class, %args) = @_;
  my $table = $args{table};
  die qq{No table exists corresponding to '$class'} unless $table;
  my $info = $tables->{$table};
  die qq{No information available for table '$table'} unless $info;
  my $self = {table => $table,
              columns => $info->{columns},
              id => $info->{id},
	      name => $info->{name},
             };
  foreach (qw(name has_a has_many)) {
    next unless defined $info->{$_};
    $self->{$_} = $info->{$_};
  }
  bless $self, $class;
}

sub db_error {
  my ($obj, $sth) = @_;
  return unless $dbh;
  if ($sth) {
    $sth->finish;
    undef $sth;
  }
  $obj->{error_msg} = q{Database error: } . $dbh->errstr;
}


1;

__END__

=head1 NAME

CPAN::SQLite::DBI - DBI information for the CPAN::SQLite database

=head1 DESCRIPTION

This module is used by L<CPAN::SQLite::Index> and
L<CPAN::SQLite::Search> to set up some basic database
information. It exports two variables:

=over 3

=item C<$tables>

This is a hash reference whose keys are the table names, with
corresponding values being hash references whose keys are the
columns of the table and values being the associated data types.

=item C<$dbh>

This is a L<DBI> database handle used to connect to the
database.

=back

The main method of this module is C<make>, which is used
to make the tables of the database.

=head1 SEE ALSO

L<CPAN::SQLite::Index> and L<CPAN::SQLite::Search>

=cut