| 1 |
require 'active_record/connection_adapters/abstract_adapter' |
|---|
| 2 |
|
|---|
| 3 |
begin |
|---|
| 4 |
require_library_or_gem 'pg' |
|---|
| 5 |
rescue LoadError => e |
|---|
| 6 |
begin |
|---|
| 7 |
require_library_or_gem 'postgres' |
|---|
| 8 |
class PGresult |
|---|
| 9 |
alias_method :nfields, :num_fields unless self.method_defined?(:nfields) |
|---|
| 10 |
alias_method :ntuples, :num_tuples unless self.method_defined?(:ntuples) |
|---|
| 11 |
alias_method :ftype, :type unless self.method_defined?(:ftype) |
|---|
| 12 |
alias_method :cmd_tuples, :cmdtuples unless self.method_defined?(:cmd_tuples) |
|---|
| 13 |
end |
|---|
| 14 |
rescue LoadError |
|---|
| 15 |
raise e |
|---|
| 16 |
end |
|---|
| 17 |
end |
|---|
| 18 |
|
|---|
| 19 |
module ActiveRecord |
|---|
| 20 |
class Base |
|---|
| 21 |
|
|---|
| 22 |
def self.postgresql_connection(config) |
|---|
| 23 |
config = config.symbolize_keys |
|---|
| 24 |
host = config[:host] |
|---|
| 25 |
port = config[:port] || 5432 |
|---|
| 26 |
username = config[:username].to_s |
|---|
| 27 |
password = config[:password].to_s |
|---|
| 28 |
|
|---|
| 29 |
if config.has_key?(:database) |
|---|
| 30 |
database = config[:database] |
|---|
| 31 |
else |
|---|
| 32 |
raise ArgumentError, "No database specified. Missing argument: database." |
|---|
| 33 |
end |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
ConnectionAdapters::PostgreSQLAdapter.new(nil, logger, [host, port, nil, nil, database, username, password], config) |
|---|
| 38 |
end |
|---|
| 39 |
end |
|---|
| 40 |
|
|---|
| 41 |
module ConnectionAdapters |
|---|
| 42 |
|
|---|
| 43 |
class PostgreSQLColumn < Column |
|---|
| 44 |
|
|---|
| 45 |
def initialize(name, default, sql_type = nil, null = true) |
|---|
| 46 |
super(name, self.class.extract_value_from_default(default), sql_type, null) |
|---|
| 47 |
end |
|---|
| 48 |
|
|---|
| 49 |
private |
|---|
| 50 |
|
|---|
| 51 |
def extract_scale(sql_type) |
|---|
| 52 |
|
|---|
| 53 |
sql_type =~ /^money/ ? 2 : super |
|---|
| 54 |
end |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
def extract_precision(sql_type) |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
super |
|---|
| 61 |
end |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
def self.string_to_binary(value) |
|---|
| 65 |
if PGconn.respond_to?(:escape_bytea) |
|---|
| 66 |
self.class.module_eval do |
|---|
| 67 |
define_method(:string_to_binary) do |value| |
|---|
| 68 |
PGconn.escape_bytea(value) if value |
|---|
| 69 |
end |
|---|
| 70 |
end |
|---|
| 71 |
else |
|---|
| 72 |
self.class.module_eval do |
|---|
| 73 |
define_method(:string_to_binary) do |value| |
|---|
| 74 |
if value |
|---|
| 75 |
result = '' |
|---|
| 76 |
value.each_byte { |c| result << sprintf('\\\\%03o', c) } |
|---|
| 77 |
result |
|---|
| 78 |
end |
|---|
| 79 |
end |
|---|
| 80 |
end |
|---|
| 81 |
end |
|---|
| 82 |
self.class.string_to_binary(value) |
|---|
| 83 |
end |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
def self.binary_to_string(value) |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
if PGconn.respond_to?(:unescape_bytea) |
|---|
| 90 |
self.class.module_eval do |
|---|
| 91 |
define_method(:binary_to_string) do |value| |
|---|
| 92 |
if value =~ /\\\d{3}/ |
|---|
| 93 |
PGconn.unescape_bytea(value) |
|---|
| 94 |
else |
|---|
| 95 |
value |
|---|
| 96 |
end |
|---|
| 97 |
end |
|---|
| 98 |
end |
|---|
| 99 |
else |
|---|
| 100 |
self.class.module_eval do |
|---|
| 101 |
define_method(:binary_to_string) do |value| |
|---|
| 102 |
if value =~ /\\\d{3}/ |
|---|
| 103 |
result = '' |
|---|
| 104 |
i, max = 0, value.size |
|---|
| 105 |
while i < max |
|---|
| 106 |
char = value[i] |
|---|
| 107 |
if char == ?\\ |
|---|
| 108 |
if value[i+1] == ?\\ |
|---|
| 109 |
char = ?\\ |
|---|
| 110 |
i += 1 |
|---|
| 111 |
else |
|---|
| 112 |
char = value[i+1..i+3].oct |
|---|
| 113 |
i += 3 |
|---|
| 114 |
end |
|---|
| 115 |
end |
|---|
| 116 |
result << char |
|---|
| 117 |
i += 1 |
|---|
| 118 |
end |
|---|
| 119 |
result |
|---|
| 120 |
else |
|---|
| 121 |
value |
|---|
| 122 |
end |
|---|
| 123 |
end |
|---|
| 124 |
end |
|---|
| 125 |
end |
|---|
| 126 |
self.class.binary_to_string(value) |
|---|
| 127 |
end |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
def simplified_type(field_type) |
|---|
| 131 |
case field_type |
|---|
| 132 |
|
|---|
| 133 |
when /^(?:real|double precision)$/ |
|---|
| 134 |
:float |
|---|
| 135 |
|
|---|
| 136 |
when /^money$/ |
|---|
| 137 |
:decimal |
|---|
| 138 |
|
|---|
| 139 |
when /^(?:character varying|bpchar)(?:\(\d+\))?$/ |
|---|
| 140 |
:string |
|---|
| 141 |
|
|---|
| 142 |
when /^bytea$/ |
|---|
| 143 |
:binary |
|---|
| 144 |
|
|---|
| 145 |
when /^timestamp with(?:out)? time zone$/ |
|---|
| 146 |
:datetime |
|---|
| 147 |
when /^interval$/ |
|---|
| 148 |
:string |
|---|
| 149 |
|
|---|
| 150 |
when /^(?:point|line|lseg|box|"?path"?|polygon|circle)$/ |
|---|
| 151 |
:string |
|---|
| 152 |
|
|---|
| 153 |
when /^(?:cidr|inet|macaddr)$/ |
|---|
| 154 |
:string |
|---|
| 155 |
|
|---|
| 156 |
when /^bit(?: varying)?(?:\(\d+\))?$/ |
|---|
| 157 |
:string |
|---|
| 158 |
|
|---|
| 159 |
when /^xml$/ |
|---|
| 160 |
:string |
|---|
| 161 |
|
|---|
| 162 |
when /^\D+\[\]$/ |
|---|
| 163 |
:string |
|---|
| 164 |
|
|---|
| 165 |
when /^oid$/ |
|---|
| 166 |
:integer |
|---|
| 167 |
|
|---|
| 168 |
else |
|---|
| 169 |
super |
|---|
| 170 |
end |
|---|
| 171 |
end |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
def self.extract_value_from_default(default) |
|---|
| 175 |
case default |
|---|
| 176 |
|
|---|
| 177 |
when /\A-?\d+(\.\d*)?\z/ |
|---|
| 178 |
default |
|---|
| 179 |
|
|---|
| 180 |
when /\A'(.*)'::(?:character varying|bpchar|text)\z/m |
|---|
| 181 |
$1 |
|---|
| 182 |
|
|---|
| 183 |
when /\AE'(.*)'::(?:character varying|bpchar|text)\z/m |
|---|
| 184 |
$1.gsub(/\\(\d\d\d)/) { $1.oct.chr } |
|---|
| 185 |
|
|---|
| 186 |
when /\A'(.*)'::bytea\z/m |
|---|
| 187 |
$1 |
|---|
| 188 |
|
|---|
| 189 |
when /\A'(.+)'::(?:time(?:stamp)? with(?:out)? time zone|date)\z/ |
|---|
| 190 |
$1 |
|---|
| 191 |
when /\A'(.*)'::interval\z/ |
|---|
| 192 |
$1 |
|---|
| 193 |
|
|---|
| 194 |
when 'true' |
|---|
| 195 |
true |
|---|
| 196 |
when 'false' |
|---|
| 197 |
false |
|---|
| 198 |
|
|---|
| 199 |
when /\A'(.*)'::(?:point|line|lseg|box|"?path"?|polygon|circle)\z/ |
|---|
| 200 |
$1 |
|---|
| 201 |
|
|---|
| 202 |
when /\A'(.*)'::(?:cidr|inet|macaddr)\z/ |
|---|
| 203 |
$1 |
|---|
| 204 |
|
|---|
| 205 |
when /\AB'(.*)'::"?bit(?: varying)?"?\z/ |
|---|
| 206 |
$1 |
|---|
| 207 |
|
|---|
| 208 |
when /\A'(.*)'::xml\z/m |
|---|
| 209 |
$1 |
|---|
| 210 |
|
|---|
| 211 |
when /\A'(.*)'::"?\D+"?\[\]\z/ |
|---|
| 212 |
$1 |
|---|
| 213 |
|
|---|
| 214 |
when /\A-?\d+\z/ |
|---|
| 215 |
$1 |
|---|
| 216 |
else |
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
nil |
|---|
| 220 |
end |
|---|
| 221 |
end |
|---|
| 222 |
end |
|---|
| 223 |
end |
|---|
| 224 |
|
|---|
| 225 |
module ConnectionAdapters |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
class PostgreSQLAdapter < AbstractAdapter |
|---|
| 241 |
|
|---|
| 242 |
def adapter_name |
|---|
| 243 |
'PostgreSQL' |
|---|
| 244 |
end |
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
def initialize(connection, logger, connection_parameters, config) |
|---|
| 248 |
super(connection, logger) |
|---|
| 249 |
@connection_parameters, @config = connection_parameters, config |
|---|
| 250 |
|
|---|
| 251 |
connect |
|---|
| 252 |
end |
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
def active? |
|---|
| 256 |
if @connection.respond_to?(:status) |
|---|
| 257 |
@connection.status == PGconn::CONNECTION_OK |
|---|
| 258 |
else |
|---|
| 259 |
|
|---|
| 260 |
@connection.query 'SELECT 1' |
|---|
| 261 |
true |
|---|
| 262 |
end |
|---|
| 263 |
|
|---|
| 264 |
rescue PGError, NoMethodError |
|---|
| 265 |
false |
|---|
| 266 |
end |
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
def reconnect! |
|---|
| 270 |
if @connection.respond_to?(:reset) |
|---|
| 271 |
@connection.reset |
|---|
| 272 |
configure_connection |
|---|
| 273 |
else |
|---|
| 274 |
disconnect! |
|---|
| 275 |
connect |
|---|
| 276 |
end |
|---|
| 277 |
end |
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
def disconnect! |
|---|
| 281 |
@connection.close rescue nil |
|---|
| 282 |
end |
|---|
| 283 |
|
|---|
| 284 |
def native_database_types |
|---|
| 285 |
{ |
|---|
| 286 |
:primary_key => "serial primary key", |
|---|
| 287 |
:string => { :name => "character varying", :limit => 255 }, |
|---|
| 288 |
:text => { :name => "text" }, |
|---|
| 289 |
:integer => { :name => "integer" }, |
|---|
| 290 |
:float => { :name => "float" }, |
|---|
| 291 |
:decimal => { :name => "decimal" }, |
|---|
| 292 |
:datetime => { :name => "timestamp" }, |
|---|
| 293 |
:timestamp => { :name => "timestamp" }, |
|---|
| 294 |
:time => { :name => "time" }, |
|---|
| 295 |
:date => { :name => "date" }, |
|---|
| 296 |
:binary => { :name => "bytea" }, |
|---|
| 297 |
:boolean => { :name => "boolean" } |
|---|
| 298 |
} |
|---|
| 299 |
end |
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
def supports_migrations? |
|---|
| 303 |
true |
|---|
| 304 |
end |
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
def supports_standard_conforming_strings? |
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 |
client_min_messages_old = client_min_messages |
|---|
| 312 |
self.client_min_messages = 'panic' |
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 |
has_support = query('SHOW standard_conforming_strings')[0][0] rescue false |
|---|
| 318 |
self.client_min_messages = client_min_messages_old |
|---|
| 319 |
has_support |
|---|
| 320 |
end |
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
def table_alias_length |
|---|
| 325 |
@table_alias_length ||= (postgresql_version >= 80000 ? query('SHOW max_identifier_length')[0][0].to_i : 63) |
|---|
| 326 |
end |
|---|
| 327 |
|
|---|
| 328 |
|
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
def quote(value, column = nil) |
|---|
| 332 |
if value.kind_of?(String) && column && column.type == :binary |
|---|
| 333 |
"#{quoted_string_prefix}'#{column.class.string_to_binary(value)}'" |
|---|
| 334 |
elsif value.kind_of?(String) && column && column.sql_type =~ /^xml$/ |
|---|
| 335 |
"xml '#{quote_string(value)}'" |
|---|
| 336 |
elsif value.kind_of?(Numeric) && column && column.sql_type =~ /^money$/ |
|---|
| 337 |
|
|---|
| 338 |
"'#{value.to_s}'" |
|---|
| 339 |
elsif value.kind_of?(String) && column && column.sql_type =~ /^bit/ |
|---|
| 340 |
case value |
|---|
| 341 |
when /^[01]*$/ |
|---|
| 342 |
"B'#{value}'" |
|---|
| 343 |
when /^[0-9A-F]*$/i |
|---|
| 344 |
"X'#{value}'" |
|---|
| 345 |
end |
|---|
| 346 |
else |
|---|
| 347 |
super |
|---|
| 348 |
end |
|---|
| 349 |
end |
|---|
| 350 |
|
|---|
| 351 |
|
|---|
| 352 |
def quote_string(s) |
|---|
| 353 |
if PGconn.respond_to?(:escape) |
|---|
| 354 |
self.class.instance_eval do |
|---|
| 355 |
define_method(:quote_string) do |s| |
|---|
| 356 |
PGconn.escape(s) |
|---|
| 357 |
end |
|---|
| 358 |
end |
|---|
| 359 |
else |
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
self.class.instance_eval do |
|---|
| 363 |
undef_method(:quote_string) |
|---|
| 364 |
end |
|---|
| 365 |
end |
|---|
| 366 |
quote_string(s) |
|---|
| 367 |
end |
|---|
| 368 |
|
|---|
| 369 |
|
|---|
| 370 |
def quote_column_name(name) |
|---|
| 371 |
%("#{name}") |
|---|
| 372 |
end |
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 |
def quoted_date(value) |
|---|
| 377 |
if value.acts_like?(:time) && value.respond_to?(:usec) |
|---|
| 378 |
"#{super}.#{sprintf("%06d", value.usec)}" |
|---|
| 379 |
else |
|---|
| 380 |
super |
|---|
| 381 |
end |
|---|
| 382 |
end |
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 |
def supports_disable_referential_integrity?() |
|---|
| 387 |
version = query("SHOW server_version")[0][0].split('.') |
|---|
| 388 |
(version[0].to_i >= 8 && version[1].to_i >= 1) ? true : false |
|---|
| 389 |
rescue |
|---|
| 390 |
return false |
|---|
| 391 |
end |
|---|
| 392 |
|
|---|
| 393 |
def disable_referential_integrity(&block) |
|---|
| 394 |
if supports_disable_referential_integrity?() then |
|---|
| 395 |
execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} DISABLE TRIGGER ALL" }.join(";")) |
|---|
| 396 |
end |
|---|
| 397 |
yield |
|---|
| 398 |
ensure |
|---|
| 399 |
if supports_disable_referential_integrity?() then |
|---|
| 400 |
execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} ENABLE TRIGGER ALL" }.join(";")) |
|---|
| 401 |
end |
|---|
| 402 |
end |
|---|
| 403 |
|
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
|
|---|
| 407 |
|
|---|
| 408 |
def select_rows(sql, name = nil) |
|---|
| 409 |
select_raw(sql, name).last |
|---|
| 410 |
end |
|---|
| 411 |
|
|---|
| 412 |
|
|---|
| 413 |
def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) |
|---|
| 414 |
table = sql.split(" ", 4)[2].gsub('"', '') |
|---|
| 415 |
super || pk && last_insert_id(table, sequence_name || default_sequence_name(table, pk)) |
|---|
| 416 |
end |
|---|
| 417 |
|
|---|
| 418 |
|
|---|
| 419 |
def result_as_array(res) |
|---|
| 420 |
ary = [] |
|---|
| 421 |
for i in 0...res.ntuples do |
|---|
| 422 |
ary << [] |
|---|
| 423 |
for j in 0...res.nfields do |
|---|
| 424 |
ary[i] << res.getvalue(i,j) |
|---|
| 425 |
end |
|---|
| 426 |
end |
|---|
| 427 |
return ary |
|---|
| 428 |
end |
|---|
| 429 |
|
|---|
| 430 |
|
|---|
| 431 |
|
|---|
| 432 |
def query(sql, name = nil) |
|---|
| 433 |
log(sql, name) do |
|---|
| 434 |
if @async |
|---|
| 435 |
res = @connection.async_exec(sql) |
|---|
| 436 |
else |
|---|
| 437 |
res = @connection.exec(sql) |
|---|
| 438 |
end |
|---|
| 439 |
return result_as_array(res) |
|---|
| 440 |
end |
|---|
| 441 |
end |
|---|
| 442 |
|
|---|
| 443 |
|
|---|
| 444 |
|
|---|
| 445 |
def execute(sql, name = nil) |
|---|
| 446 |
log(sql, name) do |
|---|
| 447 |
if @async |
|---|
| 448 |
@connection.async_exec(sql) |
|---|
| 449 |
else |
|---|
| 450 |
@connection.exec(sql) |
|---|
| 451 |
end |
|---|
| 452 |
end |
|---|
| 453 |
end |
|---|
| 454 |
|
|---|
| 455 |
|
|---|
| 456 |
def update_sql(sql, name = nil) |
|---|
| 457 |
super.cmd_tuples |
|---|
| 458 |
end |
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 |
def begin_db_transaction |
|---|
| 462 |
execute "BEGIN" |
|---|
| 463 |
end |
|---|
| 464 |
|
|---|
| 465 |
|
|---|
| 466 |
def commit_db_transaction |
|---|
| 467 |
execute "COMMIT" |
|---|
| 468 |
end |
|---|
| 469 |
|
|---|
| 470 |
|
|---|
| 471 |
def rollback_db_transaction |
|---|
| 472 |
execute "ROLLBACK" |
|---|
| 473 |
end |
|---|
| 474 |
|
|---|
| 475 |
|
|---|
| 476 |
|
|---|
| 477 |
def recreate_database(name) |
|---|
| 478 |
drop_database(name) |
|---|
| 479 |
create_database(name) |
|---|
| 480 |
end |
|---|
| 481 |
|
|---|
| 482 |
|
|---|
| 483 |
|
|---|
| 484 |
|
|---|
| 485 |
|
|---|
| 486 |
|
|---|
| 487 |
|
|---|
| 488 |
|
|---|
| 489 |
def create_database(name, options = {}) |
|---|
| 490 |
options = options.reverse_merge(:encoding => "utf8") |
|---|
| 491 |
|
|---|
| 492 |
option_string = options.symbolize_keys.sum do |key, value| |
|---|
| 493 |
case key |
|---|
| 494 |
when :owner |
|---|
| 495 |
" OWNER = '#{value}'" |
|---|
| 496 |
when :template |
|---|
| 497 |
" TEMPLATE = #{value}" |
|---|
| 498 |
when :encoding |
|---|
| 499 |
" ENCODING = '#{value}'" |
|---|
| 500 |
when :tablespace |
|---|
| 501 |
" TABLESPACE = #{value}" |
|---|
| 502 |
when :connection_limit |
|---|
| 503 |
" CONNECTION LIMIT = #{value}" |
|---|
| 504 |
else |
|---|
| 505 |
"" |
|---|
| 506 |
end |
|---|
| 507 |
end |
|---|
| 508 |
|
|---|
| 509 |
execute "CREATE DATABASE #{name}#{option_string}" |
|---|
| 510 |
end |
|---|
| 511 |
|
|---|
| 512 |
|
|---|
| 513 |
|
|---|
| 514 |
|
|---|
| 515 |
|
|---|
| 516 |
def drop_database(name) |
|---|
| 517 |
execute "DROP DATABASE IF EXISTS #{name}" |
|---|
| 518 |
end |
|---|
| 519 |
|
|---|
| 520 |
|
|---|
| 521 |
|
|---|
| 522 |
def tables(name = nil) |
|---|
| 523 |
schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',') |
|---|
| 524 |
query(<<-SQL, name).map { |row| row[0] } |
|---|
| 525 |
SELECT tablename |
|---|
| 526 |
FROM pg_tables |
|---|
| 527 |
WHERE schemaname IN ( |
|---|
| 528 |
SQL |
|---|
| 529 |
end |
|---|
| 530 |
|
|---|
| 531 |
|
|---|
| 532 |
def indexes(table_name, name = nil) |
|---|
| 533 |
schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',') |
|---|
| 534 |
result = query(<<-SQL, name) |
|---|
| 535 |
SELECT distinct i.relname, d.indisunique, a.attname |
|---|
| 536 |
FROM pg_class t, pg_class i, pg_index d, pg_attribute a |
|---|
| 537 |
WHERE i.relkind = 'i' |
|---|
| 538 |
AND d.indexrelid = i.oid |
|---|
| 539 |
AND d.indisprimary = 'f' |
|---|
| 540 |
AND t.oid = d.indrelid |
|---|
| 541 |
AND t.relname = '#{table_name}' |
|---|
| 542 |
AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname IN ( |
|---|
| 543 |
AND a.attrelid = t. |
|---|