Permalink
Browse files

let the connection buffer for now, add exec update/delete/insert methors

  • Loading branch information...
1 parent dc74a8c commit b0ea6f13d0c7bb5c51813a7ec639250f49265e20 @camilo committed Apr 9, 2012
Showing with 79 additions and 10 deletions.
  1. +16 −10 lib/sequel/adapters/vertica.rb
  2. +63 −0 spec/adapters/vertica_spec.rb
@@ -3,6 +3,8 @@
module Sequel
module Vertica
class Database < Sequel::Database
+ ::Vertica::Connection.send(:alias_method,:execute, :query)
+ PK_NAME = 'C_PRIMARY'
set_adapter_scheme :vertica
def initialize(opts={})
@@ -23,10 +25,14 @@ def connect(server)
def execute(sql, opts={}, &block)
synchronize(opts[:server]) do |conn|
- conn.query(sql, &block)
+ res = conn.query(sql)
+ res.each(&block)
end
end
+ alias_method :execute_insert, :execute
+ alias_method :execute_dui, :execute
+
def supports_create_table_if_not_exists?
true
end
@@ -48,17 +54,16 @@ def identifier_output_method_default
end
def schema_parse_table(table_name, opts)
- # left join table_constrains and make pk true when
- # constrain name is C_PRIMARY
- metadata_dataset.select(:column_name,
- :is_nullable.as(:allow_null),
- (:column_default).as(:default),
- (:data_type).as(:db_type), :constraint_name
- ).filter(:table_name => table_name).from(:columns).
- left_outer_join(:table_constraints, :table_id => :table_id).map do |row|
+ selector = [:column_name, :constraint_name, :is_nullable.as(:allow_null),
+ (:column_default).as(:default), (:data_type).as(:db_type)]
+
+ dataset = metadata_dataset.select(*selector).filter(:table_name => table_name).
+ from(:columns).left_outer_join(:table_constraints, :table_id => :table_id)
+
+ dataset.map do |row|
row[:default] = nil if blank_object?(row[:default])
row[:type] = schema_column_type(row[:db_type])
- row[:primary_key] = row.delete(:constraint_name) == 'C_PRIMARY'
+ row[:primary_key] = row.delete(:constraint_name) == PK_NAME
[row.delete(:column_name).to_sym, row]
end
end
@@ -70,6 +75,7 @@ class Dataset < Sequel::Dataset
def fetch_rows(sql)
execute(sql) { |row| yield row }
end
+
end
end
end
@@ -42,3 +42,66 @@
end
end
+
+describe "A vertica dataset" do
+ before do
+ @d = VERTICA_DB[:test]
+ @d.delete if @d.count > 0 # Vertica will throw an error if the table has just been created and does not have a super projection yet.
+ end
+
+ specify "should quote columns and tables using double quotes if quoting identifiers" do
+ @d.select(:name).sql.should == \
+ 'SELECT "name" FROM "test"'
+
+ @d.select('COUNT(*)'.lit).sql.should == \
+ 'SELECT COUNT(*) FROM "test"'
+
+ @d.select(:max.sql_function(:value)).sql.should == \
+ 'SELECT max("value") FROM "test"'
+
+ @d.select(:NOW.sql_function).sql.should == \
+ 'SELECT NOW() FROM "test"'
+
+ @d.select(:max.sql_function(:items__value)).sql.should == \
+ 'SELECT max("items"."value") FROM "test"'
+
+ @d.order(:name.desc).sql.should == \
+ 'SELECT * FROM "test" ORDER BY "name" DESC'
+
+ @d.select('test.name AS item_name'.lit).sql.should == \
+ 'SELECT test.name AS item_name FROM "test"'
+
+ @d.select('"name"'.lit).sql.should == \
+ 'SELECT "name" FROM "test"'
+
+ @d.select('max(test."name") AS "max_name"'.lit).sql.should == \
+ 'SELECT max(test."name") AS "max_name" FROM "test"'
+
+ @d.insert_sql(:x => :y).should =~ \
+ /\AINSERT INTO "test" \("x"\) VALUES \("y"\)( RETURNING NULL)?\z/
+
+ end
+
+ specify "should quote fields correctly when reversing the order if quoting identifiers" do
+ @d.reverse_order(:name).sql.should == \
+ 'SELECT * FROM "test" ORDER BY "name" DESC'
+
+ @d.reverse_order(:name.desc).sql.should == \
+ 'SELECT * FROM "test" ORDER BY "name" ASC'
+
+ @d.reverse_order(:name, :test.desc).sql.should == \
+ 'SELECT * FROM "test" ORDER BY "name" DESC, "test" ASC'
+
+ @d.reverse_order(:name.desc, :test).sql.should == \
+ 'SELECT * FROM "test" ORDER BY "name" ASC, "test" DESC'
+ end
+
+ specify "should support regexps" do
+ @d << {:name => 'abc', :value => 1}
+ @d << {:name => 'bcd', :value => 2}
+
+ @d.filter(:name => /bc/).count.should == 2
+ @d.filter(:name => /^bc/).count.should == 1
+ end
+
+end

0 comments on commit b0ea6f1

Please sign in to comment.