compat-ruby-postgres/postgres.c in postgres-0.7.9.2007.12.22 vs compat-ruby-postgres/postgres.c in postgres-0.7.9.2008.01.03
- old
+ new
@@ -12,12 +12,20 @@
$Date: 2007-12-07 09:51:23 -0800 (Fri, 07 Dec 2007) $
************************************************/
#include "ruby.h"
#include "rubyio.h"
+
+#if RUBY_VM == 1
+ /* ruby 1.9 */
+#include "ruby/st.h"
+#include "ruby/intern.h"
+#else
+ /* ruby 1.8 */
#include "st.h"
#include "intern.h"
+#endif
/* grep '^#define' $(pg_config --includedir)/server/catalog/pg_type.h | grep OID */
#include "type-oids.h"
#include <libpq-fe.h>
#include <libpq/libpq-fs.h> /* large-object interface */
@@ -31,18 +39,30 @@
{
rb_raise(rb_eArgError,"this version of libpq doesn't support PQserverVersion");
}
#endif /* HAVE_PQSERVERVERSION */
+#ifndef RHASH_SIZE
+#define RHASH_SIZE(x) RHASH((x))->tbl->num_entries
+#endif /* RHASH_SIZE */
+
#ifndef RSTRING_LEN
#define RSTRING_LEN(x) RSTRING((x))->len
#endif /* RSTRING_LEN */
#ifndef RSTRING_PTR
#define RSTRING_PTR(x) RSTRING((x))->ptr
#endif /* RSTRING_PTR */
+#if RUBY_VM == 1
+ /* ruby 1.9 */
+#define RB_REG_NEW(s, len, opt) rb_reg_new(rb_str_new((s),(len)), opt)
+#else
+ /* ruby 1.8 */
+#define RB_REG_NEW(s, len, opt) rb_reg_new((s), (len), (opt))
+#endif
+
#ifndef HAVE_PG_ENCODING_TO_CHAR
#define pg_encoding_to_char(x) "SQL_ASCII"
#endif
#ifndef HAVE_PQFREEMEM
@@ -131,11 +151,11 @@
if (!NIL_P(conninfo = rb_check_string_type(arg))) {
/* do nothing */
}
else if (!NIL_P(conninfo = rb_check_hash_type(arg))) {
- VALUE key_values = rb_ary_new2(RHASH(conninfo)->tbl->num_entries);
+ VALUE key_values = rb_ary_new2(RHASH_SIZE(conninfo));
rb_hash_foreach(conninfo, build_key_value_string_i, key_values);
conninfo = rb_ary_join(key_values, rb_str_new2(" "));
}
else {
return NULL;
@@ -463,25 +483,23 @@
static VALUE
pgconn_s_escape_bytea(self, obj)
VALUE self;
VALUE obj;
{
- char *from, *to;
+ unsigned char *from, *to;
size_t from_len, to_len;
VALUE ret;
Check_Type(obj, T_STRING);
- from = RSTRING_PTR(obj);
+ from = (unsigned char*)RSTRING_PTR(obj);
from_len = RSTRING_LEN(obj);
- to = (char *)PQescapeBytea(from, from_len, &to_len);
+ to = PQescapeBytea(from, from_len, &to_len);
- ret = rb_str_new(to, to_len - 1);
+ ret = rb_str_new((char*)to, to_len - 1);
OBJ_INFECT(ret, obj);
-
PQfreemem(to);
-
return ret;
}
/*
* call-seq:
@@ -501,25 +519,23 @@
static VALUE
pgconn_escape_bytea(self, obj)
VALUE self;
VALUE obj;
{
- char *from, *to;
+ unsigned char *from, *to;
size_t from_len, to_len;
VALUE ret;
Check_Type(obj, T_STRING);
- from = RSTRING_PTR(obj);
+ from = (unsigned char*)RSTRING_PTR(obj);
from_len = RSTRING_LEN(obj);
- to = (char *)PQescapeByteaConn(get_pgconn(self),from, from_len, &to_len);
+ to = PQescapeByteaConn(get_pgconn(self),from, from_len, &to_len);
- ret = rb_str_new(to, to_len - 1);
+ ret = rb_str_new((char*)to, to_len - 1);
OBJ_INFECT(ret, obj);
-
PQfreemem(to);
-
return ret;
}
/*
* call-seq:
@@ -533,20 +549,20 @@
*/
static VALUE
pgconn_s_unescape_bytea(self, obj)
VALUE self, obj;
{
- char *from, *to;
+ unsigned char *from, *to;
size_t to_len;
VALUE ret;
Check_Type(obj, T_STRING);
- from = StringValuePtr(obj);
+ from = (unsigned char*)StringValuePtr(obj);
- to = (char *) PQunescapeBytea(from, &to_len);
+ to = PQunescapeBytea(from, &to_len);
- ret = rb_str_new(to, to_len);
+ ret = rb_str_new((char*)to, to_len);
OBJ_INFECT(ret, obj);
PQfreemem(to);
return ret;
}
@@ -1349,11 +1365,11 @@
pgconn_set_client_encoding(obj, str)
VALUE obj, str;
{
Check_Type(str, T_STRING);
if ((PQsetClientEncoding(get_pgconn(obj), StringValuePtr(str))) == -1){
- rb_raise(rb_ePGError, "invalid encoding name %s",str);
+ rb_raise(rb_ePGError, "invalid encoding name: %s",StringValuePtr(str));
}
return Qnil;
}
#endif
@@ -1880,80 +1896,10 @@
return PQgetisnull(result, i, j) ? Qtrue : Qfalse;
}
/*
* call-seq:
- * res.print( file, opt )
- *
- * MISSING: Documentation
- */
-static VALUE
-pgresult_print(obj, file, opt)
- VALUE obj, file, opt;
-{
- VALUE value;
- ID mem;
- OpenFile* fp;
- PQprintOpt po;
-
- Check_Type(file, T_FILE);
- Check_Type(opt, T_STRUCT);
- GetOpenFile(file, fp);
-
- memset(&po, 0, sizeof(po));
-
- mem = rb_intern("header");
- value = rb_struct_getmember(opt, mem);
- po.header = value == Qtrue ? 1 : 0;
-
- mem = rb_intern("align");
- value = rb_struct_getmember(opt, mem);
- po.align = value == Qtrue ? 1 : 0;
-
- mem = rb_intern("standard");
- value = rb_struct_getmember(opt, mem);
- po.standard = value == Qtrue ? 1 : 0;
-
- mem = rb_intern("html3");
- value = rb_struct_getmember(opt, mem);
- po.html3 = value == Qtrue ? 1 : 0;
-
- mem = rb_intern("expanded");
- value = rb_struct_getmember(opt, mem);
- po.expanded = value == Qtrue ? 1 : 0;
-
- mem = rb_intern("pager");
- value = rb_struct_getmember(opt, mem);
- po.pager = value == Qtrue ? 1 : 0;
-
- mem = rb_intern("fieldSep");
- value = rb_struct_getmember(opt, mem);
- if (!NIL_P(value)) {
- Check_Type(value, T_STRING);
- po.fieldSep = StringValuePtr(value);
- }
-
- mem = rb_intern("tableOpt");
- value = rb_struct_getmember(opt, mem);
- if (!NIL_P(value)) {
- Check_Type(value, T_STRING);
- po.tableOpt = StringValuePtr(value);
- }
-
- mem = rb_intern("caption");
- value = rb_struct_getmember(opt, mem);
- if (!NIL_P(value)) {
- Check_Type(value, T_STRING);
- po.caption = StringValuePtr(value);
- }
-
- PQprint(fp->f2?fp->f2:fp->f, get_pgresult(obj), &po);
- return obj;
-}
-
-/*
- * call-seq:
* res.cmdtuples()
*
* Returns the number of tuples (rows) affected by the SQL command.
*
* If the SQL command that generated the PGresult was not one of +INSERT+, +UPDATE+, +DELETE+, +MOVE+, or +FETCH+, or if no tuples (rows) were affected, <tt>0</tt> is returned.
@@ -2330,32 +2276,31 @@
pglarge_read(argc, argv, obj)
int argc;
VALUE *argv;
VALUE obj;
{
- int len;
- PGlarge *pglarge = get_pglarge(obj);
- VALUE str;
- VALUE length;
-
- rb_scan_args(argc, argv, "01", &length);
- if (NIL_P(length)) {
- return loread_all(obj);
- }
-
- len = NUM2INT(length);
- if (len < 0){
- rb_raise(rb_ePGError,"nagative length %d given", len);
- }
- str = rb_tainted_str_new(0,len);
+ int len;
+ PGlarge *pglarge = get_pglarge(obj);
+ VALUE length;
+ char *buffer;
- if((len = lo_read(pglarge->pgconn, pglarge->lo_fd, StringValuePtr(str), len)) < 0) {
- rb_raise(rb_ePGError, "error while reading");
- }
- if (len == 0) return Qnil;
- RSTRING_LEN(str) = len;
- return str;
+ rb_scan_args(argc, argv, "01", &length);
+ if (NIL_P(length)) {
+ return loread_all(obj);
+ }
+
+ len = NUM2INT(length);
+ if (len < 0){
+ rb_raise(rb_ePGError,"nagative length %d given", len);
+ }
+ buffer = ALLOCA_N(char, len);
+
+ if((len = lo_read(pglarge->pgconn, pglarge->lo_fd, buffer, len)) < 0) {
+ rb_raise(rb_ePGError, "error while reading");
+ }
+ if (len == 0) return Qnil;
+ return rb_str_new(buffer,len);
}
/*
* call-seq:
* lrg.write( str )
@@ -2670,11 +2615,11 @@
void
Init_postgres()
{
pg_gsub_bang_id = rb_intern("gsub!");
- pg_escape_regex = rb_reg_new("([\\t\\n\\\\])", 10, 0);
+ pg_escape_regex = RB_REG_NEW("([\\t\\n\\\\])", 10, 0);
rb_global_variable(&pg_escape_regex);
pg_escape_str = rb_str_new("\\\\\\1", 4);
rb_global_variable(&pg_escape_str);
rb_require("bigdecimal");
@@ -2813,10 +2758,9 @@
rb_define_method(rb_cPGresult, "getlength", pgresult_getlength, 2);
rb_define_method(rb_cPGresult, "getisnull", pgresult_getisnull, 2);
rb_define_method(rb_cPGresult, "cmdtuples", pgresult_cmdtuples, 0);
rb_define_method(rb_cPGresult, "cmdstatus", pgresult_cmdstatus, 0);
rb_define_method(rb_cPGresult, "oid", pgresult_oid, 0);
- rb_define_method(rb_cPGresult, "print", pgresult_print, 2);
rb_define_method(rb_cPGresult, "clear", pgresult_clear, 0);
rb_define_alias(rb_cPGresult, "close", "clear");
rb_cPGrow = rb_define_class("PGrow", rb_cArray);
rb_define_method(rb_cPGrow, "initialize", pgrow_init, 1);