File: mysql.pm

package info (click to toggle)
movabletype-opensource 5.1.4%2Bdfsg-4%2Bdeb7u3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 32,996 kB
  • sloc: perl: 197,285; php: 62,405; sh: 166; xml: 117; makefile: 83; sql: 32
file content (268 lines) | stat: -rw-r--r-- 7,107 bytes parent folder | download
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# Movable Type (r) Open Source (C) 2001-2012 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$

package MT::ObjectDriver::DDL::mysql;

use strict;
use warnings;
use base qw( MT::ObjectDriver::DDL );

sub can_add_column   {1}
sub can_drop_column  {1}
sub can_alter_column {1}

sub index_defs {
    my $ddl          = shift;
    my ($class)      = @_;
    my $driver       = $class->driver;
    my $dbh          = $driver->r_handle;
    my $field_prefix = $class->datasource;
    my $table_name   = $class->table_name;
    local $dbh->{RaiseError} = 0;
    my $sth = $dbh->prepare( 'SHOW INDEX FROM ' . $table_name )
        or return undef;
    $sth->execute or return undef;

    my $bags   = {};
    my $unique = {};
    my $sizes  = {};
    while ( my $row = $sth->fetchrow_hashref ) {
        my $key = $row->{'Key_name'};
        next unless $key =~ m/^(mt_)?\Q$field_prefix\E_/;
        $key = 'mt_' . $key unless $key =~ m/^mt_/;

        my $type = $row->{'Index_type'};

        # ignore fulltext or other unrecognized indexes for now
        next unless $type eq 'BTREE';

        my $seq        = $row->{'Seq_in_index'};
        my $col        = $row->{'Column_name'};
        my $non_unique = $row->{'Non_unique'};
        my $null       = $row->{'Null'};
        my $size       = $row->{'Sub_part'};

        $key =~ s/^mt_\Q$field_prefix\E_//;
        $col =~ s/^\Q$field_prefix\E_//;

        $unique->{$key} = 1 unless $non_unique;
        $sizes->{$key}->{$col} = $size if defined $size;
        my $idx_bag = $bags->{$key} ||= [];
        $idx_bag->[ $seq - 1 ] = $col;
    }
    $sth->finish;
    if ( !%$bags ) {
        return undef;
    }

    my $defs = {};
    foreach my $key ( keys %$bags ) {
        my $cols = $bags->{$key};
        my %sizes = %{ $sizes->{$key} || {} };
        if ( $unique->{$key} ) {
            $defs->{$key} = {
                columns => $cols,
                unique  => 1,
                %sizes ? ( sizes => \%sizes ) : (),
            };
        }
        else {
            if ( ( @$cols == 1 ) && ( $key eq $cols->[0] ) && !%sizes ) {
                $defs->{$key} = 1;
            }
            else {
                $defs->{$key} = {
                    columns => $cols,
                    %sizes ? ( sizes => \%sizes ) : (),
                };
            }
        }
    }

    return $defs;
}

sub column_defs {
    my $ddl = shift;
    my ($class) = @_;

    my $driver       = $class->driver;
    my $dbh          = $driver->r_handle;
    my $field_prefix = $class->datasource;
    my $table_name   = $class->table_name;

    # Disable RaiseError if set, since the table we're about to describe
    # may not actually exist (in which case, the return value is undef,
    # signalling an nonexistent table to the caller).
    local $dbh->{RaiseError} = 0;
    my $sth = $dbh->prepare( 'describe ' . $table_name ) or return undef;
    $sth->execute or return undef;
    my $defs = {};
    while ( my $row = $sth->fetchrow_hashref ) {
        my $colname = lc $row->{Field};
        next if $colname !~ m/^\Q$field_prefix\E_/i;
        $colname =~ s/^\Q$field_prefix\E_//i;
        my $coltype = $row->{Type};
        my ($size) = ( $coltype =~ m/(?:var)?char\((\d+)\)/i ? $1 : undef );
        $coltype = $ddl->db2type($coltype);
        $defs->{$colname}{type} = $coltype;
        $defs->{$colname}{auto}
            = ( $row->{Extra} =~ m/auto_increment/i ) ? 1 : 0;
        $defs->{$colname}{key} = $row->{Key} eq 'PRI' ? 1 : 0;

        if ( ( $coltype eq 'string' ) && $size ) {
            $defs->{$colname}{size} = $size;
        }
        if (  !$row->{Null}
            || $row->{Null} eq 'NO'
            || ( $coltype eq 'timestamp' ) )
        {
            $defs->{$colname}{not_null} = 1;
        }
        else {
            $defs->{$colname}{not_null} = 0;
        }
    }
    $sth->finish;
    if ( !%$defs ) {
        return undef;
    }
    return $defs;
}

sub db2type {
    my $ddl = shift;
    my ($type) = @_;
    $type = lc $type;
    $type =~ s/\(.+//;
    if ( $type eq 'int' ) {
        return 'integer';
    }
    elsif ( $type eq 'smallint' ) {
        return 'smallint';
    }
    elsif ( $type eq 'bigint' ) {
        return 'bigint';
    }
    elsif ( $type eq 'mediumint' ) {
        return 'integer';
    }
    elsif ( $type eq 'bigint' ) {
        return 'integer';
    }
    elsif ( $type eq 'varchar' ) {
        return 'string';
    }
    elsif ( $type eq 'varbinary' ) {
        return 'string';
    }
    elsif ( $type eq 'char' ) {
        return 'string';
    }
    elsif ( $type eq 'mediumtext' ) {
        return 'text';
    }
    elsif ( $type eq 'blob' ) {
        return 'blob';
    }
    elsif ( $type eq 'mediumblob' ) {
        return 'blob';
    }
    elsif ( $type eq 'tinyint' ) {
        return 'boolean';
    }
    elsif ( $type eq 'datetime' ) {
        return 'datetime';
    }
    elsif ( $type eq 'timestamp' ) {
        return 'timestamp';
    }
    elsif ( $type eq 'text' ) {
        return 'text';
    }
    elsif ( $type eq 'float' ) {
        return 'float';
    }
    Carp::croak( "undefined type: " . $type );
}

sub type2db {
    my $ddl = shift;
    my ($def) = @_;
    return undef if !defined $def;
    my $type = $def->{type};
    if ( $type eq 'string' ) {
        return 'varchar(' . $def->{size} . ')';
    }
    elsif ( $type eq 'smallint' ) {
        return 'smallint';
    }
    elsif ( $type eq 'bigint' ) {
        return 'bigint';
    }
    elsif ( $type eq 'boolean' ) {
        return 'tinyint';
    }
    elsif ( $type eq 'datetime' ) {
        return 'datetime';
    }
    elsif ( $type eq 'timestamp' ) {
        return 'timestamp';
    }
    elsif ( $type eq 'integer' ) {
        return 'integer';
    }
    elsif ( $type eq 'blob' ) {
        return 'mediumblob';
    }
    elsif ( $type eq 'text' ) {
        return 'mediumtext';
    }
    elsif ( $type eq 'float' ) {
        return 'float';
    }
    Carp::croak( "undefined type: " . $type );
}

sub column_sql {
    my $ddl = shift;
    my ( $class, $name ) = @_;
    my $sql = $ddl->SUPER::column_sql( $class, $name );
    my $def = $class->column_def($name);
    $sql .= ' auto_increment' if $def->{auto};
    return $sql;
}

sub cast_column_sql {
    my $ddl = shift;
    my ( $class, $name, $from_def ) = @_;

    my $def          = $class->column_def($name);
    my $field_prefix = $class->datasource;
    my %cast_type    = (
        'string'    => 'char',
        'smallint'  => 'signed',
        'bigint'    => 'signed',
        'integer'   => 'signed',
        'blob'      => 'binary',
        'text'      => 'char',
        'datetime'  => 'datetime',
        'timestamp' => 'timestamp',
        'boolean'   => 'signed',
        'float'     => 'signed',
    );
    return
        "CAST(${field_prefix}_$name AS " . $cast_type{ $def->{type} } . ')';
}

sub drop_table_sql {
    my $ddl        = shift;
    my ($class)    = @_;
    my $table_name = $class->table_name;
    return "DROP TABLE IF EXISTS $table_name";
}

1;