Class: CouchbaseOrm::IndexMigration::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase-orm/index_migration/query_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(config: CouchbaseOrm.config.index) ⇒ QueryBuilder

Returns a new instance of QueryBuilder.



8
9
10
# File 'lib/couchbase-orm/index_migration/query_builder.rb', line 8

def initialize(config: CouchbaseOrm.config.index)
  @config = config
end

Instance Method Details

#build_indexes(index_names) ⇒ Object

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
# File 'lib/couchbase-orm/index_migration/query_builder.rb', line 25

def build_indexes(index_names)
  bucket = @config.effective_bucket
  raise ArgumentError.new('Missing index bucket configuration') if bucket.to_s.strip.empty?

  names = Array(index_names)
  raise ArgumentError.new('At least one index name is required') if names.empty?

  index_lines = names.map { |name| "  `#{name}`" }.join(",\n")
  "BUILD INDEX ON `#{bucket}`\n(\n#{index_lines}\n);"
end

#create_index(index_definition) ⇒ Object

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/couchbase-orm/index_migration/query_builder.rb', line 12

def create_index(index_definition)
  bucket = @config.effective_bucket
  raise ArgumentError.new('Missing index bucket configuration') if bucket.to_s.strip.empty?
  raise ArgumentError.new('Missing index keys configuration') if Array(index_definition.keys).empty?

  query = +"CREATE INDEX `#{index_definition.name}`\n"
  query << "ON `#{bucket}`(#{Array(index_definition.keys).map { |key| format_key(key) }.join(',')})"
  query << "\nWHERE (#{index_definition.where})" if index_definition.where
  options = with_options(defer_build: index_definition.defer_build, num_replica: index_definition.num_replica)
  query << "\nWITH #{JSON.pretty_generate(options)}" unless options.empty?
  query
end

#remove_index(name) ⇒ Object

Raises:

  • (ArgumentError)


36
37
38
39
40
41
# File 'lib/couchbase-orm/index_migration/query_builder.rb', line 36

def remove_index(name)
  bucket = @config.effective_bucket
  raise ArgumentError.new('Missing index bucket configuration') if bucket.to_s.strip.empty?

  "DROP INDEX `#{bucket}`.`#{name}`"
end

#states_query(bucket, index_names) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/couchbase-orm/index_migration/query_builder.rb', line 43

def states_query(bucket, index_names)
  names = Array(index_names).map { |name| quote(name.to_s) }.join(', ')
  <<~SQL.strip
    SELECT name, state
    FROM system:indexes
    WHERE keyspace_id = #{quote(bucket.to_s)}
      AND name IN [#{names}]
  SQL
end