PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ validate_remote_loader_columns()

static int validate_remote_loader_columns ( SHPLOADERCONFIG config,
PGresult *  result 
)
static

Definition at line 1150 of file shp2pgsql-gui.c.

References _, shp_loader_state::field_names, shp_loader_state::num_fields, shp_loader_config::opt, pgui_logf(), pgui_seterr(), shp_loader_config::schema, shp_loader_config::shp_file, ShpLoaderCreate(), ShpLoaderDestroy(), SHPLOADERERR, SHPLOADEROK, ShpLoaderOpenShape(), SHPLOADERWARN, shp_loader_config::table, shp_loader_state::types, and validate_shape_column_against_pg_column().

Referenced by pgui_action_import().

1151 {
1152  ExecStatusType status;
1153  SHPLOADERSTATE *state;
1154  int ntuples;
1155  char *pg_fieldname, *pg_fieldtype;
1156  int ret, i, j, found, response = SHPLOADEROK;
1157 
1158  /* Check the status of the result set */
1159  status = PQresultStatus(result);
1160  if (status == PGRES_TUPLES_OK)
1161  {
1162  ntuples = PQntuples(result);
1163 
1164  switch (config->opt)
1165  {
1166  case 'c':
1167  /* If we have a row matching the table given in the config, then it already exists */
1168  if (ntuples > 0)
1169  {
1170  pgui_seterr(_("ERROR: Create mode selected for existing table: %s.%s"), config->schema, config->table);
1171  response = SHPLOADERERR;
1172  }
1173  break;
1174 
1175  case 'p':
1176  /* If we have a row matching the table given in the config, then it already exists */
1177  if (ntuples > 0)
1178  {
1179  pgui_seterr(_("ERROR: Prepare mode selected for existing table: %s.%s"), config->schema, config->table);
1180  response = SHPLOADERERR;
1181  }
1182  break;
1183 
1184  case 'a':
1185  /* If we are trying to append to a table but it doesn't exist, emit a warning */
1186  if (ntuples == 0)
1187  {
1188  pgui_seterr(_("ERROR: Destination table %s.%s could not be found for appending"), config->schema, config->table);
1189  response = SHPLOADERERR;
1190  }
1191  else
1192  {
1193  /* If we have a row then lets do some simple column validation... */
1194  state = ShpLoaderCreate(config);
1195  ret = ShpLoaderOpenShape(state);
1196  if (ret != SHPLOADEROK)
1197  {
1198  pgui_logf(_("Warning: Could not load shapefile %s"), config->shp_file);
1199  ShpLoaderDestroy(state);
1200  }
1201 
1202  /* Find each column based upon its name and then validate type separately... */
1203  for (i = 0; i < state->num_fields; i++)
1204  {
1205  /* Make sure we find a column */
1206  found = 0;
1207  for (j = 0; j < ntuples; j++)
1208  {
1209  pg_fieldname = PQgetvalue(result, j, PQfnumber(result, "field"));
1210  pg_fieldtype = PQgetvalue(result, j, PQfnumber(result, "type"));
1211 
1212  if (!strcmp(state->field_names[i], pg_fieldname))
1213  {
1214  found = -1;
1215 
1216  ret = validate_shape_column_against_pg_column(state->types[i], pg_fieldtype);
1217  if (!ret)
1218  {
1219  pgui_logf(_("Warning: DBF Field '%s' is not compatible with PostgreSQL column '%s' in %s.%s"), state->field_names[i], pg_fieldname, config->schema, config->table);
1220  response = SHPLOADERWARN;
1221  }
1222  }
1223  }
1224 
1225  /* Flag a warning if we can't find a match */
1226  if (!found)
1227  {
1228  pgui_logf(_("Warning: DBF Field '%s' within file %s could not be matched to a column within table %s.%s"),
1229  state->field_names[i], config->shp_file, config->schema, config->table);
1230  response = SHPLOADERWARN;
1231  }
1232  }
1233 
1234  ShpLoaderDestroy(state);
1235  }
1236 
1237  break;
1238  }
1239  }
1240  else
1241  {
1242  pgui_seterr(_("ERROR: unable to process validation response from remote server"));
1243  response = SHPLOADERERR;
1244  }
1245 
1246  return response;
1247 }
DBFFieldType * types
int ShpLoaderOpenShape(SHPLOADERSTATE *state)
#define _(String)
Definition: shpcommon.h:24
static void pgui_logf(const char *fmt,...)
#define SHPLOADERWARN
void ShpLoaderDestroy(SHPLOADERSTATE *state)
static void pgui_seterr(const char *fmt,...)
SHPLOADERSTATE * ShpLoaderCreate(SHPLOADERCONFIG *config)
#define SHPLOADEROK
static int validate_shape_column_against_pg_column(int dbf_fieldtype, char *pg_fieldtype)
#define SHPLOADERERR
Here is the call graph for this function:
Here is the caller graph for this function: