int rc;
char fnamebuf[1024];
char buf[4096];
- char rex1[257];
- char rex2[257];
- char rex3[257];
- char rex4[257];
- char rex5[257];
+ size_t num_read;
FILE *stmtp;
- replacement_token replacements[4];
-
++ replacement_token replacements[5];
if (db_begin_xact(stmt, adminfo, true) < 0)
return -1;
return -1;
}
- dstring_init(&query);
-
- sprintf(rex2, "\"_%s\"", stmt->script->clustername);
-
- while (fgets(rex1, 256, stmtp) != NULL)
+ /*
+ * Initialize string objects and read the content of the SQL file
+ * into file_content.
+ */
+ dstring_init(&file_content);
+ dstring_init(&file_rewritten);
+ while (!feof(stmtp))
{
- rc = strlen(rex1);
- rex1[rc] = '\0';
- rex3[0] = '\0';
- replace_token(rex3, rex1, "@CLUSTERNAME@", stmt->script->clustername);
- replace_token(rex4, rex3, "@MODULEVERSION@", SLONY_I_VERSION_STRING);
-#define EXPAND2(x) #x
-#define EXPAND(x) EXPAND2(x)
- replace_token(rex5,rex4, "@FUNCVERSION@" , EXPAND(SLONY_I_FUNC_VERSION_STRING));
-#undef EXPAND
-#undef EXPAND2
- replace_token(buf, rex5, "@NAMESPACE@", rex2);
- rc = strlen(buf);
- dstring_nappend(&query, buf, rc);
+ num_read = fread(buf, 1, sizeof(buf), stmtp);
+ if (num_read == 0)
+ break;
+ dstring_nappend(&file_content, buf, num_read);
}
-
fclose(stmtp);
+ dstring_terminate(&file_content);
- dstring_terminate(&query);
+ /*
+ * Setup the string replacement table and call replace_tokens() to
+ * perform all the substitutions.
+ */
+ replacements[0].old_str = "@CLUSTERNAME@";
+ replacements[0].old_len = strlen(replacements[0].old_str);
+ replacements[0].new_str = stmt->script->clustername;
+ replacements[1].old_str = "@MODULEVERSION@";
+ replacements[1].old_len = strlen(replacements[1].old_str);
+ replacements[1].new_str = SLONY_I_VERSION_STRING;
+ replacements[2].old_str = "@NAMESPACE@";
+ replacements[2].old_len = strlen(replacements[2].old_str);
+ replacements[2].new_str = alloca(strlen(stmt->script->clustername) + 4);
+ sprintf(replacements[2].new_str, "\"_%s\"", stmt->script->clustername);
- replacements[3].old_str = NULL;
- replacements[3].old_len = 0;
- replacements[3].new_str = NULL;
++ replacements[3].old_str = "@FUNCVERSION@";
++ replacements[3].old_len = strlen(replacements[3].old_str);
++ replacements[3].new_str = SLONY_I_FUNC_VERSION_STRING;
++ replacements[4].old_str = NULL;
++ replacements[4].old_len = 0;
++ replacements[4].new_str = NULL;
+
+ replace_tokens(&file_rewritten, &file_content, replacements);
- res = PQexec(adminfo->dbconn, dstring_data(&query));
+ /*
+ * Execute the resulting SQL.
+ */
+ res = PQexec(adminfo->dbconn, dstring_data(&file_rewritten));
rc = PQresultStatus(res);
if ((rc != PGRES_TUPLES_OK) && (rc != PGRES_COMMAND_OK) && (rc != PGRES_EMPTY_QUERY))