pg 0.7.9.2008.10.13 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,103 @@
1
+
2
+ require 'rubygems'
3
+ require 'rake/clean'
4
+ require 'rake/gempackagetask'
5
+ require 'spec/rake/spectask'
6
+ require 'rake/rdoctask'
7
+ require 'ext_helper'
8
+ require 'date'
9
+
10
+ # House-keeping
11
+ CLEAN.include '**/*.o', 'ext/*.so', '**/*.bundle', '**/*.a',
12
+ '**/*.log', "{ext,lib}/*.{bundle,so,obj,pdb,lib,def,exp}",
13
+ "ext/Makefile", 'lib', '**/*.db'
14
+
15
+ FILES = FileList[
16
+ 'Rakefile',
17
+ 'README',
18
+ 'LICENSE',
19
+ 'COPYING.txt',
20
+ 'ChangeLog',
21
+ 'Contributors',
22
+ 'GPL',
23
+ 'BSD',
24
+ 'doc/**/*',
25
+ 'ext/*',
26
+ 'ext/mingw/Rakefile',
27
+ 'ext/mingw/build.rake',
28
+ 'ext/vc/*.sln',
29
+ 'ext/vc/*.vcproj',
30
+ 'lib/**/*',
31
+ 'sample/**/*',
32
+ 'spec/**/*'
33
+ ]
34
+
35
+ spec = Gem::Specification.new do |s|
36
+ s.name = 'pg'
37
+ s.platform = Gem::Platform::RUBY
38
+ s.require_path = "lib"
39
+ s.rubyforge_project = 'ruby-pg'
40
+ s.version = "0.8.0"
41
+ s.date = DateTime.now
42
+ s.summary = 'Ruby extension library providing an API to PostgreSQL'
43
+ s.authors = [
44
+ 'Yukihiro Matsumoto',
45
+ 'Eiji Matsumoto',
46
+ 'Noboru Saitou',
47
+ 'Dave Lee',
48
+ 'Jeff Davis']
49
+ s.email = 'ruby-pg@j-davis.com'
50
+ s.homepage = 'http://rubyforge.org/projects/ruby-pg'
51
+ s.requirements = 'PostgreSQL libpq library and headers'
52
+ s.has_rdoc = true
53
+ s.extra_rdoc_files = ['ext/pg.c']
54
+ s.extensions = [ 'ext/extconf.rb' ]
55
+ s.required_ruby_version = '>= 1.8.4'
56
+ s.files = FILES.to_a.reject { |x| CLEAN.include?(x) }
57
+ end
58
+
59
+ Rake::GemPackageTask.new(spec) do |pkg|
60
+ pkg.gem_spec = spec
61
+ pkg.need_tar = true
62
+ end
63
+
64
+ # ------- Windows Package ----------
65
+ if RUBY_PLATFORM.match(/win32/)
66
+ binaries = (FileList['ext/mingw/*.so',
67
+ 'ext/mingw/*.dll*'])
68
+
69
+ # Windows specification
70
+ win_spec = spec.clone
71
+ win_spec.extensions = ['ext/mingw/Rakefile']
72
+ win_spec.platform = Gem::Platform::CURRENT
73
+ win_spec.files += binaries.to_a
74
+
75
+ # Rake task to build the windows package
76
+ Rake::GemPackageTask.new(win_spec) do |pkg|
77
+ end
78
+ end
79
+
80
+ # --------- RDoc Documentation ---------
81
+ desc "Generate rdoc documentation"
82
+ Rake::RDocTask.new("rdoc") do |rdoc|
83
+ rdoc.rdoc_dir = 'doc/rdoc'
84
+ rdoc.title = "pg"
85
+ # Show source inline with line numbers
86
+ rdoc.options << "--line-numbers"
87
+ # Make the readme file the start page for the generated html
88
+ rdoc.options << '--main' << 'README'
89
+ rdoc.rdoc_files.include('ext/**/*.c',
90
+ 'ChangeLog',
91
+ 'README*',
92
+ 'LICENSE')
93
+ end
94
+
95
+
96
+ setup_extension 'pg', spec
97
+
98
+ desc "Run all specs in spec directory"
99
+ Spec::Rake::SpecTask.new("spec") do |t|
100
+ t.spec_opts = ["--format", "specdoc", "--colour"]
101
+ t.spec_files = FileList["spec/**/*_spec.rb"]
102
+ end
103
+ task :spec => [:compile]
@@ -283,7 +283,7 @@ PQsetClientEncoding(PGconn *conn, const char *encoding)
283
283
  PGresult *res;
284
284
  int status;
285
285
 
286
- if (!conn || conn->status != CONNECTION_OK)
286
+ if (!conn || PQstatus(conn) != CONNECTION_OK)
287
287
  return -1;
288
288
 
289
289
  if (!encoding)
@@ -299,7 +299,7 @@ PQsetClientEncoding(PGconn *conn, const char *encoding)
299
299
 
300
300
  if (res == NULL)
301
301
  return -1;
302
- if (res->resultStatus != PGRES_COMMAND_OK)
302
+ if (PQresultStatus(res) != PGRES_COMMAND_OK)
303
303
  status = -1;
304
304
  else
305
305
  {
@@ -309,7 +309,7 @@ PQsetClientEncoding(PGconn *conn, const char *encoding)
309
309
  * backend to report the parameter value, and we'll change state at
310
310
  * that time.
311
311
  */
312
- if (PG_PROTOCOL_MAJOR(conn->pversion) < 3)
312
+ if (PQprotocolVersion(conn) < 3)
313
313
  pqSaveParameterStatus(conn, "client_encoding", encoding);
314
314
  status = 0; /* everything is ok */
315
315
  }
@@ -0,0 +1,24 @@
1
+ # We can't use Ruby's standard build procedures
2
+ # on Windows because the Ruby executable is
3
+ # built with VC++ while here we want to build
4
+ # with MingW. So just roll our own...
5
+
6
+ require 'fileutils'
7
+ require 'rbconfig'
8
+
9
+ EXTENSION_NAME = "pg.#{Config::CONFIG["DLEXT"]}"
10
+
11
+ # This is called when the Windows GEM is installed!
12
+ task :install do
13
+ # Gems will pass these two environment variables:
14
+ # RUBYARCHDIR=#{dest_path}
15
+ # RUBYLIBDIR=#{dest_path}
16
+
17
+ dest_path = ENV['RUBYLIBDIR']
18
+ mkdir_p(dest_path)
19
+
20
+ # Copy the extension
21
+ cp(EXTENSION_NAME, dest_path)
22
+ end
23
+
24
+ task :default => :install
@@ -0,0 +1,40 @@
1
+ # We can't use Ruby's standard build procedures
2
+ # on Windows because the Ruby executable is
3
+ # built with VC++ while here we want to build
4
+ # with MingW. So just roll our own...
5
+
6
+ require 'rake/clean'
7
+ require 'rbconfig'
8
+
9
+ RUBY_INCLUDE_DIR = Config::CONFIG["archdir"]
10
+ RUBY_BIN_DIR = Config::CONFIG["bindir"]
11
+ RUBY_LIB_DIR = Config::CONFIG["libdir"]
12
+ RUBY_SHARED_LIB = Config::CONFIG["LIBRUBY"]
13
+ RUBY_SHARED_DLL = RUBY_SHARED_LIB.gsub(/lib$/, 'dll')
14
+
15
+ EXTENSION_NAME = "pg.#{Config::CONFIG["DLEXT"]}"
16
+
17
+ CLEAN.include('*.o')
18
+ CLOBBER.include(EXTENSION_NAME)
19
+
20
+ task :default => "pg"
21
+
22
+ DEFINES = "-DHAVE_LIBPQ_FE_H -DHAVE_LIBPQ_LIBPQ_FS_H -DHAVE_PQCONNECTIONUSEDPASSWORD -DHAVE_PQISTHREADSAFE -DHAVE_LO_CREATE -DHAVE_PQPREPARE -DHAVE_PQEXECPARAMS -DHAVE_PQESCAPESTRING -DHAVE_PQESCAPESTRINGCONN -DHAVE_PG_ENCODING_TO_CHAR -DHAVE_PQSETCLIENTENCODING"
23
+ LIBS = "-lpq -lm"
24
+ SRC = FileList['../*.c']
25
+ OBJ = SRC.collect do |file_name|
26
+ File.basename(file_name).ext('o')
27
+ end
28
+
29
+ SRC.each do |srcfile|
30
+ objfile = File.basename(srcfile).ext('o')
31
+ file objfile => srcfile do
32
+ command = "gcc -c -O2 -Wall #{DEFINES} -o #{objfile} -I/usr/local/include #{srcfile} -I#{RUBY_INCLUDE_DIR}"
33
+ sh "sh -c '#{command}'"
34
+ end
35
+ end
36
+
37
+ file "pg" => OBJ do
38
+ command = "gcc -shared -o #{EXTENSION_NAME} #{OBJ} -L/usr/local/lib #{LIBS} #{RUBY_BIN_DIR}/#{RUBY_SHARED_DLL}"
39
+ sh "sh -c '#{command}'"
40
+ end
@@ -1,7 +1,14 @@
1
1
  require 'rubygems'
2
2
  require 'mkrf'
3
3
 
4
- unless system("pg_config --bindir > /dev/null")
4
+ pg_config_command =
5
+ if RUBY_PLATFORM.match(/win32/)
6
+ "pg_config --bindir > nul"
7
+ else
8
+ "pg_config --bindir > /dev/null"
9
+ end
10
+
11
+ unless system(pg_config_command)
5
12
  $stderr.write("ERROR: can't find pg_config.\n")
6
13
  $stderr.write("HINT: Make sure pg_config is in your PATH\n")
7
14
  exit 1
data/ext/pg.c CHANGED
@@ -9,7 +9,7 @@
9
9
  modified at: Wed Jan 20 16:41:51 1999
10
10
 
11
11
  $Author: jdavis $
12
- $Date: 2008-10-05 12:43:27 -0700 (Sun, 05 Oct 2008) $
12
+ $Date: 2009-03-28 09:49:43 -0700 (Sat, 28 Mar 2009) $
13
13
  ************************************************/
14
14
 
15
15
  #include "pg.h"
@@ -79,17 +79,10 @@ pgconn_s_quote_connstr(VALUE string)
79
79
  }
80
80
  str[j++] = '\'';
81
81
  result = rb_str_new(str, j);
82
- free(str);
82
+ xfree(str);
83
83
  return result;
84
84
  }
85
85
 
86
- static char *
87
- value_as_cstring(VALUE in_str)
88
- {
89
- VALUE rstring = rb_obj_as_string(in_str);
90
- return StringValuePtr(rstring);
91
- }
92
-
93
86
  static void
94
87
  free_pgconn(PGconn *conn)
95
88
  {
@@ -228,9 +221,7 @@ static VALUE
228
221
  parse_connect_args(int argc, VALUE *argv, VALUE self)
229
222
  {
230
223
  VALUE args,arg;
231
- PGconn *conn = NULL;
232
224
  VALUE conninfo_rstr = rb_str_new("",0);
233
- VALUE error;
234
225
  char *host, *port, *opt, *tty, *dbname, *login, *pwd;
235
226
  host=port=opt=tty=dbname=login=pwd=NULL;
236
227
 
@@ -372,15 +363,16 @@ pgconn_init(int argc, VALUE *argv, VALUE self)
372
363
 
373
364
  if(conn == NULL)
374
365
  rb_raise(rb_ePGError, "PQconnectStart() unable to allocate structure");
366
+
367
+ Check_Type(self, T_DATA);
368
+ DATA_PTR(self) = conn;
369
+
375
370
  if (PQstatus(conn) == CONNECTION_BAD) {
376
371
  error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
377
372
  rb_iv_set(error, "@connection", self);
378
373
  rb_exc_raise(error);
379
374
  }
380
375
 
381
- Check_Type(self, T_DATA);
382
- DATA_PTR(self) = conn;
383
-
384
376
  if (rb_block_given_p()) {
385
377
  return rb_ensure(rb_yield, self, pgconn_finish, self);
386
378
  }
@@ -957,7 +949,7 @@ pgconn_exec(int argc, VALUE *argv, VALUE self)
957
949
  sym_type = ID2SYM(rb_intern("type"));
958
950
  sym_value = ID2SYM(rb_intern("value"));
959
951
  sym_format = ID2SYM(rb_intern("format"));
960
- nParams = RARRAY(params)->len;
952
+ nParams = RARRAY_LEN(params);
961
953
  paramTypes = ALLOC_N(Oid, nParams);
962
954
  paramValues = ALLOC_N(char *, nParams);
963
955
  paramLengths = ALLOC_N(int, nParams);
@@ -1010,10 +1002,10 @@ pgconn_exec(int argc, VALUE *argv, VALUE self)
1010
1002
 
1011
1003
  rb_gc_unregister_address(&gc_array);
1012
1004
 
1013
- free(paramTypes);
1014
- free(paramValues);
1015
- free(paramLengths);
1016
- free(paramFormats);
1005
+ xfree(paramTypes);
1006
+ xfree(paramValues);
1007
+ xfree(paramLengths);
1008
+ xfree(paramFormats);
1017
1009
 
1018
1010
  rb_pgresult = new_pgresult(result);
1019
1011
  pgresult_check(self, rb_pgresult);
@@ -1062,7 +1054,7 @@ pgconn_prepare(int argc, VALUE *argv, VALUE self)
1062
1054
 
1063
1055
  if(! NIL_P(in_paramtypes)) {
1064
1056
  Check_Type(in_paramtypes, T_ARRAY);
1065
- nParams = RARRAY(in_paramtypes)->len;
1057
+ nParams = RARRAY_LEN(in_paramtypes);
1066
1058
  paramTypes = ALLOC_N(Oid, nParams);
1067
1059
  for(i = 0; i < nParams; i++) {
1068
1060
  param = rb_ary_entry(in_paramtypes, i);
@@ -1076,7 +1068,7 @@ pgconn_prepare(int argc, VALUE *argv, VALUE self)
1076
1068
  result = PQprepare(conn, StringValuePtr(name), StringValuePtr(command),
1077
1069
  nParams, paramTypes);
1078
1070
 
1079
- free(paramTypes);
1071
+ xfree(paramTypes);
1080
1072
 
1081
1073
  rb_pgresult = new_pgresult(result);
1082
1074
  pgresult_check(self, rb_pgresult);
@@ -1148,7 +1140,7 @@ pgconn_exec_prepared(int argc, VALUE *argv, VALUE self)
1148
1140
  rb_gc_register_address(&gc_array);
1149
1141
  sym_value = ID2SYM(rb_intern("value"));
1150
1142
  sym_format = ID2SYM(rb_intern("format"));
1151
- nParams = RARRAY(params)->len;
1143
+ nParams = RARRAY_LEN(params);
1152
1144
  paramValues = ALLOC_N(char *, nParams);
1153
1145
  paramLengths = ALLOC_N(int, nParams);
1154
1146
  paramFormats = ALLOC_N(int, nParams);
@@ -1193,9 +1185,9 @@ pgconn_exec_prepared(int argc, VALUE *argv, VALUE self)
1193
1185
 
1194
1186
  rb_gc_unregister_address(&gc_array);
1195
1187
 
1196
- free(paramValues);
1197
- free(paramLengths);
1198
- free(paramFormats);
1188
+ xfree(paramValues);
1189
+ xfree(paramLengths);
1190
+ xfree(paramFormats);
1199
1191
 
1200
1192
  rb_pgresult = new_pgresult(result);
1201
1193
  pgresult_check(self, rb_pgresult);
@@ -1327,7 +1319,7 @@ pgconn_s_escape(VALUE self, VALUE string)
1327
1319
  RSTRING_LEN(string));
1328
1320
  }
1329
1321
  result = rb_str_new(escaped, size);
1330
- free(escaped);
1322
+ xfree(escaped);
1331
1323
  OBJ_INFECT(result, string);
1332
1324
  return result;
1333
1325
  }
@@ -1488,7 +1480,7 @@ pgconn_send_query(int argc, VALUE *argv, VALUE self)
1488
1480
  sym_type = ID2SYM(rb_intern("type"));
1489
1481
  sym_value = ID2SYM(rb_intern("value"));
1490
1482
  sym_format = ID2SYM(rb_intern("format"));
1491
- nParams = RARRAY(params)->len;
1483
+ nParams = RARRAY_LEN(params);
1492
1484
  paramTypes = ALLOC_N(Oid, nParams);
1493
1485
  paramValues = ALLOC_N(char *, nParams);
1494
1486
  paramLengths = ALLOC_N(int, nParams);
@@ -1541,10 +1533,10 @@ pgconn_send_query(int argc, VALUE *argv, VALUE self)
1541
1533
 
1542
1534
  rb_gc_unregister_address(&gc_array);
1543
1535
 
1544
- free(paramTypes);
1545
- free(paramValues);
1546
- free(paramLengths);
1547
- free(paramFormats);
1536
+ xfree(paramTypes);
1537
+ xfree(paramValues);
1538
+ xfree(paramLengths);
1539
+ xfree(paramFormats);
1548
1540
 
1549
1541
  if(result == 0) {
1550
1542
  error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
@@ -1592,7 +1584,7 @@ pgconn_send_prepare(int argc, VALUE *argv, VALUE self)
1592
1584
 
1593
1585
  if(! NIL_P(in_paramtypes)) {
1594
1586
  Check_Type(in_paramtypes, T_ARRAY);
1595
- nParams = RARRAY(in_paramtypes)->len;
1587
+ nParams = RARRAY_LEN(in_paramtypes);
1596
1588
  paramTypes = ALLOC_N(Oid, nParams);
1597
1589
  for(i = 0; i < nParams; i++) {
1598
1590
  param = rb_ary_entry(in_paramtypes, i);
@@ -1606,7 +1598,7 @@ pgconn_send_prepare(int argc, VALUE *argv, VALUE self)
1606
1598
  result = PQsendPrepare(conn, StringValuePtr(name), StringValuePtr(command),
1607
1599
  nParams, paramTypes);
1608
1600
 
1609
- free(paramTypes);
1601
+ xfree(paramTypes);
1610
1602
 
1611
1603
  if(result == 0) {
1612
1604
  error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
@@ -1681,7 +1673,7 @@ pgconn_send_query_prepared(int argc, VALUE *argv, VALUE self)
1681
1673
  rb_gc_register_address(&gc_array);
1682
1674
  sym_value = ID2SYM(rb_intern("value"));
1683
1675
  sym_format = ID2SYM(rb_intern("format"));
1684
- nParams = RARRAY(params)->len;
1676
+ nParams = RARRAY_LEN(params);
1685
1677
  paramValues = ALLOC_N(char *, nParams);
1686
1678
  paramLengths = ALLOC_N(int, nParams);
1687
1679
  paramFormats = ALLOC_N(int, nParams);
@@ -1727,9 +1719,9 @@ pgconn_send_query_prepared(int argc, VALUE *argv, VALUE self)
1727
1719
 
1728
1720
  rb_gc_unregister_address(&gc_array);
1729
1721
 
1730
- free(paramValues);
1731
- free(paramLengths);
1732
- free(paramFormats);
1722
+ xfree(paramValues);
1723
+ xfree(paramLengths);
1724
+ xfree(paramFormats);
1733
1725
 
1734
1726
  if(result == 0) {
1735
1727
  error = rb_exc_new2(rb_ePGError, PQerrorMessage(conn));
@@ -2138,7 +2130,6 @@ pgconn_set_error_verbosity(VALUE self, VALUE in_verbosity)
2138
2130
  static VALUE
2139
2131
  pgconn_trace(VALUE self, VALUE stream)
2140
2132
  {
2141
- FILE *fp;
2142
2133
  VALUE fileno;
2143
2134
  FILE *new_fp;
2144
2135
  int old_fd, new_fd;
@@ -2658,15 +2649,15 @@ static VALUE
2658
2649
  pgconn_loread(VALUE self, VALUE in_lo_desc, VALUE in_len)
2659
2650
  {
2660
2651
  int ret;
2661
- PGconn *conn = get_pgconn(self);
2652
+ PGconn *conn = get_pgconn(self);
2662
2653
  int len = NUM2INT(in_len);
2663
2654
  int lo_desc = NUM2INT(in_lo_desc);
2664
2655
  VALUE str;
2665
2656
  char *buffer;
2666
2657
 
2667
- buffer = malloc(len);
2658
+ buffer = ALLOC_N(char, len);
2668
2659
  if(buffer == NULL)
2669
- rb_raise(rb_eNoMemError, "Malloc failed!");
2660
+ rb_raise(rb_eNoMemError, "ALLOC failed!");
2670
2661
 
2671
2662
  if (len < 0){
2672
2663
  rb_raise(rb_ePGError,"nagative length %d given", len);
@@ -2676,12 +2667,12 @@ pgconn_loread(VALUE self, VALUE in_lo_desc, VALUE in_len)
2676
2667
  rb_raise(rb_ePGError, "lo_read failed");
2677
2668
 
2678
2669
  if(ret == 0) {
2679
- free(buffer);
2670
+ xfree(buffer);
2680
2671
  return Qnil;
2681
2672
  }
2682
2673
 
2683
2674
  str = rb_tainted_str_new(buffer, len);
2684
- free(buffer);
2675
+ xfree(buffer);
2685
2676
 
2686
2677
  return str;
2687
2678
  }
@@ -2759,7 +2750,7 @@ pgconn_loclose(VALUE self, VALUE in_lo_desc)
2759
2750
  PGconn *conn = get_pgconn(self);
2760
2751
  int lo_desc = NUM2INT(in_lo_desc);
2761
2752
 
2762
- if(lo_unlink(conn,lo_desc) < 0)
2753
+ if(lo_close(conn,lo_desc) < 0)
2763
2754
  rb_raise(rb_ePGError,"lo_close failed");
2764
2755
 
2765
2756
  return Qnil;