PostGIS  2.5.0beta2dev-r@@SVN_REVISION@@

◆ insert_records()

static int insert_records ( const char *  schema,
const char *  table,
const char *  column,
const char *  filename,
const char *  file_column_name,
int  copy_statements,
int  out_srid,
STRINGBUFFER tileset,
STRINGBUFFER buffer 
)
static

Definition at line 880 of file raster2pgsql.c.

References _, append_sql_to_buffer(), copy_from(), copy_from_end(), stringbuffer_t::length, stringbuffer_t::line, rtalloc(), rtdealloc(), rterror(), rtgdalraster::sql, SRID_UNKNOWN, strreplace(), and pixval::x.

Referenced by build_overview(), convert_raster(), and process_rasters().

885  {
886  char *fn = NULL;
887  uint32_t len = 0;
888  char *sql = NULL;
889  uint32_t x = 0;
890 
891  assert(table != NULL);
892  assert(column != NULL);
893 
894  /* COPY statements */
895  if (copy_statements) {
896 
897  if (!copy_from(
898  schema, table, column,
899  (file_column_name ? filename : NULL), file_column_name,
900  buffer
901  )) {
902  rterror(_("insert_records: Could not add COPY statement to string buffer"));
903  return 0;
904  }
905 
906 
907  /* escape tabs in filename */
908  if (filename != NULL)
909  fn = strreplace(filename, "\t", "\\t", NULL);
910 
911  /* rows */
912  for (x = 0; x < tileset->length; x++) {
913  len = strlen(tileset->line[x]) + 1;
914 
915  if (filename != NULL)
916  len += strlen(fn) + 1;
917 
918  sql = rtalloc(sizeof(char) * len);
919  if (sql == NULL) {
920  rterror(_("insert_records: Could not allocate memory for COPY statement"));
921  return 0;
922  }
923  sprintf(sql, "%s%s%s",
924  tileset->line[x],
925  (filename != NULL ? "\t" : ""),
926  (filename != NULL ? fn : "")
927  );
928 
929  append_sql_to_buffer(buffer, sql);
930  sql = NULL;
931  }
932 
933  if (!copy_from_end(buffer)) {
934  rterror(_("process_rasters: Could not add COPY end statement to string buffer"));
935  return 0;
936  }
937 
938  }
939  /* INSERT statements */
940  else {
941  len = strlen("INSERT INTO () VALUES (ST_Transform(''::raster,xxxxxxxxx));") + 1;
942  if (schema != NULL)
943  len += strlen(schema);
944  len += strlen(table);
945  len += strlen(column);
946  if (filename != NULL)
947  len += strlen(",") + strlen(file_column_name);
948 
949  /* escape single-quotes in filename */
950  if (filename != NULL)
951  fn = strreplace(filename, "'", "''", NULL);
952 
953  for (x = 0; x < tileset->length; x++) {
954  char *ptr;
955  int sqllen = len;
956 
957  sqllen += strlen(tileset->line[x]);
958  if (filename != NULL)
959  sqllen += strlen(",''") + strlen(fn);
960 
961  sql = rtalloc(sizeof(char) * sqllen);
962  if (sql == NULL) {
963  rterror(_("insert_records: Could not allocate memory for INSERT statement"));
964  return 0;
965  }
966  ptr = sql;
967  ptr += sprintf(sql, "INSERT INTO %s%s (%s%s%s) VALUES (",
968  (schema != NULL ? schema : ""),
969  table,
970  column,
971  (filename != NULL ? "," : ""),
972  (filename != NULL ? file_column_name : "")
973  );
974  if (out_srid != SRID_UNKNOWN) {
975  ptr += sprintf(ptr, "ST_Transform(");
976  }
977  ptr += sprintf(ptr, "'%s'::raster",
978  tileset->line[x]
979  );
980  if (out_srid != SRID_UNKNOWN) {
981  ptr += sprintf(ptr, ", %d)", out_srid);
982  }
983  if (filename != NULL) {
984  ptr += sprintf(ptr, ",'%s'", fn);
985  }
986  ptr += sprintf(ptr, ");");
987 
988  append_sql_to_buffer(buffer, sql);
989  sql = NULL;
990  }
991  }
992 
993  if (fn != NULL) rtdealloc(fn);
994  return 1;
995 }
#define _(String)
Definition: shpcommon.h:24
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
Definition: rt_context.c:199
void * rtalloc(size_t size)
Wrappers used for managing memory.
Definition: rt_context.c:171
uint32_t length
Definition: raster2pgsql.h:196
static char * strreplace(const char *str, const char *oldstr, const char *newstr, int *count)
Definition: raster2pgsql.c:141
unsigned int uint32_t
Definition: uthash.h:78
static int append_sql_to_buffer(STRINGBUFFER *buffer, const char *str)
Definition: raster2pgsql.c:825
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:187
void rtdealloc(void *mem)
Definition: rt_context.c:186
static int copy_from(const char *schema, const char *table, const char *column, const char *filename, const char *file_column_name, STRINGBUFFER *buffer)
Definition: raster2pgsql.c:833
static int copy_from_end(STRINGBUFFER *buffer)
Definition: raster2pgsql.c:871
Here is the call graph for this function:
Here is the caller graph for this function: