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

Returns a new instance of Index.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/activerecord_spanner_adapter/index.rb', line 14

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.



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

def columns
  @columns
end

#interleave_inObject

Returns the value of attribute interleave_in.



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

def interleave_in
  @interleave_in
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#null_filteredObject

Returns the value of attribute null_filtered.



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

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.



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

def state
  @state
end

#storingObject

Returns the value of attribute storing.



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

def storing
  @storing
end

#tableObject

Returns the value of attribute table.



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

def table
  @table
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

#uniqueObject

Returns the value of attribute unique.



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

def unique
  @unique
end

Instance Method Details

#column_namesObject



47
48
49
# File 'lib/activerecord_spanner_adapter/index.rb', line 47

def column_names
  columns_by_position.map(&:name)
end

#columns_by_positionObject



41
42
43
44
45
# File 'lib/activerecord_spanner_adapter/index.rb', line 41

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

#optionsObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/activerecord_spanner_adapter/index.rb', line 57

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



51
52
53
54
55
# File 'lib/activerecord_spanner_adapter/index.rb', line 51

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

#primary?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/activerecord_spanner_adapter/index.rb', line 37

def primary?
  @type == "PRIMARY_KEY"
end

#rename_column_options(old_column, new_column) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/activerecord_spanner_adapter/index.rb', line 68

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