Class: CleanSweep::TableSchema
- Inherits:
-
Object
- Object
- CleanSweep::TableSchema
- Defined in:
- lib/clean_sweep/table_schema.rb
Defined Under Namespace
Classes: ColumnSchema, IndexSchema
Instance Attribute Summary (collapse)
-
- (Object) columns
readonly
The list of columns used when selecting, the union of pk and traversing key columns.
-
- (Object) name
readonly
Returns the value of attribute name.
-
- (Object) primary_key
readonly
The schema for the primary key.
-
- (Object) traversing_key
readonly
The schema for the traversing key, or nil.
Instance Method Summary (collapse)
- - (Object) column_names
- - (Object) delete_statement(rows)
- - (Boolean) first_only?
- - (Object) initial_scope
-
- (TableSchema) initialize(model, options = {})
constructor
A new instance of TableSchema.
- - (Object) insert_statement(rows)
- - (Object) scope_to_next_chunk(scope, last_row)
Constructor Details
- (TableSchema) initialize(model, options = {})
Returns a new instance of TableSchema
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/clean_sweep/table_schema.rb', line 15 def initialize(model, ={}) traversing_key_name = [:key_name] ascending = .include?(:ascending) ? [:ascending] : true first_only = [:first_only] @model = model @dest_model = [:dest_model] || @model # Downcase and symbolize the entries in the column name map: dest_columns_map = Hash[*([:dest_columns] || {}).to_a.flatten.map{|n| n.to_s.downcase.to_sym}] @name = @model.table_name @columns = ([:extra_columns] || []).map do | extra_col_name | CleanSweep::TableSchema::ColumnSchema.new extra_col_name, model end key_schemas = build_indexes # Primary key only supported, but we could probably get around this by adding # all columns as 'primary key columns' @primary_key = find_primary_key(key_schemas) raise "Table #{model.table_name} must have a primary key" unless @primary_key @primary_key.add_columns_to @columns if traversing_key_name traversing_key_name.downcase! raise "BTREE Index #{traversing_key_name} not found in #@name" unless key_schemas.include? traversing_key_name @traversing_key = key_schemas[traversing_key_name] @traversing_key.add_columns_to @columns @traversing_key.ascending = ascending @traversing_key.first_only = first_only end # Specify the column names in the destination map, if provided @columns.each do | column | column.dest_name = dest_columns_map[column.name] end end |
Instance Attribute Details
- (Object) columns (readonly)
The list of columns used when selecting, the union of pk and traversing key columns
5 6 7 |
# File 'lib/clean_sweep/table_schema.rb', line 5 def columns @columns end |
- (Object) name (readonly)
Returns the value of attribute name
13 14 15 |
# File 'lib/clean_sweep/table_schema.rb', line 13 def name @name end |
- (Object) primary_key (readonly)
The schema for the primary key
8 9 10 |
# File 'lib/clean_sweep/table_schema.rb', line 8 def primary_key @primary_key end |
- (Object) traversing_key (readonly)
The schema for the traversing key, or nil
11 12 13 |
# File 'lib/clean_sweep/table_schema.rb', line 11 def traversing_key @traversing_key end |
Instance Method Details
- (Object) column_names
57 58 59 |
# File 'lib/clean_sweep/table_schema.rb', line 57 def column_names @columns.map(&:name) end |
- (Object) delete_statement(rows)
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/clean_sweep/table_schema.rb', line 65 def delete_statement(rows) rec_criteria = rows.map do | row | row_compares = [] @primary_key.columns.each do |column| row_compares << "#{column.quoted_dest_name(@dest_model)} = #{column.quoted_value(row)}" end "(" + row_compares.join(" AND ") + ")" end "DELETE FROM #{@dest_model.quoted_table_name} WHERE #{rec_criteria.join(" OR ")}" end |
- (Boolean) first_only?
90 91 92 |
# File 'lib/clean_sweep/table_schema.rb', line 90 def first_only? @traversing_key && @traversing_key.first_only end |
- (Object) initial_scope
76 77 78 79 80 |
# File 'lib/clean_sweep/table_schema.rb', line 76 def initial_scope scope = @model.all.select(quoted_column_names).from(from_clause) scope = @traversing_key.order(scope) if @traversing_key return scope end |
- (Object) insert_statement(rows)
61 62 63 |
# File 'lib/clean_sweep/table_schema.rb', line 61 def insert_statement(rows) "insert into #{@dest_model.quoted_table_name} (#{quoted_dest_column_names}) values #{quoted_row_values(rows)}" end |
- (Object) scope_to_next_chunk(scope, last_row)
82 83 84 85 86 87 88 |
# File 'lib/clean_sweep/table_schema.rb', line 82 def scope_to_next_chunk scope, last_row if @traversing_key.blank? scope else @traversing_key.scope_to_next_chunk(scope, last_row) end end |