line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
2
|
|
|
|
|
|
|
# DBO::Visitor::Create - create database |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# DESCRIPTION |
5
|
|
|
|
|
|
|
# A visitor class that creates tables in a database. |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
# AUTHOR |
8
|
|
|
|
|
|
|
# Gareth Rees |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
# COPYRIGHT |
11
|
|
|
|
|
|
|
# Copyright (c) 1999 Canon Research Centre Europe Ltd/ |
12
|
|
|
|
|
|
|
# |
13
|
|
|
|
|
|
|
# $Id: Create.pm,v 1.2 1999/06/14 17:05:21 garethr Exp $ |
14
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
15
|
|
|
|
|
|
|
|
16
|
4
|
|
|
4
|
|
3512
|
use strict; |
|
4
|
|
|
|
|
56
|
|
|
4
|
|
|
|
|
192
|
|
17
|
|
|
|
|
|
|
package DBO::Visitor::Create; |
18
|
4
|
|
|
4
|
|
20
|
use base qw(DBO::Visitor); |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
1965
|
|
19
|
4
|
|
|
4
|
|
23
|
use Class::Multimethods; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
24
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
multimethod visit_table => |
22
|
|
|
|
|
|
|
qw(DBO::Visitor::Create DBO::Table DBO::Handle::DBI) => |
23
|
|
|
|
|
|
|
sub { |
24
|
|
|
|
|
|
|
my ($vis, $table, $dbh) = @_; |
25
|
|
|
|
|
|
|
my @sql = ("CREATE TABLE $table->{name} ("); |
26
|
|
|
|
|
|
|
foreach my $col (@{$table->{columns}}) { |
27
|
|
|
|
|
|
|
$vis->{sql} = []; |
28
|
|
|
|
|
|
|
visit_column($vis, $col, $dbh); |
29
|
|
|
|
|
|
|
push @sql, @{$vis->{sql}}, "," if @{$vis->{sql}}; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
splice @sql, -1, 1, ")"; |
32
|
|
|
|
|
|
|
$dbh->dosql(@sql); |
33
|
|
|
|
|
|
|
}; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
multimethod visit_column => |
36
|
|
|
|
|
|
|
qw(DBO::Visitor::Create DBO::Column::Base DBO::Handle::DBI) => |
37
|
|
|
|
|
|
|
sub { |
38
|
|
|
|
|
|
|
my ($vis, $col, $dbh) = @_; |
39
|
|
|
|
|
|
|
}; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
multimethod visit_column => |
42
|
|
|
|
|
|
|
qw(DBO::Visitor::Create DBO::Column::Integer DBO::Handle::DBI) => |
43
|
|
|
|
|
|
|
sub { |
44
|
|
|
|
|
|
|
my ($vis, $col, $dbh) = @_; |
45
|
|
|
|
|
|
|
push @{$vis->{sql}}, "$col->{name} INT"; |
46
|
|
|
|
|
|
|
push @{$vis->{sql}}, "NOT NULL" if $col->{not_null}; |
47
|
|
|
|
|
|
|
}; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
multimethod visit_column => |
50
|
|
|
|
|
|
|
qw(DBO::Visitor::Create DBO::Column::Unsigned DBO::Handle::DBI) => |
51
|
|
|
|
|
|
|
sub { |
52
|
|
|
|
|
|
|
my ($vis, $col, $dbh) = @_; |
53
|
|
|
|
|
|
|
push @{$vis->{sql}}, "$col->{name} INT"; |
54
|
|
|
|
|
|
|
push @{$vis->{sql}}, "NOT NULL" if $col->{not_null}; |
55
|
|
|
|
|
|
|
}; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
multimethod visit_column => |
58
|
|
|
|
|
|
|
qw(DBO::Visitor::Create DBO::Column::Char DBO::Handle::DBI) => |
59
|
|
|
|
|
|
|
sub { |
60
|
|
|
|
|
|
|
my ($vis, $col, $dbh) = @_; |
61
|
|
|
|
|
|
|
push @{$vis->{sql}}, "$col->{name} CHAR ($col->{max_length})"; |
62
|
|
|
|
|
|
|
push @{$vis->{sql}}, "NOT NULL" if $col->{not_null}; |
63
|
|
|
|
|
|
|
}; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
multimethod visit_column => |
66
|
|
|
|
|
|
|
qw(DBO::Visitor::Create DBO::Column::Text DBO::Handle::DBI) => |
67
|
|
|
|
|
|
|
sub { |
68
|
|
|
|
|
|
|
my ($vis, $col, $dbh) = @_; |
69
|
|
|
|
|
|
|
push @{$vis->{sql}}, "$col->{name} VARCHAR ($col->{max_length})"; |
70
|
|
|
|
|
|
|
push @{$vis->{sql}}, "NOT NULL" if $col->{not_null}; |
71
|
|
|
|
|
|
|
}; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
75
|
|
|
|
|
|
|
# Method instances for mSQL databases |
76
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
multimethod visit_table => |
79
|
|
|
|
|
|
|
qw(DBO::Visitor::Create DBO::Table DBO::Handle::DBI::mSQL) => |
80
|
|
|
|
|
|
|
sub { |
81
|
|
|
|
|
|
|
my ($vis, $table, $dbh) = @_; |
82
|
|
|
|
|
|
|
$vis->{sequence} = 0; |
83
|
|
|
|
|
|
|
# visit_table($vis, $table, superclass($dbh)); |
84
|
|
|
|
|
|
|
call_next_method(); |
85
|
|
|
|
|
|
|
if (@{$table->{keys}}) { |
86
|
|
|
|
|
|
|
my @sql = "CREATE INDEX index_$table->{name} ON $table->{name}("; |
87
|
|
|
|
|
|
|
foreach my $key (@{$table->{keys}}) { |
88
|
|
|
|
|
|
|
push @sql, $key->{name}, ","; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
splice @sql, -1, 1, ")"; |
91
|
|
|
|
|
|
|
$dbh->dosql(@sql); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
if ($vis->{sequence}) { |
94
|
|
|
|
|
|
|
$dbh->dosql("CREATE SEQUENCE ON $table->{name}"); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
}; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
multimethod visit_column => |
99
|
|
|
|
|
|
|
qw(DBO::Visitor::Create DBO::Column::Text DBO::Handle::DBI::mSQL) => |
100
|
|
|
|
|
|
|
sub { |
101
|
|
|
|
|
|
|
my ($vis, $col, $dbh) = @_; |
102
|
|
|
|
|
|
|
push @{$vis->{sql}}, "$col->{name} TEXT ($col->{avg_length})"; |
103
|
|
|
|
|
|
|
push @{$vis->{sql}}, "NOT NULL" if $col->{not_null}; |
104
|
|
|
|
|
|
|
}; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
multimethod visit_column => |
107
|
|
|
|
|
|
|
qw(DBO::Visitor::Create DBO::Column::Unsigned DBO::Handle::DBI::mSQL) => |
108
|
|
|
|
|
|
|
sub { |
109
|
|
|
|
|
|
|
my ($vis, $col, $dbh) = @_; |
110
|
|
|
|
|
|
|
push @{$vis->{sql}}, "$col->{name} UINT"; |
111
|
|
|
|
|
|
|
push @{$vis->{sql}}, "NOT NULL" if $col->{not_null}; |
112
|
|
|
|
|
|
|
}; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
multimethod visit_column => |
115
|
|
|
|
|
|
|
qw(DBO::Visitor::Create DBO::Column::AutoIncrement DBO::Handle::DBI::mSQL) => |
116
|
|
|
|
|
|
|
sub { |
117
|
|
|
|
|
|
|
my ($vis, $col, $dbh) = @_; |
118
|
|
|
|
|
|
|
$vis->{sequence} = 1; |
119
|
|
|
|
|
|
|
# visit_column($vis, superclass($col), $dbh); |
120
|
|
|
|
|
|
|
call_next_method(); |
121
|
|
|
|
|
|
|
}; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
125
|
|
|
|
|
|
|
# Method instances for MySQL databases. |
126
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
multimethod visit_table => |
129
|
|
|
|
|
|
|
qw(DBO::Visitor::Create DBO::Table DBO::Handle::DBI::mysql) => |
130
|
|
|
|
|
|
|
sub { |
131
|
|
|
|
|
|
|
my ($vis, $table, $dbh) = @_; |
132
|
|
|
|
|
|
|
my @sql = ("CREATE TABLE $table->{name} ("); |
133
|
|
|
|
|
|
|
$vis->{sql} = \@sql; |
134
|
|
|
|
|
|
|
foreach my $col (@{$table->{columns}}) { |
135
|
|
|
|
|
|
|
$vis->{sql} = []; |
136
|
|
|
|
|
|
|
visit_column($vis, $col, $dbh); |
137
|
|
|
|
|
|
|
push @sql, @{$vis->{sql}}, "," if @{$vis->{sql}}; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
if (@{$table->{keys}}) { |
140
|
|
|
|
|
|
|
push @sql, "INDEX index_$table->{name} ("; |
141
|
|
|
|
|
|
|
foreach my $key (@{$table->{keys}}) { |
142
|
|
|
|
|
|
|
push @sql, $key->{name}, ","; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
splice @sql, -1, 1, ")", ","; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
splice @sql, -1, 1, ")"; |
147
|
|
|
|
|
|
|
$dbh->dosql(@sql); |
148
|
|
|
|
|
|
|
}; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
multimethod visit_column => |
151
|
|
|
|
|
|
|
qw(DBO::Visitor::Create DBO::Column::Unsigned DBO::Handle::DBI::mysql) => |
152
|
|
|
|
|
|
|
sub { |
153
|
|
|
|
|
|
|
my ($vis, $col, $dbh) = @_; |
154
|
|
|
|
|
|
|
push @{$vis->{sql}}, "$col->{name} INT UNSIGNED"; |
155
|
|
|
|
|
|
|
push @{$vis->{sql}}, "NOT NULL" if $col->{not_null}; |
156
|
|
|
|
|
|
|
}; |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
multimethod visit_column => |
159
|
|
|
|
|
|
|
qw(DBO::Visitor::Create DBO::Column::AutoIncrement DBO::Handle::DBI::mysql) => |
160
|
|
|
|
|
|
|
sub { |
161
|
|
|
|
|
|
|
my ($vis, $col, $dbh) = @_; |
162
|
|
|
|
|
|
|
# visit_column($vis, superclass($col), $dbh); |
163
|
|
|
|
|
|
|
call_next_method(); |
164
|
|
|
|
|
|
|
push @{$vis->{sql}}, "AUTO_INCREMENT"; |
165
|
|
|
|
|
|
|
}; |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
multimethod visit_column => |
168
|
|
|
|
|
|
|
qw(DBO::Visitor::Create DBO::Column::Text DBO::Handle::DBI::mysql) => |
169
|
|
|
|
|
|
|
sub { |
170
|
|
|
|
|
|
|
my ($vis, $col, $dbh) = @_; |
171
|
|
|
|
|
|
|
my $type; |
172
|
|
|
|
|
|
|
if ($col->{max_length} < 0x100) { $type = 'TINYTEXT' } |
173
|
|
|
|
|
|
|
elsif ($col->{max_length} < 0x10000) { $type = 'TEXT' } |
174
|
|
|
|
|
|
|
elsif ($col->{max_length} < 0x1000000) { $type = 'MEDIUMTEXT' } |
175
|
|
|
|
|
|
|
else { $type = 'LONGTEXT' } |
176
|
|
|
|
|
|
|
push @{$vis->{sql}}, "$col->{name} $type"; |
177
|
|
|
|
|
|
|
push @{$vis->{sql}}, "NOT NULL" if $col->{not_null}; |
178
|
|
|
|
|
|
|
}; |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
1; |