PostGIS  2.2.7dev-r@@SVN_REVISION@@
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 877 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().

882  {
883  char *fn = NULL;
884  uint32_t len = 0;
885  char *sql = NULL;
886  uint32_t x = 0;
887 
888  assert(table != NULL);
889  assert(column != NULL);
890 
891  /* COPY statements */
892  if (copy_statements) {
893 
894  if (!copy_from(
895  schema, table, column,
896  (file_column_name ? filename : NULL), file_column_name,
897  buffer
898  )) {
899  rterror(_("insert_records: Could not add COPY statement to string buffer"));
900  return 0;
901  }
902 
903 
904  /* escape tabs in filename */
905  if (filename != NULL)
906  fn = strreplace(filename, "\t", "\\t", NULL);
907 
908  /* rows */
909  for (x = 0; x < tileset->length; x++) {
910  len = strlen(tileset->line[x]) + 1;
911 
912  if (filename != NULL)
913  len += strlen(fn) + 1;
914 
915  sql = rtalloc(sizeof(char) * len);
916  if (sql == NULL) {
917  rterror(_("insert_records: Could not allocate memory for COPY statement"));
918  return 0;
919  }
920  sprintf(sql, "%s%s%s",
921  tileset->line[x],
922  (filename != NULL ? "\t" : ""),
923  (filename != NULL ? fn : "")
924  );
925 
926  append_sql_to_buffer(buffer, sql);
927  sql = NULL;
928  }
929 
930  if (!copy_from_end(buffer)) {
931  rterror(_("process_rasters: Could not add COPY end statement to string buffer"));
932  return 0;
933  }
934 
935  }
936  /* INSERT statements */
937  else {
938  len = strlen("INSERT INTO () VALUES (ST_Transform(''::raster,xxxxxxxxx));") + 1;
939  if (schema != NULL)
940  len += strlen(schema);
941  len += strlen(table);
942  len += strlen(column);
943  if (filename != NULL)
944  len += strlen(",") + strlen(file_column_name);
945 
946  /* escape single-quotes in filename */
947  if (filename != NULL)
948  fn = strreplace(filename, "'", "''", NULL);
949 
950  for (x = 0; x < tileset->length; x++) {
951  char *ptr;
952  int sqllen = len;
953 
954  sqllen += strlen(tileset->line[x]);
955  if (filename != NULL)
956  sqllen += strlen(",''") + strlen(fn);
957 
958  sql = rtalloc(sizeof(char) * sqllen);
959  if (sql == NULL) {
960  rterror(_("insert_records: Could not allocate memory for INSERT statement"));
961  return 0;
962  }
963  ptr = sql;
964  ptr += sprintf(sql, "INSERT INTO %s%s (%s%s%s) VALUES (",
965  (schema != NULL ? schema : ""),
966  table,
967  column,
968  (filename != NULL ? "," : ""),
969  (filename != NULL ? file_column_name : "")
970  );
971  if (out_srid != SRID_UNKNOWN) {
972  ptr += sprintf(ptr, "ST_Transform(");
973  }
974  ptr += sprintf(ptr, "'%s'::raster",
975  tileset->line[x]
976  );
977  if (out_srid != SRID_UNKNOWN) {
978  ptr += sprintf(ptr, ", %d)", out_srid);
979  }
980  if (filename != NULL) {
981  ptr += sprintf(ptr, ",'%s'", fn);
982  }
983  ptr += sprintf(ptr, ");");
984 
985  append_sql_to_buffer(buffer, sql);
986  sql = NULL;
987  }
988  }
989 
990  if (fn != NULL) rtdealloc(fn);
991  return 1;
992 }
#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
static int append_sql_to_buffer(STRINGBUFFER *buffer, const char *str)
Definition: raster2pgsql.c:822
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:172
tuple x
Definition: pixval.py:53
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:830
static int copy_from_end(STRINGBUFFER *buffer)
Definition: raster2pgsql.c:868

Here is the call graph for this function:

Here is the caller graph for this function: