From octave-sources-request at bevo dot che dot wisc dot edu Mon Aug 23 20:03:16 1999 Subject: octave-pg: Access PostgreSQL directly from Octave From: Dirk Eddelbuettel To: octave-sources at bevo dot che dot wisc dot edu Date: Mon, 23 Aug 1999 21:03:12 -0400 (EDT) --kWb2fvAev5 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit [ The README below is an ascii copy of the web page at http://rosebud.sps.queensu.ca/~edd/code/octave-pg/html and the tar.gz is attached. -edd ] --kWb2fvAev5 Content-Type: application/octet-stream Content-Description: octave-pg.tar.gz Content-Disposition: attachment; filename="octave-pg.tar.gz" Content-Transfer-Encoding: base64 H4sIAKLqwTcAA+1abVPbSBLO1+VX9HJ7h32RhQ2EJJDsHQkkoSoBFpNNUkBlZWlsK8iSo5Fw vC/3y+/DPd2jkWzC2+5VdmvvNFtZwJ7p6ddnuntmPIjzUeBlnuv7d77QaHfa7bW1tTvtdrtz /1579ieP+6vte3fa6/fXMauztraO+Z211ZU71P5SDM2OXGdeSnRHBcG18276/k86FpaXaWx9 gFoton0/884V9fPYz8IkpiyhVGVpqPAh5qk09L2IZHo/TUZ0kOhskCr9MQItJvc0GU/TcDDM qPG0SZ2HDx/Sdpie0U4QqKiXqyxTET2CPv8ZqF7oxW6SDr7lhTz459Ew1DROk0HqjQi/9lOl SCf9bOKlapOmSU6+F4OrINRgrJdnisKMvDhYTlIaJUHYnzIdfJbHgUopGyrKVDrSlPTlj+d7 r+m5ilUKSQ7yXhT69DL0VawVediaP9FDFVBP6PCKZ8xDt+CBniUg7LF6NkmF+D6lc5VqVteK 3aMg6FCSMpGGlzHnKSVjXtcEu1OKvKxa6vK0y+SvxAwojIX4MBlDpCFoQshJGEXUU5Rr1c8j h0lgMr3ZPXqx//qItvbe0Zutw8OtvaN3m5icDRN8C3MaUuFoHIWgDMFSL86m4J8pvNo5fPoC S7ae7L7cPXoHKejZ7tHeTrdLz/YPaYsOtg6Pdp++frl1SAevDw/2uzsuUVcxW4oJXKPjvpgJ egx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bv81u93/bbjMeabd7cJvtHt6gok77FlQ6N5mrM2+u6tZbvly9 jch1X6se9ahHPepRj3rUox71qEc96lGPetSjHvWoRz3qUY961KMe9ajHrcd/ABl+1FgAUAAA --kWb2fvAev5 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Octave modules to access PostgreSQL =20 Overview [1]PostgreSQL has a pretty decent C++ library called libpq++, with good documentation. It was quite straightforward to wrap these in C+= + code that is callable from [2]Octave. For the time being, these functions can execute select queries only. I will add code for inser= t and update later. =20 There is however a fundamental problem in as much [3]PostgreSQL, as = a proper RDBMS (i.e. ignoring the object-oriented extensions for a moment) supports a lot more data types than [4]Octave as a numerically-minded language. I therefore reduced the interface to tw= o functions. The first is to be used for strictly numerical data and returns a single (numerical) matrix. The second can be used for various datatypes as they will be returned in string vectors, one fo= r each column retrieved. =20 Examples The first function, [5]pgnumdata, is meant really only for strictly numerical data. For Postgres, this covers floats and ints of various= sizes. For [6]Octave, this means doubles, i.e. a superset of these. The following example would retrieve price and volume information fr= om a sales database: R =3D pgnumdata("sales", "select price, volume from sales"); Now R should be a two-column matrix with as many rows as there were matching tuples in the database. =20 The second function, [7]pgtextdata, allows to retrieve all types of data and stores them in string vectors, one per column. This [8]Octa= ve command would retrieve names and phone numbers from all customers: [N,P] =3D pgtextdata("biz", ["select name, phone from customers " ,\= "order by name"]); Both N and P are now vectors of strings where the i-th row of each corresponds to the data in the i-th tupel retrieved from the databas= e. =20 You can also use this for dates. However, [9]PostgreSQL usually prin= ts dates with extra slashes or hyphens. With an additional SQL function= , these can be filtered out within the query and can then be retrieved= into [10]Octave. =20 I use the ISO datestyle. You can either select it postmaster.init (a= s per postgres 6.5.1), or prefix the SQL statement with an explicit set DateStyle to 'ISO'; Next, the following adds a datefilter function suitable for the ISO dates: create function date2text (date) returns text as 'select substr(text($1),1,4) || substr(text($1),6,2) || substr(text($1),9,2)' language 'sql'; With this, dates can be retrieved as X =3D pgnumdata("biz", ["set DateStyle to 'ISO';", \ "select date2text(date), price from sales" \ "order by date"]); Again, if you have postgres 6.5.1, you select ISO dates in postmaster.init and skip the first statement here. =20 Building the .oct files You have to supply the [11]PostgreSQL library to mkoctfile, and possibly also its location and/or the location of the include files.= On my [12]Debian system, I use mkoctfile pgtextdata.cc -I/usr/include/postgresql -lpq++ mkoctfile pgnumdata.cc -I/usr/include/postgresql -lpq++ If you load either file into (X)Emacs, C-c C-c will built it with these settings. _________________________________________________________________ =20 [13]Dirk Eddelb=FCttel Last modified: Sun Aug 22 14:23:06 EDT 1999 References 1. http://www.postgresql.org/ 2. http://www.che.wisc.edu/octave 3. http://www.postgresql.org/ 4. http://www.che.wisc.edu/octave 5. http://rosebud.sps.queensu.ca/~edd/code/octave-pg/pgnumdata.cc.tx= t 6. http://www.che.wisc.edu/octave 7. http://rosebud.sps.queensu.ca/~edd/code/octave-pg/pgtextdata.cc.t= xt 8. http://www.che.wisc.edu/octave 9. http://www.postgresql.org/ 10. http://www.che.wisc.edu/octave 11. http://www.postgresql.org/ 12. http://www.debian.org/ 13. http://rosebud.sps.queensu.ca/~edd --=20 According to the latest figures, 43% of all statistics are totally wort= hless. --kWb2fvAev5--