PostGIS  2.1.10dev-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,
STRINGBUFFER tileset,
STRINGBUFFER buffer 
)
static

Definition at line 828 of file raster2pgsql.c.

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

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

833  {
834  char *fn = NULL;
835  uint32_t len = 0;
836  char *sql = NULL;
837  uint32_t x = 0;
838 
839  assert(table != NULL);
840  assert(column != NULL);
841 
842  /* COPY statements */
843  if (copy_statements) {
844 
845  /* escape tabs in filename */
846  if (filename != NULL)
847  fn = strreplace(filename, "\t", "\\t", NULL);
848 
849  /* rows */
850  for (x = 0; x < tileset->length; x++) {
851  len = strlen(tileset->line[x]) + 1;
852 
853  if (filename != NULL)
854  len += strlen(fn) + 1;
855 
856  sql = rtalloc(sizeof(char) * len);
857  if (sql == NULL) {
858  rterror(_("insert_records: Could not allocate memory for COPY statement"));
859  return 0;
860  }
861  sprintf(sql, "%s%s%s",
862  tileset->line[x],
863  (filename != NULL ? "\t" : ""),
864  (filename != NULL ? fn : "")
865  );
866 
867  append_sql_to_buffer(buffer, sql);
868  rtdealloc(sql);
869  sql = NULL;
870  }
871 
872  }
873  /* INSERT statements */
874  else {
875  len = strlen("INSERT INTO () VALUES (''::raster);") + 1;
876  if (schema != NULL)
877  len += strlen(schema);
878  len += strlen(table);
879  len += strlen(column);
880  if (filename != NULL)
881  len += strlen(",") + strlen(file_column_name);
882 
883  /* escape single-quotes in filename */
884  if (filename != NULL)
885  fn = strreplace(filename, "'", "''", NULL);
886 
887  for (x = 0; x < tileset->length; x++) {
888  int sqllen = len;
889 
890  sqllen += strlen(tileset->line[x]);
891  if (filename != NULL)
892  sqllen += strlen(",''") + strlen(fn);
893 
894  sql = rtalloc(sizeof(char) * sqllen);
895  if (sql == NULL) {
896  rterror(_("insert_records: Could not allocate memory for INSERT statement"));
897  return 0;
898  }
899  sprintf(sql, "INSERT INTO %s%s (%s%s%s) VALUES ('%s'::raster%s%s%s);",
900  (schema != NULL ? schema : ""),
901  table,
902  column,
903  (filename != NULL ? "," : ""),
904  (filename != NULL ? file_column_name : ""),
905  tileset->line[x],
906  (filename != NULL ? ",'" : ""),
907  (filename != NULL ? fn : ""),
908  (filename != NULL ? "'" : "")
909  );
910 
911  append_sql_to_buffer(buffer, sql);
912  rtdealloc(sql);
913  sql = NULL;
914  }
915  }
916 
917  if (fn != NULL) rtdealloc(fn);
918  return 1;
919 }
void rtdealloc(void *mem)
Definition: rt_api.c:882
#define _(String)
Definition: shpcommon.h:23
uint32_t length
Definition: raster2pgsql.h:191
static char * strreplace(const char *str, const char *oldstr, const char *newstr, int *count)
Definition: raster2pgsql.c:142
static int append_sql_to_buffer(STRINGBUFFER *buffer, const char *str)
Definition: raster2pgsql.c:820
tuple x
Definition: pixval.py:53
void * rtalloc(size_t size)
Raster core memory management functions.
Definition: rt_api.c:867
void rterror(const char *fmt,...)
Raster core error and info handlers.
Definition: rt_api.c:895

Here is the call graph for this function:

Here is the caller graph for this function: