module Sequel::SqlAnywhere::DatabaseMethods
Constants
- AUTO_INCREMENT
- DATABASE_ERROR_REGEXPS
- DECIMAL_TYPE_RE
- SMALLINT_RE
- SQL_BEGIN
- SQL_COMMIT
- SQL_ROLLBACK
- TEMPORARY
Attributes
Override the default Sequel::SqlAnywhere.convert_smallint_to_bool setting for this database.
Public Instance Methods
Whether to convert smallint to boolean arguments for this dataset. Defaults to the SqlAnywhere module setting.
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 31 def convert_smallint_to_bool defined?(@convert_smallint_to_bool) ? @convert_smallint_to_bool : (@convert_smallint_to_bool = ::Sequel::SqlAnywhere.convert_smallint_to_bool) end
Sysbase Server uses the :sqlanywhere type.
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 36 def database_type :sqlanywhere end
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 104 def foreign_key_list(table, opts=OPTS) m = output_identifier_meth im = input_identifier_meth fk_indexes = {} metadata_dataset. from{sys[:sysforeignkey].as(:fk)}. select{[ fk[:role].as(:name), fks[:columns].as(:column_map), si[:indextype].as(:type), si[:colnames].as(:columns), fks[:primary_tname].as(:table_name)]}. join(Sequel[:sys][:sysforeignkeys].as(:fks), :role => :role). join(Sequel[:sys][:sysindexes].as(:si), [:iname=> Sequel[:fk][:role]], {:implicit_qualifier => :fk}). where{{fks[:foreign_tname]=>im.call(table)}}. each do |r| unless r[:type].downcase == 'primary key' fk_indexes[r[:name]] = {:name=>m.call(r[:name]), :columns=>r[:columns].split(',').map{|v| m.call(v.split(' ').first)}, :table=>m.call(r[:table_name]), :key=>r[:column_map].split(',').map{|v| m.call(v.split(' IS ').last)}} end end fk_indexes.values end
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 40 def freeze convert_smallint_to_bool @conversion_procs.freeze super end
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 82 def indexes(table, opts = OPTS) m = output_identifier_meth im = input_identifier_meth indexes = {} metadata_dataset. from(Sequel[:dbo][:sysobjects].as(:z)). select{[ z[:name].as(:table_name), i[:name].as(:index_name), si[:indextype].as(:type), si[:colnames].as(:columns)]}. join(Sequel[:dbo][:sysindexes].as(:i), :id=>:id). join(Sequel[:sys][:sysindexes].as(:si), :iname=> :name). where{{z[:type] => 'U', :table_name=>im.call(table)}}. each do |r| indexes[m.call(r[:index_name])] = {:unique=>(r[:type].downcase=='unique'), :columns=>r[:columns].split(',').map{|v| m.call(v.split(' ').first)}} unless r[:type].downcase == 'primary key' end indexes end
Convert smallint type to boolean if #convert_smallint_to_bool is true
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 51 def schema_column_type(db_type) if convert_smallint_to_bool && db_type =~ SMALLINT_RE :boolean else super end end
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 59 def schema_parse_table(table, opts) m = output_identifier_meth(opts[:dataset]) im = input_identifier_meth(opts[:dataset]) metadata_dataset. from{sa_describe_query("select * from #{im.call(table)}").as(:a)}. join(Sequel[:syscolumn].as(:b), :table_id=>:base_table_id, :column_id=>:base_column_id). order{a[:column_number]}. map do |row| auto_increment = row.delete(:is_autoincrement) row[:auto_increment] = auto_increment == 1 || auto_increment == true row[:primary_key] = row.delete(:pkey) == 'Y' row[:allow_null] = row[:nulls_allowed].is_a?(Fixnum) ? row.delete(:nulls_allowed) == 1 : row.delete(:nulls_allowed) row[:db_type] = row.delete(:domain_name) row[:type] = if row[:db_type] =~ DECIMAL_TYPE_RE and (row[:scale].is_a?(Fixnum) ? row[:scale] == 0 : !row[:scale]) :integer else schema_column_type(row[:db_type]) end row[:max_length] = row[:width] if row[:type] == :string [m.call(row.delete(:name)), row] end end
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 131 def tables(opts=OPTS) tables_and_views('U', opts) end
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 46 def to_application_timestamp_sa(v) to_application_timestamp(v.to_s) if v end
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 135 def views(opts=OPTS) tables_and_views('V', opts) end
Private Instance Methods
Sybase specific syntax for altering tables.
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 206 def alter_table_sql(table, op) case op[:op] when :add_column "ALTER TABLE #{quote_schema_table(table)} ADD #{column_definition_sql(op)}" when :drop_column "ALTER TABLE #{quote_schema_table(table)} DROP #{column_definition_sql(op)}" when :drop_constraint case op[:type] when :primary_key "ALTER TABLE #{quote_schema_table(table)} DROP PRIMARY KEY" when :foreign_key if op[:name] || op[:columns] name = op[:name] || foreign_key_name(table, op[:columns]) if name "ALTER TABLE #{quote_schema_table(table)} DROP FOREIGN KEY #{quote_identifier(name)}" end end else super end when :rename_column "ALTER TABLE #{quote_schema_table(table)} RENAME #{quote_identifier(op[:name])} TO #{quote_identifier(op[:new_name].to_s)}" when :set_column_type "ALTER TABLE #{quote_schema_table(table)} ALTER #{quote_identifier(op[:name])} #{type_literal(op)}" when :set_column_null "ALTER TABLE #{quote_schema_table(table)} ALTER #{quote_identifier(op[:name])} #{'NOT ' unless op[:null]}NULL" when :set_column_default "ALTER TABLE #{quote_schema_table(table)} ALTER #{quote_identifier(op[:name])} DEFAULT #{literal(op[:default])}" else super(table, op) end end
Sybase uses the IDENTITY column for autoincrementing columns.
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 154 def auto_increment_sql AUTO_INCREMENT end
SQL to BEGIN a transaction.
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 169 def begin_transaction_sql SQL_BEGIN end
Sybase does not allow adding primary key constraints to NULLable columns.
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 159 def can_add_primary_key_constraint_on_nullable_columns? false end
SQL to COMMIT a transaction.
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 179 def commit_transaction_sql SQL_COMMIT end
SqlAnywhere 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/sqlanywhere.rb, line 243 def create_table_as(name, ds, options) raise(Error, "must provide dataset instance as value of create_table :as option on SqlAnywhere") unless ds.is_a?(Sequel::Dataset) run(ds.into(name).sql) end
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 149 def database_error_regexps DATABASE_ERROR_REGEXPS end
Use SP_RENAME to rename the table
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 249 def rename_table_sql(name, new_name) "ALTER TABLE #{quote_schema_table(name)} RENAME #{quote_schema_table(new_name)}" end
SQL to ROLLBACK a transaction.
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 174 def rollback_transaction_sql SQL_ROLLBACK end
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 253 def tables_and_views(type, opts=OPTS) m = output_identifier_meth metadata_dataset. from{sysobjects.as(:a)}. where{{a[:type]=>type}}. select_map{a[:name]}. map{|n| m.call(n)} end
SQL fragment for marking a table as temporary
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 164 def temporary_table_sql TEMPORARY end
Sybase has both datetime and timestamp classes, most people are going to want datetime
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 185 def type_literal_generic_datetime(column) :datetime end
SQLAnywhere uses image type for blobs
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 201 def type_literal_generic_file(column) :image end
Sybase has both datetime and timestamp classes, most people are going to want datetime
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 191 def type_literal_generic_time(column) column[:only_time] ? :time : :datetime end
Sybase doesn't have a true boolean class, so it uses integer
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 196 def type_literal_generic_trueclass(column) :smallint end
SQLAnywhere supports views with check option, but not local.
# File lib/sequel/adapters/shared/sqlanywhere.rb, line 263 def view_with_check_option_support true end