Class: SqlTools::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/sql_tools/index.rb

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Index

Returns a new instance of Index.



3
4
5
# File 'lib/sql_tools/index.rb', line 3

def initialize(node)
  @node = node
end

Instance Method Details

#columnsObject



7
8
9
10
11
12
13
# File 'lib/sql_tools/index.rb', line 7

def columns
  @node.query(<<~QUERY).map do |match|
    (ordered_columns (column name: (identifier) @column_name))
  QUERY
    match["column_name"].text
  end
end

#nameObject



20
21
22
23
24
# File 'lib/sql_tools/index.rb', line 20

def name
  return :primary_key if primary_key?

  @node.name.text.delete("`")
end

#primary_key?Boolean

Returns:

  • (Boolean)


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

def primary_key?
  @node.any? { |child| child.type == :keyword_primary } &&
    @node.any? { |child| child.type == :keyword_key }
end