Class: ActiveRecordSpannerAdapter::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord_spanner_adapter/index.rb,
lib/activerecord_spanner_adapter/index/column.rb

Defined Under Namespace

Classes: Column

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, name, columns, type: nil, unique: false, null_filtered: false, interleave_in: nil, storing: nil, state: nil, schema: "") ⇒ Index



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/activerecord_spanner_adapter/index.rb', line 22

def initialize \
    table,
    name,
    columns,
    type: nil,
    unique: false,
    null_filtered: false,
    interleave_in: nil,
    storing: nil,
    state: nil,
    schema: ""
  @schema = schema.to_s
  @table = table.to_s
  @name = name.to_s
  @columns = Array(columns)
  @type = type
  @unique = unique
  @null_filtered = null_filtered
  @interleave_in = interleave_in unless interleave_in.to_s.empty?
  @storing = storing || []
  @state = state
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



14
15
16
# File 'lib/activerecord_spanner_adapter/index.rb', line 14

def columns
  @columns
end

#interleave_inObject

Returns the value of attribute interleave_in.



18
19
20
# File 'lib/activerecord_spanner_adapter/index.rb', line 18

def interleave_in
  @interleave_in
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/activerecord_spanner_adapter/index.rb', line 13

def name
  @name
end

#null_filteredObject

Returns the value of attribute null_filtered.



17
18
19
# File 'lib/activerecord_spanner_adapter/index.rb', line 17

def null_filtered
  @null_filtered
end

#schemaObject

Returns the value of attribute schema.



11
12
13
# File 'lib/activerecord_spanner_adapter/index.rb', line 11

def schema
  @schema
end

#stateObject

Returns the value of attribute state.



20
21
22
# File 'lib/activerecord_spanner_adapter/index.rb', line 20

def state
  @state
end

#storingObject

Returns the value of attribute storing.



19
20
21
# File 'lib/activerecord_spanner_adapter/index.rb', line 19

def storing
  @storing
end

#tableObject

Returns the value of attribute table.



12
13
14
# File 'lib/activerecord_spanner_adapter/index.rb', line 12

def table
  @table
end

#typeObject

Returns the value of attribute type.



15
16
17
# File 'lib/activerecord_spanner_adapter/index.rb', line 15

def type
  @type
end

#uniqueObject

Returns the value of attribute unique.



16
17
18
# File 'lib/activerecord_spanner_adapter/index.rb', line 16

def unique
  @unique
end

Instance Method Details

#column_namesObject



55
56
57
# File 'lib/activerecord_spanner_adapter/index.rb', line 55

def column_names
  columns_by_position.map(&:name)
end

#columns_by_positionObject



49
50
51
52
53
# File 'lib/activerecord_spanner_adapter/index.rb', line 49

def columns_by_position
  @columns.select(&:ordinal_position).sort do |c1, c2|
    c1.ordinal_position <=> c2.ordinal_position
  end
end

#optionsObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/activerecord_spanner_adapter/index.rb', line 65

def options
  {
    name: name,
    order: orders,
    unique: unique,
    interleave_in: interleave_in,
    null_filtered: null_filtered,
    storing: storing
  }.delete_if { |_, v| v.nil? }
end

#ordersObject



59
60
61
62
63
# File 'lib/activerecord_spanner_adapter/index.rb', line 59

def orders
  columns_by_position.each_with_object({}) do |c, r|
    r[c.name] = c.desc? ? :desc : :asc
  end
end

#primary?Boolean



45
46
47
# File 'lib/activerecord_spanner_adapter/index.rb', line 45

def primary?
  @type == "PRIMARY_KEY"
end

#rename_column_options(old_column, new_column) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/activerecord_spanner_adapter/index.rb', line 76

def rename_column_options old_column, new_column
  opts = options

  opts[:order].transform_keys do |key|
    key.to_s == new_column.to_s
  end

  columns = column_names.map do |c|
    c.to_s == old_column.to_s ? new_column : c
  end

  { options: opts, columns: columns }
end