72{
74 text *schema_input;
75 char *schema;
76 text *table_input;
77 char *table;
78 char *format;
81 uint16_t i;
82 char **column_defs;
83 size_t column_defs_total_len;
84 char *column_defs_str;
85
86 if (PG_ARGISNULL(0))
87 PG_RETURN_NULL();
88 if (PG_ARGISNULL(1))
89 PG_RETURN_NULL();
90
91 schema_input = PG_GETARG_TEXT_P(0);
92 schema = text_to_cstring(schema_input);
93
94 table_input = PG_GETARG_TEXT_P(1);
95 table = text_to_cstring(table_input);
96
97 data = PG_GETARG_BYTEA_PP(2);
98
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 %lld",
ctx->ctx->size);
104 memcpy(
ctx->ctx->buf, VARDATA_ANY(data),
ctx->ctx->size);
105 ctx->ctx->offset = 0;
106
108 flatgeobuf_decode_header(
ctx->ctx);
109
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;
124 }
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, ", ");
132 }
133
134 POSTGIS_DEBUGF(2, "column_defs_str %s", column_defs_str);
135
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);
138
139 sprintf(sql, format, schema, table, column_defs_str);
140
141 POSTGIS_DEBUGF(3, "sql: %s", sql);
142
143 if (SPI_connect() != SPI_OK_CONNECT)
144 elog(ERROR, "Failed to connect SPI");
145
146 if (SPI_execute(sql, false, 0) != SPI_OK_UTILITY)
147 elog(ERROR, "Failed to create table");
148
149 if (SPI_finish() != SPI_OK_FINISH)
150 elog(ERROR, "Failed to finish SPI");
151
152 POSTGIS_DEBUG(3, "finished");
153
154 PG_RETURN_NULL();
155}
void flatgeobuf_check_magicbytes(struct flatgeobuf_decode_ctx *ctx)
void * lwalloc(size_t size)
static char * get_pgtype(uint8_t column_type)