Module: Imparcial::Driver::AbstractExpression::Table

Included in:
Imparcial::Driver::AbstractExpression
Defined in:
lib/imparcial/driver/abstract/expression/table.rb

Instance Method Summary collapse

Instance Method Details

#create_table(options = {}) ⇒ Object

Description

Create a table.

Usage

abstract_adapter.create_table :table_name => ‘person’, :fields =>

=> ‘id’, :type => :integer,=> ‘name’, :type => :string

Option

  • :table_name

  • :fields = Having an array of hashes with information about columns.

Returning

nothing

Note

Some databases do override this method by adding up more options. Please, check out the adapter specific’s one.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/imparcial/driver/abstract/expression/table.rb', line 132

def create_table ( options = {} )                 

  check_options expected_options_for_creating_table, options                          
      
  sql = sql_for_creating_table( options )

  logger.warn sql if @table_logging
  
  query sql          
    
rescue adapter_specific_exception => ex
  
  raise TableCreateError.new(ex.message)               
      
end

#diff_columns(options = {}) ⇒ Object



250
251
252
253
254
255
256
257
258
# File 'lib/imparcial/driver/abstract/expression/table.rb', line 250

def diff_columns ( options = {} )
  
  modified = report_modified_columns options
  new      = report_new_columns options
  old      = report_old_columns options
  
  [modified, new, old]
  
end

#drop_all_tablesObject

Description

Drop all tables raising an exception if necessary.

Usage

abstract_adapter.drop_all_tables

Option

No option

Returning

nothing



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/imparcial/driver/abstract/expression/table.rb', line 80

def drop_all_tables

  for table_name in get_tables
  
    sql = sql_for_dropping_table( {:table_name => table_name} )
  
    logger.warn sql if @table_logging
  
    query sql            
  
  end
    
rescue adapter_specific_exception => ex
  
  raise TableDropError.new(ex.message)
    
end

#drop_table(options = {}) ⇒ Object

Description

Drop a table raising an exception if necessary.

Usage

abstract_adapter.drop_table :table_name => ‘person’

Options

:table_name

Returning

nothing



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/imparcial/driver/abstract/expression/table.rb', line 34

def drop_table ( options = {} )                     
 
  check_options expected_options_for_dropping_table, options
    
  sql = sql_for_dropping_table( options )
  
  logger.warn sql if @table_logging
  
  query sql
    
rescue adapter_specific_exception => ex
  
  raise TableDropError.new(ex.message)

end

#drop_table_if_necessary(options = {}) ⇒ Object

Description

Drop a table by not raising any exception.

Usage

abstract_adapter.drop_table_if_necessary :table_name => ‘person’

Option

:table_name

Returning

nothing



62
63
64
65
66
# File 'lib/imparcial/driver/abstract/expression/table.rb', line 62

def drop_table_if_necessary ( options = {} )                       

  drop_table options
 
rescue TableDropError; end

#get_tablesObject

Description

Return all tables avaliable in the current schema by name.

Usage

abstract_adapter.get_tables

Option

No options

Returning

an array [‘person’,‘invoice’,…]



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/imparcial/driver/abstract/expression/table.rb', line 210

def get_tables
    
  sql = sql_for_getting_tables
  
  logger.warn sql if @table_logging
  
  query sql          
            
  tables = []
     
  result.fetch do |table_name_column|

    tables << table_name_column.value
      
  end
     
  tables
  
rescue adapter_specific_exception => ex
  
  raise TableRetrieveError.new(ex.message)
      
end

#report_modified_columns(options = {}) ⇒ Object

Description

Execute a diff by comparing incoming fields with existent ones.

Usage

abstract_adapter.report_modified_columns :table_name => ‘person’, :fields => [=> :id, :type => :bigint]

Option

  • :table_name

  • :fields => array of hashes

Returning

fields affected



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/imparcial/driver/abstract/expression/table.rb', line 274

def report_modified_columns ( options = {} )
  
  check_options expected_options_for_reporting_tables, options

  modified_fields  = options[:fields]
                 
  modified_columns = []
  current_fields   = get_columns_information :table_name => options[:table_name]
    
  parse_fields modified_fields do |modified_field|

    current_fields.each do |current_field|
    
      if modified_field[:name] == current_field[:name]

        # We gotta check every attribute to see if a modification can occur.
    
        if modified_field[:type] != current_field[:type]||modified_field[:size]!=current_field[:size]

          modified_columns << current_field
    
        end

      end
  
    end

  end  #end of parse_fields       
  
  modified_columns

end

#report_new_columns(options = {}) ⇒ Object

Description

Execute a diff by comparing incoming fields with existent ones.

Usage

abstract_adapter.report_new_columns :table_name => ‘person’, :fields => [=> :new_id, :type => :int]

Option

  • :table_name

  • :fields => array of hashes

Returning

fields affected



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/imparcial/driver/abstract/expression/table.rb', line 321

def report_new_columns ( options = {} )
      
  check_options expected_options_for_reporting_tables, options
  
  new_fields   = options[:fields]
      
  new_columns  = []
  no_new_field = false
    
  current_fields = get_columns_information :table_name => options[:table_name]
          
  parse_fields new_fields do |new_field|

    current_fields.each do |current_field|
  
      if new_field[:name] == current_field[:name]
    
        no_new_field = true
        break
    
      end
  
    end
              
    if no_new_field
  
      no_new_field = false
      next
    
    end
  
    new_columns << new_field       

  end

  new_columns

end

#report_old_columns(options = {}) ⇒ Object

Description

Execute a diff by comparing incoming fields with existent ones.

Usage

abstract_adapter.report_old_columns :table_name => ‘person’, :fields => [=> :new_id, :type => :int]

Option

  • :table_name

  • :fields => array of hashes

Returning

fields affected



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/imparcial/driver/abstract/expression/table.rb', line 374

def report_old_columns ( options = {} )
     
  check_options expected_options_for_reporting_tables, options
  
  new_fields = options[:fields]
     
  legacy_fields = []
  no_old_field  = false
      
  current_fields = get_columns_information :table_name => options[:table_name]

  current_fields.each do |current_field|

    parse_fields new_fields do |new_field|
  
      if new_field[:name] == current_field[:name]
    
        no_old_field = true
        break
        
      end
            
      if no_old_field == true
  
        no_old_field = false
        next
  
      end
      
      legacy_fields << current_field
    
    end #end of parse_fields
      
  end

  legacy_fields
      
end

#table_exists?(options = {}) ⇒ Boolean

Description

Check if a given table exists.

Usage

abstract_adapter.table_exists?(:table_name => ‘person’)

Option

No options

Returning

true or false

Returns:

  • (Boolean)


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/imparcial/driver/abstract/expression/table.rb', line 176

def table_exists? ( options = {} )
  
  check_options expected_options_for_verifying_table_existence, options
    
  tables = get_tables
    
  tables.each do |name|
    
    return true if name == options[:table_name]
    
  end
    
  return false    
    
end

#table_not_exists?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


192
193
194
195
196
# File 'lib/imparcial/driver/abstract/expression/table.rb', line 192

def table_not_exists? ( options = {} )     
      
  not table_exists? options
      
end