Class: Cql::Model::Query::SelectStatement

Inherits:
Statement
  • Object
show all
Defined in:
lib/cql/model/query/select_statement.rb

Overview

SELECT statement DSL << A SELECT expression reads one or more records from a Cassandra column family and returns a result-set of rows.

Each row consists of a row key and a collection of columns corresponding to the query. >>

(from: www.datastax.com/docs/1.1/references/cql/SELECT)

Ex: Model.select(:col1, :col2) Model.select(:col1, :col2).where { name == ‘Joe’ } Model.select(:col1, :col2).where { name == ‘Joe’ }.and { age.in(33,34,35) } Model.select(:col1, :col2).where { name == ‘Joe’ }.and { age.in(33,34,35) }.order_by(:age).desc

Instance Method Summary collapse

Methods inherited from Statement

#consistency

Constructor Details

#initialize(klass, client = nil) ⇒ SelectStatement

Instantiate statement

Parameters:

  • klass (Class)

    Model class

  • CQL (Cql::Client)

    client used to execute statement



19
20
21
22
23
24
25
# File 'lib/cql/model/query/select_statement.rb', line 19

def initialize(klass, client=nil)
  super(klass, client)
  @columns = nil
  @where   = []
  @order   = ''
  @limit   = nil
end

Instance Method Details

#ascObject

Raises:

  • (ArgumentError)


73
74
75
76
77
78
# File 'lib/cql/model/query/select_statement.rb', line 73

def asc
  raise ArgumentError, "Cannot specify ASC / DESC twice" if @order =~ /ASC|DESC$/
  raise ArgumentError, "Must specify ORDER BY before ASC" if @order.empty?
  @order << " ASC"
  self
end

#descObject

Raises:

  • (ArgumentError)


81
82
83
84
85
86
# File 'lib/cql/model/query/select_statement.rb', line 81

def desc
  raise ArgumentError, "Cannot specify ASC / DESC twice" if @order =~ /ASC|DESC$/
  raise ArgumentError, "Must specify ORDER BY before DESC" if @order.empty?
  @order << " DESC"
  self
end

#each {|row| ... } ⇒ true

Execute this SELECT statement on the CQL client connection and yield each row of the as an instance of CQL::Model.

Yields:

  • each row of the result set

Yield Parameters:

  • row (Hash)

    a Ruby Hash representing the column names and values for a given row

Returns:

  • (true)

    always returns true



137
138
139
140
141
# File 'lib/cql/model/query/select_statement.rb', line 137

def each(&block)
  each_row do |row|
    block.call(@klass.new(row))
  end
end

#execute {|row| ... } ⇒ true Also known as: each_row

Execute this SELECT statement on the CQL client connection and yield each row of the result set as a raw-data Hash.

Yields:

  • each row of the result set

Yield Parameters:

  • row (Hash)

    a Ruby Hash representing the column names and values for a given row

Returns:

  • (true)

    always returns true



123
124
125
126
127
# File 'lib/cql/model/query/select_statement.rb', line 123

def execute(&block)
  @client.execute(to_s).each_row(&block).size

  true
end

#limit(lim) ⇒ Object

Raises:

  • (ArgumentError)


89
90
91
92
93
# File 'lib/cql/model/query/select_statement.rb', line 89

def limit(lim)
  raise ArgumentError, "Cannot specify LIMIT twice" unless @limit.nil?
  @limit = lim
  self
end

#order(*columns) ⇒ SelectStatement Also known as: order_by

Specify the order in which result rows should be returned.

Parameters:

  • *params (Array)

    glob of column names (symbols, strings, or Ruby values that correspond to valid column names)

Returns:

Raises:

  • (ArgumentError)


64
65
66
67
68
# File 'lib/cql/model/query/select_statement.rb', line 64

def order(*columns)
  raise ArgumentError, "Cannot specify ORDER BY twice" unless @order.empty?
  @order = ::Cql::Model::Query.cql_column_names(columns)
  self
end

#select(*columns) ⇒ Object

Raises:

  • (ArgumentError)


96
97
98
99
100
# File 'lib/cql/model/query/select_statement.rb', line 96

def select(*columns)
  raise ArgumentError, "Cannot specify SELECT column names twice" unless @columns.nil?
  @columns = ::Cql::Model::Query.cql_column_names(columns)
  self
end

#to_sString

Returns a CQL SELECT statement with suitable constraints and options.

Returns:

  • (String)

    a CQL SELECT statement with suitable constraints and options



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cql/model/query/select_statement.rb', line 103

def to_s
  s = "SELECT #{@columns || '*'} FROM #{@klass.table_name}"

  s << " USING CONSISTENCY " << (@consistency || @klass.write_consistency)

  unless @where.empty?
    s << " WHERE " << @where.map { |w| w.to_s }.join(' AND ')
  end

  s << " ORDER BY " << @order unless @order.empty?
  s << " LIMIT #{@limit}" unless @limit.nil?
  s << ';'
end

#where(*params) { ... } ⇒ SelectStatement Also known as: and

Create or append to the WHERE clause for this statement. The block that you pass will define the constraint and any where() parameters will be forwarded to the block as yield parameters. This allows late binding of variables in the WHERE clause, e.g. for prepared statements.

Examples:

find people named Joe

where { name == "Joe" }

find people older than 33 who are named Joe or Fred

where { age > 33 }.and { name.in("Joe", "Fred") }

find people older than 33

where { age > 33 }

find by a late-bound search term (e.g. for a named scope)

where(age_chosen_by_user) { |chosen| age > chosen }

find by a column name that is not a valid Ruby identifier

where { timestamp(12345) == "logged in" }

Parameters:

  • *params (Object)

    parameters to be forwarded to the block

Yields:

  • the block will be evaluated in the context of a ComparisonExpression to capture a CQL expression that will be appended to the WHERE clause

Returns:

See Also:



53
54
55
56
# File 'lib/cql/model/query/select_statement.rb', line 53

def where(*params, &block)
  @where << ComparisonExpression.new(*params, &block)
  self
end