Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

forgot a file

  • Loading branch information...
commit ec308976657d079dc03327f07f8160897cfcdf4b 1 parent 5b6df04
Nusco+Beppe authored
Showing with 106 additions and 0 deletions.
  1. +106 −0 lib/sequel_core/adapters/shared/mssql.rb
View
106 lib/sequel_core/adapters/shared/mssql.rb
@@ -0,0 +1,106 @@
+module Sequel
+ module MSSQL
+ module DatabaseMethods
+ AUTO_INCREMENT = 'IDENTITY(1,1)'.freeze
+
+ def auto_increment_sql
+ AUTO_INCREMENT
+ end
+
+ def dataset(opts = nil)
+ ds = super
+ ds.extend(DatasetMethods)
+ ds
+ end
+ end
+
+ module DatasetMethods
+ def complex_expression_sql(op, args)
+ case op
+ when :'||'
+ super(:+, args)
+ else
+ super(op, args)
+ end
+ end
+
+ def full_text_search(cols, terms, opts = {})
+ filter("CONTAINS (#{literal(cols)}, #{literal(terms)})")
+ end
+
+ # Allows you to do .nolock on a query
+ def nolock
+ clone(:with => "(NOLOCK)")
+ end
+
+ # Formats a SELECT statement using the given options and the dataset
+ # options.
+ def select_sql(opts = nil)
+ opts = opts ? @opts.merge(opts) : @opts
+
+ if sql = opts[:sql]
+ return sql
+ end
+
+ # ADD TOP to SELECT string for LIMITS
+ if limit = opts[:limit]
+ top = "TOP #{limit} "
+ raise Error, "Offset not supported" if opts[:offset]
+ end
+
+ columns = opts[:select]
+ select_columns = columns ? column_list(columns) : WILDCARD
+
+ if distinct = opts[:distinct]
+ distinct_clause = distinct.empty? ? "DISTINCT" : "DISTINCT ON (#{expression_list(distinct)})"
+ sql = "SELECT #{top}#{distinct_clause} #{select_columns}"
+ else
+ sql = "SELECT #{top}#{select_columns}"
+ end
+
+ if opts[:from]
+ sql << " FROM #{source_list(opts[:from])}"
+ end
+
+ # ADD WITH to SELECT string for NOLOCK
+ if with = opts[:with]
+ sql << " WITH #{with}"
+ end
+
+ if join = opts[:join]
+ join.each{|j| sql << literal(j)}
+ end
+
+ if where = opts[:where]
+ sql << " WHERE #{literal(where)}"
+ end
+
+ if group = opts[:group]
+ sql << " GROUP BY #{expression_list(group)}"
+ end
+
+ if order = opts[:order]
+ sql << " ORDER BY #{expression_list(order)}"
+ end
+
+ if having = opts[:having]
+ sql << " HAVING #{literal(having)}"
+ end
+
+ if union = opts[:union]
+ sql << (opts[:union_all] ? \
+ " UNION ALL #{union.sql}" : " UNION #{union.sql}")
+ elsif intersect = opts[:intersect]
+ sql << (opts[:intersect_all] ? \
+ " INTERSECT ALL #{intersect.sql}" : " INTERSECT #{intersect.sql}")
+ elsif except = opts[:except]
+ sql << (opts[:except_all] ? \
+ " EXCEPT ALL #{except.sql}" : " EXCEPT #{except.sql}")
+ end
+
+ sql
+ end
+ alias_method :sql, :select_sql
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.