From: Cedric Carree Date: Tue, 22 Nov 2011 09:55:53 +0000 (+0100) Subject: Patch to get correct SQL data types from Postgres X-Git-Tag: v0.11011~83 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a23f9a9db103d35a5f953033e5ab18ad65aa1fb2;p=dbsrgits%2FSQL-Translator.git Patch to get correct SQL data types from Postgres Patch submitted by Andrew Pam --- diff --git a/AUTHORS b/AUTHORS index 3e069da..c0775c7 100644 --- a/AUTHORS +++ b/AUTHORS @@ -2,8 +2,10 @@ The following people have contributed to the SQLFairy project: - Alexander Hartmaier - Allen Day +- Amiri Barksdale - Anders Nor Berle - Andrew Moore +- Andrew Pam - Arthur Axel "fREW" Schmidt - Ben Faga - Cedric Carree @@ -36,7 +38,6 @@ The following people have contributed to the SQLFairy project: - Vincent Bachelier - Wallace Reis - Ying Zhang -- Amiri Barksdale If you would like to contribute to the project, you can send patches to the developers mailing list: diff --git a/Changes b/Changes index 29dda64..ec55465 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,5 @@ +* Correct Data Type in SQLT::Parser::DBI::PostgreSQL (patch from Andrew Pam) + # ---------------------------------------------------------- # 0.11010 2011-10-05 # ---------------------------------------------------------- diff --git a/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm b/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm index 525c72c..a6dc3e8 100644 --- a/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm +++ b/lib/SQL/Translator/Parser/DBI/PostgreSQL.pm @@ -55,8 +55,8 @@ sub parse { my $schema = $tr->schema; my $column_select = $dbh->prepare( - "SELECT a.attname, t.typname, a.attnum,a.atttypmod as length, - a.attnotnull, a.atthasdef, d.adsrc + "SELECT a.attname, format_type(t.oid, a.atttypmod) as typname, a.attnum, + a.atttypmod as length, a.attnotnull, a.atthasdef, d.adsrc FROM pg_type t,pg_attribute a LEFT JOIN pg_attrdef d ON (d.adrelid = a.attrelid AND a.attnum = d.adnum) WHERE a.attrelid=? AND attnum>0 @@ -143,7 +143,8 @@ ORDER BY 1; order => $$columnhash{'attnum'}, ) || die $table->error; - $col->{size} = [$$columnhash{'length'}] if $$columnhash{'length'}>0; + $col->{size} = [$$columnhash{'length'}] + if $$columnhash{'length'}>0 && $$columnhash{'length'}<=0xFFFF; $col->{is_nullable} = $$columnhash{'attnotnull'} ? 0 : 1; } diff --git a/t/66-postgres-dbi-parser.t b/t/66-postgres-dbi-parser.t index 024c6a2..a37050c 100644 --- a/t/66-postgres-dbi-parser.t +++ b/t/66-postgres-dbi-parser.t @@ -40,7 +40,7 @@ my $sql = q[ create table sqlt_test1 ( f_serial serial NOT NULL primary key, - f_varchar character varying (255), + f_varchar character varying(255), f_text text default 'FOO' ); @@ -77,8 +77,7 @@ is( scalar @t1_fields, 3, '3 fields in sqlt_test1' ); my $f1 = shift @t1_fields; is( $f1->name, 'f_serial', 'First field is "f_serial"' ); -#FIXME: it should better be 'INTEGER' instead of 'int4' -is( $f1->data_type, 'int4', 'Field is an integer' ); +is( $f1->data_type, 'integer', 'Field is an integer' ); is( $f1->is_nullable, 0, 'Field cannot be null' ); is( $f1->default_value, "nextval('sqlt_test1_f_serial_seq'::regclass)", 'Default value is nextval()' ); is( $f1->is_primary_key, 1, 'Field is PK' ); @@ -87,7 +86,7 @@ is( $f1->is_primary_key, 1, 'Field is PK' ); my $f2 = shift @t1_fields; is( $f2->name, 'f_varchar', 'Second field is "f_varchar"' ); -is( $f2->data_type, 'varchar', 'Field is a varchar' ); +is( $f2->data_type, 'character varying(255)', 'Field is a character varying(255)' ); is( $f2->is_nullable, 1, 'Field can be null' ); #FIXME: should not be 255? is( $f2->size, 259, 'Size is "259"' ); @@ -114,7 +113,7 @@ is( scalar @t2_fields, 3, '3 fields in sqlt_test2' ); my $t2_f1 = shift @t2_fields; is( $t2_f1->name, 'f_id', 'First field is "f_id"' ); -is( $t2_f1->data_type, 'int4', 'Field is an integer' ); +is( $t2_f1->data_type, 'integer', 'Field is an integer' ); is( $t2_f1->is_nullable, 0, 'Field cannot be null' ); is( $t2_f1->size, 0, 'Size is "0"' ); is( $t2_f1->default_value, undef, 'Default value is undefined' ); @@ -122,7 +121,7 @@ is( $t2_f1->is_primary_key, 1, 'Field is PK' ); my $t2_f2= shift @t2_fields; is( $t2_f2->name, 'f_int', 'Third field is "f_int"' ); -is( $t2_f2->data_type, 'int2', 'Field is an integer' ); +is( $t2_f2->data_type, 'smallint', 'Field is an smallint' ); is( $t2_f2->is_nullable, 1, 'Field can be null' ); is( $t2_f2->size, 0, 'Size is "0"' ); is( $t2_f2->default_value, undef, 'Default value is undefined' ); @@ -130,7 +129,7 @@ is( $t2_f2->is_primary_key, 0, 'Field is not PK' ); my $t2_f3 = shift @t2_fields; is( $t2_f3->name, 'f_fk1', 'Third field is "f_fk1"' ); -is( $t2_f3->data_type, 'int4', 'Field is an integer' ); +is( $t2_f3->data_type, 'integer', 'Field is an integer' ); is( $t2_f3->is_nullable, 0, 'Field cannot be null' ); is( $t2_f3->size, 0, 'Size is "0"' ); is( $t2_f3->default_value, undef, 'Default value is undefined' );