pg_conn 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3ecdb9e5679acd6da6db9b98fc179941b5752727f7ec064bbb3168ed3ca33d33
4
- data.tar.gz: 28152343bf4490ad17f770799e4fa211662cab13cc19c639049d465a4c610f2a
3
+ metadata.gz: 410aa55b50b2d9c16aa9ab8a220475b83b91aa78e5ecb1a012e1a3524b2bcedb
4
+ data.tar.gz: '037319c6f45a570caedbcbb1ba1a725c738fa17771a1379ccab530d541d3270d'
5
5
  SHA512:
6
- metadata.gz: 68767284661c94cc8c0a5e610d967bfe8e31db67cf3a6998b68895ba1f0ca30e003e68036ac6e23788e0175226cb77a06c1ea883362dfb5386874aeeed21928c
7
- data.tar.gz: 251335c7c53cfd1cfac031576bb854aada2e034e64587a58932eb63cce35f48e9867579c52bcc48d91368e93d6d7beaddc1c5334c2207d063684cdbea4335ec4
6
+ metadata.gz: 6d1b677fbefbffd5189c29be2a185f76d5a85227c27f8e3a2883fe8db2a5b4c6054525975a93c5b3b4c0439009a70fe41d5a762493046090fa85d18dc381f87e
7
+ data.tar.gz: 03b617328dd1acd4bcfc1069c3d47b7bfc80771b924c39e7b91ad0c310f72577e6da3106e818769052f5fff7ee80ec43f2df6d127d76615b0278c353594ab70d
@@ -44,7 +44,7 @@ module PgConn
44
44
  conn.values stmt
45
45
  end
46
46
 
47
- # Return the owner of the database
47
+ # Return the owner of a given database
48
48
  def owner(database)
49
49
  conn.value %(
50
50
  select r.rolname
@@ -54,13 +54,13 @@ module PgConn
54
54
  )
55
55
  end
56
56
 
57
- # Return list of users currently logged in to the database or to any
57
+ # Return list of users currently logged in to the given database or to any
58
58
  # database if database is nil
59
59
  #
60
- # FIXME: There is a possible race-condition here where some process
61
- # (auto-vacuum) is logged in to the database but has a nil username. The
62
- # easy fix is to have usename not null but it would be nice to know what
63
- # exactly is triggering this problem
60
+ # FIXME: There is a possible race-condition here where some process (eg.
61
+ # auto-vacuum) is logged in to the database but has a nil username. The
62
+ # easy fix is to have 'usename is not null' but it would be nice to know
63
+ # what exactly is triggering this problem
64
64
  #
65
65
  def users(database)
66
66
  database_clause = database ? "datname = '#{database}'" : nil
@@ -69,7 +69,7 @@ module PgConn
69
69
  end
70
70
 
71
71
  # Hollow-out a database by removing all schemas in the database. The public
72
- # schema is recreated afterwards if :public is true. Uses the current
72
+ # schema is recreated afterwards unless if :public is false. Uses the current
73
73
  # database if @database is nil
74
74
  #
75
75
  # Note that the database can have active users logged in while the database
@@ -85,9 +85,11 @@ module PgConn
85
85
  .select { |schema| !exclude.include?(schema) }
86
86
  .join(", ")
87
87
  conn.exec "drop schema #{schemas} cascade"
88
+
89
+ # FIXME SECURITY Why grant 'create' to public?
88
90
  conn.exec %(
89
91
  create schema public authorization postgres;
90
- grant usage, create on schema public to public
92
+ grant usage, create on schema public to public
91
93
  ) if public
92
94
  ensure
93
95
  conn&.terminate if local
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.6.1"
2
+ VERSION = "0.7.0"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -49,8 +49,7 @@ module PgConn
49
49
  def name() @pg_connection.db end
50
50
  alias_method :database, :name # Obsolete
51
51
 
52
- # Database manipulation methods: #exist?, #create, #drop, #list. It is
53
- # named 'rdbms' because #database is already defined
52
+ # Database manipulation methods: #exist?, #create, #drop, #list
54
53
  attr_reader :rdbms
55
54
 
56
55
  # Role manipulation methods: #exist?, #create, #drop, #list
@@ -64,6 +63,17 @@ module PgConn
64
63
  # #exec or #transaction block
65
64
  attr_reader :timestamp
66
65
 
66
+ # PG::Error object if the last statement failed; otherwise nil
67
+ attr_reader :err
68
+
69
+ # Last error message. The error message is the first line of the PG error
70
+ # message that may contain additional info. It doesn't contain a
71
+ # terminating newline
72
+ def errmsg = err && err.message.sub(/\n.*/m, "")
73
+
74
+ # The line number of the last PG::Error. It is not always present
75
+ def errline() err && err.message =~ /\n\s*LINE\s+(\d+):/m and $1.to_i end
76
+
67
77
  # :call-seq:
68
78
  # initialize(dbname = nil, user = nil, field_name_class: Symbol)
69
79
  # initialize(connection_hash, field_name_class: Symbol)
@@ -79,10 +89,6 @@ module PgConn
79
89
  # of <key>=<value> pairs with the same keys as the hash, or a URI with the
80
90
  # format 'postgres[ql]://[user[:password]@][host][:port][/name]
81
91
  #
82
- # The :field_name_class option controls the Ruby type of column names. It can be
83
- # Symbol (the default) or String. The :timestamp option is used
84
- # internally to set the timestamp for transactions
85
- #
86
92
  # If given an array argument, PgConn will not connect to the database and
87
93
  # instead write its commands to the array. In this case, methods extracting
88
94
  # values from the database (eg. #value) will return nil or raise an
@@ -93,6 +99,10 @@ module PgConn
93
99
  # recommended except in cases where you want to piggyback on an existing
94
100
  # connection (eg. a Rails connection)
95
101
  #
102
+ # The :field_name_class option controls the Ruby type of column names. It can be
103
+ # Symbol (the default) or String. The :timestamp option is used
104
+ # internally to set the timestamp for transactions
105
+ #
96
106
  # Note that the connection hash and the connection string may support more
97
107
  # parameters than documented here. Consult
98
108
  # https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING
@@ -478,9 +488,10 @@ module PgConn
478
488
 
479
489
  # Execute SQL statement(s) in a transaction and return the number of
480
490
  # affected records (if any). Also sets #timestamp unless a transaction is
481
- # already in progress. The +sql+ argument can be a String or an arbitrarily
482
- # nested array of strings. The empty array is a NOP but the empty string is
483
- # not.
491
+ # already in progress. The +sql+ argument can be a command (String) or an
492
+ # arbitrarily nested array of commands. Note that you can't have commands
493
+ # that span multiple lines. The empty array is a NOP but the empty string
494
+ # is not.
484
495
  #
485
496
  # #exec pass Postgres exceptions to the caller unless :fail is false. If
486
497
  # fail is false #exec instead return nil but note that postgres doesn't
@@ -495,10 +506,10 @@ module PgConn
495
506
 
496
507
  # Execute SQL statement(s) without a transaction block and return the
497
508
  # number of affected records (if any). This used to call procedures that
498
- # may manipulate transactions. The +sql+ argument can be a String or
499
- # an arbitrarily nested array of strings. The empty array is a NOP but the
500
- # empty string is not. #exec pass Postgres exceptions to the caller unless
501
- # :fail is false in which case it returns nil
509
+ # may manipulate transactions. The +sql+ argument can be a SQL command or
510
+ # an arbitrarily nested array of commands. The empty array is a NOP but the
511
+ # empty string is not. #execute pass Postgres exceptions to the caller
512
+ # unless :fail is false in which case it returns nil
502
513
  #
503
514
  # TODO: Handle postgres exceptions wrt transaction state and stack
504
515
  def execute(sql, fail: true, silent: false)
@@ -589,7 +600,7 @@ module PgConn
589
600
  push_transaction
590
601
  result = yield
591
602
  rescue PgConn::Rollback
592
- pop_trancaction(commit: false)
603
+ pop_transaction(commit: false)
593
604
  return nil
594
605
  # FIXME: Rescue other postgres errors and wipe-out stack
595
606
  end
@@ -626,25 +637,27 @@ module PgConn
626
637
  PG::Connection.new *args, **opts
627
638
  end
628
639
  end
629
-
640
+
630
641
  # :call-seq:
631
642
  # pg_exec(string)
632
643
  # pg_exec(array)
633
644
  #
634
- # +arg+ can be a statement or an array of statements. The statements are
635
- # concatenated before being sent to the server. It returns a PG::Result
636
- # object or nil if +arg+ was empty. #exec pass Postgres exceptions to the
637
- # caller unless :fail is false
645
+ # Execute statement(s) on the server. If the argument is an array of
646
+ # commands, the commands are concatenated with ';' before being sent to the
647
+ # server. #pg_exec returns a PG::Result object or nil if +arg+ was empty.
648
+ # #exec pass Postgres exceptions to the caller unless :fail is false
638
649
  #
639
650
  # FIXME: Error message prints the last statement but what if another
640
651
  # statement failed?
641
652
  #
642
- # TODO WILD: Parse sql and split it into statements that are executed
643
- # one-by-one so we're able to pinpoint errors in the source
653
+ # TODO: Connsider executing statements one-by-one so we're able to
654
+ # pin-point Postgres errors without a line number. This may be expensive,
655
+ # though
644
656
  #
645
657
  # TODO: Fix silent by not handling exceptions
646
658
  def pg_exec(arg, fail: true, silent: false)
647
659
  if @pg_connection
660
+ @err = nil
648
661
  begin
649
662
  last_stmt = nil # To make the current SQL statement visible to the rescue clause. FIXME Not used?
650
663
  if arg.is_a?(String)
@@ -659,6 +672,7 @@ module PgConn
659
672
  end
660
673
 
661
674
  rescue PG::Error => ex
675
+ @err = ex
662
676
  if fail
663
677
  if !silent # FIXME Why do we handle this?
664
678
  $stderr.puts arg
@@ -670,6 +684,7 @@ module PgConn
670
684
  return nil
671
685
  end
672
686
  end
687
+
673
688
  else # @pg_commands is defined
674
689
  if arg.is_a?(String)
675
690
  @pg_commands << arg if arg != ""
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_conn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-06 00:00:00.000000000 Z
11
+ date: 2024-01-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg