// pgtextdata.cc --- retrieve text data from PostGreSQL into Octave // // Copyright (C) 1999 Dirk Eddelbuettel // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // // $Id: pgtextdata.cc,v 1.1 1999/08/22 18:12:36 edd Exp edd $ #include // defines Octave configuration #include // defines octave_value_list #include // defines the function header #include // PostGreSQL; needs -lpq++ DEFUN_DLD (pgtextdata, args, nargout, "[S1,S2,...] = pgtextdata( db, select_command)\n\n\ Retrieve text data from `db' using `select_command'. Each column is returned\n\ as one string vector, so make sure you call with the correct number of\n\ return arguments. Numerical string vectors can be converted at once using\n\ the standard str2num function.\n\n\ Example:\n\n\ [N,P] = pgtextdata(\"biz\", [\"select name, phone from customers \" ,\\ \n\ \"order by name\"]);\n\n\ See also: pgnumdata, str2num.\n") { octave_value_list retval; // list of return values int nargs = args.length (); // number of arguments supplied if (nargs != 2) { // if not enough arguments, show message usage("Try 'help pgtextdata' for info"); return retval; // and return empty } string dbbase = "dbname="; // beginning PostGreSQL connect string string db = args(0).string_value(); // get name of database string sqlcmd = args(1).string_value(); // get SQL command // form `dbname=foodb' argument; then convert to char* and pass to PostGreSQL PgDatabase DB((dbbase+db).c_str()); if ( DB.ConnectionBad() ) { // if backend connection not successful ... error("Connection to database failed.\n"); return retval; // ... then return empty } // pass SQL argument as char*, and check if executed allright if ( ! DB.ExecTuplesOk(sqlcmd.c_str())) { error("Query failed"); return retval; } long k = DB.Fields(); // we know that query was executed, long n = DB.Tuples(); if (n == 0) { // but did we get any data ? error("Query returned no data"); return retval; } for (long j=0; j