Class: ActiveRecord::ConnectionAdapters::QuickBaseAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/quickbase_adapter.rb

Overview

The QuickBase Adapter for Rails

Instance Method Summary collapse

Constructor Details

#initialize(connection, logger, connection_options = nil) ⇒ QuickBaseAdapter

Returns a new instance of QuickBaseAdapter.



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/quickbase_adapter.rb', line 110

def initialize(connection, logger, connection_options=nil)
  super(connection, logger)
  @connection_options = connection_options
  @qbc = @connection
  @main_dbid = @qbc.dbid.dup
  @main_dbname = @qbc.dbname.dup.downcase
  @id_field_name = "Record ID#"
  @id_fid = "3"
  @useActiveTableColumns = false
  @cachedColumns = {}
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


283
284
285
# File 'lib/quickbase_adapter.rb', line 283

def active?
  @qbc.ticket
end

#adapter_nameObject



122
123
124
# File 'lib/quickbase_adapter.rb', line 122

def adapter_name
  'QuickBase'
end

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

Raises:

  • (NotImplementedError)


302
303
304
# File 'lib/quickbase_adapter.rb', line 302

def add_column(table_name, column_name, type, options = {})
  raise NotImplementedError, "add_column is not supported by the QuickBase adapter"
end

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

Raises:

  • (NotImplementedError)


310
311
312
# File 'lib/quickbase_adapter.rb', line 310

def add_index(table_name, column_name, options = {})
  raise NotImplementedError, "indexing is not supported by the QuickBase"
end

#columns(table_name, name = nil) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/quickbase_adapter.rb', line 126

def columns(table_name, name = nil)

   return [] if table_name.nil? or table_name.blank?
   
   if @useActiveTableColumns
      dbid = @qbc.dbid
      @useActiveTableColumns = false
   else
      @qbc.getSchema(@main_dbid)
      if table_name.downcase != @main_dbname or @qbc.chdbids
         dbid = @qbc.lookupChdbid(table_name)
         @qbc.getSchema(dbid)
      end
   end
   
   if @qbc.cacheSchemas and @cachedColumns[dbid]
      quickBaseColumns = @cachedColumns[dbid]
   else
      quickBaseColumns = []
      columnNames = @qbc.getFieldNames(dbid)
      key_fid = @qbc.key_fid
      primary = false
      columnNames.each{|columnName|
      
         fieldAttributes = {}
         quickBaseFieldId = @qbc.lookupFieldIDByName(columnName)
         quickBaseFieldType = @qbc.lookupFieldTypeByName(columnName)
         
         fieldAttributes["quickBaseFieldName"] =  columnName.dup
         fieldAttributes["quickBaseFieldId"] =  quickBaseFieldId.dup
         fieldAttributes["quickBaseFieldType"] =  quickBaseFieldType.dup
         fieldAttributes["default_value"] =  @qbc.lookupFieldPropertyByName(columnName, "default_value" )
         fieldAttributes["decimal_places"] =  @qbc.lookupFieldPropertyByName(columnName, "decimal_places" )
         
         if key_fid and key_fid == quickBaseFieldId
            fieldAttributes["primary"] =  true
         elsif quickBaseFieldType == "recordid"
            @id_field_name = columnName.dup 
            @id_fid = quickBaseFieldId.dup
            columnName = "id"
         elsif quickBaseFieldId.to_i < 6  
            columnName << "_id" 
         end
         fieldAttributes["columnName"] =  columnName.dup
         quickBaseColumn = QuickBaseColumn.new(fieldAttributes)
         quickBaseColumns << quickBaseColumn
      }
      if @qbc.cacheSchemas
         @cachedColumns[dbid] = quickBaseColumns
      end
   end
   quickBaseColumns
end

#create_table(name, options = {}) ⇒ Object

Raises:

  • (NotImplementedError)


298
299
300
# File 'lib/quickbase_adapter.rb', line 298

def create_table(name, options = {})
  raise NotImplementedError, "create_table is not supported by the QuickBase adapter"
end

#delete(sql, name = nil) ⇒ Object



199
200
201
202
203
204
205
206
207
208
# File 'lib/quickbase_adapter.rb', line 199

def delete(sql, name = nil)
   selectString = sql.dup
   selectString.gsub!("DELETE","SELECT [id] ")
   selectString.gsub!("[id]",@id_fid)
   rows = select(selectString,name)
   rows.each{|row|
      recordID = row["id"]
      @qbc._deleteRecord(recordID) if recordID
   }
end

#disconnect!Object



293
294
295
296
# File 'lib/quickbase_adapter.rb', line 293

def disconnect!
  @qbc.signOut 
  @active = @qbc.ticket
end

#execute(sql, name = nil) ⇒ Object



273
274
275
276
277
# File 'lib/quickbase_adapter.rb', line 273

def execute(sql, name = nil)
  if sql and sql.is_a?(String) and sql.length > 0
     @qbc.instance_eval(sql)
  end
end

#index_name(table_name, options) ⇒ Object

:nodoc:

Raises:

  • (NotImplementedError)


314
315
316
# File 'lib/quickbase_adapter.rb', line 314

def index_name(table_name, options) #:nodoc:
  raise NotImplementedError, "indexing is not supported by the QuickBase"
end

#insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) ⇒ Object



188
189
190
191
# File 'lib/quickbase_adapter.rb', line 188

def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil)
  sql.gsub!("NULL,","'',")
  @qbc.doSQLInsert(sql)
end

#quote(value, column = nil) ⇒ Object



184
185
186
# File 'lib/quickbase_adapter.rb', line 184

def quote(value, column = nil)
   ret = value ? "'#{value}'" : ''
end

#quote_column_name(name) ⇒ Object



180
181
182
# File 'lib/quickbase_adapter.rb', line 180

def quote_column_name(name)
  "[#{name}]"
end

#reconnect!Object



287
288
289
290
291
# File 'lib/quickbase_adapter.rb', line 287

def reconnect!
    @qbc.signOut
    @qbc.authenticate(@qbc.username,@qbc.password) 
    @active = !@qbc.ticket.nil?
end

#remove_column(table_name, column_name) ⇒ Object

Raises:

  • (NotImplementedError)


306
307
308
# File 'lib/quickbase_adapter.rb', line 306

def remove_column(table_name, column_name)
  raise NotImplementedError, "remove_column is not supported by the QuickBase adapter"
end

#select(sql, name) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/quickbase_adapter.rb', line 210

def select(sql,name)
   puts sql
   sql = sql.to_s
   
   if sql.match(/([^:]+)(:)(.+)/)
      table = $1
      sql = $3
   elsif name  
      table = name.split()[0]
   end
   
   rows = nil
   if sql.include?("SELECT count(*)")
      rows = []
      rows << {"count" => @qbc.doSQLQuery(sql).to_i }
   elsif sql.include?("SELECT ")
      sql.gsub!(".id",".#{@id_field_name}")
      rows = @qbc.doSQLQuery(sql, :Array)
      rows.each{|row| row["id"] = row[@id_field_name]} if rows
   elsif sql.match(/\{[^\}]+\}/)  # QuickBase query 
      setActiveTable(table)
      rows = @qbc.getAllValuesForFieldsAsArray(@qbc.dbid,@qbc.getFieldNames,sql)            
      rows.each{|row| row["id"] = row[@id_field_name]} if rows
   elsif sql.length > 0 # named QuickBase query 
      setActiveTable(table)
      rows = @qbc.getAllValuesForFieldsAsArray(@qbc.dbid,nil,nil,nil,sql)            
      rows.each{|row| row["id"] = row[@id_field_name]} if rows
   end
   puts rows.inspect
   
   rows
   
end

#setActiveTable(table) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/quickbase_adapter.rb', line 244

def setActiveTable(table)
   if table
      foundTable  = false
      save_dbid = @qbc.dbid.dup
      @qbc.getSchema(@main_dbid)
      if table.downcase != @main_dbname
         if @qbc.lookupChdbid(table)
            if @qbc._getSchema
               foundTable  = true
            end
         elsif @qbc.findDBByname(table)                      
            if @qbc._getSchema
               foundTable  = true
            end
         else
            if @qbc.getSchema(table)
               foundTable  = true
            end
         end
      end
      if foundTable
         @useActiveTableColumns = true
      else
         @qbc.getSchema(save_dbid)
      end
   end
   @qbc.dbid
end

#supports_count_distinct?Boolean

Returns:

  • (Boolean)


279
280
281
# File 'lib/quickbase_adapter.rb', line 279

def supports_count_distinct?
  false
end

#update(sql, name = nil) ⇒ Object



193
194
195
196
197
# File 'lib/quickbase_adapter.rb', line 193

def update(sql, name = nil)
   selectString = sql.dup
   selectString.gsub!("[id]",@id_fid) 
   @qbc.doSQLUpdate(selectString)
end