PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ validate_remote_loader_columns()

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

Definition at line 1151 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().

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