Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nNLt2-0007dT-Bn for pgsql-hackers@arkaria.postgresql.org; Thu, 24 Feb 2022 21:42:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nNLt0-0001pB-1Q for pgsql-hackers@arkaria.postgresql.org; Thu, 24 Feb 2022 21:42:54 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nNLsz-0001p2-Ly for pgsql-hackers@lists.postgresql.org; Thu, 24 Feb 2022 21:42:53 +0000 Received: from mail-qt1-x832.google.com ([2607:f8b0:4864:20::832]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nNLsr-0003wI-Pf for pgsql-hackers@lists.postgresql.org; Thu, 24 Feb 2022 21:42:52 +0000 Received: by mail-qt1-x832.google.com with SMTP id bt3so771858qtb.0 for ; Thu, 24 Feb 2022 13:42:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:subject:message-id:mime-version:content-disposition; bh=2pl772+QUsqcjeqmN8sLoyKvtQAZOqiXu/E/DNwcqBg=; b=et34BJyUjxXMIhp6Xr7sIm9l/iklVwoaHkk3JS9xNfyHUS+qUL/gNg9NBPh7+DRMdI njqXnzwzU67VvwZjLJz96ytPd3pfFwlK+NPklcp/bmY6yVy6dff+veJ55z4QLsFucEWI 6rLNwgGPFezkFB+RkJlg2F78xvAZmiZohp8B2Pw2r7P4bQYI7dJf6zrxZ2GJ/ncKn133 6c2lDljUOfjXcO/pYIpB1S2vPf6qKd0umDIlUE78Qgwd6yMWKT8XBQD5Ygs7MrIXEeA8 Yr+eiDHildCpSua1ZroAg+c2fCj1C33Oz5qPXYpZKwXAxsx55R4bgu85YnNwcjHgFalv evhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition; bh=2pl772+QUsqcjeqmN8sLoyKvtQAZOqiXu/E/DNwcqBg=; b=la+iTvYAlGnE/FMZDXsSPCtiw3RiR9Kq7IaL0gm5AHLBYeNzmh3ijnbQ3P2y0k0c1E VORdHVZQHhPWlTSNPckDubX+u2kFaQgqEMWwE2M/bZCGT+iOpZGmnYQ59Mm0EocRheN6 PawhdEnaicaSTbeVZbIcAo9a3c8FIiJHTFL4rpHEIdeppe+28Z/J+kqCaTxqfXZC5ufk BsOn354eAGjAWL3T/fWcl4+2/gSCaMS9FRqT4w6W4/XL6sJoae8HVrJgzLy4/TRn3Vgo 0FXbF8COPYYfmIR6U6OXZhpENNC3rkIh9wGEN0zlIDwuv18sM8IS1vwqfRtUiHIeC8MR KmwQ== X-Gm-Message-State: AOAM530PDsd26mBkjDTypPQWlK0EbtmGli8hCso0YOQnBMXS2CVwbQQ+ nRZn4JWRPl6ssvqktbfboYcnavO15BU= X-Google-Smtp-Source: ABdhPJz2GatWnwshUQ/fX9hpHhWMepT4nNhj0qzjeL6+qKDrEOOgH66FF9VLIoZQkyJu1NAgCHAbow== X-Received: by 2002:ac8:7d51:0:b0:2de:4c77:d775 with SMTP id h17-20020ac87d51000000b002de4c77d775mr4254146qtb.507.1645738964119; Thu, 24 Feb 2022 13:42:44 -0800 (PST) Received: from workstation-mark-wong (75-164-130-196.ptld.qwest.net. [75.164.130.196]) by smtp.gmail.com with ESMTPSA id f14-20020a05620a12ee00b00508b2c61482sm373895qkl.25.2022.02.24.13.42.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Feb 2022 13:42:43 -0800 (PST) Date: Thu, 24 Feb 2022 13:42:40 -0800 From: Mark Wong To: pgsql-hackers@lists.postgresql.org Subject: real/float example for testlibpq3 Message-ID: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="kIX80AVWZIsFbvgR" Content-Disposition: inline List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --kIX80AVWZIsFbvgR Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi everyone, Would adding additional examples to testlibpq3.c on handling more data types be helpful? I've attached a patch adding how to handle a REAL to current example. Regards, Mark --kIX80AVWZIsFbvgR Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="testlibpq3-real-v1.patch" diff --git a/src/test/examples/testlibpq3.c b/src/test/examples/testlibpq3.c index 4f7b791388..0c1739fcbb 100644 --- a/src/test/examples/testlibpq3.c +++ b/src/test/examples/testlibpq3.c @@ -11,19 +11,21 @@ * CREATE SCHEMA testlibpq3; * SET search_path = testlibpq3; * SET standard_conforming_strings = ON; - * CREATE TABLE test1 (i int4, t text, b bytea); - * INSERT INTO test1 values (1, 'joe''s place', '\000\001\002\003\004'); - * INSERT INTO test1 values (2, 'ho there', '\004\003\002\001\000'); + * CREATE TABLE test1 (i int4, r real, t text, b bytea); + * INSERT INTO test1 values (1, 2.3, 'joe''s place', '\000\001\002\003\004'); + * INSERT INTO test1 values (2, 4.5 'ho there', '\004\003\002\001\000'); * * The expected output is: * * tuple 0: got * i = (4 bytes) 1 + * r = (4 bytes) 2.3 * t = (11 bytes) 'joe's place' * b = (5 bytes) \000\001\002\003\004 * * tuple 0: got * i = (4 bytes) 2 + * r = (4 bytes) 4.5 * t = (8 bytes) 'ho there' * b = (5 bytes) \004\003\002\001\000 */ @@ -62,32 +64,41 @@ show_binary_results(PGresult *res) int i, j; int i_fnum, + r_fnum, t_fnum, b_fnum; /* Use PQfnumber to avoid assumptions about field order in result */ i_fnum = PQfnumber(res, "i"); + r_fnum = PQfnumber(res, "r"); t_fnum = PQfnumber(res, "t"); b_fnum = PQfnumber(res, "b"); for (i = 0; i < PQntuples(res); i++) { char *iptr; + char *rptr; char *tptr; char *bptr; int blen; int ival; + union { + int ival; + float fval; + } rval; /* Get the field values (we ignore possibility they are null!) */ iptr = PQgetvalue(res, i, i_fnum); + rptr = PQgetvalue(res, i, r_fnum); tptr = PQgetvalue(res, i, t_fnum); bptr = PQgetvalue(res, i, b_fnum); /* - * The binary representation of INT4 is in network byte order, which - * we'd better coerce to the local byte order. + * The binary representation of INT4 and REAL are in network byte + * order, which we'd better coerce to the local byte order. */ ival = ntohl(*((uint32_t *) iptr)); + rval.ival = ntohl(*((uint32_t *) rptr)); /* * The binary representation of TEXT is, well, text, and since libpq @@ -102,6 +113,8 @@ show_binary_results(PGresult *res) printf("tuple %d: got\n", i); printf(" i = (%d bytes) %d\n", PQgetlength(res, i, i_fnum), ival); + printf(" r = (%d bytes) %f\n", + PQgetlength(res, i, r_fnum), rval.fval); printf(" t = (%d bytes) '%s'\n", PQgetlength(res, i, t_fnum), tptr); printf(" b = (%d bytes) ", blen); diff --git a/src/test/examples/testlibpq3.sql b/src/test/examples/testlibpq3.sql index 35a95ca347..e3a70cec27 100644 --- a/src/test/examples/testlibpq3.sql +++ b/src/test/examples/testlibpq3.sql @@ -1,6 +1,6 @@ CREATE SCHEMA testlibpq3; SET search_path = testlibpq3; SET standard_conforming_strings = ON; -CREATE TABLE test1 (i int4, t text, b bytea); -INSERT INTO test1 values (1, 'joe''s place', '\000\001\002\003\004'); -INSERT INTO test1 values (2, 'ho there', '\004\003\002\001\000'); +CREATE TABLE test1 (i int4, r real, t text, b bytea); +INSERT INTO test1 values (1, 2.3, 'joe''s place', '\000\001\002\003\004'); +INSERT INTO test1 values (2, 4.5, 'ho there', '\004\003\002\001\000'); --kIX80AVWZIsFbvgR--