Class: RDO::Postgres::Driver

Inherits:
Driver
  • Object
show all
Defined in:
lib/rdo/postgres/driver.rb

Overview

Driver for the Postgres server.

All default behaviour is overloaded.

Instance Method Summary collapse

Instance Method Details

#closeObject

Disconnect from the postgres server, and release memory



77
78
79
80
81
82
83
84
85
86
87
88
# File 'ext/rdo_postgres/driver.c', line 77

static VALUE rdo_postgres_driver_close(VALUE self) {
  RDOPostgresDriver * driver;
  Data_Get_Struct(self, RDOPostgresDriver, driver);

  PQfinish(driver->conn_ptr);
  driver->conn_ptr   = NULL;
  driver->is_open    = 0;
  driver->stmt_count = 0;
  driver->encoding   = -1;

  return Qtrue;
}

#execute(stmt, *args) ⇒ RDO::Result

Internally this driver uses prepared statements.

Parameters:

  • stmt (String)

    the statement to execute

  • *args (Object...)

    bind parameters to execute with

Returns:

  • (RDO::Result)

    a result containing any tuples and query info



26
27
28
# File 'lib/rdo/postgres/driver.rb', line 26

def execute(stmt, *args)
  prepare(stmt).execute(*args)
end

#openObject

Connect to the postgres server



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'ext/rdo_postgres/driver.c', line 46

static VALUE rdo_postgres_driver_open(VALUE self) {
  RDOPostgresDriver * driver;
  Data_Get_Struct(self, RDOPostgresDriver, driver);

  if (driver->is_open) {
    return Qtrue;
  }

  driver->conn_ptr = PQconnectdb(
      RSTRING_PTR(rb_funcall(self, rb_intern("connect_db_string"), 0)));

  if (driver->conn_ptr == NULL || PQstatus(driver->conn_ptr) == CONNECTION_BAD) {
    RDO_ERROR("PostgreSQL connection failed: %s",
        PQerrorMessage(driver->conn_ptr));
  } else if (PQprotocolVersion(driver->conn_ptr) < 3) {
    RDO_ERROR("rdo-postgres requires PostgreSQL protocol version >= 3 (using %u). "
        "PostgreSQL >= 7.4 required.",
        PQprotocolVersion(driver->conn_ptr));
  } else {
    PQsetNoticeProcessor(driver->conn_ptr, &rdo_postgres_driver_notice_processor, NULL);
    driver->is_open    = 1;
    driver->stmt_count = 0;
    driver->encoding   = rb_enc_find_index(
        RSTRING_PTR(rb_funcall(self, rb_intern("encoding"), 0)));
    rb_funcall(self, rb_intern("after_open"), 0);
  }

  return Qtrue;
}

#open?Boolean

Preciate test if connection is open

Returns:

  • (Boolean)


91
92
93
94
95
# File 'ext/rdo_postgres/driver.c', line 91

static VALUE rdo_postgres_driver_open_p(VALUE self) {
  RDOPostgresDriver * driver;
  Data_Get_Struct(self, RDOPostgresDriver, driver);
  return driver->is_open ? Qtrue : Qfalse;
}

#prepare(cmd) ⇒ Object

Prepare a statement for execution



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'ext/rdo_postgres/driver.c', line 98

static VALUE rdo_postgres_driver_prepare(VALUE self, VALUE cmd) {
  Check_Type(cmd, T_STRING);

  RDOPostgresDriver * driver;
  Data_Get_Struct(self, RDOPostgresDriver, driver);

  if (!(driver->is_open)) {
    RDO_ERROR("Unable to prepare statement: connection is not open");
  }

  char name[32];
  sprintf(name, "rdo_stmt_%i", ++driver->stmt_count);

  return rdo_postgres_statement_executor_new(self, cmd, rb_str_new2(name));
}

#quote(str) ⇒ Object

Quote a string literal for safe insertion in a statement



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'ext/rdo_postgres/driver.c', line 115

static VALUE rdo_postgres_driver_quote(VALUE self, VALUE str) {
  if (TYPE(str) == T_NIL) {
    return rb_str_new2("NULL");
  } else if (TYPE(str) != T_STRING) {
    str = RDO_OBJ_TO_S(str);
  }

  RDOPostgresDriver * driver;
  Data_Get_Struct(self, RDOPostgresDriver, driver);

  if (!(driver->is_open)) {
    RDO_ERROR("Unable to quote string: connection is not open");
  }

  char * quoted = malloc(sizeof(char) * RSTRING_LEN(str) * 2 + 1);
  PQescapeStringConn(driver->conn_ptr, quoted,
      RSTRING_PTR(str), RSTRING_LEN(str), NULL);

  VALUE newstr = rb_str_new2(quoted);

  free(quoted);

  return newstr;
}