Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

osm2pgsql: Allow printf style arguments to pgsql_exec(). Use table sp…

…ecific temporary name during final data indexing

git-svn-id: http://svn.openstreetmap.org/applications/utils/export/osm2pgsql@8109 b9d5c4c9-76e1-0310-9c85-f3177eceb1e4
  • Loading branch information...
commit d3a7f9128735c4dd2616598150134b8a72b9992a 1 parent 471fd4c
jonb authored
Showing with 63 additions and 55 deletions.
  1. +8 −8 middle-pgsql.c
  2. +15 −44 output-pgsql.c
  3. +39 −2 pgsql.c
  4. +1 −1  pgsql.h
View
16 middle-pgsql.c
@@ -539,7 +539,7 @@ int pgsql_endCopy( enum table_id i )
}
PQclear(res);
if (tables[i].analyze) {
- pgsql_exec(sql_conn, tables[i].analyze, PGRES_COMMAND_OK);
+ pgsql_exec(sql_conn, PGRES_COMMAND_OK, "%s", tables[i].analyze);
}
tables[i].copyMode = 0;
}
@@ -736,7 +736,7 @@ static void pgsql_analyze(void)
PGconn *sql_conn = sql_conns[i];
if (tables[i].analyze) {
- pgsql_exec(sql_conn, tables[i].analyze, PGRES_COMMAND_OK );
+ pgsql_exec(sql_conn, PGRES_COMMAND_OK, "%s", tables[i].analyze);
}
}
}
@@ -750,7 +750,7 @@ static void pgsql_end(void)
// Commit transaction
if (tables[i].stop) {
- pgsql_exec(sql_conn, tables[i].stop, PGRES_COMMAND_OK);
+ pgsql_exec(sql_conn, PGRES_COMMAND_OK, "%s", tables[i].stop);
}
}
@@ -813,19 +813,19 @@ static int pgsql_start(const struct output_options *options)
}
if (tables[i].start) {
- pgsql_exec(sql_conn, tables[i].start, PGRES_COMMAND_OK);
+ pgsql_exec(sql_conn, PGRES_COMMAND_OK, "%s", tables[i].start);
}
if (dropcreate && tables[i].create) {
- pgsql_exec(sql_conn, tables[i].create, PGRES_COMMAND_OK);
+ pgsql_exec(sql_conn, PGRES_COMMAND_OK, "%s", tables[i].create);
}
if (tables[i].prepare) {
- pgsql_exec(sql_conn, tables[i].prepare, PGRES_COMMAND_OK);
+ pgsql_exec(sql_conn, PGRES_COMMAND_OK, "%s", tables[i].prepare);
}
if (tables[i].copy) {
- pgsql_exec(sql_conn, tables[i].copy, PGRES_COPY_IN);
+ pgsql_exec(sql_conn, PGRES_COPY_IN, "%s", tables[i].copy);
tables[i].copyMode = 1;
}
}
@@ -854,7 +854,7 @@ static void pgsql_stop(void)
pgsql_endCopy(i);
sql_conn = sql_conns[i];
if (tables[i].stop) {
- pgsql_exec(sql_conn, tables[i].stop, PGRES_COMMAND_OK);
+ pgsql_exec(sql_conn, PGRES_COMMAND_OK, "%s", tables[i].stop);
}
PQfinish(sql_conn);
sql_conns[i] = NULL;
View
59 output-pgsql.c
@@ -867,7 +867,7 @@ static int pgsql_out_start(const struct output_options *options)
PQclear(res); /* Will be an error if table does not exist */
}
- pgsql_exec(sql_conn, "BEGIN", PGRES_COMMAND_OK);
+ pgsql_exec(sql_conn, PGRES_COMMAND_OK, "BEGIN");
enum OsmType type = (i == t_point)?OSMTYPE_NODE:OSMTYPE_WAY;
int numTags = exportListCount[type];
@@ -881,18 +881,15 @@ static int pgsql_out_start(const struct output_options *options)
strcat(sql, tmp);
}
strcat(sql, " );\n");
- sprintf( sql + strlen(sql), "SELECT AddGeometryColumn('%s', 'way', %d, '%s', 2 );\n",
+ pgsql_exec(sql_conn, PGRES_COMMAND_OK, sql);
+ pgsql_exec(sql_conn, PGRES_TUPLES_OK, "SELECT AddGeometryColumn('%s', 'way', %d, '%s', 2 );\n",
tables[i].name, SRID, tables[i].type );
-
- pgsql_exec(sql_conn, sql, PGRES_TUPLES_OK);
- sprintf( sql, "ALTER TABLE %s ALTER COLUMN way SET NOT NULL;\n", tables[i].name);
- pgsql_exec(sql_conn, sql, PGRES_COMMAND_OK);
+ pgsql_exec(sql_conn, PGRES_COMMAND_OK, "ALTER TABLE %s ALTER COLUMN way SET NOT NULL;\n", tables[i].name);
}
- sprintf(sql, "COPY %s FROM STDIN", tables[i].name);
- pgsql_exec(sql_conn, sql, PGRES_COPY_IN);
+ pgsql_exec(sql_conn, PGRES_COPY_IN, "COPY %s FROM STDIN", tables[i].name);
}
-
+
options->mid->start(options);
return 0;
@@ -900,7 +897,6 @@ static int pgsql_out_start(const struct output_options *options)
static void *pgsql_out_stop_one(void *arg)
{
- char sql[1024];
PGresult *res;
struct s_table *table = arg;
PGconn *sql_conn = table->sql_conn;
@@ -927,40 +923,15 @@ static void *pgsql_out_stop_one(void *arg)
PQclear(res);
// Commit transaction
- pgsql_exec(sql_conn, "COMMIT", PGRES_COMMAND_OK);
-
- sql[0] = '\0';
- strcat(sql, "ANALYZE ");
- strcat(sql, table->name);
- strcat(sql, ";\n");
-
- strcat(sql, "CREATE TABLE tmp AS SELECT * FROM ");
- strcat(sql, table->name);
- strcat(sql, " ORDER BY way;\n");
-
- strcat(sql, "DROP TABLE ");
- strcat(sql, table->name);
- strcat(sql, ";\n");
-
- strcat(sql, "ALTER TABLE tmp RENAME TO ");
- strcat(sql, table->name);
- strcat(sql, ";\n");
-
- strcat(sql, "CREATE INDEX ");
- strcat(sql, table->name);
- strcat(sql, "_index ON ");
- strcat(sql, table->name);
- strcat(sql, " USING GIST (way GIST_GEOMETRY_OPS);\n");
-
- strcat(sql, "GRANT SELECT ON ");
- strcat(sql, table->name);
- strcat(sql, " TO PUBLIC;\n");
-
- strcat(sql, "ANALYZE ");
- strcat(sql, table->name);
- strcat(sql, ";\n");
-
- pgsql_exec(sql_conn, sql, PGRES_COMMAND_OK);
+ pgsql_exec(sql_conn, PGRES_COMMAND_OK, "COMMIT");
+
+ pgsql_exec(sql_conn, PGRES_COMMAND_OK, "ANALYZE %s;\n", table->name);
+ pgsql_exec(sql_conn, PGRES_COMMAND_OK, "CREATE TABLE %s_tmp AS SELECT * FROM %s ORDER BY way;\n", table->name, table->name);
+ pgsql_exec(sql_conn, PGRES_COMMAND_OK, "DROP TABLE %s;\n", table->name);
+ pgsql_exec(sql_conn, PGRES_COMMAND_OK, "ALTER TABLE %s_tmp RENAME TO %s;\n", table->name, table->name);
+ pgsql_exec(sql_conn, PGRES_COMMAND_OK, "CREATE INDEX %s_index ON %s USING GIST (way GIST_GEOMETRY_OPS);\n", table->name, table->name);
+ pgsql_exec(sql_conn, PGRES_COMMAND_OK, "GRANT SELECT ON %s TO PUBLIC;\n", table->name);
+ pgsql_exec(sql_conn, PGRES_COMMAND_OK, "ANALYZE %s;\n", table->name);
free(table->name);
return NULL;
}
View
41 pgsql.c
@@ -1,9 +1,10 @@
/* Helper functions for the postgresql connections */
#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
#include <string.h>
#include <libpq-fe.h>
-
#include "osmtypes.h" // For exit_nicely()
#include "pgsql.h"
@@ -38,18 +39,54 @@ void escape(char *out, int len, const char *in)
fprintf(stderr, "%s truncated at %d chars: %s\n%s\n", __FUNCTION__, count, old_in, old_out);
}
-int pgsql_exec(PGconn *sql_conn, const char *sql, ExecStatusType expect)
+int pgsql_exec(PGconn *sql_conn, ExecStatusType expect, const char *fmt, ...)
{
PGresult *res;
+ va_list ap;
+ char *sql, *nsql;
+ int n, size = 100;
+
+ /* Based on vprintf manual page */
+ /* Guess we need no more than 100 bytes. */
+
+ if ((sql = malloc(size)) == NULL) {
+ fprintf(stderr, "Memory allocation failed\n");
+ exit_nicely();
+ }
+
+ while (1) {
+ /* Try to print in the allocated space. */
+ va_start(ap, fmt);
+ n = vsnprintf(sql, size, fmt, ap);
+ va_end(ap);
+ /* If that worked, return the string. */
+ if (n > -1 && n < size)
+ break;
+ /* Else try again with more space. */
+ if (n > -1) /* glibc 2.1 */
+ size = n+1; /* precisely what is needed */
+ else /* glibc 2.0 */
+ size *= 2; /* twice the old size */
+ if ((nsql = realloc (sql, size)) == NULL) {
+ free(sql);
+ fprintf(stderr, "Memory re-allocation failed\n");
+ exit_nicely();
+ } else {
+ sql = nsql;
+ }
+ }
+
#ifdef DEBUG_PGSQL
fprintf( stderr, "Executing: %s\n", sql );
#endif
res = PQexec(sql_conn, sql);
if (PQresultStatus(res) != expect) {
fprintf(stderr, "%s failed: %s\n", sql, PQerrorMessage(sql_conn));
+ free(sql);
PQclear(res);
exit_nicely();
}
+ free(sql);
PQclear(res);
return 0;
}
View
2  pgsql.h
@@ -7,5 +7,5 @@
PGresult *pgsql_execPrepared( PGconn *sql_conn, const char *stmtName, int nParams, const char *const * paramValues, ExecStatusType expect);
int pgsql_CopyData(const char *context, PGconn *sql_conn, const char *sql);
-int pgsql_exec(PGconn *sql_conn, const char *sql, ExecStatusType expect);
+int pgsql_exec(PGconn *sql_conn, ExecStatusType expect, const char *fmt, ...) __attribute__ ((format (printf, 3, 4)));
void escape(char *out, int len, const char *in);
Please sign in to comment.
Something went wrong with that request. Please try again.