mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fixed the MS SQL adapter to work with the new limit/offset approach and with binary data (still suffering from 7KB limit, though) #901 [delynnb]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@982 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
4043b5cdb3
commit
f30b7a31bd
7 changed files with 185 additions and 184 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
*SVN*
|
||||||
|
|
||||||
|
* Fixed the MS SQL adapter to work with the new limit/offset approach and with binary data (still suffering from 7KB limit, though) #901 [delynnb]
|
||||||
|
|
||||||
|
|
||||||
*1.9.0* (22th March, 2005)
|
*1.9.0* (22th March, 2005)
|
||||||
|
|
||||||
* Added adapter independent limit clause as a two-element array with the first being the limit, the second being the offset #795 [Sam Stephenson]. Example:
|
* Added adapter independent limit clause as a two-element array with the first being the limit, the second being the offset #795 [Sam Stephenson]. Example:
|
||||||
|
|
|
@ -5,6 +5,27 @@ require 'active_record/connection_adapters/abstract_adapter'
|
||||||
# Author: Joey Gibson <joey@joeygibson.com>
|
# Author: Joey Gibson <joey@joeygibson.com>
|
||||||
# Date: 10/14/2004
|
# Date: 10/14/2004
|
||||||
#
|
#
|
||||||
|
# Modifications: DeLynn Berry <delynnb@megastarfinancial.com>
|
||||||
|
# Date: 3/22/2005
|
||||||
|
#
|
||||||
|
# This adapter will ONLY work on Windows systems, since it relies on Win32OLE, which,
|
||||||
|
# to my knowledge, is only available on Window.
|
||||||
|
#
|
||||||
|
# It 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 http://ruby-dbi.sourceforge.net/
|
||||||
|
# unzip it, and copy the file <tt>src/lib/dbd_ado/ADO.rb</tt> to
|
||||||
|
# <tt>X:/Ruby/lib/ruby/site_ruby/1.8/DBD/ADO/ADO.rb</tt> (you will need to create
|
||||||
|
# the ADO directory). Once you've installed that file, you are ready to go.
|
||||||
|
#
|
||||||
|
# Options:
|
||||||
|
#
|
||||||
|
# * <tt>:host</tt> -- Defaults to localhost
|
||||||
|
# * <tt>:username</tt> -- Defaults to sa
|
||||||
|
# * <tt>:password</tt> -- Defaults to nothing
|
||||||
|
# * <tt>:database</tt> -- The name of the database. No default, must be provided.
|
||||||
|
#
|
||||||
# I have tested this code on a WindowsXP Pro SP1 system,
|
# I have tested this code on a WindowsXP Pro SP1 system,
|
||||||
# ruby 1.8.2 (2004-07-29) [i386-mswin32], SQL Server 2000.
|
# ruby 1.8.2 (2004-07-29) [i386-mswin32], SQL Server 2000.
|
||||||
#
|
#
|
||||||
|
@ -19,7 +40,7 @@ module ActiveRecord
|
||||||
username = config[:username] ? config[:username].to_s : 'sa'
|
username = config[:username] ? config[:username].to_s : 'sa'
|
||||||
password = config[:password].to_s
|
password = config[:password].to_s
|
||||||
|
|
||||||
if config.has_key? :database
|
if config.has_key?(:database)
|
||||||
database = config[:database]
|
database = config[:database]
|
||||||
else
|
else
|
||||||
raise ArgumentError, "No database specified. Missing argument: database."
|
raise ArgumentError, "No database specified. Missing argument: database."
|
||||||
|
@ -38,92 +59,110 @@ module ActiveRecord
|
||||||
|
|
||||||
def initialize(name, default, sql_type = nil, is_identity = false, scale_value = 0)
|
def initialize(name, default, sql_type = nil, is_identity = false, scale_value = 0)
|
||||||
super(name, default, sql_type)
|
super(name, default, sql_type)
|
||||||
|
|
||||||
@scale = scale_value
|
@scale = scale_value
|
||||||
@identity = is_identity
|
@identity = is_identity
|
||||||
end
|
end
|
||||||
|
|
||||||
def binary_to_string(value)
|
|
||||||
value
|
|
||||||
end
|
|
||||||
|
|
||||||
def string_to_binary(value)
|
|
||||||
value
|
|
||||||
end
|
|
||||||
|
|
||||||
def simplified_type(field_type)
|
def simplified_type(field_type)
|
||||||
case field_type
|
case field_type
|
||||||
when /int/i
|
when /int|bigint|smallint|tinyint/i : :integer
|
||||||
:integer
|
when /float|double|decimal|money|numeric|real|smallmoney/i : @scale == 0 ? :integer : :float
|
||||||
when /float|double|decimal|numeric/i
|
when /datetime|smalldatetime/i : :datetime
|
||||||
if @scale == 0
|
when /timestamp/i : :timestamp
|
||||||
:integer
|
when /time/i : :time
|
||||||
else
|
when /text|ntext/i : :text
|
||||||
:float
|
when /binary|image|varbinary/i : :binary
|
||||||
nil
|
when /char|nchar|nvarchar|string|varchar/i : :string
|
||||||
end
|
when /bit/i : :boolean
|
||||||
when /datetime/i
|
|
||||||
:datetime
|
|
||||||
when /timestamp/i
|
|
||||||
:timestamp
|
|
||||||
when /time/i
|
|
||||||
:time
|
|
||||||
when /date/i
|
|
||||||
:date
|
|
||||||
when /clob|text|ntext/i
|
|
||||||
:text
|
|
||||||
when /blob|binary|image/i
|
|
||||||
:binary
|
|
||||||
when /char|string/i
|
|
||||||
:string
|
|
||||||
when /boolean|bit/i
|
|
||||||
:boolean
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def string_to_time(string)
|
def type_cast(value)
|
||||||
return string if string.is_a?(Time)
|
return nil if value.nil? || value =~ /^\s*null\s*$/i
|
||||||
time_array = ParseDate.parsedate(string, true)
|
case type
|
||||||
time_array.each_index do |i|
|
when :string then value
|
||||||
case i
|
when :integer then value == true || value == false ? value == true ? '1' : '0' : value.to_i
|
||||||
when 0
|
when :float then value.to_f
|
||||||
time_array[i] = time_array[i].nil? ? "2000" : time_array[i].to_s
|
when :datetime then cast_to_date_or_time(value)
|
||||||
when 1
|
when :timestamp then cast_to_time(value)
|
||||||
time_array[i] = time_array[i].nil? ? "Jan" : time_array[i].to_s
|
when :time then cast_to_time(value)
|
||||||
when 2
|
else value
|
||||||
time_array[i] = time_array[i].nil? ? "1" : time_array[i].to_s
|
end
|
||||||
when 3
|
end
|
||||||
time_array[i] = time_array[i].nil? ? "0" : time_array[i].to_s
|
|
||||||
when 4
|
def cast_to_date_or_time(value)
|
||||||
time_array[i] = time_array[i].nil? ? "0" : time_array[i].to_s
|
return value if value.is_a?(Date)
|
||||||
when 5
|
guess_date_or_time (value.is_a?(Time)) ? value : cast_to_time(value)
|
||||||
time_array[i] = time_array[i].nil? ? "0" : time_array[i].to_s
|
end
|
||||||
|
|
||||||
|
def cast_to_time(value)
|
||||||
|
return value if value.is_a?(Time)
|
||||||
|
time_array = ParseDate.parsedate value
|
||||||
|
time_array[0] ||= 2000; time_array[1] ||= 1; time_array[2] ||= 1;
|
||||||
|
Time.send Base.default_timezone, *time_array
|
||||||
|
end
|
||||||
|
|
||||||
|
def guess_date_or_time(value)
|
||||||
|
(value.hour == 0 and value.min == 0 and value.sec == 0) ?
|
||||||
|
Date.new(value.year, value.month, value.day) : value
|
||||||
|
end
|
||||||
|
|
||||||
|
# These methods will only allow the adapter to insert binary data with a length of 7K or less
|
||||||
|
# because of a SQL Server statement length policy.
|
||||||
|
def string_to_binary(value)
|
||||||
|
value.gsub(/(\r|\n|\0|\x1a)/) do
|
||||||
|
case $1
|
||||||
|
when "\r"
|
||||||
|
"%00"
|
||||||
|
when "\n"
|
||||||
|
"%01"
|
||||||
|
when "\0"
|
||||||
|
"%02"
|
||||||
|
when "\x1a"
|
||||||
|
"%03"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def binary_to_string(value)
|
||||||
|
value.gsub(/(%00|%01|%02|%03)/) do
|
||||||
|
case $1
|
||||||
|
when "%00"
|
||||||
|
"\r"
|
||||||
|
when "%01"
|
||||||
|
"\n"
|
||||||
|
when "%02\0"
|
||||||
|
"\0"
|
||||||
|
when "%03"
|
||||||
|
"\x1a"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
# treat 0000-00-00 00:00:00 as nil
|
|
||||||
Time.send(Base.default_timezone, *time_array) rescue nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# This adapter will ONLY work on Windows systems, since it relies on Win32OLE, which,
|
|
||||||
# to my knowledge, is only available on Window.
|
|
||||||
#
|
|
||||||
# It 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 http://ruby-dbi.sourceforge.net/
|
|
||||||
# unzip it, and copy the file <tt>src/lib/dbd_ado/ADO.rb</tt> to
|
|
||||||
# <tt>X:/Ruby/lib/ruby/site_ruby/1.8/DBD/ADO/ADO.rb</tt> (you will need to create
|
|
||||||
# the ADO directory). Once you've installed that file, you are ready to go.
|
|
||||||
#
|
|
||||||
# Options:
|
|
||||||
#
|
|
||||||
# * <tt>:host</tt> -- Defaults to localhost
|
|
||||||
# * <tt>:username</tt> -- Defaults to sa
|
|
||||||
# * <tt>:password</tt> -- Defaults to nothing
|
|
||||||
# * <tt>:database</tt> -- The name of the database. No default, must be provided.
|
|
||||||
class SQLServerAdapter < AbstractAdapter
|
class SQLServerAdapter < AbstractAdapter
|
||||||
|
|
||||||
|
def native_database_types
|
||||||
|
{
|
||||||
|
:primary_key => "int NOT NULL IDENTITY(1, 1) PRIMARY KEY",
|
||||||
|
:string => { :name => "varchar(255)" },
|
||||||
|
:text => { :name => "text(16)" },
|
||||||
|
:integer => { :name => "int(4)", :limit => 11 },
|
||||||
|
:float => { :name => "float(8)" },
|
||||||
|
:datetime => { :name => "datetime(8)" },
|
||||||
|
:timestamp => { :name => "datetime(8)" },
|
||||||
|
:time => { :name => "datetime(8)" },
|
||||||
|
:date => { :name => "datetime(8)" },
|
||||||
|
:binary => { :name => "image(16)" },
|
||||||
|
:boolean => { :name => "bit(1)" }
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def adapter_name
|
||||||
|
'SQLServer'
|
||||||
|
end
|
||||||
|
|
||||||
def select_all(sql, name = nil)
|
def select_all(sql, name = nil)
|
||||||
add_limit!(sql, nil)
|
add_limit!(sql, nil)
|
||||||
select(sql, name)
|
select(sql, name)
|
||||||
|
@ -136,36 +175,22 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def columns(table_name, name = nil)
|
def columns(table_name, name = nil)
|
||||||
sql = <<EOL
|
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}'"
|
||||||
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}'
|
|
||||||
EOL
|
|
||||||
|
|
||||||
result = nil
|
result = nil
|
||||||
# Uncomment if you want to have the Columns select statment logged.
|
# Uncomment if you want to have the Columns select statment logged.
|
||||||
# Personnally, I think it adds unneccessary SQL statement bloat to the log.
|
# Personnally, I think it adds unneccessary bloat to the log.
|
||||||
# If you do uncomment, make sure to comment the "result" line that follows
|
# If you do uncomment, make sure to comment the "result" line that follows
|
||||||
log(sql, name, @connection) { |conn| result = conn.select_all(sql) }
|
log(sql, name, @connection) { |conn| result = conn.select_all(sql) }
|
||||||
#result = @connection.select_all(sql)
|
#result = @connection.select_all(sql)
|
||||||
columns = []
|
columns = []
|
||||||
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]) }
|
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]) }
|
||||||
|
|
||||||
columns
|
columns
|
||||||
end
|
end
|
||||||
|
|
||||||
def insert(sql, name = nil, pk = nil, id_value = nil)
|
def insert(sql, name = nil, pk = nil, id_value = nil)
|
||||||
begin
|
begin
|
||||||
table_name = get_table_name(sql)
|
table_name = get_table_name(sql)
|
||||||
|
|
||||||
col = get_identity_column(table_name)
|
col = get_identity_column(table_name)
|
||||||
|
|
||||||
ii_enabled = false
|
ii_enabled = false
|
||||||
|
|
||||||
if col != nil
|
if col != nil
|
||||||
|
@ -181,14 +206,12 @@ EOL
|
||||||
|
|
||||||
log(sql, name, @connection) do |conn|
|
log(sql, name, @connection) do |conn|
|
||||||
conn.execute(sql)
|
conn.execute(sql)
|
||||||
|
|
||||||
select_one("SELECT @@IDENTITY AS Ident")["Ident"]
|
select_one("SELECT @@IDENTITY AS Ident")["Ident"]
|
||||||
end
|
end
|
||||||
ensure
|
ensure
|
||||||
if ii_enabled
|
if ii_enabled
|
||||||
begin
|
begin
|
||||||
execute enable_identity_insert(table_name, false)
|
execute enable_identity_insert(table_name, false)
|
||||||
|
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
# Couldn't turn off IDENTITY_INSERT
|
# Couldn't turn off IDENTITY_INSERT
|
||||||
end
|
end
|
||||||
|
@ -196,11 +219,24 @@ EOL
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def update(sql, name = nil)
|
def execute(sql, name = nil)
|
||||||
execute(sql, name)
|
if sql =~ /^INSERT/i
|
||||||
affected_rows(name)
|
insert(sql, name)
|
||||||
|
elsif sql =~ /^UPDATE|DELETE/i
|
||||||
|
log(sql, name, @connection) do |conn|
|
||||||
|
conn.execute(sql)
|
||||||
|
retVal = select_one("SELECT @@ROWCOUNT AS AffectedRows")["AffectedRows"]
|
||||||
|
end
|
||||||
|
else
|
||||||
|
log(sql, name, @connection) do |conn|
|
||||||
|
conn.execute(sql)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update(sql, name = nil)
|
||||||
|
execute(sql, name)
|
||||||
|
end
|
||||||
alias_method :delete, :update
|
alias_method :delete, :update
|
||||||
|
|
||||||
def begin_db_transaction
|
def begin_db_transaction
|
||||||
|
@ -236,8 +272,8 @@ EOL
|
||||||
"'#{quote_string(value)}'"
|
"'#{quote_string(value)}'"
|
||||||
end
|
end
|
||||||
when NilClass then "NULL"
|
when NilClass then "NULL"
|
||||||
when TrueClass then (column && column.type == :boolean ? "'t'" : "1")
|
when TrueClass then '1'
|
||||||
when FalseClass then (column && column.type == :boolean ? "'f'" : "0")
|
when FalseClass then '0'
|
||||||
when Float, Fixnum, Bignum then value.to_s
|
when Float, Fixnum, Bignum then value.to_s
|
||||||
when Date then "'#{value.to_s}'"
|
when Date then "'#{value.to_s}'"
|
||||||
when Time, DateTime then "'#{value.strftime("%Y-%m-%d %H:%M:%S")}'"
|
when Time, DateTime then "'#{value.strftime("%Y-%m-%d %H:%M:%S")}'"
|
||||||
|
@ -254,11 +290,12 @@ EOL
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_limit_with_offset!(sql, limit, offset)
|
def add_limit_with_offset!(sql, limit, offset)
|
||||||
raise ArgumentError, 'add_limit_with_offset! not implemented'
|
order_by = sql.include?("ORDER BY") ? get_order_by(sql.sub(/.*ORDER\sBY./, "")) : nil
|
||||||
|
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]}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_limit_without_offset!(sql, limit)
|
def add_limit_without_offset!(sql, limit)
|
||||||
raise ArgumentError, 'add_limit_without_offset! not implemented'
|
limit.nil? ? sql : sql.gsub!(/SELECT/i, "SELECT TOP #{limit}")
|
||||||
end
|
end
|
||||||
|
|
||||||
def recreate_database(name)
|
def recreate_database(name)
|
||||||
|
@ -274,36 +311,18 @@ EOL
|
||||||
execute "CREATE DATABASE #{name}"
|
execute "CREATE DATABASE #{name}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute(sql, name = nil)
|
|
||||||
if sql =~ /^INSERT/i
|
|
||||||
insert(sql, name)
|
|
||||||
else
|
|
||||||
log(sql, name, @connection) do |conn|
|
|
||||||
conn.execute(sql)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def adapter_name()
|
|
||||||
'SqlServer'
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
def select(sql, name = nil)
|
def select(sql, name = nil)
|
||||||
rows = []
|
rows = []
|
||||||
|
|
||||||
log(sql, name, @connection) do |conn|
|
log(sql, name, @connection) do |conn|
|
||||||
conn.select_all(sql) do |row|
|
conn.select_all(sql) do |row|
|
||||||
record = {}
|
record = {}
|
||||||
|
|
||||||
row.column_names.each do |col|
|
row.column_names.each do |col|
|
||||||
record[col] = row[col]
|
record[col] = row[col]
|
||||||
end
|
end
|
||||||
|
|
||||||
rows << record
|
rows << record
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
rows
|
rows
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -346,10 +365,6 @@ EOL
|
||||||
return sql =~ /[\(\.\,]\s*#{col}/
|
return sql =~ /[\(\.\,]\s*#{col}/
|
||||||
end
|
end
|
||||||
|
|
||||||
def query_contains_text_column(sql, col)
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
def get_order_by(sql)
|
def get_order_by(sql)
|
||||||
return sql, sql.gsub(/\s*DESC\s*/, "").gsub(/\s*ASC\s*/, " DESC")
|
return sql, sql.gsub(/\s*DESC\s*/, "").gsub(/\s*ASC\s*/, " DESC")
|
||||||
end
|
end
|
||||||
|
@ -358,15 +373,6 @@ EOL
|
||||||
limit = limit.gsub!(/.OFFSET./i, ",").split(',')
|
limit = limit.gsub!(/.OFFSET./i, ",").split(',')
|
||||||
return limit[0].to_i, limit[0].to_i+limit[1].to_i
|
return limit[0].to_i, limit[0].to_i+limit[1].to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
def affected_rows(name = nil)
|
|
||||||
sql = "SELECT @@ROWCOUNT AS AffectedRows"
|
|
||||||
log(sql, name, @connection) do |conn|
|
|
||||||
conn.select_all(sql) do |row|
|
|
||||||
return row[:AffectedRows].to_i
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -11,7 +11,7 @@ class TestColumnAlias < Test::Unit::TestCase
|
||||||
assert_equal(records[0].keys[0], "pk")
|
assert_equal(records[0].keys[0], "pk")
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
records = topic.connection.select_all("SELECT id AS pk FROM topics LIMIT 1")
|
records = topic.connection.select_all("SELECT id AS pk FROM topics")
|
||||||
assert_equal(records[0].keys[0], "pk")
|
assert_equal(records[0].keys[0], "pk")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,6 +4,9 @@ require 'logger'
|
||||||
|
|
||||||
ActiveRecord::Base.logger = Logger.new("debug.log")
|
ActiveRecord::Base.logger = Logger.new("debug.log")
|
||||||
|
|
||||||
|
db1 = 'activerecord_unittest'
|
||||||
|
db2 = 'activerecord_unittest2'
|
||||||
|
|
||||||
ActiveRecord::Base.establish_connection(
|
ActiveRecord::Base.establish_connection(
|
||||||
:adapter => "sqlserver",
|
:adapter => "sqlserver",
|
||||||
:host => "localhost",
|
:host => "localhost",
|
||||||
|
|
|
@ -2,10 +2,11 @@ require 'abstract_unit'
|
||||||
require 'fixtures/company'
|
require 'fixtures/company'
|
||||||
require 'fixtures/topic'
|
require 'fixtures/topic'
|
||||||
require 'fixtures/entrant'
|
require 'fixtures/entrant'
|
||||||
|
require 'fixtures/developer'
|
||||||
|
|
||||||
class FinderTest < Test::Unit::TestCase
|
class FinderTest < Test::Unit::TestCase
|
||||||
fixtures :companies, :topics, :entrants, :developers
|
fixtures :companies, :topics, :entrants, :developers
|
||||||
|
|
||||||
def test_find
|
def test_find
|
||||||
assert_equal(@topics["first"]["title"], Topic.find(1).title)
|
assert_equal(@topics["first"]["title"], Topic.find(1).title)
|
||||||
end
|
end
|
||||||
|
@ -43,10 +44,10 @@ class FinderTest < Test::Unit::TestCase
|
||||||
def test_find_all_with_prepared_limit_and_offset
|
def test_find_all_with_prepared_limit_and_offset
|
||||||
if ActiveRecord::ConnectionAdapters.const_defined? :OracleAdapter
|
if ActiveRecord::ConnectionAdapters.const_defined? :OracleAdapter
|
||||||
if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::OracleAdapter)
|
if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::OracleAdapter)
|
||||||
assert_raises(ArgumentError) { Entrant.find_all nil, "id ASC", ["? OFFSET ?", 2, 1] }
|
assert_raises(ArgumentError) { Entrant.find_all nil, "id ASC", [2, 1] }
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
entrants = Entrant.find_all nil, "id ASC", ["? OFFSET ?", 2, 1]
|
entrants = Entrant.find_all nil, "id ASC", [2, 1]
|
||||||
|
|
||||||
assert_equal(2, entrants.size)
|
assert_equal(2, entrants.size)
|
||||||
assert_equal(@entrants["second"]["name"], entrants.first.name)
|
assert_equal(@entrants["second"]["name"], entrants.first.name)
|
||||||
|
@ -256,11 +257,11 @@ class FinderTest < Test::Unit::TestCase
|
||||||
assert_equal first_five_developers, Developer.find_all(nil, 'id ASC', [5])
|
assert_equal first_five_developers, Developer.find_all(nil, 'id ASC', [5])
|
||||||
assert_equal no_developers, Developer.find_all(nil, 'id ASC', [0])
|
assert_equal no_developers, Developer.find_all(nil, 'id ASC', [0])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_find_all_with_limit_and_offset
|
def test_find_all_with_limit_and_offset
|
||||||
first_three_developers = Developer.find_all nil, 'id ASC', [3, 0]
|
first_three_developers = Developer.find_all nil, 'id ASC', [3, 0]
|
||||||
second_three_developers = Developer.find_all nil, 'id ASC', [3, 3]
|
second_three_developers = Developer.find_all nil, 'id ASC', [3, 3]
|
||||||
last_two_developers = Developer.find_all nil, 'id ASC', [3, 8]
|
last_two_developers = Developer.find_all nil, 'id ASC', [2, 8]
|
||||||
|
|
||||||
assert_equal 3, first_three_developers.length
|
assert_equal 3, first_three_developers.length
|
||||||
assert_equal 3, second_three_developers.length
|
assert_equal 3, second_three_developers.length
|
||||||
|
|
|
@ -14,5 +14,4 @@ DROP TABLE colnametests;
|
||||||
DROP TABLE mixins;
|
DROP TABLE mixins;
|
||||||
DROP TABLE people;
|
DROP TABLE people;
|
||||||
DROP TABLE binaries;
|
DROP TABLE binaries;
|
||||||
DROP TABLE computers;
|
DROP TABLE computers;
|
||||||
|
|
|
@ -1,88 +1,79 @@
|
||||||
CREATE TABLE accounts (
|
CREATE TABLE accounts (
|
||||||
id int NOT NULL IDENTITY(1, 1),
|
id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
|
||||||
firm_id int default NULL,
|
firm_id int default NULL,
|
||||||
credit_limit int default NULL,
|
credit_limit int default NULL
|
||||||
PRIMARY KEY (id)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
CREATE TABLE companies (
|
CREATE TABLE companies (
|
||||||
id int NOT NULL IDENTITY(1, 1),
|
id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
|
||||||
type varchar(50) default NULL,
|
type varchar(50) default NULL,
|
||||||
ruby_type varchar(50) default NULL,
|
ruby_type varchar(50) default NULL,
|
||||||
firm_id int default NULL,
|
firm_id int default NULL,
|
||||||
name varchar(50) default NULL,
|
name varchar(50) default NULL,
|
||||||
client_of int default NULL,
|
client_of int default NULL,
|
||||||
companies_count int default 0,
|
rating int default 1
|
||||||
rating int default 1,
|
|
||||||
PRIMARY KEY (id)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
CREATE TABLE topics (
|
CREATE TABLE topics (
|
||||||
id int NOT NULL IDENTITY(1, 1),
|
id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
|
||||||
title varchar(255) default NULL,
|
title varchar(255) default NULL,
|
||||||
author_name varchar(255) default NULL,
|
author_name varchar(255) default NULL,
|
||||||
author_email_address varchar(255) default NULL,
|
author_email_address varchar(255) default NULL,
|
||||||
written_on datetime default NULL,
|
written_on datetime default NULL,
|
||||||
|
bonus_time datetime default NULL,
|
||||||
last_read datetime default NULL,
|
last_read datetime default NULL,
|
||||||
content text,
|
content text,
|
||||||
approved tinyint default 1,
|
approved tinyint default 1,
|
||||||
replies_count int default 0,
|
replies_count int default 0,
|
||||||
parent_id int default NULL,
|
parent_id int default NULL,
|
||||||
type varchar(50) default NULL,
|
type varchar(50) default NULL
|
||||||
PRIMARY KEY (id)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
CREATE TABLE developers (
|
CREATE TABLE developers (
|
||||||
id int NOT NULL IDENTITY(1, 1),
|
id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
|
||||||
name varchar(100) default NULL,
|
name varchar(100) default NULL,
|
||||||
PRIMARY KEY (id)
|
salary int default 70000
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE projects (
|
CREATE TABLE projects (
|
||||||
id int NOT NULL IDENTITY(1, 1),
|
id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
|
||||||
name varchar(100) default NULL,
|
name varchar(100) default NULL
|
||||||
salary int default 70000,
|
|
||||||
PRIMARY KEY (id)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE developers_projects (
|
CREATE TABLE developers_projects (
|
||||||
developer_id int NOT NULL,
|
developer_id int NOT NULL,
|
||||||
project_id int NOT NULL
|
project_id int NOT NULL,
|
||||||
|
joined_on datetime default NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE customers (
|
CREATE TABLE customers (
|
||||||
id int NOT NULL IDENTITY(1, 1),
|
id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
|
||||||
name varchar(100) default NULL,
|
name varchar(100) default NULL,
|
||||||
balance int default 0,
|
balance int default 0,
|
||||||
address_street varchar(100) default NULL,
|
address_street varchar(100) default NULL,
|
||||||
address_city varchar(100) default NULL,
|
address_city varchar(100) default NULL,
|
||||||
address_country varchar(100) default NULL,
|
address_country varchar(100) default NULL,
|
||||||
gps_location varchar(100) default NULL,
|
gps_location varchar(100) default NULL
|
||||||
PRIMARY KEY (id)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE movies (
|
CREATE TABLE movies (
|
||||||
movieid int NOT NULL IDENTITY(1, 1),
|
movieid int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
|
||||||
name varchar(100) default NULL,
|
name varchar(100) default NULL
|
||||||
PRIMARY KEY (movieid)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE subscribers (
|
CREATE TABLE subscribers (
|
||||||
nick varchar(100) NOT NULL,
|
nick varchar(100) NOT NULL PRIMARY KEY,
|
||||||
name varchar(100) default NULL,
|
name varchar(100) default NULL
|
||||||
PRIMARY KEY (nick)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE booleantests (
|
CREATE TABLE booleantests (
|
||||||
id int NOT NULL IDENTITY(1, 1),
|
id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
|
||||||
value integer default NULL,
|
value bit default NULL
|
||||||
PRIMARY KEY (id)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE auto_id_tests (
|
CREATE TABLE auto_id_tests (
|
||||||
auto_id int NOT NULL IDENTITY(1, 1),
|
auto_id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
|
||||||
value int default NULL,
|
value int default NULL
|
||||||
PRIMARY KEY (auto_id)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE entrants (
|
CREATE TABLE entrants (
|
||||||
|
@ -92,22 +83,20 @@ CREATE TABLE entrants (
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE colnametests (
|
CREATE TABLE colnametests (
|
||||||
id int NOT NULL IDENTITY(1, 1),
|
id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
|
||||||
[references] int NOT NULL,
|
[references] int NOT NULL
|
||||||
PRIMARY KEY (id)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE mixins (
|
CREATE TABLE mixins (
|
||||||
id int NOT NULL IDENTITY(1, 1),
|
id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
|
||||||
parent_id int default NULL,
|
parent_id int default NULL,
|
||||||
type varchar(40) default NULL,
|
|
||||||
pos int default NULL,
|
pos int default NULL,
|
||||||
lft int default NULL,
|
|
||||||
rgt int default NULL,
|
|
||||||
root_id int default NULL,
|
|
||||||
created_at datetime default NULL,
|
created_at datetime default NULL,
|
||||||
updated_at datetime default NULL,
|
updated_at datetime default NULL,
|
||||||
PRIMARY KEY (id)
|
lft int default NULL,
|
||||||
|
rgt int default NULL,
|
||||||
|
root_id int default NULL,
|
||||||
|
type varchar(40) default NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE people (
|
CREATE TABLE people (
|
||||||
|
@ -118,14 +107,12 @@ CREATE TABLE people (
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE binaries (
|
CREATE TABLE binaries (
|
||||||
id int NOT NULL IDENTITY(1, 1),
|
id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
|
||||||
data image NULL,
|
data image NULL
|
||||||
PRIMARY KEY (id)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE computers (
|
CREATE TABLE computers (
|
||||||
id int NOT NULL IDENTITY(1, 1),
|
id int NOT NULL IDENTITY(1, 1) PRIMARY KEY,
|
||||||
developer int NOT NULL,
|
developer int NOT NULL
|
||||||
PRIMARY KEY (id)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue