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 291
291: def add_limit_with_offset!(sql, limit, offset)
292: order_by = sql.include?("ORDER BY") ? get_order_by(sql.sub(/.*ORDER\sBY./, "")) : nil
293: 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]}"
294: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 296
296: def add_limit_without_offset!(sql, limit)
297: limit.nil? ? sql : sql.gsub!(/SELECT/i, "SELECT TOP #{limit}")
298: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 241
241: def begin_db_transaction
242: begin
243: @connection["AutoCommit"] = false
244: rescue Exception => e
245: @connection["AutoCommit"] = true
246: end
247: 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 249
249: def commit_db_transaction
250: begin
251: @connection.commit
252: ensure
253: @connection["AutoCommit"] = true
254: end
255: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 309
309: def create_database(name)
310: execute "CREATE DATABASE #{name}"
311: end
Alias for update
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 305
305: def drop_database(name)
306: execute "DROP DATABASE #{name}"
307: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 221
221: def execute(sql, name = nil)
222: if sql =~ /^INSERT/i
223: insert(sql, name)
224: elsif sql =~ /^UPDATE|DELETE/i
225: log(sql, name, @connection) do |conn|
226: conn.execute(sql)
227: retVal = select_one("SELECT @@ROWCOUNT AS AffectedRows")["AffectedRows"]
228: end
229: else
230: log(sql, name, @connection) do |conn|
231: conn.execute(sql)
232: end
233: end
234: 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: log(sql, name, @connection) do |conn|
207: conn.execute(sql)
208: select_one("SELECT @@IDENTITY AS Ident")["Ident"]
209: end
210: ensure
211: if ii_enabled
212: begin
213: execute enable_identity_insert(table_name, false)
214: rescue Exception => e
215: # Couldn't turn off IDENTITY_INSERT
216: end
217: end
218: end
219: 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 265
265: def quote(value, column = nil)
266: case value
267: when String
268: if column && column.type == :binary
269: "'#{quote_string(column.string_to_binary(value))}'"
270: else
271: "'#{quote_string(value)}'"
272: end
273: when NilClass then "NULL"
274: when TrueClass then '1'
275: when FalseClass then '0'
276: when Float, Fixnum, Bignum then value.to_s
277: when Date then "'#{value.to_s}'"
278: when Time, DateTime then "'#{value.strftime("%Y-%m-%d %H:%M:%S")}'"
279: else "'#{quote_string(value.to_yaml)}'"
280: end
281: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 287
287: def quote_column_name(name)
288: "[#{name}]"
289: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 283
283: def quote_string(s)
284: s.gsub(/\'/, "''")
285: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 300
300: def recreate_database(name)
301: drop_database(name)
302: create_database(name)
303: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 257
257: def rollback_db_transaction
258: begin
259: @connection.rollback
260: ensure
261: @connection["AutoCommit"] = true
262: end
263: 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 236
236: def update(sql, name = nil)
237: execute(sql, name)
238: end