pqt-composites(3)

NAME

   pqt-composites - A manual for libpqtypes composite handling.

NOTE TO READER

   Please  read the pqt-specs(3) manual page prior to this document.  This
   document does not explain how to  put  or  get  data  types.   It  only
   describes how to put or get composites and composite arrays.

DESCRIPTION

   A  composite  is  put using the PGparam structure.  Each attribute of a
   composite is put into a PGparam.  When all attributes  have  been  put,
   the PGparam is put into another PGparam.  Composites must be registered
   on a per connection basis, `man pqt-handlers(3).

   To get a composite, a  PGresult  structure  is  used.   Each  composite
   attribute is a field of the result.  For non-array composites, there is
   always only one tuple.

   Composites are only handled using binary format.  This means  that  any
   type  used  as  a  composite attribute must be put and gotten in binary
   format.  If a user-defined type does not  implement  a  send  and  recv
   function in the backend, it can not be used as a composite attribute.

   Simple Composite Example
   This  example demostrates the basics of putting and getting a composite
   type.
          CREATE TYPE simple AS (a int4, t text);

          PGregisterType type = {"simple", NULL, NULL};

          /* need to register the simple composite */
          PQregisterTypes(conn, PQT_COMPOSITE, &type, 1, 0);

          /* Composite attributes are put into PGparam structures */
          PGparam *simple = PQparamCreate(conn);

          /* put the simple composite attributes */
          PQputf(simple, "%int4 %text*", 45, "foobar");

          /* Put an int4 and a simple composite */
          PGparam *param = PQparamCreate(conn);
          PQputf(param, "%int4 %simple", 10, simple);
          PQparamClear(simple);

          /* exec an insert */
          res = PQparamExec(conn, param, "INSERT INTO t VALUES($1,$2)", resfmt);
          PQparamClear(param);

          /* -------------------------
           * Getting a composite
           */

          PGint4 i4;
          PGtext textp;
          char text[80];
          PGresult *simple;

          /* Get a simple composite, provide a ptr to a PGresult ptr. */
          PQgetf(result, 0, "%simple", 0, &simple);

          /* no longer needed */
          PQclear(result);

          /* Get the simple composite attributes from the simple result.
           * Reference fields by name by using a '#' rather than a '%'.
           * The field names are the composite attributes.
           */
          PQgetf(simple, 0, "#int4 #text", "a", &i4, "t", &textp);
          strcpy(text, textp);
          PQclear(simple);

   In the above example, we used the # specifier mark to reference
   fields by their name.  The field names for a composite result object
   are the composite attribute names.

   Nested Composite example:
   The below example  puts  and  gets  a  nested  composite.   The  simple
   composite is used as an attribute within the complex composite.
          CREATE TYPE simple AS (a int4, t text)
          CREATE TYPE complex AS (f8 float8, s simple);

          /* need to register simple and complex */
          PGregisterType types[] = {
               {"simple", NULL, NULL},
               {"complex", NULL, NULL}
          };

          PQregisterTypes(conn, PQT_COMPOSITE, types, 2, 0);

          /* Composite attributes are put into PGparam structures */
          PGparam *simple = PQparamCreate(conn);
          PGparam *complex = PQparamCreate(conn);

          /* put the simple composite attributes */
          PQputf(simple, "%int4 %text*", 45, "foobar");

          /* put the complex composite attributes, which includes
           * a nested composite.
           */
          PQputf(complex, "%float8 %simple", 111.2223334, simple);

          /* no longer needed */
          PQparamClear(simple);

          /* Put an int4 and a complex composite */
          PGparam *param = PQparamCreate(conn);
          PQputf(param, "%int4 %complex", 10, complex);
          PQparamClear(complex);

          /* exec an insert */
          res = PQparamExec(conn, param, "INSERT INTO t VALUES($1,$2)", resfmt);
          PQparamClear(param);

          /* -------------------------
           * Getting a nested composite
           */

          PGfloat8 f8;
          PGint4 i4;
          PGtext textp;
          char text[80];
          PGresult *complex;
          PGresult *simple;

          /* Get the complex composite, provide a ptr to a PGresult ptr. */
          PQgetf(result, 0, "%complex", 0, &complex);

          /* no longer needed */
          PQclear(result);

          /* Get the complex composite attributes from the complex result.
           * Composite attributes are the result fields.  When getting
           * a single composite, non-array, only tuple 0 will exist.
           * For the nested simple composite, we again provide a ptr to
           * a PGresult ptr.
           */
          PQgetf(complex, 0, "%float8 %simple", 0, &f8, 1, &simple);

          /* no longer needed */
          PQclear(complex);

          /* Get the simple composite attributes from the simple result.
           * Reference fields by name by using a '#' rather than a '%'.
           */
          PQgetf(simple, 0, "#int4 #text", "a", &i4, "t", &textp);
          strcpy(text, textp);
          PQclear(simple);

   An array of composites:
   This  example  makes an array of complex composites.  It builds off the
   previous example.
          int i;
          PGarray complex_arr;
          PGparam *simple = PQparamCreate(conn);
          PGparam *complex = PQparamCreate(conn);

          complex_arr.ndims = 0;
          complex_arr.param = PQparamCreate(conn);

          for(i=0; i < 100; i++)
          {
            /* put the simple composite attributes */
            PQputf(simple, "%int4 %text*", 45, "foobar");

            /* put the complex composite attributes, which includes
             * a nested composite.
             */
            PQputf(complex, "%float8 %simple", 111.2223334, simple);

            /* put the complex composite */
            PQputf(complex_arr.param, "%complex", complex);

            /* You must reset the simple and complex composites for
             * the next loop iteration.
             */
            PQparamReset(simple);
            PQparamReset(complex);
          }

          /* not needed anymore */
          PQparamClear(simple);
          PQparamClear(complex);

          /* Put a complex composite array */
          PGparam *param = PQparamCreate(conn);
          PQputf(param, "%complex[]", &complex_arr);
          PQparamClear(complex_arr.param);

          /* exec an insert */
          res = PQparamExec(conn, param, "INSERT INTO t VALUES($1)", resfmt);
          PQparamClear(param);

          /* -------------------------
           * Getting an array of composites
           */

          int i;
          int ntups;
          PGfloat8 f8;
          PGint4 i4;
          PGtext textp;
          PGresult *simple;
          PGarray complex_arr;

          /* Get the complex[], provide a ptr to a PGarray. */
          PQgetf(exec_result, 0, "%complex[]", 0, &complex_arr);

          /* no longer needed */
          PQclear(exec_result);

          ntups = PQntuples(complex_arr.res);
          for(i=0; i < ntups; i++)
          {
            PQgetf(complex_arr.res, i, "%float8 %simple", 0, &f8, 1, &simple);

            /* Nested composites are like any other composite, tuple 0!  Unless,
             * its a nested composite array.
             */
            PQgetf(simple, 0, "#int4 #text", "a", &i4, "t", &textp);

            printf("(%f, (%d, %s))\n", f8, i4, textp);
            PQclear(simple);
          }

          PQclear(complex_arr.res);

EXAMPLES

   None.

AUTHOR

   A contribution of eSilo, LLC. for the  PostgreSQL  Database  Management
   System.  Written by Andrew Chernow and Merlin Moncure.

REPORTING BUGS

   Report bugs to <[email protected]>.

COPYRIGHT

   Copyright (c) 2011 eSilo, LLC. All rights reserved.
   This is free software; see the source for copying conditions.  There is
   NO warranty; not even for MERCHANTABILITY or  FITNESS FOR A  PARTICULAR
   PURPOSE.

SEE ALSO

   PQgetf(3), PQputf(3), PQputvf(3)



Opportunity


Personal Opportunity - Free software gives you access to billions of dollars of software at no cost. Use this software for your business, personal use or to develop a profitable skill. Access to source code provides access to a level of capabilities/information that companies protect though copyrights. Open source is a core component of the Internet and it is available to you. Leverage the billions of dollars in resources and capabilities to build a career, establish a business or change the world. The potential is endless for those who understand the opportunity.

Business Opportunity - Goldman Sachs, IBM and countless large corporations are leveraging open source to reduce costs, develop products and increase their bottom lines. Learn what these companies know about open source and how open source can give you the advantage.


Free Software


Free Software provides computer programs and capabilities at no cost but more importantly, it provides the freedom to run, edit, contribute to, and share the software. The importance of free software is a matter of access, not price. Software at no cost is a benefit but ownership rights to the software and source code is far more significant.

Free Office Software - The Libre Office suite provides top desktop productivity tools for free. This includes, a word processor, spreadsheet, presentation engine, drawing and flowcharting, database and math applications. Libre Office is available for Linux or Windows.


Free Books


The Free Books Library is a collection of thousands of the most popular public domain books in an online readable format. The collection includes great classical literature and more recent works where the U.S. copyright has expired. These books are yours to read and use without restrictions.

Source Code - Want to change a program or know how it works? Open Source provides the source code for its programs so that anyone can use, modify or learn how to write those programs themselves. Visit the GNU source code repositories to download the source.


Education


Study at Harvard, Stanford or MIT - Open edX provides free online courses from Harvard, MIT, Columbia, UC Berkeley and other top Universities. Hundreds of courses for almost all major subjects and course levels. Open edx also offers some paid courses and selected certifications.

Linux Manual Pages - A man or manual page is a form of software documentation found on Linux/Unix operating systems. Topics covered include computer programs (including library and system calls), formal standards and conventions, and even abstract concepts.