| 1293 |
dpurdie |
1 |
###############################################################################
|
|
|
2 |
# Codestriker: Copyright (c) 2001, 2002 David Sitsky. All rights reserved.
|
|
|
3 |
# sits@users.sourceforge.net
|
|
|
4 |
#
|
|
|
5 |
# This program is free software; you can redistribute it and modify it under
|
|
|
6 |
# the terms of the GPL.
|
|
|
7 |
|
|
|
8 |
# Object which represents a database table specification.
|
|
|
9 |
|
|
|
10 |
package Codestriker::DB::Table;
|
|
|
11 |
|
|
|
12 |
use strict;
|
|
|
13 |
|
|
|
14 |
# A table consists of an array of Column objects, plus an array of indexes.
|
|
|
15 |
# usage: Table->new{name=>"table_name", columns=>\@columns, indexes=>\@indexes}
|
|
|
16 |
sub new {
|
|
|
17 |
my $type = shift;
|
|
|
18 |
my %params = @_;
|
|
|
19 |
|
|
|
20 |
my $self = {};
|
|
|
21 |
$self->{name} = $params{name};
|
|
|
22 |
|
|
|
23 |
if (defined $params{columns}) {
|
|
|
24 |
$self->{columns} = $params{columns};
|
|
|
25 |
} else {
|
|
|
26 |
$self->{columns} = [];
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
if (defined $params{indexes}) {
|
|
|
30 |
$self->{indexes} = $params{indexes};
|
|
|
31 |
} else {
|
|
|
32 |
$self->{indexes} = [];
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
# Check that the column names in the indexes actually exist.
|
|
|
36 |
my @columns = @{$params{columns}};
|
|
|
37 |
|
|
|
38 |
# Check each index is valid for this table.
|
|
|
39 |
foreach my $index (@{$params{indexes}}) {
|
|
|
40 |
|
|
|
41 |
# Check each column in this index refers to a column in this table.
|
|
|
42 |
foreach my $index_column (@{$index->get_column_names()}) {
|
|
|
43 |
my $found = 0;
|
|
|
44 |
|
|
|
45 |
# Check this column exists.
|
|
|
46 |
foreach my $column (@columns) {
|
|
|
47 |
if ($column->get_name() eq $index_column) {
|
|
|
48 |
$found = 1;
|
|
|
49 |
last;
|
|
|
50 |
}
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
if ($found == 0) {
|
|
|
54 |
die "Index $index->get_name() has bad column $index_column\n";
|
|
|
55 |
}
|
|
|
56 |
}
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
return bless $self, $type;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
# Return the table name.
|
|
|
63 |
sub get_name {
|
|
|
64 |
my $self = shift;
|
|
|
65 |
return $self->{name};
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
# Return the columns associated with this table.
|
|
|
69 |
sub get_columns {
|
|
|
70 |
my $self = shift;
|
|
|
71 |
return $self->{columns};
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
# Return the indexes associated with this table.
|
|
|
75 |
sub get_indexes {
|
|
|
76 |
my $self = shift;
|
|
|
77 |
return $self->{indexes};
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
1;
|