]> granicus.if.org Git - postgis/commitdiff
Fixed a bug in string attributes handling truncating values of maximum
authorSandro Santilli <strk@keybit.net>
Mon, 24 Oct 2005 11:30:59 +0000 (11:30 +0000)
committerSandro Santilli <strk@keybit.net>
Mon, 24 Oct 2005 11:30:59 +0000 (11:30 +0000)
allowed length, curtesy of Lars Roessiger.
Reworked integer attributes handling to be stricter in dbf->sql mapping
and to allow for big int8 values in sql->dbf conversion

git-svn-id: http://svn.osgeo.org/postgis/trunk@1987 b70326c6-7e19-0410-871a-916f4a2858ee

loader/pgsql2shp.c
loader/shp2pgsql.c

index c23dfa33dcb6c85fcbdafc3cbec0084d30ba404a..6c2b3494ddee9a6cd7e5d372eeaed3dfeedeae20 100644 (file)
@@ -2578,10 +2578,56 @@ initialize(void)
                 * Find appropriate type of dbf attributes
                 */
 
-               /* integer type */
-               if(type == 20 || type == 21 || type == 23)
+               /* int2 type */
+               if ( type == 21 )
                {
-                       if(DBFAddField(dbf, field_name, FTInteger,16,0) == -1)
+                       /* 
+                        * Longest text representation for
+                        * an int2 type (16bit) is 6 bytes
+                        * (-32768)
+                        */
+                       if ( DBFAddField(dbf, field_name, FTInteger,
+                               6, 0) == -1 )
+                       {
+                               printf( "error - Field could not "
+                                       "be created.\n");
+                               return 0;
+                       }
+                       type_ary[mainscan_nflds]=1;
+                       mainscan_flds[mainscan_nflds++] = fname;
+                       continue;
+               }
+
+               /* int4 type */
+               if ( type == 23 )
+               {
+                       /* 
+                        * Longest text representation for
+                        * an int4 type (32bit) is 11 bytes
+                        * (-2147483648)
+                        */
+                       if ( DBFAddField(dbf, field_name, FTInteger,
+                               11, 0) == -1 )
+                       {
+                               printf( "error - Field could not "
+                                       "be created.\n");
+                               return 0;
+                       }
+                       type_ary[mainscan_nflds]=1;
+                       mainscan_flds[mainscan_nflds++] = fname;
+                       continue;
+               }
+
+               /* int8 type */
+               if ( type == 20 )
+               {
+                       /* 
+                        * Longest text representation for
+                        * an int8 type (64bit) is 20 bytes
+                        * (-9223372036854775808)
+                        */
+                       if ( DBFAddField(dbf, field_name, FTInteger,
+                               20, 0) == -1 )
                        {
                                printf( "error - Field could not "
                                        "be created.\n");
@@ -2592,7 +2638,15 @@ initialize(void)
                        continue;
                }
                
-               /* double type */
+               /*
+                * double or numeric types:
+                *    700: float4
+                *    701: float8
+                *   1700: numeric
+                *
+                *
+                * TODO: stricter handling of sizes
+                */
                if(type == 700 || type == 701 || type == 1700 )
                {
                        if(DBFAddField(dbf, field_name,FTDouble,32,10) == -1)
@@ -2625,16 +2679,20 @@ initialize(void)
                }
 
                /*
-                * For variable-sized fields we'll use max size in table
-                * as dbf field size
+                * For variable-sized fields we'll use either 
+                * maximum allowed size (atttypmod) or max actual
+                * attribute value in table.
                 */
                else if(size == -1)
                {
+                       /*
+                        * 1042 is bpchar,  1043 is varchar
+                        * mod is maximum allowed size, including
+                        * header which contains *real* size.
+                        */
                        if ( (type == 1042 || type == 1043) && mod != -1 )
                        {
-                               size = mod-3; // 4 is header size, we
-                                             // keep 1 for terminating 
-                                             // NULL.
+                               size = mod-4; // 4 is header size
                        }
                        else
                        {
@@ -3136,6 +3194,12 @@ create_usrquerytable(void)
 
 /**********************************************************************
  * $Log$
+ * Revision 1.80  2005/10/24 11:30:59  strk
+ * Fixed a bug in string attributes handling truncating values of maximum
+ * allowed length, curtesy of Lars Roessiger.
+ * Reworked integer attributes handling to be stricter in dbf->sql mapping
+ * and to allow for big int8 values in sql->dbf conversion
+ *
  * Revision 1.79  2005/10/03 18:08:55  strk
  * Stricter string attributes lenght handling. DBF header will be used
  * to set varchar maxlenght, (var)char typmod will be used to set DBF header
index 14cfc333ce71c3fa39b9d05e1144dd0e126ac2e6..5e72ca4225a3c07b3c27ea0db7f806e25539f668 100644 (file)
@@ -520,7 +520,7 @@ CreateTable(void)
                field_width = widths[j];
                field_precision = precisions[j];
 
-               printf(", \"%s\" ", field_names[j]);
+               printf(",\n\"%s\" ", field_names[j]);
 
                if(hDBFHandle->pachFieldType[j] == 'D' ) /* Date field */
                {
@@ -532,24 +532,32 @@ CreateTable(void)
 
                        if(type == FTString)
                        {
-                               // do not need space for terminating NULL
-                               printf ("varchar(%d)", field_width-1);
+                               // use DBF attribute size as maximum width
+                               printf ("varchar(%d)", field_width);
                        }
                        else if(type == FTInteger)
                        {
-                               if ( forceint4 || field_width <= 9 )
+                               if ( forceint4 )
                                {
                                        printf ("int4");
                                }
-                               else if( field_width > 18 )
+                               else if  ( field_width <= 6 )
                                {
-                                       printf("numeric(%d,0)",
-                                               field_width);
+                                       printf ("int2");
                                }
-                               else 
+                               else if  ( field_width <= 11 )
+                               {
+                                       printf ("int4");
+                               }
+                               else if  ( field_width <= 20 )
                                {
                                        printf ("int8");
                                }
+                               else 
+                               {
+                                       printf("numeric(%d,0)",
+                                               field_width);
+                               }
                        }
                        else if(type == FTDouble)
                        {
@@ -1667,6 +1675,12 @@ utf8 (const char *fromcode, char *inputbuf)
 
 /**********************************************************************
  * $Log$
+ * Revision 1.102  2005/10/24 11:30:59  strk
+ * Fixed a bug in string attributes handling truncating values of maximum
+ * allowed length, curtesy of Lars Roessiger.
+ * Reworked integer attributes handling to be stricter in dbf->sql mapping
+ * and to allow for big int8 values in sql->dbf conversion
+ *
  * Revision 1.101  2005/10/21 11:33:55  strk
  * Applied patch by Lars Roessiger handling numerical values with a trailing decima
  * l dot