diff options
Diffstat (limited to 'src/bin/pg_dump/pg_backup_db.c')
-rw-r--r-- | src/bin/pg_dump/pg_backup_db.c | 173 |
1 files changed, 91 insertions, 82 deletions
diff --git a/src/bin/pg_dump/pg_backup_db.c b/src/bin/pg_dump/pg_backup_db.c index 7ae61c80825..1489bfbc167 100644 --- a/src/bin/pg_dump/pg_backup_db.c +++ b/src/bin/pg_dump/pg_backup_db.c @@ -5,7 +5,7 @@ * Implements the basic DB functions used by the archiver. * * IDENTIFICATION - * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.56 2004/08/28 22:52:50 momjian Exp $ + * $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.57 2004/08/29 05:06:53 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -37,8 +37,8 @@ static void notice_processor(void *arg, const char *message); static char *_sendSQLLine(ArchiveHandle *AH, char *qry, char *eos); static char *_sendCopyLine(ArchiveHandle *AH, char *qry, char *eos); -static int _isIdentChar(unsigned char c); -static int _isDQChar(unsigned char c, int atStart); +static int _isIdentChar(unsigned char c); +static int _isDQChar(unsigned char c, int atStart); #define DB_MAX_ERR_STMT 128 @@ -304,7 +304,7 @@ static int _executeSqlCommand(ArchiveHandle *AH, PGconn *conn, PQExpBuffer qry, char *desc) { PGresult *res; - char errStmt[DB_MAX_ERR_STMT]; + char errStmt[DB_MAX_ERR_STMT]; /* fprintf(stderr, "Executing: '%s'\n\n", qry->data); */ res = PQexec(conn, qry->data); @@ -323,15 +323,16 @@ _executeSqlCommand(ArchiveHandle *AH, PGconn *conn, PQExpBuffer qry, char *desc) else { strncpy(errStmt, qry->data, DB_MAX_ERR_STMT); - if (errStmt[DB_MAX_ERR_STMT-1] != '\0') { - errStmt[DB_MAX_ERR_STMT-4] = '.'; - errStmt[DB_MAX_ERR_STMT-3] = '.'; - errStmt[DB_MAX_ERR_STMT-2] = '.'; - errStmt[DB_MAX_ERR_STMT-1] = '\0'; + if (errStmt[DB_MAX_ERR_STMT - 1] != '\0') + { + errStmt[DB_MAX_ERR_STMT - 4] = '.'; + errStmt[DB_MAX_ERR_STMT - 3] = '.'; + errStmt[DB_MAX_ERR_STMT - 2] = '.'; + errStmt[DB_MAX_ERR_STMT - 1] = '\0'; } warn_or_die_horribly(AH, modulename, "%s: %s Command was: %s\n", - desc, PQerrorMessage(AH->connection), - errStmt); + desc, PQerrorMessage(AH->connection), + errStmt); } } @@ -431,8 +432,8 @@ static char * _sendSQLLine(ArchiveHandle *AH, char *qry, char *eos) { int pos = 0; /* Current position */ - char *sqlPtr; - int consumed; + char *sqlPtr; + int consumed; int startDT = 0; /* @@ -454,22 +455,24 @@ _sendSQLLine(ArchiveHandle *AH, char *qry, char *eos) /* Loop until character consumed */ do { - /* If a character needs to be scanned in a different state, - * consumed can be set to 0 to avoid advancing. Care must - * be taken to ensure internal state is not damaged. + /* + * If a character needs to be scanned in a different state, + * consumed can be set to 0 to avoid advancing. Care must be + * taken to ensure internal state is not damaged. */ consumed = 1; switch (AH->sqlparse.state) - { - - case SQL_SCAN: /* Default state == 0, set in _allocAH */ + { + + case SQL_SCAN: /* Default state == 0, set in _allocAH */ if (qry[pos] == ';' && AH->sqlparse.braceDepth == 0) { - /* We've got the end of a statement. - * Send It & reset the buffer. + /* + * We've got the end of a statement. Send It & + * reset the buffer. */ - + /* * fprintf(stderr, " sending: '%s'\n\n", * AH->sqlBuf->data); @@ -477,28 +480,30 @@ _sendSQLLine(ArchiveHandle *AH, char *qry, char *eos) ExecuteSqlCommand(AH, AH->sqlBuf, "could not execute query", false); resetPQExpBuffer(AH->sqlBuf); AH->sqlparse.lastChar = '\0'; - + /* - * Remove any following newlines - so that embedded - * COPY commands don't get a starting newline. + * Remove any following newlines - so that + * embedded COPY commands don't get a starting + * newline. */ pos++; for (; pos < (eos - qry) && qry[pos] == '\n'; pos++); - + /* We've got our line, so exit */ return qry + pos; } else { - /* - * Look for normal boring quote chars, or dollar-quotes. We make - * the assumption that $-quotes will not have an ident character + /* + * Look for normal boring quote chars, or + * dollar-quotes. We make the assumption that + * $-quotes will not have an ident character * before them in all pg_dump output. */ - if ( qry[pos] == '"' - || qry[pos] == '\'' - || ( qry[pos] == '$' && _isIdentChar(AH->sqlparse.lastChar) == 0 ) - ) + if (qry[pos] == '"' + || qry[pos] == '\'' + || (qry[pos] == '$' && _isIdentChar(AH->sqlparse.lastChar) == 0) + ) { /* fprintf(stderr,"[startquote]\n"); */ AH->sqlparse.state = SQL_IN_QUOTE; @@ -524,18 +529,20 @@ _sendSQLLine(ArchiveHandle *AH, char *qry, char *eos) AH->sqlparse.braceDepth++; else if (qry[pos] == ')') AH->sqlparse.braceDepth--; - + AH->sqlparse.lastChar = qry[pos]; } break; - + case SQL_IN_DOLLARTAG: - - /* Like a quote, we look for a closing char *but* we only - * allow a very limited set of contained chars, and no escape chars. - * If invalid chars are found, we abort tag processing. + + /* + * Like a quote, we look for a closing char *but* we + * only allow a very limited set of contained chars, + * and no escape chars. If invalid chars are found, we + * abort tag processing. */ - + if (qry[pos] == '$') { /* fprintf(stderr,"[endquote]\n"); */ @@ -545,18 +552,21 @@ _sendSQLLine(ArchiveHandle *AH, char *qry, char *eos) } else { - if ( _isDQChar(qry[pos], startDT) ) + if (_isDQChar(qry[pos], startDT)) { /* Valid, so add */ appendPQExpBufferChar(AH->sqlparse.tagBuf, qry[pos]); } else { - /* Jump back to 'scan' state, we're not really in a tag, - * and valid tag chars do not include the various chars - * we look for in this state machine, so it's safe to just - * jump from this state back to SCAN. We set consumed = 0 - * so that this char gets rescanned in new state. + /* + * Jump back to 'scan' state, we're not really + * in a tag, and valid tag chars do not + * include the various chars we look for in + * this state machine, so it's safe to just + * jump from this state back to SCAN. We set + * consumed = 0 so that this char gets + * rescanned in new state. */ destroyPQExpBuffer(AH->sqlparse.tagBuf); AH->sqlparse.state = SQL_SCAN; @@ -565,32 +575,35 @@ _sendSQLLine(ArchiveHandle *AH, char *qry, char *eos) } startDT = 0; break; - + case SQL_IN_DOLLARQUOTE: + /* - * Comparing the entire string backwards each time is NOT efficient, - * but dollar quotes in pg_dump are small and the code is a lot simpler. + * Comparing the entire string backwards each time is + * NOT efficient, but dollar quotes in pg_dump are + * small and the code is a lot simpler. */ sqlPtr = AH->sqlBuf->data + AH->sqlBuf->len - AH->sqlparse.tagBuf->len; - - if (strncmp(AH->sqlparse.tagBuf->data, sqlPtr, AH->sqlparse.tagBuf->len) == 0) { + + if (strncmp(AH->sqlparse.tagBuf->data, sqlPtr, AH->sqlparse.tagBuf->len) == 0) + { /* End of $-quote */ AH->sqlparse.state = SQL_SCAN; destroyPQExpBuffer(AH->sqlparse.tagBuf); } break; - + case SQL_IN_SQL_COMMENT: if (qry[pos] == '\n') AH->sqlparse.state = SQL_SCAN; break; - + case SQL_IN_EXT_COMMENT: if (AH->sqlparse.lastChar == '*' && qry[pos] == '/') AH->sqlparse.state = SQL_SCAN; break; - + case SQL_IN_QUOTE: if (!AH->sqlparse.backSlash && AH->sqlparse.quoteChar == qry[pos]) @@ -600,7 +613,7 @@ _sendSQLLine(ArchiveHandle *AH, char *qry, char *eos) } else { - + if (qry[pos] == '\\') { if (AH->sqlparse.lastChar == '\\') @@ -612,13 +625,13 @@ _sendSQLLine(ArchiveHandle *AH, char *qry, char *eos) AH->sqlparse.backSlash = 0; } break; - + } } while (consumed == 0); - AH->sqlparse.lastChar = qry[pos]; - /* fprintf(stderr, "\n"); */ + AH->sqlparse.lastChar = qry[pos]; + /* fprintf(stderr, "\n"); */ } /* @@ -804,7 +817,7 @@ InsertBlobXref(ArchiveHandle *AH, Oid old, Oid new) PQExpBuffer qry = createPQExpBuffer(); appendPQExpBuffer(qry, - "INSERT INTO %s(oldOid, newOid) VALUES ('%u', '%u')", + "INSERT INTO %s(oldOid, newOid) VALUES ('%u', '%u')", BLOB_XREF_TABLE, old, new); ExecuteSqlCommand(AH, qry, "could not create large object cross-reference entry", true); @@ -864,37 +877,33 @@ CommitTransactionXref(ArchiveHandle *AH) destroyPQExpBuffer(qry); } -static int _isIdentChar(unsigned char c) +static int +_isIdentChar(unsigned char c) { - if ( (c >= 'a' && c <= 'z') - || (c >= 'A' && c <= 'Z') - || (c >= '0' && c <= '9') - || (c == '_') - || (c == '$') - || (c >= (unsigned char)'\200') /* no need to check <= \377 */ - ) - { + if ((c >= 'a' && c <= 'z') + || (c >= 'A' && c <= 'Z') + || (c >= '0' && c <= '9') + || (c == '_') + || (c == '$') + || (c >= (unsigned char) '\200') /* no need to check <= + * \377 */ + ) return 1; - } else - { return 0; - } } -static int _isDQChar(unsigned char c, int atStart) +static int +_isDQChar(unsigned char c, int atStart) { - if ( (c >= 'a' && c <= 'z') - || (c >= 'A' && c <= 'Z') - || (c == '_') - || (atStart == 0 && c >= '0' && c <= '9') - || (c >= (unsigned char)'\200') /* no need to check <= \377 */ - ) - { + if ((c >= 'a' && c <= 'z') + || (c >= 'A' && c <= 'Z') + || (c == '_') + || (atStart == 0 && c >= '0' && c <= '9') + || (c >= (unsigned char) '\200') /* no need to check <= + * \377 */ + ) return 1; - } else - { return 0; - } } |