pgsql 1.6 → 1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/lib/Rakefile +1 -1
  3. data/lib/conn.c +81 -87
  4. data/lib/module.c +1 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5906425e386ca8404df02a1d14dec2ec92e730cabd03c8192f624112d537c3d2
4
- data.tar.gz: 5bc40a7923bc2076aef6cdd8a2af71b679f01bee63fed44bbc475bf8e3a12dbb
3
+ metadata.gz: dc7ec22892e0bd75f3a0c63832471264c74e5bf758844f3fc87b9b5e2fb73b59
4
+ data.tar.gz: c1d31689274d2ccd7a97b4381b635f390886e419cbef1c44b7017839b8b520a9
5
5
  SHA512:
6
- metadata.gz: a32d74928d3a5c2d04dd30d4453e11a346ce7f4bb24db40665d375776b6caa89dabed59fa87774ab52fdb1d09a9d37e3e8eca13118e04a35572c33c8e827856c
7
- data.tar.gz: c405985704ae2c8a1bb52d957965a9b6ad4811bec41fe9785b3c56dba79c463fe1569ab27426a5853bfb1c5dfea4b10ae85505f7944bf5f6cdb6a0595174b895
6
+ metadata.gz: 2c94ab8fc0eb95ed34e1ffb84d0f90dd34d7ac4858b693217d94e36e913d5b02f9a7653459864f002d9d84cf02219b07191ee64757a5a42899cae6b8f31aa512
7
+ data.tar.gz: 4a893e13d60d470e8bde739c7f33f3d6260cd20670c746d115ebd93aa74531af69059c32a9dd59419fc68dd6ddd7d7630b12b892c615c1beeb4600aa336d9c31
data/lib/Rakefile CHANGED
@@ -17,7 +17,7 @@ DLs = {
17
17
  }
18
18
 
19
19
  DLs.each { |k,v|
20
- task k => v do |t|
20
+ file k => v do |t|
21
21
  l.cc t.name, t.prerequisites
22
22
  end
23
23
  }
data/lib/conn.c CHANGED
@@ -26,11 +26,11 @@ extern VALUE pgconn_mkstring( struct pgconn_data *ptr, const char *str);
26
26
  extern VALUE pgconn_mkstringn( struct pgconn_data *ptr, const char *str, int len);
27
27
  static VALUE pgconn_alloc( VALUE cls);
28
28
  static VALUE pgconn_s_connect( int argc, VALUE *argv, VALUE cls);
29
- static VALUE pgconn_s_parse( VALUE cls, VALUE str);
30
29
  static VALUE pgconn_init( int argc, VALUE *argv, VALUE self);
31
30
  static int set_connect_params( st_data_t key, st_data_t val, st_data_t args);
32
- static void connstr_to_hash( VALUE params, VALUE str);
33
- static void connstr_passwd( VALUE self, VALUE params);
31
+ static VALUE connstr_sym_dbname( void);
32
+ static VALUE connstr_sym_password( void);
33
+ static void connstr_passwd( VALUE self, VALUE orig_params, VALUE *params);
34
34
  static VALUE connstr_getparam( RB_BLOCK_CALL_FUNC_ARGLIST( yielded, params));
35
35
 
36
36
  static VALUE pgconn_close( VALUE self);
@@ -71,6 +71,9 @@ VALUE rb_cPgConn;
71
71
  static VALUE rb_ePgConnFailed;
72
72
  static VALUE rb_ePgConnInvalid;
73
73
 
74
+ static VALUE sym_dbname = Qundef;
75
+ static VALUE sym_password = Qundef;
76
+
74
77
 
75
78
 
76
79
  void
@@ -221,24 +224,6 @@ pgconn_s_connect( int argc, VALUE *argv, VALUE cls)
221
224
  rb_ensure( rb_yield, pgconn, pgconn_close, pgconn) : pgconn;
222
225
  }
223
226
 
224
- /*
225
- * call-seq:
226
- * Pg::Conn.parse( str) -> hash
227
- *
228
- * Parse a connection string and return a hash with keys <code>:dbname</code>,
229
- * <code>:user</code>, <code>:host</code>, etc.
230
- *
231
- */
232
- VALUE
233
- pgconn_s_parse( VALUE cls, VALUE str)
234
- {
235
- VALUE params;
236
-
237
- params = rb_hash_new();
238
- connstr_to_hash( params, str);
239
- return params;
240
- }
241
-
242
227
 
243
228
  /*
244
229
  * Document-method: Pg::Conn.new
@@ -252,23 +237,39 @@ pgconn_s_parse( VALUE cls, VALUE str)
252
237
  *
253
238
  * c = Pg::Conn.new :dbname => "movies", :host => "jupiter", ...
254
239
  *
255
- * The most common parameters may be given in a URL-like
256
- * connection string:
240
+ * If the +str+ argument is given but no further parameters,
241
+ * the PQconnectdb() (not PQconnectdbParams()) function will be called.
242
+ * If further arguments are present, the +str+ argument will be made into
243
+ * the "dbname" parameter, but without overwriting an existing one.
244
+ *
245
+ * The "dbname" parameter may be a +conninfo+ string as described in the
246
+ * PostgreSQL documentation:
257
247
  *
258
- * "user:password@host:port/dbname"
248
+ * c = Pg::Conn.new "postgresql://user:password@host:port/dbname"
249
+ * c = Pg::Conn.new "postgres://user:password@host:port/dbname"
250
+ * c = Pg::Conn.new "dbname=... host=... user=..."
259
251
  *
260
- * Any of these parts may be omitted. If there is no slash, the part after the
261
- * @ sign will be read as database name.
252
+ * The password may be specified as an extra parameter:
253
+ *
254
+ * c = Pg::Conn.new "postgresql://user@host/dbname", password: "verysecret"
262
255
  *
263
256
  * If the password is the empty string, and there is either an instance method
264
257
  * or a class method <code>password?</code>, that method will be asked. This
265
258
  * method may ask <code>yield :user</code> or <code>yield :dbname</code> and so
266
259
  * on to get the connection parameters.
267
260
  *
261
+ * class Pg::Conn
262
+ * def password?
263
+ * "tercesyrev".reverse
264
+ * end
265
+ * end
266
+ * c = Pg::Conn.new "postgresql://user@host/dbname", password: ""
267
+ *
268
268
  * See the PostgreSQL documentation for a full list:
269
269
  * [http://www.postgresql.org/docs/current/interactive/libpq-connect.html#LIBPQ-PQCONNECTDBPARAMS]
270
270
  *
271
271
  * On failure, a +Pg::Error+ exception will be raised.
272
+ *
272
273
  */
273
274
  VALUE
274
275
  pgconn_init( int argc, VALUE *argv, VALUE self)
@@ -280,32 +281,44 @@ pgconn_init( int argc, VALUE *argv, VALUE self)
280
281
  struct pgconn_data *c;
281
282
 
282
283
  if (rb_scan_args( argc, argv, "02", &str, &params) < 2)
283
- if (TYPE( str) != T_STRING) {
284
+ if (TYPE( str) == T_HASH) {
284
285
  params = str;
285
286
  str = Qnil;
286
287
  }
287
- if (NIL_P( params))
288
- params = rb_hash_new();
289
- else if (TYPE( params) != T_HASH)
290
- params = rb_convert_type( params, T_HASH, "Hash", "to_hash");
291
- else
292
- params = rb_obj_dup( params);
293
- if (!NIL_P( str))
294
- connstr_to_hash( params, str);
295
- connstr_passwd( self, params);
296
288
 
297
289
  Data_Get_Struct( self, struct pgconn_data, c);
298
290
 
299
- l = RHASH_SIZE( params) + 1;
300
- keywords = (const char **) ALLOCA_N( char *, l);
301
- values = (const char **) ALLOCA_N( char *, l);
302
- ptrs[ 0] = keywords;
303
- ptrs[ 1] = values;
304
- ptrs[ 2] = (const char **) c;
305
- st_foreach( RHASH_TBL( params), &set_connect_params, (st_data_t) ptrs);
306
- *(ptrs[ 0]) = *(ptrs[ 1]) = NULL;
307
-
308
- c->conn = PQconnectdbParams( keywords, values, 1);
291
+ if (NIL_P( params)) {
292
+ StringValue( str);
293
+ c->conn = PQconnectdb( RSTRING_PTR( str));
294
+ } else {
295
+ int expand_dbname;
296
+ VALUE orig_params = params;
297
+
298
+ if (TYPE( params) != T_HASH)
299
+ params = rb_convert_type( params, T_HASH, "Hash", "to_hash");
300
+
301
+ if (!NIL_P( str) && NIL_P( rb_hash_aref( params, connstr_sym_dbname()))) {
302
+ if (params == orig_params)
303
+ params = rb_obj_dup( params);
304
+ rb_hash_aset( params, sym_dbname, str);
305
+ expand_dbname = 1;
306
+ } else
307
+ expand_dbname = 0;
308
+
309
+ connstr_passwd( self, orig_params, &params);
310
+
311
+ l = RHASH_SIZE( params) + 1;
312
+ keywords = (const char **) ALLOCA_N( char *, l);
313
+ values = (const char **) ALLOCA_N( char *, l);
314
+ ptrs[ 0] = keywords;
315
+ ptrs[ 1] = values;
316
+ ptrs[ 2] = (const char **) c;
317
+ st_foreach( RHASH_TBL( params), &set_connect_params, (st_data_t) ptrs);
318
+ *(ptrs[ 0]) = *(ptrs[ 1]) = NULL;
319
+
320
+ c->conn = PQconnectdbParams( keywords, values, expand_dbname);
321
+ }
309
322
  if (PQstatus( c->conn) == CONNECTION_BAD)
310
323
  rb_exc_raise( pgconnfailederror_new( c, params));
311
324
 
@@ -331,50 +344,29 @@ set_connect_params( st_data_t key, st_data_t val, st_data_t args)
331
344
  return ST_CONTINUE;
332
345
  }
333
346
 
334
- static const char re_connstr[] =
335
- "\\A"
336
- "(?:(.*?)(?::(.*))?@)?" /* user:passwd@ */
337
- "(?:(.*?)(?::(\\d+))?/)?(?:(.+))?" /* host:port/dbname */
338
- "\\z"
339
- ;
340
-
341
- #define KEY_USER "user"
342
- #define KEY_PASSWORD "password"
343
- #define KEY_HOST "host"
344
- #define KEY_PORT "port"
345
- #define KEY_DBNAME "dbname"
347
+ VALUE
348
+ connstr_sym_dbname( void)
349
+ {
350
+ if (sym_dbname == Qundef)
351
+ sym_dbname = ID2SYM( rb_intern( "dbname"));
352
+ return sym_dbname;
353
+ }
346
354
 
347
- void
348
- connstr_to_hash( VALUE params, VALUE str)
349
- {
350
- VALUE re, match, k, m;
351
-
352
- re = rb_reg_new( re_connstr, sizeof re_connstr - 1, 0);
353
- if (RTEST( rb_reg_match( re, str))) {
354
- match = rb_backref_get();
355
- #define ADD_TO_RES( key, n) \
356
- k = ID2SYM( rb_intern( key)); m = rb_reg_nth_match( n, match); \
357
- if (NIL_P( rb_hash_aref( params, k)) && !NIL_P(m)) \
358
- rb_hash_aset( params, k, m)
359
- ADD_TO_RES( KEY_USER, 1);
360
- ADD_TO_RES( KEY_PASSWORD, 2);
361
- ADD_TO_RES( KEY_HOST, 3);
362
- ADD_TO_RES( KEY_PORT, 4);
363
- ADD_TO_RES( KEY_DBNAME, 5);
364
- #undef ADD_TO_RES
365
- } else
366
- rb_raise( rb_eArgError, "Invalid connection: %s", RSTRING_PTR( str));
355
+ VALUE
356
+ connstr_sym_password( void)
357
+ {
358
+ if (sym_password == Qundef)
359
+ sym_password = ID2SYM( rb_intern( "password"));
360
+ return sym_password;
367
361
  }
368
362
 
363
+
369
364
  void
370
- connstr_passwd( VALUE self, VALUE params)
365
+ connstr_passwd( VALUE self, VALUE orig_params, VALUE *params)
371
366
  {
372
- static VALUE sym_password = Qundef;
373
367
  VALUE pw;
374
368
 
375
- if (sym_password == Qundef)
376
- sym_password = ID2SYM( rb_intern( KEY_PASSWORD));
377
- pw = rb_hash_aref( params, sym_password);
369
+ pw = rb_hash_aref( *params, connstr_sym_password());
378
370
  if (TYPE( pw) == T_STRING && RSTRING_LEN( pw) == 0) {
379
371
  static ID id_password_q = 0;
380
372
  VALUE pwobj;
@@ -386,10 +378,13 @@ connstr_passwd( VALUE self, VALUE params)
386
378
  pwobj = self;
387
379
  if (rb_respond_to( CLASS_OF( self), id_password_q))
388
380
  pwobj = CLASS_OF( self);
389
- if (pwobj != Qundef)
390
- rb_hash_aset( params, sym_password,
381
+ if (pwobj != Qundef) {
382
+ if (*params == orig_params)
383
+ *params = rb_obj_dup( *params);
384
+ rb_hash_aset( *params, sym_password,
391
385
  rb_block_call( pwobj, id_password_q, 0, NULL,
392
- &connstr_getparam, params));
386
+ &connstr_getparam, *params));
387
+ }
393
388
  }
394
389
  }
395
390
 
@@ -866,7 +861,6 @@ Init_pgsql_conn( void)
866
861
  rb_define_alloc_func( rb_cPgConn, pgconn_alloc);
867
862
  rb_define_singleton_method( rb_cPgConn, "connect", pgconn_s_connect, -1);
868
863
  rb_define_alias( rb_singleton_class( rb_cPgConn), "open", "connect");
869
- rb_define_singleton_method( rb_cPgConn, "parse", pgconn_s_parse, 1);
870
864
  rb_define_method( rb_cPgConn, "initialize", &pgconn_init, -1);
871
865
  rb_define_method( rb_cPgConn, "close", &pgconn_close, 0);
872
866
  rb_define_alias( rb_cPgConn, "finish", "close");
data/lib/module.c CHANGED
@@ -8,7 +8,7 @@
8
8
  #include "result.h"
9
9
 
10
10
 
11
- #define PGSQL_VERSION "1.6"
11
+ #define PGSQL_VERSION "1.7"
12
12
 
13
13
 
14
14
  VALUE rb_mPg;
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgsql
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.6'
4
+ version: '1.7'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bertram Scharpf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-04 00:00:00.000000000 Z
11
+ date: 2021-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: autorake