Methods
- adapter_name
- add_limit_with_offset!
- add_limit_without_offset!
- begin_db_transaction
- columns
- commit_db_transaction
- create_database
- delete
- drop_database
- execute
- insert
- native_database_types
- quote
- quote_column_name
- quote_string
- recreate_database
- rollback_db_transaction
- select_all
- select_one
- update
Public Instance methods
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 162
162: def adapter_name
163: 'SQLServer'
164: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 292
292: def add_limit_with_offset!(sql, limit, offset)
293: order_by = sql.include?("ORDER BY") ? get_order_by(sql.sub(/.*ORDER\sBY./, "")) : nil
294: sql.gsub!(/SELECT/i, "SELECT * FROM ( SELECT TOP #{limit} * FROM ( SELECT TOP #{limit + offset}")<<" ) AS tmp1 ORDER BY #{order_by[1]} ) AS tmp2 ORDER BY #{order_by[0]}"
295: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 297
297: def add_limit_without_offset!(sql, limit)
298: limit.nil? ? sql : sql.gsub!(/SELECT/i, "SELECT TOP #{limit}")
299: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 242
242: def begin_db_transaction
243: begin
244: @connection["AutoCommit"] = false
245: rescue Exception => e
246: @connection["AutoCommit"] = true
247: end
248: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 177
177: def columns(table_name, name = nil)
178: sql = "SELECT COLUMN_NAME as ColName, COLUMN_DEFAULT as DefaultValue, DATA_TYPE as ColType, COL_LENGTH('#{table_name}', COLUMN_NAME) as Length, COLUMNPROPERTY(OBJECT_ID('#{table_name}'), COLUMN_NAME, 'IsIdentity') as IsIdentity, NUMERIC_SCALE as Scale FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME = '#{table_name}'"
179: result = nil
180: # Uncomment if you want to have the Columns select statment logged.
181: # Personnally, I think it adds unneccessary bloat to the log.
182: # If you do uncomment, make sure to comment the "result" line that follows
183: log(sql, name, @connection) { |conn| result = conn.select_all(sql) }
184: #result = @connection.select_all(sql)
185: columns = []
186: result.each { |field| columns << ColumnWithIdentity.new(field[:ColName], field[:DefaultValue].to_s.gsub!(/[()\']/,"") =~ /null/ ? nil : field[:DefaultValue], "#{field[:ColType]}(#{field[:Length]})", field[:IsIdentity] == 1 ? true : false, field[:Scale]) }
187: columns
188: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 250
250: def commit_db_transaction
251: begin
252: @connection.commit
253: ensure
254: @connection["AutoCommit"] = true
255: end
256: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 310
310: def create_database(name)
311: execute "CREATE DATABASE #{name}"
312: end
Alias for update
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 306
306: def drop_database(name)
307: execute "DROP DATABASE #{name}"
308: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 222
222: def execute(sql, name = nil)
223: if sql =~ /^INSERT/i
224: insert(sql, name)
225: elsif sql =~ /^UPDATE|DELETE/i
226: log(sql, name, @connection) do |conn|
227: conn.execute(sql)
228: retVal = select_one("SELECT @@ROWCOUNT AS AffectedRows")["AffectedRows"]
229: end
230: else
231: log(sql, name, @connection) do |conn|
232: conn.execute(sql)
233: end
234: end
235: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 190
190: def insert(sql, name = nil, pk = nil, id_value = nil)
191: begin
192: table_name = get_table_name(sql)
193: col = get_identity_column(table_name)
194: ii_enabled = false
195:
196: if col != nil
197: if query_contains_identity_column(sql, col)
198: begin
199: execute enable_identity_insert(table_name, true)
200: ii_enabled = true
201: rescue Exception => e
202: # Coulnd't turn on IDENTITY_INSERT
203: end
204: end
205: end
206:
207: log(sql, name, @connection) do |conn|
208: conn.execute(sql)
209: select_one("SELECT @@IDENTITY AS Ident")["Ident"]
210: end
211: ensure
212: if ii_enabled
213: begin
214: execute enable_identity_insert(table_name, false)
215: rescue Exception => e
216: # Couldn't turn off IDENTITY_INSERT
217: end
218: end
219: end
220: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 146
146: def native_database_types
147: {
148: :primary_key => "int NOT NULL IDENTITY(1, 1) PRIMARY KEY",
149: :string => { :name => "varchar(255)" },
150: :text => { :name => "text(16)" },
151: :integer => { :name => "int(4)", :limit => 11 },
152: :float => { :name => "float(8)" },
153: :datetime => { :name => "datetime(8)" },
154: :timestamp => { :name => "datetime(8)" },
155: :time => { :name => "datetime(8)" },
156: :date => { :name => "datetime(8)" },
157: :binary => { :name => "image(16)" },
158: :boolean => { :name => "bit(1)" }
159: }
160: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 266
266: def quote(value, column = nil)
267: case value
268: when String
269: if column && column.type == :binary
270: "'#{quote_string(column.string_to_binary(value))}'"
271: else
272: "'#{quote_string(value)}'"
273: end
274: when NilClass then "NULL"
275: when TrueClass then '1'
276: when FalseClass then '0'
277: when Float, Fixnum, Bignum then value.to_s
278: when Date then "'#{value.to_s}'"
279: when Time, DateTime then "'#{value.strftime("%Y-%m-%d %H:%M:%S")}'"
280: else "'#{quote_string(value.to_yaml)}'"
281: end
282: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 288
288: def quote_column_name(name)
289: "[#{name}]"
290: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 284
284: def quote_string(s)
285: s.gsub(/\'/, "''")
286: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 301
301: def recreate_database(name)
302: drop_database(name)
303: create_database(name)
304: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 258
258: def rollback_db_transaction
259: begin
260: @connection.rollback
261: ensure
262: @connection["AutoCommit"] = true
263: end
264: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 166
166: def select_all(sql, name = nil)
167: add_limit!(sql, nil)
168: select(sql, name)
169: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 171
171: def select_one(sql, name = nil)
172: add_limit!(sql, nil)
173: result = select(sql, name)
174: result.nil? ? nil : result.first
175: end
This method is also aliased as
delete
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 237
237: def update(sql, name = nil)
238: execute(sql, name)
239: end