Class: DataMapper::Adapters::PostgresqlAdapter::Commands::SaveCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/data_mapper/adapters/postgresql_adapter.rb

Instance Method Summary collapse

Instance Method Details

#column_long_form(column) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/data_mapper/adapters/postgresql_adapter.rb', line 181

def column_long_form(column)
  
  long_form = if column.key? 
    "#{column.to_sql} serial primary key"
  else
    "#{column.to_sql} #{@adapter.class::TYPES[column.type] || column.type}"
  end  
  long_form << " NOT NULL" unless column.nullable?
  long_form << " default #{column.options[:default]}" if column.options.has_key?(:default)

  return long_form
end

#execute_create_table(sql) ⇒ Object



163
164
165
166
167
# File 'lib/data_mapper/adapters/postgresql_adapter.rb', line 163

def execute_create_table(sql)
  @adapter.log.debug(sql)
  @adapter.connection { |db| db.exec(sql) }
  true
end

#execute_insert(sql) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/data_mapper/adapters/postgresql_adapter.rb', line 146

def execute_insert(sql)
  @adapter.connection do |db|
    @adapter.log.debug(sql)
    db.exec(sql)
    # Current id or latest value read from sequence in this session
    # See: http://www.postgresql.org/docs/8.1/interactive/functions-sequence.html
    @instance.key || db.exec("SELECT last_value from #{@adapter.sequence_name(@adapter[@instance.class])}")[0][0]
  end
end

#execute_update(sql) ⇒ Object



156
157
158
159
160
161
# File 'lib/data_mapper/adapters/postgresql_adapter.rb', line 156

def execute_update(sql)
  @adapter.connection do |db|
    @adapter.log.debug(sql)
    db.exec(sql).cmdstatus.split(' ').last.to_i > 0
  end
end

#to_create_table_sqlObject



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/data_mapper/adapters/postgresql_adapter.rb', line 169

def to_create_table_sql
  table = @adapter[@instance]

  sql = "CREATE TABLE " << table.to_sql

  sql << " (" << table.columns.map do |column|
    column_long_form(column)
  end.join(', ') << ")"

  return sql
end