Class: Hypersonic

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

Instance Method Summary collapse

Constructor Details

#initializeHypersonic

Returns a new instance of Hypersonic.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/Hypersonic.rb', line 9

def initialize
  class_instance = jstatic Class
  class_instance.forName("org.hsqldb.jdbcDriver" )

  @driver_manager = jstatic :DriverManager
  # lets reset these variables to ensure there's not leftover data
  @conn = nil
  @statement = nil
  @rs = nil
  @rows_affected = 0
  @sql_data_types = nil
  @column_names = nil
  @java_data_types = nil
  @data_sizes = nil  
end

Instance Method Details

#column_namesObject



150
151
152
# File 'lib/Hypersonic.rb', line 150

def column_names
  @column_names
end

#connect(connect_string = nil, user = nil, password = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/Hypersonic.rb', line 25

def connect (connect_string = nil, user=nil, password=nil)
  # I've established some sane defaults, mainly for testing
  if(connect_string == nil) then 
    connect_string = "jdbc:hsqldb:file:testdb"
  end
  if(user == nil) then 
    user = "sa"
  end
  if(password == nil) then 
    password = ""
  end    
  @conn = @driver_manager.getConnection(connect_string, user, password)
  @statement = @conn.createStatement
end

#convert_result_setObject



61
62
63
# File 'lib/Hypersonic.rb', line 61

def convert_result_set
  convert_this_result_set @rs
end

#convert_sql_data_type_to_string(int_type) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/Hypersonic.rb', line 158

def convert_sql_data_type_to_string int_type
  # these data types are SQL type from java.sql.Types
  type = jstatic "java.sql.Types"
  ret_val = nil
  
  case int_type
    when type.ARRAY 
      ret_val = "ARRAY"
    when type.BIGINT 
      ret_val = "BIGINT"
    when type.BINARY 
      ret_val = "BINARY"
    when type.BIT 
      ret_val = "BIT"
    when type.BLOB 
      ret_val = "BLOB"
    when type.BOOLEAN 
      ret_val = "BOOLEAN"
    when type.CHAR 
      ret_val = "CHAR"
    when type.CLOB 
      ret_val = "CLOB"
    when type.DATALINK 
      ret_val = "DATALINK"
    when type.DATE 
      ret_val = "DATE"
    when type.DECIMAL 
      ret_val = "DECIMAL"
    when type.DISTINCT 
      ret_val = "DISTINCT"
    when type.DOUBLE 
      ret_val = "DOUBLE"
    when type.FLOAT 
      ret_val = "FLOAT"
    when type.INTEGER 
      ret_val = "INTEGER"
    when type.JAVA_OBJECT 
      ret_val = "JAVA_OBJECT"
    when type.LONGVARBINARY 
      ret_val = "LONGVARBINARY"
    when type.NULL 
      ret_val = "NULL"
    when type.NUMERIC 
      ret_val = "NUMERIC"
    when type.OTHER 
      ret_val = "OTHER"
    when type.REAL 
      ret_val = "REAL"
    when type.REF 
      ret_val = "REF"
    when type.SMALLINT 
      ret_val = "SMALLINT"
    when type.STRUCT 
      ret_val = "STRUCT"
    when type.TIME 
      ret_val = "TIME"
    when type.TIMESTAMP 
      ret_val = "TIMESTAMP"
    when type.TINYINT 
      ret_val = "TINYINT"
    when type.VARBINARY 
      ret_val = "VARBINARY"
    when type.VARCHAR 
      ret_val = "VARCHAR" 
  end
  ret_val
end

#convert_this_result_set(rs) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/Hypersonic.rb', line 65

def convert_this_result_set rs
  meta = rs.
  column_count = meta.getColumnCount
  # question: can I ask the metadata for the row count?
  row_count = 0
  ruby_result_set = []
  @sql_data_types = []
  @column_names = []
  @java_data_types = []
  @data_sizes = []
  
  while(rs.next) do 
    row_count = row_count + 1
    x = 0
    this_row = []
    while( x < column_count ) do
      x = x + 1
      o = rs.getObject(x)
      
      # should I convert this to a string or convert it properly here?
      # I think that ActiveRecord does it's own conversion in the adapter
      # Double check this... -jrr
      
      value = o.to_s
      name = meta.getColumnName(x)
      this_row.push(value)
      
      # I only want to extract data types and column names on the first row
      if(@sql_data_types.length < column_count) then

        int_type = meta.getColumnType(x)
        data_type = convert_sql_data_type_to_string(int_type)
        @sql_data_types.push(data_type)
        
        @java_data_types.push(meta.getColumnClassName(x))
        @column_names.push(meta.getColumnName(x))
        @data_sizes.push(meta.getColumnDisplaySize(x))
      end
    end
    #puts this_row
    ruby_result_set.push(this_row)      
  end
  ruby_result_set
end

#current_databaseObject



136
137
138
139
# File 'lib/Hypersonic.rb', line 136

def current_database
  cdb = @rs..getTableName(1)
  cdb
end

#data_sizesObject



154
155
156
# File 'lib/Hypersonic.rb', line 154

def data_sizes
  @data_sizes
end

#data_typesObject



145
146
147
148
# File 'lib/Hypersonic.rb', line 145

def data_types
  # return the list of data types contained in the current result set
  @sql_data_types
end

#disconnectObject



51
52
53
54
55
56
57
58
59
# File 'lib/Hypersonic.rb', line 51

def disconnect
  if(@conn) then
    @conn.close
  end
  @driver_manager = nil
  @conn=nil
  @statement=nil
  @rs=nil
end

#execute_query(sql) ⇒ Object



45
46
47
48
49
# File 'lib/Hypersonic.rb', line 45

def execute_query (sql)
  # use this for insert, update, drop, and insert
  @rs = @statement.executeQuery(sql)
  ret_val = convert_result_set
end

#execute_update(sql) ⇒ Object



40
41
42
43
# File 'lib/Hypersonic.rb', line 40

def execute_update (sql)
  # use this for insert, update, drop, and insert
  @rows_affected = @statement.executeUpdate(sql)
end

#get_column_name(index) ⇒ Object



110
111
112
# File 'lib/Hypersonic.rb', line 110

def get_column_name (index)
  @rs.getColumnName(index)
end

#get_column_type_name(index) ⇒ Object



114
115
116
# File 'lib/Hypersonic.rb', line 114

def get_column_type_name(index)
  @rs.getColumnTypeName(index)
end

#get_rows_affectedObject



118
119
120
# File 'lib/Hypersonic.rb', line 118

def get_rows_affected
  @rows_affected
end

#get_table_nameObject



122
123
124
125
126
127
128
# File 'lib/Hypersonic.rb', line 122

def get_table_name
  if(@rs) then
    @rs..getTableName
  else
    nil
  end
end

#java_data_typesObject



141
142
143
# File 'lib/Hypersonic.rb', line 141

def java_data_types
  @java_data_types
end

#tablesObject



130
131
132
133
134
# File 'lib/Hypersonic.rb', line 130

def tables
  table_list = @conn..getTables(nil, nil, nil, ["TABLE"])
  rs = convert_this_result_set table_list
  rs[0]
end