module Sequel::SqlAnywhere::DatabaseMethods

Constants

DATABASE_ERROR_REGEXPS

Attributes

conversion_procs[R]
convert_smallint_to_bool[RW]

Set whether to convert smallint type to boolean for this Database instance

Public Instance Methods

database_type() click to toggle source
   # File lib/sequel/adapters/shared/sqlanywhere.rb
13 def database_type
14   :sqlanywhere
15 end
foreign_key_list(table, opts=OPTS) click to toggle source
    # File lib/sequel/adapters/shared/sqlanywhere.rb
 81 def foreign_key_list(table, opts=OPTS)
 82   m = output_identifier_meth
 83   im = input_identifier_meth
 84   fk_indexes = {}
 85   metadata_dataset.
 86    from{sys[:sysforeignkey].as(:fk)}.
 87    select{[
 88      fk[:role].as(:name),
 89      fks[:columns].as(:column_map),
 90      si[:indextype].as(:type),
 91      si[:colnames].as(:columns),
 92      fks[:primary_tname].as(:table_name)]}.
 93    join(Sequel[:sys][:sysforeignkeys].as(:fks), :role => :role).
 94    join(Sequel[:sys][:sysindexes].as(:si), {:iname => Sequel[:fk][:role]}, {:implicit_qualifier => :fk}).
 95    where{{fks[:foreign_tname]=>im.call(table)}}.
 96    each do |r|
 97     unless r[:type].downcase == 'primary key'
 98       fk_indexes[r[:name]] =
 99         {:name=>m.call(r[:name]),
100          :columns=>r[:columns].split(',').map{|v| m.call(v.split(' ').first)},
101          :table=>m.call(r[:table_name]),
102          :key=>r[:column_map].split(',').map{|v| m.call(v.split(' IS ').last)}}
103     end
104   end
105   fk_indexes.values
106 end
freeze() click to toggle source
Calls superclass method
   # File lib/sequel/adapters/shared/sqlanywhere.rb
17 def freeze
18   @conversion_procs.freeze
19   super
20 end
indexes(table, opts = OPTS) click to toggle source
   # File lib/sequel/adapters/shared/sqlanywhere.rb
58 def indexes(table, opts = OPTS)
59   m = output_identifier_meth
60   im = input_identifier_meth
61   table = table.value if table.is_a?(Sequel::SQL::Identifier)
62   indexes = {}
63   metadata_dataset.
64    from(Sequel[:dbo][:sysobjects].as(:z)).
65    select{[
66      z[:name].as(:table_name),
67      i[:name].as(:index_name),
68      si[:indextype].as(:type),
69      si[:colnames].as(:columns)]}.
70    join(Sequel[:dbo][:sysindexes].as(:i), :id=>:id).
71    join(Sequel[:sys][:sysindexes].as(:si), :iname=> :name).
72    where{{z[:type] => 'U', :table_name=>im.call(table)}}.
73    each do |r|
74     indexes[m.call(r[:index_name])] =
75       {:unique=>(r[:type].downcase=='unique'),
76        :columns=>r[:columns].split(',').map{|v| m.call(v.split(' ').first)}} unless r[:type].downcase == 'primary key'
77   end
78   indexes
79 end
schema_column_type(db_type) click to toggle source

Convert smallint type to boolean if convert_smallint_to_bool is true

Calls superclass method
   # File lib/sequel/adapters/shared/sqlanywhere.rb
27 def schema_column_type(db_type)
28   if convert_smallint_to_bool && db_type =~ /smallint/i
29     :boolean
30   else
31     super
32   end
33 end
schema_parse_table(table, opts) click to toggle source
   # File lib/sequel/adapters/shared/sqlanywhere.rb
35 def schema_parse_table(table, opts)
36   m = output_identifier_meth(opts[:dataset])
37   im = input_identifier_meth(opts[:dataset])
38   metadata_dataset.
39    from{sa_describe_query("select * from #{im.call(table)}").as(:a)}.
40    join(Sequel[:syscolumn].as(:b), :table_id=>:base_table_id, :column_id=>:base_column_id).
41    order{a[:column_number]}.
42    map do |row|
43     auto_increment = row.delete(:is_autoincrement)
44     row[:auto_increment] = auto_increment == 1 || auto_increment == true
45     row[:primary_key] = row.delete(:pkey) == 'Y'
46     row[:allow_null] = row[:nulls_allowed].is_a?(Integer) ? row.delete(:nulls_allowed) == 1 : row.delete(:nulls_allowed)
47     row[:db_type] = row.delete(:domain_name)
48     row[:type] = if row[:db_type] =~ /numeric/i and (row[:scale].is_a?(Integer) ? row[:scale] == 0 : !row[:scale])
49       :integer
50     else
51       schema_column_type(row[:db_type])
52     end
53     row[:max_length] = row[:width] if row[:type] == :string
54     [m.call(row.delete(:name)), row]
55   end
56 end
tables(opts=OPTS) click to toggle source
    # File lib/sequel/adapters/shared/sqlanywhere.rb
108 def tables(opts=OPTS)
109   tables_and_views('U', opts)
110 end
to_application_timestamp_sa(v) click to toggle source
   # File lib/sequel/adapters/shared/sqlanywhere.rb
22 def to_application_timestamp_sa(v)
23   to_application_timestamp(v.to_s) if v
24 end
views(opts=OPTS) click to toggle source
    # File lib/sequel/adapters/shared/sqlanywhere.rb
112 def views(opts=OPTS)
113   tables_and_views('V', opts)
114 end

Private Instance Methods

alter_table_sql(table, op) click to toggle source
Calls superclass method
    # File lib/sequel/adapters/shared/sqlanywhere.rb
172 def alter_table_sql(table, op)
173   case op[:op]
174   when :add_column
175     "ALTER TABLE #{quote_schema_table(table)} ADD #{column_definition_sql(op)}"
176   when :drop_column
177     "ALTER TABLE #{quote_schema_table(table)} DROP #{column_definition_sql(op)}"
178   when :drop_constraint
179     case op[:type]
180     when :primary_key
181       "ALTER TABLE #{quote_schema_table(table)} DROP PRIMARY KEY"
182     when :foreign_key
183       if op[:name] || op[:columns]
184         name = op[:name] || foreign_key_name(table, op[:columns])
185         if name
186           "ALTER TABLE #{quote_schema_table(table)} DROP FOREIGN KEY #{quote_identifier(name)}"
187         end
188       end
189     else
190       super
191     end
192   when :rename_column
193     "ALTER TABLE #{quote_schema_table(table)} RENAME #{quote_identifier(op[:name])} TO #{quote_identifier(op[:new_name].to_s)}"
194   when :set_column_type
195     "ALTER TABLE #{quote_schema_table(table)} ALTER #{quote_identifier(op[:name])} #{type_literal(op)}"
196   when :set_column_null
197     "ALTER TABLE #{quote_schema_table(table)} ALTER #{quote_identifier(op[:name])} #{'NOT ' unless op[:null]}NULL"
198   when :set_column_default
199     "ALTER TABLE #{quote_schema_table(table)} ALTER #{quote_identifier(op[:name])} DEFAULT #{literal(op[:default])}"
200   else
201     super(table, op)
202   end
203 end
auto_increment_sql() click to toggle source

Sybase uses the IDENTITY column for autoincrementing columns.

    # File lib/sequel/adapters/shared/sqlanywhere.rb
131 def auto_increment_sql
132   'IDENTITY'
133 end
begin_transaction_sql() click to toggle source
    # File lib/sequel/adapters/shared/sqlanywhere.rb
144 def begin_transaction_sql
145   "BEGIN TRANSACTION"
146 end
can_add_primary_key_constraint_on_nullable_columns?() click to toggle source

Sybase does not allow adding primary key constraints to NULLable columns.

    # File lib/sequel/adapters/shared/sqlanywhere.rb
136 def can_add_primary_key_constraint_on_nullable_columns?
137   false
138 end
commit_transaction_sql() click to toggle source
    # File lib/sequel/adapters/shared/sqlanywhere.rb
152 def commit_transaction_sql
153   "COMMIT TRANSACTION"
154 end
create_table_as(name, ds, options) click to toggle source

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
209 def create_table_as(name, ds, options)
210   raise(Error, "must provide dataset instance as value of create_table :as option on SqlAnywhere") unless ds.is_a?(Sequel::Dataset)
211   run(ds.into(name).sql)
212 end
database_error_regexps() click to toggle source
    # File lib/sequel/adapters/shared/sqlanywhere.rb
126 def database_error_regexps
127   DATABASE_ERROR_REGEXPS
128 end
rename_table_sql(name, new_name) click to toggle source

Use SP_RENAME to rename the table

    # File lib/sequel/adapters/shared/sqlanywhere.rb
215 def rename_table_sql(name, new_name)
216   "ALTER TABLE #{quote_schema_table(name)} RENAME #{quote_schema_table(new_name)}"
217 end
rollback_transaction_sql() click to toggle source
    # File lib/sequel/adapters/shared/sqlanywhere.rb
148 def rollback_transaction_sql
149   "IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION"
150 end
tables_and_views(type, opts=OPTS) click to toggle source
    # File lib/sequel/adapters/shared/sqlanywhere.rb
219 def tables_and_views(type, opts=OPTS)
220   m = output_identifier_meth
221   metadata_dataset.
222     from{sysobjects.as(:a)}.
223     where{{a[:type]=>type}}.
224     select_map{a[:name]}.
225     map{|n| m.call(n)}
226 end
temporary_table_sql() click to toggle source
    # File lib/sequel/adapters/shared/sqlanywhere.rb
140 def temporary_table_sql
141   "GLOBAL TEMPORARY "
142 end
type_literal_generic_datetime(column) click to toggle source

Sybase has both datetime and timestamp classes, most people are going to want datetime

    # File lib/sequel/adapters/shared/sqlanywhere.rb
158 def type_literal_generic_datetime(column)
159   :datetime
160 end
type_literal_generic_file(column) click to toggle source

SQLAnywhere uses image type for blobs

    # File lib/sequel/adapters/shared/sqlanywhere.rb
168 def type_literal_generic_file(column)
169   :image
170 end
type_literal_generic_trueclass(column) click to toggle source

Sybase doesn't have a true boolean class, so it uses integer

    # File lib/sequel/adapters/shared/sqlanywhere.rb
163 def type_literal_generic_trueclass(column)
164   :smallint
165 end
view_with_check_option_support() click to toggle source

SQLAnywhere supports views with check option, but not local.

    # File lib/sequel/adapters/shared/sqlanywhere.rb
229 def view_with_check_option_support
230   true
231 end