Class: ActiveRecord::ConnectionAdapters::AmalgaliteAdapter

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

Defined Under Namespace

Classes: Version

Constant Summary collapse

VERSION =
Version.to_s

Instance Method Summary collapse

Instance Method Details

#adapter_nameObject



107
108
109
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 107

def adapter_name
  "Amalgalite"
end

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



291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 291

def add_column(table_name, column_name, type, options = {})
  rc = nil
  if valid_alter_table_options( type, options ) then
    rc = super( table_name, column_name, type, options )
  else
    table_name = table_name.to_s
    rc = alter_table( table_name ) do |definition|
      definition.column( column_name, type, options )
    end
  end
  @connection.schema.load_table( table_name.to_s )
  return rc
end

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



305
306
307
308
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 305

def add_index( table_name, column_name, options = {} )
  super
  @connection.schema.load_table( table_name.to_s )
end

#add_lock!(sql, options) ⇒ Object

there is no select for update in sqlite



199
200
201
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 199

def add_lock!( sql, options )
  sql
end

#begin_db_transactionObject



194
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 194

def begin_db_transaction() @connection.transaction; end

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

:nodoc:



334
335
336
337
338
339
340
341
342
343
344
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 334

def change_column(table_name, column_name, type, options = {}) #:nodoc:
  alter_table(table_name) do |definition|
    include_default = options_include_default?(options)
    definition[column_name].instance_eval do
      self.type    = type
      self.limit   = options[:limit] if options.include?(:limit)
      self.default = options[:default] if include_default
      self.null    = options[:null] if options.include?(:null)
    end
  end
end

#change_column_default(table_name, column_name, default) ⇒ Object

:nodoc:



319
320
321
322
323
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 319

def change_column_default(table_name, column_name, default) #:nodoc:
  alter_table(table_name) do |definition|
    definition[column_name].default = default
  end
end

#change_column_null(table_name, column_name, null, default = nil) ⇒ Object



325
326
327
328
329
330
331
332
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 325

def change_column_null(table_name, column_name, null, default = nil)
  unless null || default.nil?
    execute("UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL")
  end
  alter_table(table_name) do |definition|
    definition[column_name].null = null
  end
end

#change_table(table_name, &block) ⇒ Object



279
280
281
282
283
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 279

def change_table( table_name, &block )
  super( table_name, &block )
  @connection.schema.load_table( table_name.to_s )

end

#columns(table_name, name = nil) ⇒ Object



224
225
226
227
228
229
230
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 224

def columns( table_name, name = nil )
  t = @connection.schema.tables[table_name.to_s]
  raise "Invalid table #{table_name}" unless t
  t.columns_in_order.map do |c|
    AmalgaliteColumn.from_amalgalite( c )
  end
end

#commit_db_transactionObject



195
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 195

def commit_db_transaction() @connection.commit; end

#create_table(table_name, options = {}, &block) ⇒ Object

Wrap the create table so we can mark the schema as dirty



274
275
276
277
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 274

def create_table( table_name, options = {}, &block )
  super( table_name, options, &block )
  @connection.schema.load_table( table_name.to_s )
end

#default_primary_key_typeObject

SCHEMA STATEMENTS ========================================



206
207
208
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 206

def default_primary_key_type
  'INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL'.freeze
end

#delete_sql(sql, name = nil) ⇒ Object



174
175
176
177
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 174

def delete_sql(sql, name = nil )
  sql += "WHERE 1=1" unless sql =~ /WHERE/i
  super sql, name
end

#drop_table(table_name, options = {}) ⇒ Object



285
286
287
288
289
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 285

def drop_table( table_name, options = {} )
  super( table_name, options )
  @connection.schema.tables.delete( table_name.to_s )
  puts "dropped table #{table_name} : #{@connection.schema.tables.include?( table_name )}" if table_name == "delete_me"
end

#empty_insert_statement(table_name) ⇒ Object



353
354
355
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 353

def empty_insert_statement(table_name)
  "INSERT INTO #{table_name} VALUES(NULL)"
end

#execute(sql, name = nil) ⇒ Object

DATABASE STATEMENTS ======================================



165
166
167
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 165

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

#indexes(table_name, name = nil) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 232

def indexes( table_name, name = nil )
  table = @connection.schema.tables[table_name.to_s]
  indexes = []
  if table then
    indexes = table.indexes.map do |key, idx|
      index = IndexDefinition.new( table_name, idx.name )
      index.unique = idx.unique?
      index.columns = idx.columns.map { |col| col.name }
      index
    end
  end
  return indexes
end

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



179
180
181
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 179

def insert_sql( sql, name = nil, pk = nil, id_value = nil, sequence_name = nil )
  super || @connection.last_insert_rowid
end

#native_database_typesObject

:nodoc:



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 136

def native_database_types #:nodoc:
  {
    :primary_key => default_primary_key_type,
    :string      => { :name => "varchar", :limit => 255 },
    :text        => { :name => "text" },
    :integer     => { :name => "integer" },
    :float       => { :name => "float" },
    :decimal     => { :name => "decimal" },
    :datetime    => { :name => "datetime" },
    :timestamp   => { :name => "datetime" },
    :time        => { :name => "time" },
    :date        => { :name => "date" },
    :binary      => { :name => "blob" },
    :boolean     => { :name => "boolean" }
  }
end

#primary_key(table_name) ⇒ Object



246
247
248
249
250
251
252
253
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 246

def primary_key( table_name )
  pk_list = @connection.schema.tables[table_name.to_s].primary_key
  if pk_list.empty? then
    return nil
  else
    return pk_list.first.name
  end
end

#quote_column_name(name) ⇒ Object

:nodoc:



160
161
162
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 160

def quote_column_name( name ) #:nodoc:
  return "\"#{name}\""
end

#quote_string(s) ⇒ Object

this is really escaping



156
157
158
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 156

def quote_string( s ) #:nodoc:
  @connection.escape( s )
end

#remove_column(table_name, *column_names) ⇒ Object Also known as: remove_columns



310
311
312
313
314
315
316
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 310

def remove_column( table_name, *column_names )
  column_names.flatten.each do |column_name|
    alter_table( table_name ) do |definition|
      definition.columns.delete( definition[column_name] )
    end
  end
end

#remove_index(table_name, options = {}) ⇒ Object



255
256
257
258
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 255

def remove_index( table_name, options = {} )
  execute "DROP INDEX #{quote_column_name(index_name( table_name.to_s, options ) ) }"
  @connection.schema.dirty!
end

#rename_column(table_name, column_name, new_column_name) ⇒ Object

:nodoc:



346
347
348
349
350
351
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 346

def rename_column(table_name, column_name, new_column_name) #:nodoc:
  unless columns(table_name).detect{|c| c.name == column_name.to_s }
    raise ActiveRecord::ActiveRecordError, "Missing column #{table_name}.#{column_name}"
  end
  alter_table(table_name, :rename => {column_name.to_s => new_column_name.to_s})
end

#rename_table(name, new_name) ⇒ Object



260
261
262
263
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 260

def rename_table( name, new_name )
  execute "ALTER TABLE #{name} RENAME TO #{new_name}"
  @connection.schema.dirty!
end

#requires_reloading?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 119

def requires_reloading?
  true
end

#rollback_db_transactionObject



196
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 196

def rollback_db_transaction() @connection.rollback; end

#select(sql, name = nil) ⇒ Object



187
188
189
190
191
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 187

def select( sql, name = nil )
  execute( sql, name ).map do |row|
    row.to_hash
  end
end

#select_rows(sql, name = nil) ⇒ Object



183
184
185
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 183

def select_rows( sql, name = nil )
  execute( sql, name )
end

#supports_add_column?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 123

def supports_add_column?
  true
end

#supports_autoincrement?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 131

def supports_autoincrement?
  true
end

#supports_count_distinct?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 127

def supports_count_distinct?
  true
end

#supports_ddl_transactions?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 111

def supports_ddl_transactions?
  true
end

#supports_migrations?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 115

def supports_migrations?
  true
end

#tables(name = nil) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 210

def tables( name = nil )
  sql = "SELECT name FROM sqlite_master WHERE type = 'table' AND NOT name = 'sqlite_sequence'" 
  raw_list = execute( sql, nil ).map { |row| row['name'] }
  if raw_list.sort != @connection.schema.tables.keys.sort then
    @connection.schema.load_schema!
    if raw_list.sort != @connection.schema.tables.keys.sort then
      raise "raw_list - tables : #{raw_list - @connection.schema.tables.keys} :: tables - raw_list #{@connection.schema.tables.keys - raw_list}"
    end
  end
  ActiveRecord::Base.logger.info "schema_migrations    in tables? : #{raw_list.include?( "schema_migrations" )}"
  ActiveRecord::Base.logger.info "schema_migrations(2) in tables? : #{@connection.schema.tables.keys.include?( "schema_migrations" )}"
  @connection.schema.tables.keys
end

#update_sql(sql, name = nil) ⇒ Object



169
170
171
172
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 169

def update_sql( sql, name = nil )
  super
  @connection.row_changes
end

#valid_alter_table_options(type, options) ⇒ Object

See: www.sqlite.org/lang_altertable.html SQLite has an additional restriction on the ALTER TABLE statement



267
268
269
# File 'lib/active_record/connection_adapters/amalgalite_adapter.rb', line 267

def valid_alter_table_options( type, options )
  type.to_sym != :primary_key
end