Module: PG
- Defined in:
- lib/tep/pg.rb
Overview
Public-facing PG module – mirrors the ruby-pg gem’s class layout. Callers write ‘PG.connect(…)`, `PG::Connection`, `PG::Result#each`, `rescue PG::UniqueViolation => e`, …
Defined Under Namespace
Classes: AdminShutdown, CheckViolation, Connection, ConnectionBad, ConnectionDoesNotExist, ConnectionException, DeadlockDetected, DuplicateColumn, DuplicateTable, Error, ExclusionViolation, ForeignKeyViolation, InFailedSqlTransaction, InsufficientPrivilege, IntegrityConstraintViolation, NotNullViolation, Pool, PoolExhausted, QueryCanceled, ReadOnlySqlTransaction, Result, SerializationFailure, ServerError, SyntaxError, UnableToSend, UndefinedColumn, UndefinedFunction, UndefinedTable, UniqueViolation
Constant Summary collapse
- CONNECTION_OK =
Connection status constants (libpq’s ConnStatusType collapsed to the two values tep cares about).
0- CONNECTION_BAD =
1- PQTRANS_IDLE =
Transaction status (libpq’s PGTransactionStatusType).
0- PQTRANS_ACTIVE =
1- PQTRANS_INTRANS =
2- PQTRANS_INERROR =
3- PQTRANS_UNKNOWN =
4- DIAG_SEVERITY =
Diagnostic field codes for Result#error_field. libpq uses single ASCII chars internally (PG_DIAG_SQLSTATE = ‘C’ = 67); expose them as integer constants here so callers can write ‘r.error_field(PG::DIAG_SQLSTATE)` without magic numbers.
83- DIAG_SQLSTATE =
‘S’
67- DIAG_MESSAGE_PRIMARY =
‘C’
77- DIAG_MESSAGE_DETAIL =
‘M’
68- DIAG_MESSAGE_HINT =
‘D’
72- DIAG_STATEMENT_POSITION =
‘H’
80- DIAG_CONTEXT =
‘P’
87- DIAG_SCHEMA_NAME =
‘W’
115- DIAG_TABLE_NAME =
‘s’
116- DIAG_COLUMN_NAME =
‘t’
99- DIAG_DATATYPE_NAME =
‘c’
100- DIAG_CONSTRAINT_NAME =
‘d’
110
Class Method Summary collapse
-
.connect(opts) ⇒ Object
Convenience constructor matching ruby-pg’s PG.connect entry.
-
.libpq_version ⇒ Object
libpq version string (“16.2.0” etc.).
-
.raise_for_sqlstate(state, msg) ⇒ Object
Raise the PG::Error subclass mapped from a 5-char SQLSTATE.
Class Method Details
.connect(opts) ⇒ Object
Convenience constructor matching ruby-pg’s PG.connect entry. opts is either a libpq conninfo String (“postgresql://…”) or a String=>String Hash of libpq keys (host, port, dbname, user, password, sslmode, …).
Unlike ruby-pg (which raises PG::ConnectionBad), connect does NOT raise on failure: it returns a connection-failed Connection (‘connected?` false, `last_error_message` set). This is deliberate – PG::Pool type-seeds its free list with `PG::Connection.new(“”)` at module load, before any server is reachable, so the constructor has to be non-raising. Check `conn.connected?` before use. (Query methods #exec / #exec_params DO raise; see Connection#exec.)
160 161 162 |
# File 'lib/tep/pg.rb', line 160 def self.connect(opts) Connection.new(opts) end |
.libpq_version ⇒ Object
libpq version string (“16.2.0” etc.). Diagnostic / banner use.
798 799 800 |
# File 'lib/tep/pg.rb', line 798 def self.libpq_version Pg.tep_pg_libpq_version end |
.raise_for_sqlstate(state, msg) ⇒ Object
Raise the PG::Error subclass mapped from a 5-char SQLSTATE. Connection#exec / #exec_params call this (via record_error_if_any) so a failed query surfaces as a typed exception – the ruby-pg / AR shape, where the adapter does ‘rescue PG::UniqueViolation` / `e.is_a?(PG::UndefinedTable)`. An unmapped SQLSTATE falls through to PG::ServerError, so `rescue PG::Error` still catches every server error. The mapping is the SQLSTATE-keyed subset the leaf classes cover (AR-coverage); add a leaf + an arm here together.
Literal-class dispatch (one ‘raise PG::Klass` per arm) rather than `raise klass_var` – raising a Class held in a local doesn’t lower under spinel; the constant-path raise is what matz/spinel#1041 made work.
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 |
# File 'lib/tep/pg.rb', line 886 def self.raise_for_sqlstate(state, msg) # 23 -- integrity constraint violation if state == "23502" raise PG::NotNullViolation, msg elsif state == "23503" raise PG::ForeignKeyViolation, msg elsif state == "23505" raise PG::UniqueViolation, msg elsif state == "23514" raise PG::CheckViolation, msg elsif state == "23P01" raise PG::ExclusionViolation, msg # 25 -- invalid transaction state elsif state == "25P02" raise PG::InFailedSqlTransaction, msg elsif state == "25006" raise PG::ReadOnlySqlTransaction, msg # 40 -- transaction rollback elsif state == "40001" raise PG::SerializationFailure, msg elsif state == "40P01" raise PG::DeadlockDetected, msg # 42 -- syntax / access rule violation elsif state == "42601" raise PG::SyntaxError, msg elsif state == "42703" raise PG::UndefinedColumn, msg elsif state == "42883" raise PG::UndefinedFunction, msg elsif state == "42P01" raise PG::UndefinedTable, msg elsif state == "42701" raise PG::DuplicateColumn, msg elsif state == "42P07" raise PG::DuplicateTable, msg elsif state == "42501" raise PG::InsufficientPrivilege, msg # 57 -- operator intervention elsif state == "57014" raise PG::QueryCanceled, msg elsif state == "57P01" raise PG::AdminShutdown, msg # 08 -- connection exception elsif state == "08000" raise PG::ConnectionException, msg elsif state == "08003" raise PG::ConnectionDoesNotExist, msg else raise PG::ServerError, msg end end |