Skip to content

Commit

Permalink
fix query_builder to encode timestamps correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Roguelazer committed Sep 17, 2021
1 parent e7cd483 commit 18db1b0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/query_builder/cql/operators/cql_literal.rb
Expand Up @@ -20,6 +20,7 @@ def cql_literal(value)
return "NaN" if nan?(value)
return "Infinity" if infinity?(value)
return value.to_s if unchanged?(value)
return cql_literal_time(value) if time?(value)
return cql_literal_array(value) if array?(value)
return cql_literal_hash(value) if hash?(value)
return cql_literal_set(value) if set?(value)
Expand All @@ -44,6 +45,11 @@ def cql_literal_set(value)
"{ #{value.to_a.map{ |v| "\'{v}\'" }.join(', ')} }"
end

def cql_literal_time(value)
str = value.strftime("%Y-%m-%dT%H:%M:%S.%L%z")
"'#{str}'"
end

def quote(value)
"'#{value.to_s.gsub("\'", "\'\'")}'"
end
Expand Down Expand Up @@ -88,6 +94,10 @@ def set?(value)
value.is_a? Set
end

def time?(value)
value.is_a? Time
end

end

end # module QueryBuilder::CQL::Operators
1 change: 0 additions & 1 deletion spec/integration/insert_spec.rb
Expand Up @@ -19,5 +19,4 @@

let(:cql) { "INSERT INTO wildlife.species (names, sizes) VALUES (['tiger', 'pantera tigra'], {'length': 3}) USING TIMESTAMP 100 AND TTL 500 IF NOT EXISTS;" }
end

end # describe INSERT
5 changes: 5 additions & 0 deletions spec/unit/cql/operators/cql_literal_spec.rb
Expand Up @@ -80,4 +80,9 @@
let(:output) { "['foo', 'bar', {'baz': 0x03}]" }
end

it_behaves_like :transforming_immutable_data do
let(:input) { Time.at(1631841161.2, in: "-07:00") }
let(:output) { "'2021-09-16T18:12:41.200-0700'" }
end

end # describe QueryBuilder::CQL::Operators.cql_literal

0 comments on commit 18db1b0

Please sign in to comment.