| 47 | | end |
|---|
| 48 | | end |
|---|
| 49 | | |
|---|
| 50 | | module SQLServerBaseExtensions #:nodoc: |
|---|
| 51 | | def self.append_features(base) |
|---|
| 52 | | super |
|---|
| 53 | | base.extend(ClassMethods) |
|---|
| 54 | | end |
|---|
| 55 | | |
|---|
| 56 | | module ClassMethods #:nodoc: |
|---|
| 57 | | def find_first(conditions = nil, orderings = nil) |
|---|
| 58 | | sql = "SELECT TOP 1 * FROM #{table_name} " |
|---|
| 59 | | add_conditions!(sql, conditions) |
|---|
| 60 | | sql << "ORDER BY #{orderings} " unless orderings.nil? |
|---|
| 61 | | |
|---|
| 62 | | record = connection.select_one(sql, "#{name} Load First") |
|---|
| 63 | | instantiate(record) unless record.nil? |
|---|
| 64 | | end |
|---|
| 65 | | |
|---|
| 66 | | def find_all(conditions = nil, orderings = nil, limit = nil, joins = nil) |
|---|
| 67 | | sql = "SELECT " |
|---|
| 68 | | sql << "TOP #{limit} " unless limit.nil? |
|---|
| 69 | | sql << " * FROM #{table_name} " |
|---|
| 70 | | sql << "#{joins} " if joins |
|---|
| 71 | | add_conditions!(sql, conditions) |
|---|
| 72 | | sql << "ORDER BY #{orderings} " unless orderings.nil? |
|---|
| 73 | | |
|---|
| 74 | | find_by_sql(sql) |
|---|
| 75 | | end |
|---|
| 76 | | end |
|---|
| 77 | | |
|---|
| 78 | | def attributes_with_quotes |
|---|
| 79 | | columns_hash = self.class.columns_hash |
|---|
| 80 | | |
|---|
| 81 | | attrs = @attributes.dup |
|---|
| 82 | | |
|---|
| 83 | | attrs = attrs.reject do |name, value| |
|---|
| 84 | | columns_hash[name].identity |
|---|
| 85 | | end |
|---|
| 86 | | |
|---|
| 87 | | attrs.inject({}) do |attrs_quoted, pair| |
|---|
| 88 | | attrs_quoted[pair.first] = quote(pair.last, columns_hash[pair.first]) |
|---|
| 89 | | attrs_quoted |
|---|
| 90 | | end |
|---|
| 240 | | def select(sql, name = nil) |
|---|
| 241 | | rows = [] |
|---|
| 242 | | |
|---|
| 243 | | log(sql, name, @connection) do |conn| |
|---|
| 244 | | conn.select_all(sql) do |row| |
|---|
| 245 | | record = {} |
|---|
| 246 | | |
|---|
| 247 | | row.column_names.each do |col| |
|---|
| 248 | | record[col] = row[col] |
|---|
| 249 | | end |
|---|
| 250 | | |
|---|
| 251 | | rows << record |
|---|
| 252 | | end |
|---|
| 253 | | end |
|---|
| 254 | | |
|---|
| 255 | | rows |
|---|
| 256 | | end |
|---|
| 257 | | |
|---|
| 258 | | def enable_identity_insert(table_name, enable = true) |
|---|
| 259 | | if has_identity_column(table_name) |
|---|
| 260 | | "SET IDENTITY_INSERT #{table_name} #{enable ? 'ON' : 'OFF'}" |
|---|
| 261 | | end |
|---|
| 262 | | end |
|---|
| 263 | | |
|---|
| 264 | | def get_table_name(sql) |
|---|
| 265 | | if sql =~ /into\s*([^\s]+)\s*/i or |
|---|
| 266 | | sql =~ /update\s*([^\s]+)\s*/i |
|---|
| 267 | | $1 |
|---|
| 268 | | else |
|---|
| 269 | | nil |
|---|
| 270 | | end |
|---|
| 271 | | end |
|---|
| 272 | | |
|---|
| 273 | | def has_identity_column(table_name) |
|---|
| 274 | | return get_identity_column(table_name) != nil |
|---|
| 275 | | end |
|---|
| 276 | | |
|---|
| 277 | | def get_identity_column(table_name) |
|---|
| 278 | | if not @table_columns |
|---|
| 279 | | @table_columns = {} |
|---|
| 280 | | end |
|---|
| 281 | | |
|---|
| 282 | | if @table_columns[table_name] == nil |
|---|
| 283 | | @table_columns[table_name] = columns(table_name) |
|---|
| 284 | | end |
|---|
| 285 | | |
|---|
| 286 | | @table_columns[table_name].each do |col| |
|---|
| 287 | | return col.name if col.identity |
|---|
| 288 | | end |
|---|
| 289 | | |
|---|
| 290 | | return nil |
|---|
| 291 | | end |
|---|
| 292 | | |
|---|
| 293 | | def query_contains_identity_column(sql, col) |
|---|
| 294 | | return sql =~ /[\(\.\,]\s*#{col}/ |
|---|
| 295 | | end |
|---|
| | 199 | def select(sql, name = nil) |
|---|
| | 200 | rows = [] |
|---|
| | 201 | |
|---|
| | 202 | log(sql, name, @connection) do |conn| |
|---|
| | 203 | conn.select_all(sql) do |row| |
|---|
| | 204 | record = {} |
|---|
| | 205 | |
|---|
| | 206 | row.column_names.each do |col| |
|---|
| | 207 | record[col] = row[col] |
|---|
| | 208 | end |
|---|
| | 209 | |
|---|
| | 210 | rows << record |
|---|
| | 211 | end |
|---|
| | 212 | end |
|---|
| | 213 | |
|---|
| | 214 | rows |
|---|
| | 215 | end |
|---|
| | 216 | |
|---|
| | 217 | def enable_identity_insert(table_name, enable = true) |
|---|
| | 218 | if has_identity_column(table_name) |
|---|
| | 219 | "SET IDENTITY_INSERT #{table_name} #{enable ? 'ON' : 'OFF'}" |
|---|
| | 220 | end |
|---|
| | 221 | end |
|---|
| | 222 | |
|---|
| | 223 | def get_table_name(sql) |
|---|
| | 224 | if sql =~ /into\s*([^\s]+)\s*/i or |
|---|
| | 225 | sql =~ /update\s*([^\s]+)\s*/i |
|---|
| | 226 | $1 |
|---|
| | 227 | else |
|---|
| | 228 | nil |
|---|
| | 229 | end |
|---|
| | 230 | end |
|---|
| | 231 | |
|---|
| | 232 | def has_identity_column(table_name) |
|---|
| | 233 | return get_identity_column(table_name) != nil |
|---|
| | 234 | end |
|---|
| | 235 | |
|---|
| | 236 | def get_identity_column(table_name) |
|---|
| | 237 | if not @table_columns |
|---|
| | 238 | @table_columns = {} |
|---|
| | 239 | end |
|---|
| | 240 | |
|---|
| | 241 | if @table_columns[table_name] == nil |
|---|
| | 242 | @table_columns[table_name] = columns(table_name) |
|---|
| | 243 | end |
|---|
| | 244 | |
|---|
| | 245 | @table_columns[table_name].each do |col| |
|---|
| | 246 | return col.name if col.identity |
|---|
| | 247 | end |
|---|
| | 248 | |
|---|
| | 249 | return nil |
|---|
| | 250 | end |
|---|
| | 251 | |
|---|
| | 252 | def query_contains_identity_column(sql, col) |
|---|
| | 253 | return sql =~ /[\(\.\,]\s*#{col}/ |
|---|
| | 254 | end |
|---|