aboutsummaryrefslogtreecommitdiff
path: root/src/fe_utils/string_utils.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2016-08-16 15:58:30 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2016-08-16 15:58:45 -0400
commit7f61fd10ceb715eceece49451f6dfe9058044e15 (patch)
treec2d942d9af7395ecba2d085db91afa1b8acd4000 /src/fe_utils/string_utils.c
parent639166641102871e09f9c4aebc71df57566a0a4a (diff)
downloadpostgresql-7f61fd10ceb715eceece49451f6dfe9058044e15.tar.gz
postgresql-7f61fd10ceb715eceece49451f6dfe9058044e15.zip
Fix assorted places in psql to print version numbers >= 10 in new style.
This is somewhat cosmetic, since as long as you know what you are looking at, "10.0" is a serviceable substitute for "10". But there is a potential for confusion between version numbers with minor numbers and those without --- we don't want people asking "why is psql saying 10.0 when my server is 10.2". Therefore, back-patch as far as practical, which turns out to be 9.3. I could have redone the patch to use fprintf(stderr) in place of psql_error(), but it seems more work than is warranted for branches that will be EOL or nearly so by the time v10 comes out. Although only psql seems to contain any code that needs this, I chose to put the support function into fe_utils, since it seems likely we'll need it in other client programs in future. (In 9.3-9.5, use dumputils.c, the predecessor of fe_utils/string_utils.c.) In HEAD, also fix the backend code that whines about loadable-library version mismatch. I don't see much need to back-patch that.
Diffstat (limited to 'src/fe_utils/string_utils.c')
-rw-r--r--src/fe_utils/string_utils.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/fe_utils/string_utils.c b/src/fe_utils/string_utils.c
index f986dbcf39b..2c566b1ad75 100644
--- a/src/fe_utils/string_utils.c
+++ b/src/fe_utils/string_utils.c
@@ -169,6 +169,44 @@ fmtQualifiedId(int remoteVersion, const char *schema, const char *id)
/*
+ * Format a Postgres version number (in the PG_VERSION_NUM integer format
+ * returned by PQserverVersion()) as a string. This exists mainly to
+ * encapsulate knowledge about two-part vs. three-part version numbers.
+ *
+ * For re-entrancy, caller must supply the buffer the string is put in.
+ * Recommended size of the buffer is 32 bytes.
+ *
+ * Returns address of 'buf', as a notational convenience.
+ */
+char *
+formatPGVersionNumber(int version_number, bool include_minor,
+ char *buf, size_t buflen)
+{
+ if (version_number >= 100000)
+ {
+ /* New two-part style */
+ if (include_minor)
+ snprintf(buf, buflen, "%d.%d", version_number / 10000,
+ version_number % 10000);
+ else
+ snprintf(buf, buflen, "%d", version_number / 10000);
+ }
+ else
+ {
+ /* Old three-part style */
+ if (include_minor)
+ snprintf(buf, buflen, "%d.%d.%d", version_number / 10000,
+ (version_number / 100) % 100,
+ version_number % 100);
+ else
+ snprintf(buf, buflen, "%d.%d", version_number / 10000,
+ (version_number / 100) % 100);
+ }
+ return buf;
+}
+
+
+/*
* Convert a string value to an SQL string literal and append it to
* the given buffer. We assume the specified client_encoding and
* standard_conforming_strings settings.