Changeset 2705

Show
Ignore:
Timestamp:
07/03/08 23:42:42 (12 months ago)
Author:
bchoate
Message:

Added support for determining autoincrement/nullable columns for sqlite's DDL module.

Location:
branches/release-41
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/release-41/lib/MT/ObjectDriver/DDL/SQLite.pm

    r2700 r2705  
    9090    my $table_name = $class->table_name; 
    9191    my $field_prefix = $class->datasource; 
    92     my $props = $class->properties; 
    93     my $obj_defs = $class->column_defs; 
    9492 
    9593    return undef unless $dbh; 
     
    9896    # may not actually exist (in which case, the return value is undef, 
    9997    # signalling an nonexistent table to the caller). 
    100     local $dbh->{RaiseError} = 0; 
    101     my $sth = $dbh->prepare('SELECT * FROM ' . $table_name . ' LIMIT 1') 
     98    local $dbh->{RaiseError} = 1; 
     99    my $sth = $dbh->prepare('PRAGMA table_info("' . $table_name . '")') 
    102100        or return undef; 
    103101    $sth->execute or return undef; 
    104     my $fields = $sth->{'NUM_OF_FIELDS'}; 
    105     my $coltypes = $sth->{'TYPE'}; 
    106     my $name = $sth->{'NAME'}; 
    107     my $null = $sth->{'NULLABLE'}; 
    108     #my $skip_null_checks; 
    109     #if (!$null || !@$null) { 
    110     #    $skip_null_checks = 1; 
    111     #} 
    112102    my $defs = {}; 
    113     foreach (my $col = 0; $col < $fields; $col++) { 
    114         my $colname = lc $name->[$col]; 
     103    while (my $row = $sth->fetchrow_hashref) { 
     104        my $colname = lc $row->{name}; 
    115105        $colname =~ s/^\Q$field_prefix\E_//i; 
    116         my $coltype = $ddl->db2type($coltypes->[$col]); 
    117         if ($coltypes->[$col] =~ m/\((\d+)\)/) { 
     106        my $coltype = $ddl->db2type($row->{type}); 
     107        if ($row->{type} =~ m/\((\d+)\)/) { 
    118108            $defs->{$colname}{size} = $1; 
    119109        } 
     
    122112            $defs->{$colname}{key} = 1; 
    123113        } 
    124         if ( $coltype eq 'integer' && $defs->{$colname}{key} ) { 
     114        if ( ($coltype eq 'integer') && $row->{pk} ) { 
    125115            # with sqlite, integer primary keys auto increment. always. 
     116            $defs->{$colname}{key} = 1; 
    126117            $defs->{$colname}{auto} = 1; 
    127118        } 
    128         #if ($skip_null_checks) { 
    129         if ( exists $obj_defs->{$colname} ) { 
    130             $defs->{$colname}{not_null} = $obj_defs->{$colname}{not_null}; 
    131         } 
    132         #} else { 
    133         #    if ( (defined $null->[$col]) && ($null->[$col] == 0) ) { 
    134         #        $defs->{$colname}{not_null} = 1; 
    135         #    } 
    136         #} 
     119        $defs->{$colname}{not_null} = 1 
     120            if $row->{notnull}; 
     121        $defs->{$colname}{default} = $row->{dflt_value} 
     122            if defined $row->{dflt_value}; 
    137123    } 
    138124    $sth->finish; 
     125    return undef unless %$defs; 
    139126    return $defs; 
    140127} 
     
    153140    my $ddl = shift; 
    154141    my ($def) = @_; 
     142    return undef if !defined $def; 
    155143    my $type = (ref($def) eq 'HASH') ? $def->{type} : $def; 
     144    $type = $def->{type}; 
    156145    if ($type eq 'string') { 
    157         $type = 'varchar(' . $def->{size} . ')'; 
    158     } 
    159     return $type; 
     146        return 'varchar(' . $def->{size} . ')'; 
     147    } elsif ($type eq 'smallint' ) { 
     148        return 'smallint'; 
     149    } elsif ($type eq 'bigint' ) { 
     150        return 'bigint'; 
     151    } elsif ($type eq 'boolean') { 
     152        return 'boolean'; 
     153    } elsif ($type eq 'datetime') { 
     154        return 'datetime'; 
     155    } elsif ($type eq 'timestamp') { 
     156        return 'timestamp'; 
     157    } elsif ($type eq 'integer') { 
     158        return 'integer'; 
     159    } elsif ($type eq 'blob') { 
     160        return 'blob'; 
     161    } elsif ($type eq 'text') { 
     162        return 'text'; 
     163    } elsif ($type eq 'float') { 
     164        return 'float'; 
     165    } 
     166    Carp::croak("undefined type: ". $type); 
    160167} 
    161168 
  • branches/release-41/t/ddl-tests.pl

    r2571 r2705  
    112112    my $self = shift; 
    113113 
    114     $self->init_testdb(); 
    115  
    116114    my $driver    = MT::Object->dbi_driver; 
    117115    my $dbh       = $driver->rw_handle; 
     
    312310    ok($defs->{baz}, 'Ddltest::Fixable table has baz column after creation'); 
    313311 
    314     my $sql = $ddl_class->drop_column_sql('Ddltest::Fixable', 'baz'); 
    315     ok($sql, 'Ddltest::Fixable can have column dropping sql'); 
    316     my $res = $dbh->do($sql); 
    317     ok($res, 'Ddltest::Fixable could have its column dropped'); 
     312    my $sql; 
     313    my $res; 
     314 
     315    SKIP: { 
     316        skip("Driver cannot drop columns", 2) unless $ddl_class->can_drop_column; 
     317        $sql = $ddl_class->drop_column_sql('Ddltest::Fixable', 'baz'); 
     318        ok($sql, 'Ddltest::Fixable can have column dropping sql'); 
     319        $res = $dbh->do($sql); 
     320        ok($res, 'Ddltest::Fixable could have its column dropped'); 
     321    } 
    318322 
    319323    { 
     
    327331 
    328332    $defs = $ddl_class->column_defs('Ddltest::Fixable'); 
    329     ok(!$defs->{baz},  'Ddltest::Fixable did indeed have a column dropped'); 
     333    SKIP: { 
     334        skip("Driver cannot drop columns", 1) unless $ddl_class->can_drop_column; 
     335        ok(!$defs->{baz},  'Ddltest::Fixable did indeed have a column dropped'); 
     336    } 
    330337    ok( $defs->{borf}, 'Ddltest::Fixable did indeed have a column added'); 
    331338