Class: Sequel::ODBC::MSSQL::Dataset

Inherits:
ODBC::Dataset
  • Object
show all
Defined in:
lib/sequel/adapters/odbc_mssql.rb

Instance Method Summary collapse

Instance Method Details

#nolockObject

Allows you to do .nolock on a query



19
20
21
# File 'lib/sequel/adapters/odbc_mssql.rb', line 19

def nolock
  clone_merge(:with => "(NOLOCK)")
end

#select_sql(opts = nil) ⇒ Object Also known as: sql

Formats a SELECT statement using the given options and the dataset options.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sequel/adapters/odbc_mssql.rb', line 25

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 SequelError, "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 (#{column_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]
    sql << join
  end

  if where = opts[:where]
    sql << " WHERE #{where}"
  end

  if group = opts[:group]
    sql << " GROUP BY #{column_list(group)}"
  end

  if order = opts[:order]
    sql << " ORDER BY #{column_list(order)}"
  end

  if having = opts[:having]
    sql << " HAVING #{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