Class ActiveRecord::ConnectionAdapters::SQLServerAdapter
In: vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb
Parent: AbstractAdapter

In ADO mode, this adapter will ONLY work on Windows systems, since it relies on Win32OLE, which, to my knowledge, is only available on Windows.

This mode also relies on the ADO support in the DBI module. If you are using the one-click installer of Ruby, then you already have DBI installed, but the ADO module is NOT installed. You will need to get the latest source distribution of Ruby-DBI from ruby-dbi.sourceforge.net/ unzip it, and copy the file src/lib/dbd_ado/ADO.rb to X:/Ruby/lib/ruby/site_ruby/1.8/DBD/ADO/ADO.rb (you will more than likely need to create the ADO directory). Once you’ve installed that file, you are ready to go.

In ODBC mode, the adapter requires the ODBC support in the DBI module which requires the Ruby ODBC module. Ruby ODBC 0.996 was used in development and testing, and it is available at www.ch-werner.de/rubyodbc/

Options:

  • :mode — ADO or ODBC. Defaults to ADO.
  • :username — Defaults to sa.
  • :password — Defaults to empty string.

ADO specific options:

  • :host — Defaults to localhost.
  • :database — The name of the database. No default, must be provided.

ODBC specific options:

  • :dsn — Defaults to nothing.

ADO code tested on Windows 2000 and higher systems, running ruby 1.8.2 (2004-07-29) [i386-mswin32], and SQL Server 2000 SP3.

ODBC code tested on a Fedora Core 4 system, running FreeTDS 0.63, unixODBC 2.2.11, Ruby ODBC 0.996, Ruby DBI 0.0.23 and Ruby 1.8.2. [Linux strongmad 2.6.11-1.1369_FC4 1 Thu Jun 2 22:55:56 EDT 2005 i686 i686 i386 GNU/Linux]

Methods

Public Instance methods

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 181
181:       def adapter_name
182:         'SQLServer'
183:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 300
300:       def add_limit_offset!(sql, options)
301:         if options.has_key?(:limit) and options.has_key?(:offset) and !options[:limit].nil? and !options[:offset].nil?
302:           options[:order] ||= "id ASC"
303:           total_rows = @connection.select_all("SELECT count(*) as TotalRows from #{get_table_name(sql)}")[0][:TotalRows].to_i
304:           if (options[:limit] + options[:offset]) > total_rows
305:             options[:limit] = (total_rows - options[:offset] > 0) ? (total_rows - options[:offset]) : 1
306:           end
307:           sql.gsub!(/SELECT/i, "SELECT * FROM ( SELECT TOP #{options[:limit]} * FROM ( SELECT TOP #{options[:limit] + options[:offset]}")<<" ) AS tmp1 ORDER BY #{change_order_direction(options[:order])} ) AS tmp2 ORDER BY #{options[:order]}"
308:         else
309:           sql.gsub!(/SELECT/i, "SELECT TOP #{options[:limit]}") unless options[:limit].nil?
310:         end
311:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 256
256:       def begin_db_transaction
257:         @connection["AutoCommit"] = false
258:       rescue Exception => e
259:         @connection["AutoCommit"] = true
260:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 195
195:       def columns(table_name, name = nil)
196:         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}'"
197:         # Comment out if you want to have the Columns select statment logged.
198:         # Personnally, I think it adds unneccessary bloat to the log. 
199:         # If you do comment it out, make sure to un-comment the "result" line that follows
200:         result = log(sql, name) { @connection.select_all(sql) }
201:         #result = @connection.select_all(sql)
202:         columns = []
203:         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]) }
204:         columns
205:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 262
262:       def commit_db_transaction
263:         @connection.commit
264:       ensure
265:         @connection["AutoCommit"] = true
266:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 322
322:       def create_database(name)
323:         execute "CREATE DATABASE #{name}"
324:       end
delete(sql, name = nil)

Alias for update

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 318
318:       def drop_database(name)
319:         execute "DROP DATABASE #{name}"
320:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 238
238:       def execute(sql, name = nil)
239:         if sql =~ /^\s*INSERT/i
240:           insert(sql, name)
241:         elsif sql =~ /^\s*UPDATE|^\s*DELETE/i
242:           log(sql, name) do
243:             @connection.execute(sql)
244:             retVal = select_one("SELECT @@ROWCOUNT AS AffectedRows")["AffectedRows"]
245:           end
246:         else
247:           log(sql, name) { @connection.execute(sql) }
248:         end
249:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 207
207:       def insert(sql, name = nil, pk = nil, id_value = nil)
208:         begin
209:           table_name = get_table_name(sql)
210:           col = get_identity_column(table_name)
211:           ii_enabled = false
212: 
213:           if col != nil
214:             if query_contains_identity_column(sql, col)
215:               begin
216:                 execute enable_identity_insert(table_name, true)
217:                 ii_enabled = true
218:               rescue Exception => e
219:                 raise ActiveRecordError, "IDENTITY_INSERT could not be turned ON"
220:               end
221:             end
222:           end
223:           log(sql, name) do
224:             @connection.execute(sql)
225:             select_one("SELECT @@IDENTITY AS Ident")["Ident"]
226:           end
227:         ensure
228:           if ii_enabled
229:             begin
230:               execute enable_identity_insert(table_name, false)
231:             rescue Exception => e
232:               raise ActiveRecordError, "IDENTITY_INSERT could not be turned OFF"
233:             end
234:           end
235:         end
236:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 165
165:       def native_database_types
166:         {
167:           :primary_key => "int NOT NULL IDENTITY(1, 1) PRIMARY KEY",
168:           :string      => { :name => "varchar(255)" },
169:           :text        => { :name => "text(16)" },
170:           :integer     => { :name => "int(4)", :limit => 11 },
171:           :float       => { :name => "float(8)" },
172:           :datetime    => { :name => "datetime(8)" },
173:           :timestamp   => { :name => "datetime(8)" },
174:           :time        => { :name => "datetime(8)" },
175:           :date        => { :name => "datetime(8)" },
176:           :binary      => { :name => "image(16)" },
177:           :boolean     => { :name => "bit(1)" }
178:         }
179:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 274
274:       def quote(value, column = nil)
275:         case value
276:           when String                
277:             if column && column.type == :binary
278:               "'#{quote_string(column.string_to_binary(value))}'"
279:             else
280:               "'#{quote_string(value)}'"
281:             end
282:           when NilClass              then "NULL"
283:           when TrueClass             then '1'
284:           when FalseClass            then '0'
285:           when Float, Fixnum, Bignum then value.to_s
286:           when Date                  then "'#{value.to_s}'" 
287:           when Time, DateTime        then "'#{value.strftime("%Y-%m-%d %H:%M:%S")}'"
288:           else                            "'#{quote_string(value.to_yaml)}'"
289:         end
290:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 296
296:       def quote_column_name(name)
297:         "[#{name}]"
298:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 292
292:       def quote_string(string)
293:         string.gsub(/\'/, "''")
294:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 313
313:       def recreate_database(name)
314:         drop_database(name)
315:         create_database(name)
316:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 268
268:       def rollback_db_transaction
269:         @connection.rollback
270:       ensure
271:         @connection["AutoCommit"] = true
272:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 185
185:       def select_all(sql, name = nil)
186:         select(sql, name)
187:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 189
189:       def select_one(sql, name = nil)
190:         add_limit!(sql, nil)
191:         result = select(sql, name)
192:         result.nil? ? nil : result.first
193:       end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb, line 251
251:       def update(sql, name = nil)
252:         execute(sql, name)
253:       end

[Validate]