Class: Spider::Model::Storage::Db::MSSQL

Inherits:
DbStorage show all
Includes:
Dialects::NoTotalRows
Defined in:
lib/spiderfw/model/storage/db/adapters/mssql.rb

Instance Attribute Summary

Attributes inherited from BaseStorage

#instance_name, #url

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dialects::NoTotalRows

#total_rows

Methods inherited from DbStorage

#alter_table, #change_field, #collect_having_fields, #column_attributes, #column_name, #create_index, #create_table, #describe_table, #drop_field, #drop_index, #drop_table, #dump, #foreign_key_name, #function, #get_mapper, inherited, #initialize, #list_tables, #lock, #prepare_value, #query_finished, #query_start, #safe_schema_conversion?, #schema_field_equal?, #sequence_name, #shorten_identifier, #sql_add_field, #sql_add_index, #sql_alter_field, #sql_alter_table, #sql_change_field, #sql_condition, #sql_condition_value, #sql_create_primary_key, #sql_create_table, #sql_delete, #sql_drop_field, #sql_drop_foreign_key, #sql_drop_index, #sql_drop_primary_key, #sql_drop_table, #sql_insert, #sql_joins, #sql_keys, #sql_limit, #sql_max, #sql_order, #sql_table_field, #sql_tables, #sql_tables_join, #sql_truncate, #sql_update, #sql_update_values, storage_type, #table_name, #total_rows, #value_to_mapper

Methods inherited from BaseStorage

#==, base_types, #commit, #commit!, #commit_or_continue, #configure, #connect, #connected?, #connection, connection_alive?, connection_attributes, #connection_attributes, #connection_pool, connection_pools, #create_sequence, #curr, disconnect, #do_commit, #do_rollback, #do_start_transaction, #generate_uuid, get_connection, #get_mapper, #in_transaction, #in_transaction?, inherited, #initialize, max_connections, new_connection, #parse_url, #prepare_value, #release, release_connection, remove_connection, #rollback, #rollback!, #rollback_or_continue, #rollback_savepoint, #savepoint, #sequence_exists?, #sequence_file_path, #sequence_next, sequence_sync, #start_transaction, storage_type, #supports?, supports?, #supports_transactions?, #transactions_enabled?, #update_sequence, #value_for_condition, #value_for_save, #value_to_mapper

Methods included from Logger

add, check_request_level, close, close_all, datetime_format, datetime_format=, #debug, debug, debug?, #debug?, enquire_loggers, #error, error, #error?, error?, fatal, #fatal, fatal?, #fatal?, info, #info, info?, #info?, #log, log, method_missing, open, reopen, request_level, send_to_loggers, set_request_level, unknown, #unknown, warn, #warn, warn?, #warn?

Constructor Details

This class inherits a constructor from Spider::Model::Storage::Db::DbStorage

Class Method Details

.capabilitiesObject



10
11
12
13
14
15
16
17
# File 'lib/spiderfw/model/storage/db/adapters/mssql.rb', line 10

def self.capabilities
    {
        :autoincrement => true,
        :sequences => false,
        :transactions => true,
        :foreign_keys => false # not implemented
    }
end

Instance Method Details

#column_type(type, attributes) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/spiderfw/model/storage/db/adapters/mssql.rb', line 19

def column_type(type, attributes)
    case type.name
    when 'String'
        'nvarchar'
    when 'Text'
        'ntext'
    when 'Fixnum'
        'int'
    when 'Float'
        'real'
    when 'BigDecimal', 'Spider::DataTypes::Decimal'
        'decimal'
    when 'Date', 'DateTime'
        'datetime'
    when 'Spider::DataTypes::Binary'
        'varbinary'
    when 'Spider::DataTypes::Bool'
        'bit'
    end
end

#parse_db_column(col) ⇒ Object



94
95
96
97
98
99
# File 'lib/spiderfw/model/storage/db/adapters/mssql.rb', line 94

def parse_db_column(col)
    type, attributes = col[:type].split(' ', 2)
    attributes = attributes.split(' ') if attributes
    col[:type] = type
    return col
end

#query(query) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/spiderfw/model/storage/db/adapters/mssql.rb', line 40

def query(query)
    return super unless query[:query_type] == :count
    @last_query = query
    query[:keys] = ['COUNT(*) AS N']
    query[:order] = []
    sql, bind_vars = sql_select(query)
    return execute("#{sql} AS CountResult", *bind_vars)[0]['N'].to_i
end

#reflect_column(table, column_name, column_attributes) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/spiderfw/model/storage/db/adapters/mssql.rb', line 101

def reflect_column(table, column_name, column_attributes)
    column_type = column_attributes[:type]
    el_type = nil
    el_attributes = {}
    case column_type
    when 'varchar', 'nvarchar', 'char', 'nchar'
        el_type = String
    when 'text', 'ntext'
        el_type = Spider::DataTypes::Text
    when 'int', 'smallint'
        el_type = Fixnum
    when 'bit'
        el_type = Spider::DataTypes::Bool
    when 'real'
        el_type = Float
    when 'decimal'
        el_type = BigDecimal
    when 'datetime'
        el_type = DateTime
    when 'varbinary'
        el_type = Spider::DataTypes::Binary
    end
    return el_type, el_attributes

end

#sql_select(query) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/spiderfw/model/storage/db/adapters/mssql.rb', line 49

def sql_select(query)
    bind_vars = query[:bind_vars] || []
    if query[:limit] # Oracle is so braindead
        replaced_fields = {}
        replace_cnt = 0
        # add first field to order if none is found; order is needed for limit
        query[:order] << [query[:keys][0], 'desc'] if query[:order].length < 1
        query[:order].each do |o|
            field, direction = o
            transformed = "O#{replace_cnt += 1}"
            replaced_fields[field] = transformed
            query[:keys] << "#{field} AS #{transformed}"
        end
    end
    keys = sql_keys(query)
    order = sql_order(query)
    if (query[:limit])
        keys += ", row_number() over (order by #{order}) mssql_row_num"
    end
    tables_sql, tables_values = sql_tables(query)
    sql = "SELECT #{keys} FROM #{tables_sql} "
    bind_vars += tables_values
    where, vals = sql_condition(query)
    bind_vars += vals
    sql += "WHERE #{where} " if where && !where.empty?
    order = sql_order(query)
    if (query[:limit])
        if (query[:offset])
            limit = "mssql_row_num between ? and ?"
            bind_vars << query[:offset] + 1
            bind_vars << query[:offset] + query[:limit]
        else
            limit = "mssql_row_num < ?"
            bind_vars << query[:limit] + 1
        end
        replaced_fields.each do |f, repl|
            order = order.gsub(f.to_s, repl)
        end
        sql = "SELECT * FROM (#{sql}) as RowConstrainedResult WHERE #{limit} order by #{order}"
    else
        sql += "ORDER BY #{order} " if order && !order.empty?
    end
    return sql, bind_vars
end