module Sequel::ADO::Access::DatabaseMethods

Public Instance Methods

alter_table(name, *) click to toggle source

Remove cached schema after altering a table, since otherwise it can be cached incorrectly in the rename column case.

Calls superclass method
   # File lib/sequel/adapters/ado/access.rb
95 def alter_table(name, *)
96   super
97   remove_cached_schema(name)
98   nil
99 end
disconnect_connection(conn) click to toggle source

Access doesn’t let you disconnect if inside a transaction, so try rolling back an existing transaction first.

Calls superclass method
    # File lib/sequel/adapters/ado/access.rb
103 def disconnect_connection(conn)
104   conn.RollbackTrans rescue nil
105   super
106 end
execute_insert(sql, opts=OPTS) click to toggle source
    # File lib/sequel/adapters/ado/access.rb
108 def execute_insert(sql, opts=OPTS)
109   synchronize(opts[:server]) do |conn|
110     begin
111       log_connection_yield(sql, conn){conn.Execute(sql)}
112       last_insert_sql = "SELECT @@IDENTITY"
113       res = log_connection_yield(last_insert_sql, conn){conn.Execute(last_insert_sql)}
114       res.GetRows.transpose.each{|r| return r.shift}
115     rescue ::WIN32OLERuntimeError => e
116       raise_error(e)
117     end
118   end
119   nil
120 end
foreign_key_list(table, opts=OPTS) click to toggle source

OpenSchema returns compound foreign key relationships as multiple rows

    # File lib/sequel/adapters/ado/access.rb
148 def foreign_key_list(table, opts=OPTS)
149   m = output_identifier_meth
150   fks = ado_schema_foreign_keys(table).inject({}) do |memo, fk|
151     name = m.call(fk['FK_NAME'])
152     specs = memo[name] ||= {
153       :columns => [],
154       :table   => m.call(fk['PK_TABLE_NAME']),
155       :key     => [],
156       :deferrable => fk['DEFERRABILITY'],
157       :name    => name,
158       :on_delete => fk['DELETE_RULE'],
159       :on_update => fk['UPDATE_RULE']
160     }
161     specs[:columns] << m.call(fk['FK_COLUMN_NAME'])
162     specs[:key]     << m.call(fk['PK_COLUMN_NAME'])
163     memo
164   end
165   fks.values
166 end
indexes(table_name,opts=OPTS) click to toggle source

OpenSchema returns compound indexes as multiple rows

    # File lib/sequel/adapters/ado/access.rb
133 def indexes(table_name,opts=OPTS)
134   m = output_identifier_meth
135   idxs = ado_schema_indexes(table_name).inject({}) do |memo, idx|
136     unless idx["PRIMARY_KEY"]
137       index = memo[m.call(idx["INDEX_NAME"])] ||= {
138         :columns=>[], :unique=>idx["UNIQUE"]
139       }
140       index[:columns] << m.call(idx["COLUMN_NAME"])
141     end
142     memo
143   end
144   idxs
145 end
tables(opts=OPTS) click to toggle source
    # File lib/sequel/adapters/ado/access.rb
122 def tables(opts=OPTS)
123   m = output_identifier_meth
124   ado_schema_tables.map {|tbl| m.call(tbl['TABLE_NAME'])}
125 end
views(opts=OPTS) click to toggle source
    # File lib/sequel/adapters/ado/access.rb
127 def views(opts=OPTS)
128   m = output_identifier_meth
129   ado_schema_views.map {|tbl| m.call(tbl['TABLE_NAME'])}
130 end

Private Instance Methods

ado_schema_columns(table_name) click to toggle source
    # File lib/sequel/adapters/ado/access.rb
279 def ado_schema_columns(table_name)
280   rows=[]
281   fetch_ado_schema(:columns, [nil,nil,table_name.to_s,nil]) do |row| 
282     rows << AdoSchema::Column.new(row)
283   end
284   rows.sort!{|a,b| a["ORDINAL_POSITION"] <=> b["ORDINAL_POSITION"]}
285 end
ado_schema_foreign_keys(table_name) click to toggle source
    # File lib/sequel/adapters/ado/access.rb
287 def ado_schema_foreign_keys(table_name)
288   rows=[]
289   fetch_ado_schema(:foreign_keys, [nil,nil,nil,nil,nil,table_name.to_s]) do |row| 
290     rows << row
291   end
292   rows.sort!{|a,b| a["ORDINAL"] <=> b["ORDINAL"]}
293 end
ado_schema_indexes(table_name) click to toggle source
    # File lib/sequel/adapters/ado/access.rb
271 def ado_schema_indexes(table_name)
272   rows=[]
273   fetch_ado_schema(:indexes, [nil,nil,nil,nil,table_name.to_s]) do |row|
274     rows << row
275   end
276   rows
277 end
ado_schema_tables() click to toggle source
    # File lib/sequel/adapters/ado/access.rb
255 def ado_schema_tables
256   rows=[]
257   fetch_ado_schema(:tables, [nil,nil,nil,'TABLE']) do |row|
258     rows << row
259   end
260   rows
261 end
ado_schema_views() click to toggle source
    # File lib/sequel/adapters/ado/access.rb
263 def ado_schema_views
264   rows=[]
265   fetch_ado_schema(:views, [nil,nil,nil]) do |row|
266     rows << row
267   end
268   rows
269 end
alter_table_sql(table, op) click to toggle source

Emulate rename_column by adding the column, copying data from the old column, and dropping the old column.

Calls superclass method
    # File lib/sequel/adapters/ado/access.rb
172 def alter_table_sql(table, op)
173   case op[:op]
174   when :rename_column
175     unless sch = op[:schema]
176       raise(Error, "can't find existing schema entry for #{op[:name]}") unless sch = op[:schema] || schema(table).find{|c| c.first == op[:name]}
177       sch = sch.last
178     end
179     [
180       alter_table_sql(table, :op=>:add_column, :name=>op[:new_name], :default=>sch[:ruby_default], :type=>sch[:db_type], :null=>sch[:allow_null]),
181       from(table).update_sql(op[:new_name]=>op[:name]),
182       alter_table_sql(table, :op=>:drop_column, :name=>op[:name])
183     ]
184   when :set_column_null, :set_column_default
185     raise(Error, "can't find existing schema entry for #{op[:name]}") unless sch = op[:schema] || schema(table).find{|c| c.first == op[:name]}
186     sch = sch.last
187 
188     sch = if op[:op] == :set_column_null
189       sch.merge(:allow_null=>op[:null])
190     else
191       sch.merge(:ruby_default=>op[:default])
192     end
193 
194     [
195       alter_table_sql(table, :op=>:rename_column, :name=>op[:name], :new_name=>:sequel_access_backup_column, :schema=>sch),
196       alter_table_sql(table, :op=>:rename_column, :new_name=>op[:name], :name=>:sequel_access_backup_column, :schema=>sch)
197     ]
198   else
199     super
200   end
201 end
begin_transaction(conn, opts=OPTS) click to toggle source
    # File lib/sequel/adapters/ado/access.rb
203 def begin_transaction(conn, opts=OPTS)
204   log_connection_yield('Transaction.begin', conn){conn.BeginTrans}
205 end
commit_transaction(conn, opts=OPTS) click to toggle source
    # File lib/sequel/adapters/ado/access.rb
207 def commit_transaction(conn, opts=OPTS)
208   log_connection_yield('Transaction.commit', conn){conn.CommitTrans}
209 end
execute_open_ado_schema(type, criteria=[]) { |r| ... } click to toggle source

This is like execute() in that it yields an ADO RecordSet, except instead of an SQL interface there’s this OpenSchema call cf. msdn.microsoft.com/en-us/library/ee275721(v=bts.10)

    # File lib/sequel/adapters/ado/access.rb
310 def execute_open_ado_schema(type, criteria=[])
311   ado_schema = AdoSchema.new(type, criteria)
312   synchronize(opts[:server]) do |conn|
313     begin
314       r = log_connection_yield("OpenSchema #{type.inspect}, #{criteria.inspect}", conn) { 
315         if ado_schema.criteria.empty?
316           conn.OpenSchema(ado_schema.type) 
317         else
318           conn.OpenSchema(ado_schema.type, ado_schema.criteria) 
319         end
320       }
321       yield(r) if defined?(yield)
322     rescue ::WIN32OLERuntimeError => e
323       raise_error(e)
324     end
325   end
326   nil
327 end
fetch_ado_schema(type, criteria=[]) { |row| ... } click to toggle source
    # File lib/sequel/adapters/ado/access.rb
295 def fetch_ado_schema(type, criteria=[])
296   execute_open_ado_schema(type, criteria) do |s|
297     cols = []
298     s.Fields.each{|f| cols << f.Name}
299     s.GetRows.transpose.each do |r|
300       row = {}
301       cols.each{|c| row[c] = r.shift}
302       yield row
303     end unless s.eof
304   end
305 end
rollback_transaction(conn, opts=OPTS) click to toggle source
    # File lib/sequel/adapters/ado/access.rb
211 def rollback_transaction(conn, opts=OPTS)
212   log_connection_yield('Transaction.rollback', conn){conn.RollbackTrans}
213 end
schema_column_type(db_type) click to toggle source
Calls superclass method
    # File lib/sequel/adapters/ado/access.rb
215 def schema_column_type(db_type)
216   case db_type.downcase
217   when 'bit'
218     :boolean
219   when 'byte', 'guid'
220     :integer
221   when 'image'
222     :blob
223   else
224     super
225   end
226 end
schema_parse_table(table_name, opts) click to toggle source
    # File lib/sequel/adapters/ado/access.rb
228 def schema_parse_table(table_name, opts)
229   m = output_identifier_meth(opts[:dataset])
230   m2 = input_identifier_meth(opts[:dataset])
231   tn = m2.call(table_name.to_s)
232   idxs = ado_schema_indexes(tn)
233   ado_schema_columns(tn).map {|row|
234     specs = { 
235       :allow_null => row.allow_null,
236       :db_type => row.db_type,
237       :default => row.default,
238       :primary_key => !!idxs.find {|idx| 
239                         idx["COLUMN_NAME"] == row["COLUMN_NAME"] &&
240                         idx["PRIMARY_KEY"]
241                       },
242       :type =>  if row.db_type =~ /decimal/i && row.scale == 0
243                   :integer
244                 else
245                   schema_column_type(row.db_type)
246                 end,
247       :ado_type => row["DATA_TYPE"]
248     }
249     specs[:default] = nil if blank_object?(specs[:default])
250     specs[:allow_null] = specs[:allow_null] && !specs[:primary_key]
251     [ m.call(row["COLUMN_NAME"]), specs ]
252   }
253 end