83 size_t column_defs_total_len;
84 char *column_defs_str;
91 schema_input = PG_GETARG_TEXT_P(0);
92 schema = text_to_cstring(schema_input);
94 table_input = PG_GETARG_TEXT_P(1);
95 table = text_to_cstring(table_input);
97 data = PG_GETARG_BYTEA_PP(2);
99 ctx = palloc0(
sizeof(*
ctx));
100 ctx->ctx = palloc0(
sizeof(flatgeobuf_ctx));
101 ctx->ctx->size = VARSIZE_ANY_EXHDR(
data);
102 POSTGIS_DEBUGF(3,
"bytea data size is %ld",
ctx->ctx->size);
104 memcpy(
ctx->ctx->buf, VARDATA_ANY(
data),
ctx->ctx->size);
105 ctx->ctx->offset = 0;
108 flatgeobuf_decode_header(
ctx->ctx);
110 column_defs = palloc(
sizeof(
char *) *
ctx->ctx->columns_size);
111 column_defs_total_len = 0;
112 POSTGIS_DEBUGF(2,
"found %d columns",
ctx->ctx->columns_size);
113 for (i = 0; i <
ctx->ctx->columns_size; i++) {
114 flatgeobuf_column *column =
ctx->ctx->columns[i];
115 const char *name = column->name;
116 uint8_t column_type = column->type;
118 size_t len = strlen(name) + 1 + strlen(pgtype) + 1;
119 column_defs[i] = palloc0(
sizeof(
char) * len);
120 strcat(column_defs[i], name);
121 strcat(column_defs[i],
" ");
122 strcat(column_defs[i], pgtype);
123 column_defs_total_len += len;
125 column_defs_str = palloc0(
sizeof(
char) * column_defs_total_len + (
ctx->ctx->columns_size * 2) + 2 + 1);
126 if (
ctx->ctx->columns_size > 0)
127 strcat(column_defs_str,
", ");
128 for (i = 0; i <
ctx->ctx->columns_size; i++) {
129 strcat(column_defs_str, column_defs[i]);
130 if (i < ctx->
ctx->columns_size - 1)
131 strcat(column_defs_str,
", ");
134 POSTGIS_DEBUGF(2,
"column_defs_str %s", column_defs_str);
136 format =
"create table %s.%s (id int, geom geometry%s)";
137 sql = palloc0(strlen(format) + strlen(schema) + strlen(table) + strlen(column_defs_str) + 1);
139 sprintf(
sql, format, schema, table, column_defs_str);
141 POSTGIS_DEBUGF(3,
"sql: %s",
sql);
143 if (SPI_connect() != SPI_OK_CONNECT)
144 elog(ERROR,
"Failed to connect SPI");
146 if (SPI_execute(
sql,
false, 0) != SPI_OK_UTILITY)
147 elog(ERROR,
"Failed to create table");
149 if (SPI_finish() != SPI_OK_FINISH)
150 elog(ERROR,
"Failed to finish SPI");
152 POSTGIS_DEBUG(3,
"finished");
void flatgeobuf_check_magicbytes(struct flatgeobuf_decode_ctx *ctx)
void * lwalloc(size_t size)
static char * get_pgtype(uint8_t column_type)