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
-
#columns ⇒ Object
readonly
The list of columns used when selecting, the union of pk and traversing key columns.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#primary_key ⇒ Object
readonly
The schema for the primary key.
-
#traversing_key ⇒ Object
readonly
The schema for the traversing key, or nil.
Instance Method Summary collapse
- #column_names ⇒ Object
- #delete_statement(rows) ⇒ Object
- #first_only? ⇒ Boolean
- #initial_scope ⇒ Object
-
#initialize(model, options = {}) ⇒ TableSchema
constructor
A new instance of TableSchema.
- #insert_statement(rows) ⇒ Object
- #scope_to_next_chunk(scope, last_row) ⇒ Object
Constructor Details
#initialize(model, options = {}) ⇒ TableSchema
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 56 57 58 59 |
# File 'lib/clean_sweep/table_schema.rb', line 15 def initialize(model, ={}) traversing_key_name = [:index] ascending = ![:reverse] 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 = ([:copy_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 ![:non_traversing] 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 else @traversing_key = @primary_key end @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
#columns ⇒ Object (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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/clean_sweep/table_schema.rb', line 13 def name @name end |
#primary_key ⇒ Object (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 |
#traversing_key ⇒ Object (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
#column_names ⇒ Object
61 62 63 |
# File 'lib/clean_sweep/table_schema.rb', line 61 def column_names @columns.map(&:name) end |
#delete_statement(rows) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/clean_sweep/table_schema.rb', line 69 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 |
#first_only? ⇒ Boolean
94 95 96 |
# File 'lib/clean_sweep/table_schema.rb', line 94 def first_only? @traversing_key && @traversing_key.first_only end |
#initial_scope ⇒ Object
80 81 82 83 84 |
# File 'lib/clean_sweep/table_schema.rb', line 80 def initial_scope scope = @model.select(quoted_column_names).from(from_clause) scope = @traversing_key.order(scope) if @traversing_key return scope end |
#insert_statement(rows) ⇒ Object
65 66 67 |
# File 'lib/clean_sweep/table_schema.rb', line 65 def insert_statement(rows) "insert into #{@dest_model.quoted_table_name} (#{quoted_dest_column_names}) values #{quoted_row_values(rows)}" end |
#scope_to_next_chunk(scope, last_row) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/clean_sweep/table_schema.rb', line 86 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 |