Class: Sapphire::DataAbstractions::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/sapphire/DataAbstractions/Database.rb

Instance Method Summary collapse

Instance Method Details

#closeObject



51
52
53
# File 'lib/sapphire/DataAbstractions/Database.rb', line 51

def close
  @connection.Close
end

#Open(connection_string) ⇒ Object



7
8
9
10
# File 'lib/sapphire/DataAbstractions/Database.rb', line 7

def Open(connection_string)
  @connection = WIN32OLE.new('ADODB.Connection')
  @connection.Open(connection_string)
end

#query(sql) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sapphire/DataAbstractions/Database.rb', line 12

def query(sql)
  recordset = WIN32OLE.new('ADODB.Recordset')
  recordset.Open(sql, @connection, 0 , 1)

  fields = []

  recordset.Fields.each do |field|
      fields << field.Name
  end
  begin
      recordset.MoveFirst
      data = recordset.GetRows
  rescue
      data = []
  end

  if(recordset.State != 0)
    recordset.Close
  end

  data = data.transpose
  records = []

  data.each do |row|
    y = 0
    record = Hash.new
    row.each do |column|
      if(column != nil)
        record.store fields[y].underscore.intern, column.to_s
      else
        record.store fields[y].underscore.intern, column
      end
      y += 1
    end
    records << record
  end
  records
end