Class: ActiveRecord::ConnectionAdapters::PostgreSQLAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/active_record/connection_adapters/postgresql_adapter.rb

Overview

The PostgreSQL adapter works both with the C-based (www.postgresql.jp/interfaces/ruby/) and the Ruby-base (available both as gem and from rubyforge.org/frs/?group_id=234&release_id=1145) drivers.

Options:

  • :host – Defaults to localhost

  • :port – Defaults to 5432

  • :username – Defaults to nothing

  • :password – Defaults to nothing

  • :database – The name of the database. No default, must be provided.

  • :schema_search_path – An optional schema search path for the connection given as a string of comma-separated schema names. This is backward-compatible with the :schema_order option.

  • :encoding – An optional client encoding that is using in a SET client_encoding TO <encoding> call on connection.

  • :min_messages – An optional client min messages that is using in a SET client_min_messages TO <min_messages> call on connection.

Instance Method Summary collapse

Methods inherited from AbstractAdapter

#add_column, #add_index, #add_limit!, #add_limit_offset!, #create_table, #drop_table, #initialize, #initialize_schema_information, #quote_string, #remove_column, #reset_runtime, #structure_dump, #transaction, #type_to_sql

Constructor Details

This class inherits a constructor from ActiveRecord::ConnectionAdapters::AbstractAdapter

Instance Method Details

#adapter_nameObject



134
135
136
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 134

def adapter_name
  'PostgreSQL'
end

#begin_db_transactionObject



118
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 118

def begin_db_transaction()    execute "BEGIN" end

#change_column(table_name, column_name, type, options = {}) ⇒ Object



153
154
155
156
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 153

def change_column(table_name, column_name, type, options = {})
  execute = "ALTER TABLE #{table_name} ALTER  #{column_name} TYPE #{type}"
  change_column_default(table_name, column_name, options[:default]) unless options[:default].nil?
end

#change_column_default(table_name, column_name, default) ⇒ Object



158
159
160
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 158

def change_column_default(table_name, column_name, default)
  execute "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} SET DEFAULT '#{default}'"
end

#columns(table_name, name = nil) ⇒ Object



92
93
94
95
96
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 92

def columns(table_name, name = nil)
  column_definitions(table_name).collect do |name, type, default|
    Column.new(name, default_value(default), translate_field_type(type))
  end
end

#commit_db_transactionObject



119
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 119

def commit_db_transaction()   execute "COMMIT" end

#execute(sql, name = nil) ⇒ Object



108
109
110
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 108

def execute(sql, name = nil)
  log(sql, name) { @connection.exec(sql) }
end

#insert(sql, name = nil, pk = nil, id_value = nil) ⇒ Object



98
99
100
101
102
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 98

def insert(sql, name = nil, pk = nil, id_value = nil)
  execute(sql, name)
  table = sql.split(" ", 4)[2]
  return id_value || last_insert_id(table, pk)
end

#native_database_typesObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 63

def native_database_types
  {
    :primary_key => "serial primary key",
    :string      => { :name => "character varying", :limit => 255 },
    :text        => { :name => "text" },
    :integer     => { :name => "integer" },
    :float       => { :name => "float" },
    :datetime    => { :name => "timestamp" },
    :timestamp   => { :name => "timestamp" },
    :time        => { :name => "timestamp" },
    :date        => { :name => "date" },
    :binary      => { :name => "bytea" },
    :boolean     => { :name => "boolean"}
  }
end

#query(sql, name = nil) ⇒ Object



104
105
106
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 104

def query(sql, name = nil)
  log(sql, name) { @connection.query(sql) }
end

#quote(value, column = nil) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 122

def quote(value, column = nil)
  if value.class == String && column && column.type == :binary
    quote_bytea(value)
  else
    super
  end
end

#quote_column_name(name) ⇒ Object



130
131
132
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 130

def quote_column_name(name)
  %("#{name}")
end

#remove_index(table_name, column_name) ⇒ Object



166
167
168
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 166

def remove_index(table_name, column_name)
  execute "DROP INDEX #{table_name}_#{column_name}_index"
end

#rename_column(table_name, column_name, new_column_name) ⇒ Object



162
163
164
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 162

def rename_column(table_name, column_name, new_column_name)
  execute "ALTER TABLE #{table_name} RENAME COLUMN #{column_name} TO #{new_column_name}"
end

#rollback_db_transactionObject



120
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 120

def rollback_db_transaction() execute "ROLLBACK" end

#schema_search_pathObject



149
150
151
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 149

def schema_search_path
  @schema_search_path ||= query('SHOW search_path')[0][0]
end

#schema_search_path=(schema_csv) ⇒ Object

Set the schema search path to a string of comma-separated schema names. Names beginning with $ are quoted (e.g. $user => ‘$user’) See www.postgresql.org/docs/8.0/interactive/ddl-schemas.html



142
143
144
145
146
147
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 142

def schema_search_path=(schema_csv)
  if schema_csv
    execute "SET search_path TO #{schema_csv}"
    @schema_search_path = nil
  end
end

#select_all(sql, name = nil) ⇒ Object



83
84
85
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 83

def select_all(sql, name = nil)
  select(sql, name)
end

#select_one(sql, name = nil) ⇒ Object



87
88
89
90
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 87

def select_one(sql, name = nil)
  result = select(sql, name)
  result.nil? ? nil : result.first
end

#supports_migrations?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 79

def supports_migrations?
  true
end

#update(sql, name = nil) ⇒ Object Also known as: delete



112
113
114
# File 'lib/active_record/connection_adapters/postgresql_adapter.rb', line 112

def update(sql, name = nil)
  execute(sql, name).cmdtuples
end