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
72 def foreign_key_list(table, opts=OPTS)
73   m = output_identifier_meth
74   im = input_identifier_meth
75   fk_indexes = {}
76   metadata_dataset.
77    from{sys[:sysforeignkey].as(:fk)}.
78    select{[
79      fk[:role].as(:name),
80      fks[:columns].as(:column_map),
81      si[:indextype].as(:type),
82      si[:colnames].as(:columns),
83      fks[:primary_tname].as(:table_name)]}.
84    join(Sequel[:sys][:sysforeignkeys].as(:fks), :role => :role).
85    join(Sequel[:sys][:sysindexes].as(:si), {:iname => Sequel[:fk][:role]}, {:implicit_qualifier => :fk}).
86    where{{fks[:foreign_tname]=>im.call(table)}}.
87    each do |r|
88     unless r[:type].downcase == 'primary key'
89       fk_indexes[r[:name]] =
90         {:name=>m.call(r[:name]),
91          :columns=>r[:columns].split(',').map{|v| m.call(v.split(' ').first)},
92          :table=>m.call(r[:table_name]),
93          :key=>r[:column_map].split(',').map{|v| m.call(v.split(' IS ').last)}}
94     end
95   end
96   fk_indexes.values
97 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
49 def indexes(table, opts = OPTS)
50   m = output_identifier_meth
51   im = input_identifier_meth
52   table = table.value if table.is_a?(Sequel::SQL::Identifier)
53   indexes = {}
54   metadata_dataset.
55    from(Sequel[:dbo][:sysobjects].as(:z)).
56    select{[
57      z[:name].as(:table_name),
58      i[:name].as(:index_name),
59      si[:indextype].as(:type),
60      si[:colnames].as(:columns)]}.
61    join(Sequel[:dbo][:sysindexes].as(:i), :id=>:id).
62    join(Sequel[:sys][:sysindexes].as(:si), :iname=> :name).
63    where{{z[:type] => 'U', :table_name=>im.call(table)}}.
64    each do |r|
65     indexes[m.call(r[:index_name])] =
66       {:unique=>(r[:type].downcase=='unique'),
67        :columns=>r[:columns].split(',').map{|v| m.call(v.split(' ').first)}} unless r[:type].downcase == 'primary key'
68   end
69   indexes
70 end
schema_parse_table(table, opts) click to toggle source
   # File lib/sequel/adapters/shared/sqlanywhere.rb
26 def schema_parse_table(table, opts)
27   m = output_identifier_meth(opts[:dataset])
28   im = input_identifier_meth(opts[:dataset])
29   metadata_dataset.
30    from{sa_describe_query("select * from #{im.call(table)}").as(:a)}.
31    join(Sequel[:syscolumn].as(:b), :table_id=>:base_table_id, :column_id=>:base_column_id).
32    order{a[:column_number]}.
33    map do |row|
34     auto_increment = row.delete(:is_autoincrement)
35     row[:auto_increment] = auto_increment == 1 || auto_increment == true
36     row[:primary_key] = row.delete(:pkey) == 'Y'
37     row[:allow_null] = row[:nulls_allowed].is_a?(Integer) ? row.delete(:nulls_allowed) == 1 : row.delete(:nulls_allowed)
38     row[:db_type] = row.delete(:domain_name)
39     row[:type] = if row[:db_type] =~ /numeric/i and (row[:scale].is_a?(Integer) ? row[:scale] == 0 : !row[:scale])
40       :integer
41     else
42       schema_column_type(row[:db_type])
43     end
44     row[:max_length] = row[:width] if row[:type] == :string
45     [m.call(row.delete(:name)), row]
46   end
47 end
tables(opts=OPTS) click to toggle source
    # File lib/sequel/adapters/shared/sqlanywhere.rb
 99 def tables(opts=OPTS)
100   tables_and_views('U', opts)
101 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
103 def views(opts=OPTS)
104   tables_and_views('V', opts)
105 end

Private Instance Methods

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

Sybase uses the IDENTITY column for autoincrementing columns.

    # File lib/sequel/adapters/shared/sqlanywhere.rb
122 def auto_increment_sql
123   'IDENTITY'
124 end
begin_transaction_sql() click to toggle source
    # File lib/sequel/adapters/shared/sqlanywhere.rb
135 def begin_transaction_sql
136   "BEGIN TRANSACTION"
137 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
127 def can_add_primary_key_constraint_on_nullable_columns?
128   false
129 end
commit_transaction_sql() click to toggle source
    # File lib/sequel/adapters/shared/sqlanywhere.rb
143 def commit_transaction_sql
144   "COMMIT TRANSACTION"
145 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
200 def create_table_as(name, ds, options)
201   raise(Error, "must provide dataset instance as value of create_table :as option on SqlAnywhere") unless ds.is_a?(Sequel::Dataset)
202   run(ds.into(name).sql)
203 end
database_error_regexps() click to toggle source
    # File lib/sequel/adapters/shared/sqlanywhere.rb
117 def database_error_regexps
118   DATABASE_ERROR_REGEXPS
119 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
206 def rename_table_sql(name, new_name)
207   "ALTER TABLE #{quote_schema_table(name)} RENAME #{quote_schema_table(new_name)}"
208 end
rollback_transaction_sql() click to toggle source
    # File lib/sequel/adapters/shared/sqlanywhere.rb
139 def rollback_transaction_sql
140   "IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION"
141 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
211 def schema_column_type(db_type)
212   if convert_smallint_to_bool && db_type =~ /smallint/i
213     :boolean
214   else
215     super
216   end
217 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
131 def temporary_table_sql
132   "GLOBAL TEMPORARY "
133 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
149 def type_literal_generic_datetime(column)
150   :datetime
151 end
type_literal_generic_file(column) click to toggle source

SQLAnywhere uses image type for blobs

    # File lib/sequel/adapters/shared/sqlanywhere.rb
159 def type_literal_generic_file(column)
160   :image
161 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
154 def type_literal_generic_trueclass(column)
155   :smallint
156 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