Class: Cql::Model::Query::SelectStatement
- 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
- #asc ⇒ Object
- #desc ⇒ Object
-
#each {|row| ... } ⇒ true
Execute this SELECT statement on the CQL client connection and yield each row of the as an instance of CQL::Model.
-
#execute {|row| ... } ⇒ true
(also: #each_row)
Execute this SELECT statement on the CQL client connection and yield each row of the result set as a raw-data Hash.
-
#initialize(klass, client = nil) ⇒ SelectStatement
constructor
Instantiate statement.
- #limit(lim) ⇒ Object
-
#order(*columns) ⇒ SelectStatement
(also: #order_by)
Specify the order in which result rows should be returned.
- #select(*columns) ⇒ Object
-
#to_s ⇒ String
A CQL SELECT statement with suitable constraints and options.
-
#where(*params) { ... } ⇒ SelectStatement
(also: #and)
Create or append to the WHERE clause for this statement.
Methods inherited from Statement
Constructor Details
#initialize(klass, client = nil) ⇒ SelectStatement
Instantiate 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
#asc ⇒ Object
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 |
#desc ⇒ Object
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.
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.
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
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.
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
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_s ⇒ String
Returns 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.
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 |