Changeset 2705
- Timestamp:
- 07/03/08 23:42:42 (16 months ago)
- Location:
- branches/release-41
- Files:
-
- 2 modified
-
lib/MT/ObjectDriver/DDL/SQLite.pm (modified) (4 diffs)
-
t/ddl-tests.pl (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/release-41/lib/MT/ObjectDriver/DDL/SQLite.pm
r2700 r2705 90 90 my $table_name = $class->table_name; 91 91 my $field_prefix = $class->datasource; 92 my $props = $class->properties;93 my $obj_defs = $class->column_defs;94 92 95 93 return undef unless $dbh; … … 98 96 # may not actually exist (in which case, the return value is undef, 99 97 # 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 . '")') 102 100 or return undef; 103 101 $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 #}112 102 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}; 115 105 $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+)\)/) { 118 108 $defs->{$colname}{size} = $1; 119 109 } … … 122 112 $defs->{$colname}{key} = 1; 123 113 } 124 if ( $coltype eq 'integer' && $defs->{$colname}{key} ) {114 if ( ($coltype eq 'integer') && $row->{pk} ) { 125 115 # with sqlite, integer primary keys auto increment. always. 116 $defs->{$colname}{key} = 1; 126 117 $defs->{$colname}{auto} = 1; 127 118 } 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}; 137 123 } 138 124 $sth->finish; 125 return undef unless %$defs; 139 126 return $defs; 140 127 } … … 153 140 my $ddl = shift; 154 141 my ($def) = @_; 142 return undef if !defined $def; 155 143 my $type = (ref($def) eq 'HASH') ? $def->{type} : $def; 144 $type = $def->{type}; 156 145 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); 160 167 } 161 168 -
branches/release-41/t/ddl-tests.pl
r2571 r2705 112 112 my $self = shift; 113 113 114 $self->init_testdb();115 116 114 my $driver = MT::Object->dbi_driver; 117 115 my $dbh = $driver->rw_handle; … … 312 310 ok($defs->{baz}, 'Ddltest::Fixable table has baz column after creation'); 313 311 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 } 318 322 319 323 { … … 327 331 328 332 $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 } 330 337 ok( $defs->{borf}, 'Ddltest::Fixable did indeed have a column added'); 331 338
