The types to check for 0 scale to transform :decimal types to :integer.
Whether to use N’’ to quote strings, which allows unicode characters inside the strings. True by default for compatibility, can be set to false for a possible performance increase. This sets the default for all datasets created from this Database object.
Microsoft SQL Server uses the :mssql type.
# File lib/sequel/adapters/shared/mssql.rb, line 27 27: def database_type 28: :mssql 29: end
Microsoft SQL Server namespaces indexes per table.
# File lib/sequel/adapters/shared/mssql.rb, line 32 32: def global_index_namespace? 33: false 34: end
Use the system tables to get index information
# File lib/sequel/adapters/shared/mssql.rb, line 37 37: def indexes(table, opts={}) 38: m = output_identifier_meth 39: im = input_identifier_meth 40: indexes = {} 41: metadata_dataset.from(:sys__tables___t). 42: join(:sys__indexes___i, :object_id=>:object_id). 43: join(:sys__index_columns___ic, :object_id=>:object_id, :index_id=>:index_id). 44: join(:sys__columns___c, :object_id=>:object_id, :column_id=>:column_id). 45: select(:i__name, :i__is_unique, :c__name___column). 46: where{{t__name=>im.call(table)}}. 47: where(:i__is_primary_key=>0, :i__is_disabled=>0). 48: order(:i__name, :ic__index_column_id). 49: each do |r| 50: index = indexes[m.call(r[:name])] ||= {:columns=>[], :unique=>(r[:is_unique] && r[:is_unique]!=0)} 51: index[:columns] << m.call(r[:column]) 52: end 53: indexes 54: end
The version of the MSSQL server, as an integer (e.g. 10001600 for SQL Server 2008 Express).
# File lib/sequel/adapters/shared/mssql.rb, line 58 58: def server_version(server=nil) 59: return @server_version if @server_version 60: @server_version = synchronize(server) do |conn| 61: (conn.server_version rescue nil) if conn.respond_to?(:server_version) 62: end 63: unless @server_version 64: m = SERVER_VERSION_RE.match(fetch(SERVER_VERSION_SQL).single_value.to_s) 65: @server_version = (m[1].to_i * 1000000) + (m[2].to_i * 10000) + m[3].to_i 66: end 67: @server_version 68: end
MSSQL supports savepoints, though it doesn’t support committing/releasing them savepoint
# File lib/sequel/adapters/shared/mssql.rb, line 71 71: def supports_savepoints? 72: true 73: end
MSSQL supports transaction isolation levels
# File lib/sequel/adapters/shared/mssql.rb, line 76 76: def supports_transaction_isolation_levels? 77: true 78: end
MSSQL supports transaction DDL statements.
# File lib/sequel/adapters/shared/mssql.rb, line 81 81: def supports_transactional_ddl? 82: true 83: end
Microsoft SQL Server supports using the INFORMATION_SCHEMA to get information on tables.
# File lib/sequel/adapters/shared/mssql.rb, line 87 87: def tables(opts={}) 88: information_schema_tables('BASE TABLE', opts) 89: end
Microsoft SQL Server supports using the INFORMATION_SCHEMA to get information on views.
# File lib/sequel/adapters/shared/mssql.rb, line 93 93: def views(opts={}) 94: information_schema_tables('VIEW', opts) 95: end
MSSQL specific syntax for altering tables.
# File lib/sequel/adapters/shared/mssql.rb, line 105 105: def alter_table_sql(table, op) 106: case op[:op] 107: when :add_column 108: "ALTER TABLE #{quote_schema_table(table)} ADD #{column_definition_sql(op)}" 109: when :rename_column 110: "sp_rename #{literal("#{quote_schema_table(table)}.#{quote_identifier(op[:name])}")}, #{literal(op[:new_name].to_s)}, 'COLUMN'" 111: when :set_column_type 112: sqls = [] 113: if sch = schema(table) 114: if cs = sch.each{|k, v| break v if k == op[:name]; nil} 115: cs = cs.dup 116: if constraint = default_constraint_name(table, op[:name]) 117: sqls << "ALTER TABLE #{quote_schema_table(table)} DROP CONSTRAINT #{constraint}" 118: end 119: cs[:default] = cs[:ruby_default] 120: op = cs.merge!(op) 121: default = op.delete(:default) 122: end 123: end 124: sqls << "ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{column_definition_sql(op)}" 125: sqls << alter_table_sql(table, op.merge(:op=>:set_column_default, :default=>default)) if default 126: sqls 127: when :set_column_null 128: sch = schema(table).find{|k,v| k.to_s == op[:name].to_s}.last 129: type = sch[:db_type] 130: if [:string, :decimal].include?(sch[:type]) and size = (sch[:max_chars] || sch[:column_size]) 131: type += "(#{size}#{", #{sch[:scale]}" if sch[:scale] && sch[:scale].to_i > 0})" 132: end 133: "ALTER TABLE #{quote_schema_table(table)} ALTER COLUMN #{quote_identifier(op[:name])} #{type_literal(:type=>type)} #{'NOT ' unless op[:null]}NULL" 134: when :set_column_default 135: "ALTER TABLE #{quote_schema_table(table)} ADD CONSTRAINT #{quote_identifier("sequel_#{table}_#{op[:name]}_def")} DEFAULT #{literal(op[:default])} FOR #{quote_identifier(op[:name])}" 136: else 137: super(table, op) 138: end 139: end
MSSQL uses the IDENTITY(1,1) column for autoincrementing columns.
# File lib/sequel/adapters/shared/mssql.rb, line 100 100: def auto_increment_sql 101: AUTO_INCREMENT 102: end
SQL to start a new savepoint
# File lib/sequel/adapters/shared/mssql.rb, line 142 142: def begin_savepoint_sql(depth) 143: SQL_SAVEPOINT % depth 144: end
SQL to BEGIN a transaction.
# File lib/sequel/adapters/shared/mssql.rb, line 147 147: def begin_transaction_sql 148: SQL_BEGIN 149: end
Commit the active transaction on the connection, does not commit/release savepoints.
# File lib/sequel/adapters/shared/mssql.rb, line 153 153: def commit_transaction(conn, opts={}) 154: log_connection_execute(conn, commit_transaction_sql) unless _trans(conn)[:savepoint_level] > 1 155: end
SQL to COMMIT a transaction.
# File lib/sequel/adapters/shared/mssql.rb, line 158 158: def commit_transaction_sql 159: SQL_COMMIT 160: end
MSSQL doesn’t support CREATE TABLE AS, it only supports SELECT INTO. Emulating CREATE TABLE AS using SELECT INTO is only possible if a dataset is given as the argument, it can’t work with a string, so raise an Error if a string is given.
# File lib/sequel/adapters/shared/mssql.rb, line 173 173: def create_table_as(name, ds, options) 174: raise(Error, "must provide dataset instance as value of create_table :as option on MSSQL") unless ds.is_a?(Sequel::Dataset) 175: run(ds.into(name).sql) 176: end
MSSQL uses the name of the table to decide the difference between a regular and temporary table, with temporary table names starting with a #.
# File lib/sequel/adapters/shared/mssql.rb, line 165 165: def create_table_prefix_sql(name, options) 166: "CREATE TABLE #{quote_schema_table(options[:temp] ? "##{name}" : name)}" 167: end
The name of the constraint for setting the default value on the table and column.
# File lib/sequel/adapters/shared/mssql.rb, line 179 179: def default_constraint_name(table, column) 180: from(:sysobjects___c_obj). 181: join(:syscomments___com, :id=>:id). 182: join(:sysobjects___t_obj, :id=>:c_obj__parent_obj). 183: join(:sysconstraints___con, :constid=>:c_obj__id). 184: join(:syscolumns___col, :id=>:t_obj__id, :colid=>:colid). 185: where{{c_obj__uid=>user_id{}}}. 186: where(:c_obj__xtype=>'D', :t_obj__name=>table.to_s, :col__name=>column.to_s). 187: get(:c_obj__name) 188: end
The SQL to drop an index for the table.
# File lib/sequel/adapters/shared/mssql.rb, line 191 191: def drop_index_sql(table, op) 192: "DROP INDEX #{quote_identifier(op[:name] || default_index_name(table, op[:columns]))} ON #{quote_schema_table(table)}" 193: end
support for clustered index type
# File lib/sequel/adapters/shared/mssql.rb, line 196 196: def index_definition_sql(table_name, index) 197: index_name = index[:name] || default_index_name(table_name, index[:columns]) 198: if index[:type] == :full_text 199: "CREATE FULLTEXT INDEX ON #{quote_schema_table(table_name)} #{literal(index[:columns])} KEY INDEX #{literal(index[:key_index])}" 200: else 201: "CREATE #{'UNIQUE ' if index[:unique]}#{'CLUSTERED ' if index[:type] == :clustered}INDEX #{quote_identifier(index_name)} ON #{quote_schema_table(table_name)} #{literal(index[:columns])}#{" INCLUDE #{literal(index[:include])}" if index[:include]}#{" WHERE #{filter_expr(index[:where])}" if index[:where]}" 202: end 203: end
Backbone of the tables and views support.
# File lib/sequel/adapters/shared/mssql.rb, line 206 206: def information_schema_tables(type, opts) 207: m = output_identifier_meth 208: metadata_dataset.from(:information_schema__tables___t). 209: select(:table_name). 210: filter(:table_type=>type, :table_schema=>(opts[:schema]||default_schema||'dbo').to_s). 211: map{|x| m.call(x[:table_name])} 212: end
Always quote identifiers in the metadata_dataset, so schema parsing works.
# File lib/sequel/adapters/shared/mssql.rb, line 215 215: def metadata_dataset 216: ds = super 217: ds.quote_identifiers = true 218: ds 219: end
Use sp_rename to rename the table
# File lib/sequel/adapters/shared/mssql.rb, line 222 222: def rename_table_sql(name, new_name) 223: "sp_rename #{literal(quote_schema_table(name))}, #{quote_identifier(schema_and_table(new_name).pop)}" 224: end
SQL to rollback to a savepoint
# File lib/sequel/adapters/shared/mssql.rb, line 227 227: def rollback_savepoint_sql(depth) 228: SQL_ROLLBACK_TO_SAVEPOINT % depth 229: end
SQL to ROLLBACK a transaction.
# File lib/sequel/adapters/shared/mssql.rb, line 232 232: def rollback_transaction_sql 233: SQL_ROLLBACK 234: end
The closest MSSQL equivalent of a boolean datatype is the bit type.
# File lib/sequel/adapters/shared/mssql.rb, line 237 237: def schema_column_type(db_type) 238: case db_type 239: when /\A(bit)\z/o 240: :boolean 241: else 242: super 243: end 244: end
MSSQL uses the INFORMATION_SCHEMA to hold column information, and parses primary key information from the sysindexes, sysindexkeys, and syscolumns system tables.
# File lib/sequel/adapters/shared/mssql.rb, line 249 249: def schema_parse_table(table_name, opts) 250: m = output_identifier_meth(opts[:dataset]) 251: m2 = input_identifier_meth(opts[:dataset]) 252: tn = m2.call(table_name.to_s) 253: table_id = get{object_id(tn)} 254: pk_index_id = metadata_dataset.from(:sysindexes). 255: where(:id=>table_id, :indid=>1..254){{(status & 2048)=>2048}}. 256: get(:indid) 257: pk_cols = metadata_dataset.from(:sysindexkeys___sik). 258: join(:syscolumns___sc, :id=>:id, :colid=>:colid). 259: where(:sik__id=>table_id, :sik__indid=>pk_index_id). 260: select_order_map(:sc__name) 261: ds = metadata_dataset.from(:information_schema__tables___t). 262: join(:information_schema__columns___c, :table_catalog=>:table_catalog, 263: :table_schema => :table_schema, :table_name => :table_name). 264: select(:column_name___column, :data_type___db_type, :character_maximum_length___max_chars, :column_default___default, :is_nullable___allow_null, :numeric_precision___column_size, :numeric_scale___scale). 265: filter(:c__table_name=>tn) 266: if schema = opts[:schema] || default_schema 267: ds.filter!(:c__table_schema=>schema) 268: end 269: ds.map do |row| 270: row[:primary_key] = pk_cols.include?(row[:column]) 271: row[:allow_null] = row[:allow_null] == 'YES' ? true : false 272: row[:default] = nil if blank_object?(row[:default]) 273: row[:type] = if row[:db_type] =~ DECIMAL_TYPE_RE && row[:scale] == 0 274: :integer 275: else 276: schema_column_type(row[:db_type]) 277: end 278: [m.call(row.delete(:column)), row] 279: end 280: end
Set the mssql_unicode_strings settings from the given options.
# File lib/sequel/adapters/shared/mssql.rb, line 283 283: def set_mssql_unicode_strings 284: @mssql_unicode_strings = typecast_value_boolean(@opts.fetch(:mssql_unicode_strings, true)) 285: end
MSSQL has both datetime and timestamp classes, most people are going to want datetime
# File lib/sequel/adapters/shared/mssql.rb, line 289 289: def type_literal_generic_datetime(column) 290: :datetime 291: end
MSSQL uses varbinary(max) type for blobs
# File lib/sequel/adapters/shared/mssql.rb, line 305 305: def type_literal_generic_file(column) 306: :'varbinary(max)' 307: end
MSSQL has both datetime and timestamp classes, most people are going to want datetime
# File lib/sequel/adapters/shared/mssql.rb, line 295 295: def type_literal_generic_time(column) 296: column[:only_time] ? :time : :datetime 297: end
MSSQL doesn’t have a true boolean class, so it uses bit
# File lib/sequel/adapters/shared/mssql.rb, line 300 300: def type_literal_generic_trueclass(column) 301: :bit 302: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.