PostgreSQL and embedded Ruby 
Author Message
 PostgreSQL and embedded Ruby

Hello,

INTRO: I would like to build simple application in C to deal with
PostgreSQL, but part of work should be done by RUBY.

PROBLEM: I don't want to make new connection with PGSQL in Ruby script
but attach existing one ('conn' variable as below) from C.

QUESTION: Is it possible to attach to Postgres connection from C in Ruby
script? And how to do it?

Szymon Drejewicz

#=======================================================================
#include <stdlib.h>
#include <libpq-fe.h>
#include <ruby.h>
#include "postgres.c"

int main(int argc, char** argv) {
   PGresult *result;
   PGconn *conn;
   int nFields;
   int i;
   int j;
   conn = PQconnectdb("");

   if(PQstatus(conn) == CONNECTION_OK) {
     printf("connection made\n");
     result = PQexec(conn, "SELECT COUNT(*) FROM studenci");

     printf("Wy?wietlam wynik za pomoc C:\n");
     /* first, print out the attribute names */
     nFields = PQnfields(result);
     for (i = 0; i < nFields; i++)
       printf("%-15s", PQfname(result, i));
     printf("\n\n");
     /* next, print out the rows */
     for (i = 0; i < PQntuples(result); i++) {
       for (j = 0; j < nFields; j++)
        printf("%-15s", PQgetvalue(result, i, j));
       printf("\n");
     }

     printf("Teraz sprbuj przekaza? wynik SELECTA do skryptu Ruby'ego,
ktry wy?wietli to samo co powy?ej:");
     ruby_init();
     ruby_options(argc,argv);
     /* Udostpniam Ruby'emu po3czenie z PostgreSQL'em  */
     ruby_init_loadpath();
     rb_require("postgres");
     //-----------------PROBLEM BEGIN
     VALUE DBconn = Data_Wrap_Struct(rb_cPGconn, 0, free_pgconn, conn);
     //    rb_obj_call_init(DBconn,0,0);
     VALUE connection = conn;
     rb_define_variable("$conn",&connection);
     rb_eval_string("puts '\nTEST\n'");
     rb_eval_string("a = $conn.query('select count(*) from
studenci')[0][0] \n puts a");
     //-----------------PROBLEM END
   }
   else
     printf("connection failed: %s\n", PQerrorMessage(conn));

   PQfinish(conn);
   return EXIT_SUCCESS;

Quote:
}



Sun, 31 Jul 2005 16:18:42 GMT  
 PostgreSQL and embedded Ruby

S>      VALUE DBconn = Data_Wrap_Struct(rb_cPGconn, 0, free_pgconn, conn);
S>      //    rb_obj_call_init(DBconn,0,0);
S>      VALUE connection = conn;
S>      rb_define_variable("$conn",&connection);
                                   ^^^^^^^^^^^
                                   &DBconn

S>      rb_eval_string("puts '\nTEST\n'");
S>      rb_eval_string("a = $conn.query('select count(*) from
S> studenci')[0][0] \n puts a");

Guy Decoux



Sun, 31 Jul 2005 17:27:59 GMT  
 PostgreSQL and embedded Ruby

Quote:


> S>      VALUE DBconn = Data_Wrap_Struct(rb_cPGconn, 0, free_pgconn, conn);
> S>      //    rb_obj_call_init(DBconn,0,0);
> S>      VALUE connection = conn;
> S>      rb_define_variable("$conn",&connection);
>                                    ^^^^^^^^^^^
>                                    &DBconn

> S>      rb_eval_string("puts '\nTEST\n'");
> S>      rb_eval_string("a = $conn.query('select count(*) from
> S> studenci')[0][0] \n puts a");

> Guy Decoux

Thanks for reply. I've changed this, but:

-e:1: method call on terminated object (NotImplementedError)
-e:1: [BUG] Segmentation fault

---
Szymon Drejewicz



Sun, 31 Jul 2005 18:36:45 GMT  
 PostgreSQL and embedded Ruby

S> -e:1: method call on terminated object (NotImplementedError)
S> -e:1: [BUG] Segmentation fault

pigeon% cat a.c
#include <stdlib.h>
#include <libpq-fe.h>
#include <ruby.h>

int main(int argc, char** argv) {
   PGresult *result;
   PGconn *conn;
   int nFields;
   int i;
   int j;
   VALUE rb_cPGconn;
   conn = PQconnectdb("");

   if(PQstatus(conn) == CONNECTION_OK) {
     printf("connection made\n");
     result = PQexec(conn, "SELECT COUNT(*) FROM studenci");

     nFields = PQnfields(result);
     for (i = 0; i < nFields; i++)
       printf("%-15s", PQfname(result, i));
     printf("\n\n");
     for (i = 0; i < PQntuples(result); i++) {
       for (j = 0; j < nFields; j++)
        printf("%-15s", PQgetvalue(result, i, j));
       printf("\n");
     }

     ruby_init();
     //     ruby_options(argc,argv);
     ruby_init_loadpath();
     rb_require("postgres");
     rb_cPGconn = rb_const_get(rb_cObject, rb_intern("PGconn"));
     //-----------------PROBLEM BEGIN
     {
         VALUE DBconn = Data_Wrap_Struct(rb_cPGconn, 0, 0, conn);
     //    rb_obj_call_init(DBconn,0,0);
         VALUE connection = conn;
         rb_define_variable("$conn",&DBconn);
         rb_eval_string("puts '\nTEST\n'");
         rb_eval_string("a = $conn.query('select count(*) from studenci')[0][0] \n puts a");
     }
     //-----------------PROBLEM END
   }
   else
     printf("connection failed: %s\n", PQerrorMessage(conn));

   PQfinish(conn);
   return EXIT_SUCCESS;

Quote:
}

pigeon%

pigeon% ./a.out
connection made
count          

1              

TEST
1
pigeon%

Guy Decoux



Sun, 31 Jul 2005 18:59:51 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. embedded trasactions in postgreSQL with python?

2. Embedded Ruby (PART II)- Ruby hackers help wanted :)

3. OS X + postgreSQL + ruby

4. Bi-Temporal PostgreSQL - http://raa.ruby-lang.org/list.rhtml?name=btpgsql

5. PL/Ruby for postgresql

6. embed Ruby/TK app in a web page

7. Multithreaded/Embedded Ruby?

8. Embedding Ruby

9. Embedded Ruby

10. tutorial on embedding ruby (review)

11. embedding ruby

12. problems embedding ruby in win32

 

 
Powered by phpBB® Forum Software