Hi Steve, Maarten,

the attached file postgres.r was on the command beta list
at some time (maybe I reworked it a little), pg.r was a
higher level I created. 

I don't have /Command, nor /Pro (yet), so I can't test if
it is working ... 


Once upon a time Maarten Koopmans spoketh thus:
> Steve,
> 
> As Postgres probably has a C client lib you can easily use the Rebol Library
> interface of /Pro /Command to write a simple mapping. I did so for MySQL, so
> you can use that as a sample.
> 
> You can find the mysql stuff on www.erebol.com
> 
> Postgres is next on my todo list, but as I'm quite busy it may take a few
> months before I find the time.
> 
> Thanks,
> 
> Maarten
> 
> ----- Original Message -----
> From: "steve jenson" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Thursday, April 26, 2001 9:01 AM
> Subject: [REBOL] Postgres 7.x support
> 
> 
> > Carl, gang,
> >
> > Does anyone know of possible Postgres support in an upcoming version of
> > REBOL/*/Pro? Oracle and ODBC support are great but some of us (who
> > are more than willing to pay the cash for */Pro) don't use Oracle but
> > Postgres. I'm not worried about MySQL support, since MySQL users don't
> > know what an ACID-test is. ;-)


-- Attached file included as plaintext by Listar --

REBOL [
    Title: "Postgres Database REBOL/command API"
    Purpose: {
        Exposes the Postgres API in REBOL/command
    }
    File: %postgres.r
]


pq: load/library %/usr/lib/libpq.so

routines: [
    ;; added or changed (iho)
    PQconnectdb [
        "Login to postgres Database"
        conninfo [string!]
        return: [long]
    ]

    PQgetline [
        "copy line from db"
        pgconn [int]
        buffer [string!]
        length [int]
        return: [int]
    ]

    PQputline [
        "copy line to db"
        pgconn [int]
        data [string!]
        return: [int]
    ]   

    PQsetdbLogin [
        "Login to postgres Database"
        pghost [string!] pgport [string!]
        pgoptions [string!] pgtty [string!]
        dbName [string!] login [string!]
        pwd [string!] return: [long]
    ]

    PQuser [
        "Name of User"
         PGconn [int] return: [string!]
    ]

    PQdisplayTuples [
        "print out all tuples"
        pgresult [int]
        fout [string!]
        fillAlign [int]
        fieldSep [string!]
        printHeader [int]
        quiet [int]
    ]
    ;; end iho

    PQdb           ["Name of database"
                    PGconn [int] return: [string!]]
    PQhost         ["Name of host"
                    PGconn [int] return: [string!]]
    PQoptions      ["Options of connection"
                    PGconn [int] return: [string!]]
    PQport         ["Port number"
                    PGconn [int] return: [string!]]
    PQtty          ["Tty of database"
                    PGconn [int] return: [string!]]
    PQstatus       ["Status of connection"
                    PGconn [int] return: [int]]
    PQerrorMessage ["Error associated with connection"
                    PGconn [int] return: [string!]]
    PQfinish       ["Close connection"
                    PGconn [int]]
    PQreset        ["Reset communication with database"
                    PGconn [int]]
    PQexec         [ "Execute SQL statement"
                     PGconn [int] query [string!] return: [int]]
    PQresultStatus ["Status of query"
                    result [int] return: [int]]
    PQresStatus    ["Convert status to string"
                    res-stat [int] return: [string!]]
    PQresultErrorMessage ["Error message for query"
                          PGresult [int] return: [string!]]
    PQntuples   ["Number of tuples in result"
                 PGresult [int] return: [int]]
    PQnfields   ["Number of fields in result"
                 PGresult [int] return: [int]]
    PQfname     ["Field attribute associated with field index"
                 PGresult [int] field_index [int] return: [string!]]
    PQfnumber   ["Field index associated with field name"
                 PGresult [int] field_name [string!] return: [int]]
    PQftype     ["Field type associated with given index"
                 PGresult [int] field_num [int] return: [int]]
    PQfsize     ["Size in bytes of field associated with index"
                 PGresult [int] field_index [int] return: [int]]
    PQfmod      ["Type-specific modification data per field at index"
                 PGresult [int] field_index [int] return: [int]]
    PQgetvalue  ["Get field (attribute) value"
                 PGresult [int] tup_num [int] field_num [int] return: [char*]]
    PQgetlength ["Length of a field in bytes"
                 PGresult [int] tup_num [int] field_num [int] return: [int]]
    PQgetisnull ["NULL status of a field"
                 PGresult [int] tup_num [int] field_num [int] return: [int]]
    PQcmdStatus ["Command status of last query command"
                 PGresult [int] return: [string!]]
    PQcmdTuples ["Number of tuples affected by INSERT, UPDATE, DELETE queries"
                 PGresult [int] return: [string!]]
    PQoidStatus ["String with the object id  of  the tuple inserted"
                 PGresult [int] return: [string!]]
    PQclear        ["Frees the storage  associated  with  the  PGresult"
                    PQresult [int]]
    PQsendQuery    ["Submit a query to Postgres without waiting result(s)"
                    PGconn [int] query [string!] return: [string!]]
    PQgetResult    ["Wait for the next result from a prior PQsendQuery"
                    PGconn [int] return: [int]]
    PQconsumeInput ["If input is available from the backend, consume it"
                    PGconn [int]]
    PQisBusy       ["Returns TRUE if a query is busy (would block)"
                    PGconn [int] return: [int]]
    PQsocket       ["file descriptor number for the backend connection socket"
                    PGconn [int] return: [int]]
    PQrequestCancel["Request abandon processing of the current query"
                    PGconn [int] return: [int]]
    fe_getauthname ["Name the user has authenticated"
                    errorMessage [string!] return: [string!]]
    fe_setauthsvc  ["use authentication service name rather default"
                    name [string!] errorMessage [string!]]
]

foreach [name spec] routines [
    set name make routine! spec pq form name
]



-- Attached file included as plaintext by Listar --

REBOL [
   Title: "Postgres Database driver" 
   Date: 1-Aug-2000 
   Version: 0.0.1 
   File: %pg.r 
   Author: "Ingo Hohmann" 
   Rights: "(c) 8/2000 Ingo Hohmann" 
   Usage: [] 
   Comment: [] 
   History: [[1-Aug-2000 "Ingo Hohmann" "initial Version"]] 
   Email: [EMAIL PROTECTED] 
   Category: [] 
   Known-bugs: [] 
   Status: 'initial
]

yamm/import 'postgres.r

pg: make object! [
   help: "Postgres driver"

   conn: none
   status: none
   qry-status: none
   qry-error: none
   err: none
   res: none

   open: func [
      "Open the database"
      [catch]
      /db
      dbname [string!] 
      /user 
      username [string!] 
      /pass
      passwd [string!]
      /host
      hostname [string!]
      /port
      port-id [integer!]
      /debug
      to-tty [file!]
      /local para 
   ] [ 
      if conn [ throw make error! "already connected" ]
      para: copy ""
      if db [
                 append para rejoin ["dbname='" dbname "'" ]
      ]
      if user [
                 append para rejoin [" user='" username "'" ]
      ]
      if pass [
                 append para rejoin [" password='" passwd "'" ]
      ]
      if host [
                 append para rejoin [" host=" hostname ]
      ]
      if port [
                 append para rejoin [" port=" port-id ]
      ]
      if debug [
                 append para rejoin [" tty=" to-tty ]
      ]
      conn: pqconnectdb para
      either 0 = pqstatus conn [
                 status: make object! [
                        db: pqdb conn
                        user: pquser conn
                        host: pqhost conn
                        port: pqport conn
                        tty: pqtty conn
                        options: pqoptions conn
                        err: pqerrormessage conn
                 ]
                 'connection-ok
      ] [
                 conn: none
                 'connection-bad
      ]
   ]
   
   reset: func [
      "Reset database communication"
   ] [
      pqreset conn
      either 0 = pqstatus conn [
                 'connection-ok
      ] [
                 'connection-bad
      ]
   ]

   close: func [
      "Close the database"
   ] [
      pqfinish conn
      conn: none
      status: none
      'connection-closed
   ]

   {
      >> pqresstatus 0
      == "PGRES_EMPTY_QUERY"
      >> pqresstatus 1
      == "PGRES_COMMAND_OK"
      >> pqresstatus 2
      == "PGRES_TUPLES_OK"
      >> pqresstatus 3
      == "PGRES_COPY_OUT"
      >> pqresstatus 4
      == "PGRES_COPY_IN"
      >> pqresstatus 5
      == "PGRES_BAD_RESPONSE"
      >> pqresstatus 6
      == "PGRES_NONFATAL_ERROR"
      >> pqresstatus 7
      == "PGRES_FATAL_ERROR"
   }

   exec: func [
      "Execute SQL String"
      sql [string!]
      /local
      status i j row col rows cols table retval
   ] [
      table: make block! 100

      res: pqexec conn sql
      qry-status: pqresstatus status: pqresultstatus res
      qry-error: PQresultErrorMessage res 
      switch/default status [
                 0 [ retval: copy [] ]
                 1 [ retval: 'command-ok ]
                 2 [                                   ;; tuples-ok
                        rows: PQntuples res
                        cols: PQnfields res
                        row: make block! cols
                        
                        i: 0
                        loop cols [
                           append row pqfname res i
                           i: i + 1
                        ]
                        append/only table row
                        
                        i: 0 
                        loop rows [
                           row: make block! cols
                           j: 0 
                           loop cols [
                                  append row PQgetvalue res i j
                                  j: j + 1
                           ]
                           append/only table row
                           i: i + 1
                        ]
                        retval: next table
                 ]
      ] [
                 retval: none
      ]
      PQclear res
      retval
   ]
]







-- 
To unsubscribe from this list, please send an email to
[EMAIL PROTECTED] with "unsubscribe" in the 
subject, without the quotes.

Reply via email to