Module: ArJdbc::SybaseSQLAnywhere

Defined in:
lib/arjdbc/sqlanywhere/adapter.rb,
lib/activerecord-jdbcsqlanywhere-adapter.rb

Defined Under Namespace

Modules: Column

Constant Summary collapse

VERSION =
"1.1.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.arel2_visitors(config) ⇒ Object



11
12
13
14
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 11

def self.arel2_visitors(config)
  require 'arel/visitors/sqlanywhere'
  {}.tap {|v| %w(sqlanywhere jdbcsqlanywhere).each {|x| v[x] = ::Arel::Visitors::SybaseSQLAnywhere } }
end

.column_selectorObject



7
8
9
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 7

def self.column_selector
  [/sqlanywhere/i, lambda {|cfg,col| col.extend(::ArJdbc::SybaseSQLAnywhere::Column)}]
end

.jdbc_connection_classObject



3
4
5
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 3

def self.jdbc_connection_class
  ::ActiveRecord::ConnectionAdapters::SQLAnywhereJdbcConnection
end

Instance Method Details

#_execute(sql, name = nil) ⇒ Object



78
79
80
81
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 78

def _execute(sql, name = nil)
  result = super
  ActiveRecord::ConnectionAdapters::JdbcConnection::insert?(sql) ? last_insert_id : result
end

#add_column(table_name, column_name, type, options = {}) ⇒ Object

Adds a new column to the named table. See TableDefinition#column for details of the options you can use.



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 126

def add_column(table_name, column_name, type, options = {})
  add_column_sql = "ALTER TABLE #{table_name} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
  fix_null = false
  if options[:null] == false
    fix_null = true
    options.delete(:null)
  end
  add_column_options!(add_column_sql, options)
  # TODO: Add support to mimic date columns, using constraints to mark them as such in the database
  # add_column_sql << " CONSTRAINT ck__#{table_name}__#{column_name}__date_only CHECK ( CONVERT(CHAR(12), #{quote_column_name(column_name)}, 14)='00:00:00:000' )" if type == :date
  execute(add_column_sql)
  execute("ALTER TABLE #{table_name} MODIFY #{quote_column_name(column_name)} NOT NULL") if fix_null
end

#add_column_options!(sql, options) ⇒ Object

:nodoc:



117
118
119
120
121
122
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 117

def add_column_options!(sql, options) #:nodoc:
  super
  if options[:null] != false
    sql << " NULL"
  end
end

#add_limit_offset!(sql, options) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 107

def add_limit_offset!(sql, options)
  if options[:limit] and options[:offset] and options[:offset].to_i > 0
    sql.sub!(/^\s*SELECT(\s+DISTINCT)?/i,  "SELECT\\1 TOP #{options[:limit]} START AT #{options[:offset].to_i+1}")
  elsif sql !~ /^\s*SELECT (@@|COUNT\()/i
    sql.sub!(/^\s*SELECT(\s+DISTINCT)?/i) do
      "SELECT#{$1} TOP #{options[:limit]}"
    end unless options[:limit].nil?
  end
end

#last_insert_idObject



74
75
76
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 74

def last_insert_id
  Integer(select_value("SELECT @@IDENTITY"))
end

#modify_types(tp) ⇒ Object

:nodoc:



83
84
85
86
87
88
89
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 83

def modify_types(tp) #:nodoc:
  tp[:primary_key] = "NUMERIC(22,0) DEFAULT AUTOINCREMENT PRIMARY KEY"
  tp[:integer][:limit] = nil
  tp[:boolean] = {:name => "bit"}
  tp[:binary] = {:name => "image"}
  tp
end

#quote_column_name(name) ⇒ Object



58
59
60
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 58

def quote_column_name(name)
  "\"#{name}\""
end

#quoted_falseObject



66
67
68
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 66

def quoted_false
  quote(0)
end

#quoted_trueObject



62
63
64
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 62

def quoted_true
  quote(1)
end

#remove_check_constraints(table_name, column_name) ⇒ Object



150
151
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 150

def remove_check_constraints(table_name, column_name)
end

#remove_column(table_name, column_name) ⇒ Object



140
141
142
143
144
145
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 140

def remove_column(table_name, column_name)
  remove_from_primary_key(table_name, column_name)
  remove_from_index(table_name, column_name)
  remove_check_constraints(table_name, column_name)
  execute "ALTER TABLE #{table_name} DROP #{quote_column_name(column_name)}"
end

#remove_from_index(table_name, column_name) ⇒ Object



147
148
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 147

def remove_from_index(table_name, column_name)
end

#remove_from_primary_key(table_name, column_name) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 153

def remove_from_primary_key(table_name, column_name)
  columns = select "select col.cname from SYS.syscolumns col WHERE col.tname = '#{table_name}' AND in_primary_key = 'Y'"
  the_column, columns = columns.partition{|c| c['cname'].casecmp(column_name.to_s) == 0 }
  if the_column.size > 0
    execute "ALTER TABLE #{table_name} DROP PRIMARY KEY"
    if columns.size > 0
      columns.map! {|c| quote_column_name(c['cname'])}
      execute "ALTER TABLE #{table_name} ADD PRIMARY KEY (#{columns.join(', ')})"
    end
  end
end

#remove_index(table_name, options = {}) ⇒ Object



165
166
167
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 165

def remove_index(table_name, options = {})
  execute "DROP INDEX #{table_name}.#{index_name(table_name, options)}"
end

#sybaseserver_versionObject



70
71
72
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 70

def sybaseserver_version
  @sybaseserver_version ||= select_value("select @@version").split('.').map(&:to_i)
end

#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object

:nodoc:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 91

def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc:
  limit = nil if %w(text binary).include? type.to_s
  return 'uniqueidentifier' if (type.to_s == 'uniqueidentifier')
  return super unless type.to_s == 'integer'

  if limit.nil? || limit == 4
    'int'
  elsif limit == 2
    'smallint'
  elsif limit == 1
    'tinyint'
  else
    'bigint'
  end
end