Class: PgSqlCaller::Base
- Inherits:
-
Object
- Object
- PgSqlCaller::Base
- Extended by:
- Forwardable, SingleForwardable
- Includes:
- Singleton
- Defined in:
- lib/pg_sql_caller/base.rb
Constant Summary collapse
- CONNECTION_SQL_METHODS =
[ :select_value, :select_values, :execute, :select_all, :select_rows ].freeze
Class Method Summary collapse
- .define_sql_methods(*names) ⇒ Object
- .delegate(*names, **options) ⇒ Object
- .model_class(klass) ⇒ Object
Instance Method Summary collapse
- #current_database_name ⇒ Object
- #explain_analyze(sql) ⇒ Object
- #next_sequence_value(table_name) ⇒ Object
- #sanitize_sql_array(sql, *bindings) ⇒ Object
- #select_all_serialized(sql, *bindings) ⇒ Object
- #select_row(sql, *bindings) ⇒ Object
- #select_value_serialized(sql, *bindings) ⇒ Object
- #select_values_serialized(sql, *bindings) ⇒ Object
- #table_data_size(table_name) ⇒ Object
- #table_full_size(table_name) ⇒ Object
- #transaction ⇒ Object
- #transaction_open? ⇒ Boolean
- #typecast_array(values, type:) ⇒ Object
Class Method Details
.define_sql_methods(*names) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/pg_sql_caller/base.rb', line 39 def define_sql_methods(*names) names.each do |name| define_method(name) do |sql, *bindings| sql = sanitize_sql_array(sql, *bindings) if bindings.any? connection.send(name, sql) end end end |
.delegate(*names, **options) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/pg_sql_caller/base.rb', line 25 def delegate(*names, **) raise ArgumentError, 'provide at least one method name' if names.empty? target = .fetch(:to) type = .fetch(:type, :instance) raise ArgumentError, ':type can be :single or :instance' unless [:single, :instance].include?(type) if type == :instance instance_delegate names => target else single_delegate names => target end end |
.model_class(klass) ⇒ Object
49 50 51 |
# File 'lib/pg_sql_caller/base.rb', line 49 def model_class(klass) self._model_class = klass end |
Instance Method Details
#current_database_name ⇒ Object
142 143 144 |
# File 'lib/pg_sql_caller/base.rb', line 142 def current_database_name select_value('SELECT current_database();') end |
#explain_analyze(sql) ⇒ Object
127 128 129 130 |
# File 'lib/pg_sql_caller/base.rb', line 127 def explain_analyze(sql) result = select_values("EXPLAIN ANALYZE #{sql}") ['QUERY_PLAN', *result].join("\n") end |
#next_sequence_value(table_name) ⇒ Object
105 106 107 |
# File 'lib/pg_sql_caller/base.rb', line 105 def next_sequence_value(table_name) select_value("SELECT last_value FROM #{table_name}_id_seq") + 1 end |
#sanitize_sql_array(sql, *bindings) ⇒ Object
138 139 140 |
# File 'lib/pg_sql_caller/base.rb', line 138 def sanitize_sql_array(sql, *bindings) model_class.send :sanitize_sql_array, bindings.unshift(sql) end |
#select_all_serialized(sql, *bindings) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/pg_sql_caller/base.rb', line 82 def select_all_serialized(sql, *bindings) result = select_all(sql, *bindings) result.map do |row| row.map { |key, value| [key.to_sym, deserialize_result(result, key, value)] }.to_h end end |
#select_row(sql, *bindings) ⇒ Object
117 118 119 |
# File 'lib/pg_sql_caller/base.rb', line 117 def select_row(sql, *bindings) select_rows(sql, *bindings)[0] end |
#select_value_serialized(sql, *bindings) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/pg_sql_caller/base.rb', line 89 def select_value_serialized(sql, *bindings) result = select_all(sql, *bindings) key = result.first&.keys&.first return if key.nil? value = result.first.values.first deserialize_result(result, key, value) end |
#select_values_serialized(sql, *bindings) ⇒ Object
98 99 100 101 102 103 |
# File 'lib/pg_sql_caller/base.rb', line 98 def select_values_serialized(sql, *bindings) result = select_all(sql, *bindings) result.map do |row| row.map { |key, value| deserialize_result(result, key, value) } end end |
#table_data_size(table_name) ⇒ Object
113 114 115 |
# File 'lib/pg_sql_caller/base.rb', line 113 def table_data_size(table_name) select_value('SELECT pg_relation_size(?)', table_name) end |
#table_full_size(table_name) ⇒ Object
109 110 111 |
# File 'lib/pg_sql_caller/base.rb', line 109 def table_full_size(table_name) select_value('SELECT pg_total_relation_size(?)', table_name) end |
#transaction ⇒ Object
121 122 123 124 125 |
# File 'lib/pg_sql_caller/base.rb', line 121 def transaction raise ArgumentError, 'block must be given' unless block_given? connection.transaction { yield } end |
#transaction_open? ⇒ Boolean
78 79 80 |
# File 'lib/pg_sql_caller/base.rb', line 78 def transaction_open? connection.send(:transaction_open?) end |
#typecast_array(values, type:) ⇒ Object
132 133 134 135 136 |
# File 'lib/pg_sql_caller/base.rb', line 132 def typecast_array(values, type:) type = ActiveRecord::Type.lookup(type, array: true) data = type.serialize(values) data.encoder.encode(data.values) end |