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.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.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



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

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.



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 120

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:



111
112
113
114
115
116
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 111

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

#add_limit_offset!(sql, options) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 101

def add_limit_offset!(sql, options)
  if options[:limit] and options[:offset] and options[:offset] > 0
    sql.sub!(/^\s*SELECT(\s+DISTINCT)?/i,  "SELECT\\1 TOP #{options[:limit]} START AT #{options[:offset]+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



68
69
70
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 68

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

#modify_types(tp) ⇒ Object

:nodoc:



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

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



52
53
54
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 52

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

#quoted_falseObject



60
61
62
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 60

def quoted_false
  quote(0)
end

#quoted_trueObject



56
57
58
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 56

def quoted_true
  quote(1)
end

#remove_check_constraints(table_name, column_name) ⇒ Object



144
145
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 144

def remove_check_constraints(table_name, column_name)
end

#remove_column(table_name, column_name) ⇒ Object



134
135
136
137
138
139
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 134

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



141
142
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 141

def remove_from_index(table_name, column_name)
end

#remove_from_primary_key(table_name, column_name) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 147

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



159
160
161
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 159

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

#sybaseserver_versionObject



64
65
66
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 64

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:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/arjdbc/sqlanywhere/adapter.rb', line 85

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