Permalink
Browse files

Added support for multi_insert and unicode string literals in MSSQL.

Fixed ODBC::Database#transaction.
  • Loading branch information...
1 parent 8365e54 commit d6d45a37e4ae29a4d1fc87c3e6046c3ece1213d5 David Lee committed with Nov 13, 2008
Showing with 46 additions and 11 deletions.
  1. +0 −11 lib/sequel_core/adapters/ado.rb
  2. +24 −0 lib/sequel_core/adapters/odbc.rb
  3. +22 −0 lib/sequel_core/adapters/shared/mssql.rb
@@ -52,17 +52,6 @@ def execute(sql, opts={})
end
class Dataset < Sequel::Dataset
- def literal(v)
- case v
- when Time
- literal(v.iso8601)
- when Date, DateTime
- literal(v.to_s)
- else
- super
- end
- end
-
def fetch_rows(sql)
execute(sql) do |s|
@columns = s.Fields.extend(Enumerable).map do |column|
@@ -63,6 +63,30 @@ def execute_dui(sql, opts={})
synchronize(opts[:server]){|conn| conn.do(sql)}
end
alias_method :do, :execute_dui
+
+ # Support single level transactions on ODBC
+ def transaction(server=nil)
+ synchronize(server) do |conn|
+ return yield(conn) if @transactions.include?(Thread.current)
+ log_info(begin_transaction_sql)
+ conn.do(begin_transaction_sql)
+ begin
+ @transactions << Thread.current
+ yield(conn)
+ rescue ::Exception => e
+ log_info(rollback_transaction_sql)
+ conn.do(rollback_transaction_sql)
+ transaction_error(e)
+ ensure
+ unless e
+ log_info(commit_transaction_sql)
+ conn.do(commit_transaction_sql)
+ end
+ @transactions.delete(Thread.current)
+ end
+ end
+ end
+
end
class Dataset < Sequel::Dataset
@@ -48,11 +48,33 @@ def full_text_search(cols, terms, opts = {})
filter("CONTAINS (#{literal(cols)}, #{literal(terms)})")
end
+ def literal(v)
+ case v
+ when String
+ "N#{super}"
+ when Time
+ literal(v.iso8601)
+ when Date, DateTime
+ literal(v.to_s)
+ else
+ super
+ end
+ end
+
+ def multi_insert_sql(columns, values)
+ values = values.map {|r| "SELECT #{expression_list(r)}" }.join(" UNION ALL ")
+ ["INSERT INTO #{source_list(@opts[:from])} (#{identifier_list(columns)}) #{values}"]
+ end
+
# Allows you to do .nolock on a query
def nolock
clone(:with => "(NOLOCK)")
end
+ def quoted_identifier(name)
+ "[#{name}]"
+ end
+
# Formats a SELECT statement using the given options and the dataset
# options.
def select_sql(opts = nil)

0 comments on commit d6d45a3

Please sign in to comment.