rubyfb 0.6.4 → 0.6.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -35,17 +35,9 @@
35
35
  #define RUBY_H_INCLUDED
36
36
  #endif
37
37
 
38
- #ifndef FIRERUBY_DATABASE_H
39
- #include "ResultSet.h"
40
- #endif
41
-
42
38
  /* Function prototypes. */
43
- void Init_TypeMap();
44
- VALUE toArray(VALUE);
39
+ void Init_TypeMap(VALUE);
40
+ VALUE toValue(XSQLVAR *, VALUE, VALUE);
45
41
  void setParameters(XSQLDA *, VALUE, VALUE, VALUE);
46
- VALUE getModule(const char *);
47
- VALUE getClass(const char *);
48
- VALUE getClassInModule(const char *, VALUE);
49
- VALUE getModuleInModule(const char *, VALUE);
50
42
 
51
43
  #endif /* FIRERUBY_TYPE_MAP_H */
@@ -918,25 +918,26 @@ module ActiveRecord
918
918
  end
919
919
 
920
920
  def select_raw(sql, name = nil, binds = [])
921
- fields = []
922
- rows = []
923
- exec_query(sql, name, binds) do |row|
924
- array_row = []
925
- row.each do |column, value|
926
- fields << fb_to_ar_case(column) if row.number == 1
927
- case value
928
- when Rubyfb::Blob
929
- temp = value.to_s
930
- value.close
931
- value = temp
932
- when Time, DateTime
933
- value = create_time_with_default_timezone(value)
921
+ fields = rows = []
922
+ exec_result = exec_query(sql, name, binds)
923
+ if exec_result.instance_of?(Rubyfb::ResultSet)
924
+ fields = exec_result.statement.metadata.collect{ |m| fb_to_ar_case(m.key) }
925
+ exec_result.each do |row|
926
+ rows << row.values.collect do |value|
927
+ case value
928
+ when Rubyfb::Blob
929
+ value.to_s.tap do
930
+ value.close
931
+ end
932
+ when Time, DateTime
933
+ create_time_with_default_timezone(value)
934
+ else
935
+ value
936
+ end
934
937
  end
935
- array_row << value
936
938
  end
937
- rows << array_row
938
939
  end
939
- return fields, rows
940
+ return fields, rows
940
941
  end
941
942
 
942
943
  def index_metadata(table_name, pk, name = nil)
File without changes
@@ -0,0 +1,109 @@
1
+ module Rubyfb
2
+ class ResultSet
3
+ include Enumerable
4
+ attr_reader :statement, :transaction, :row_count
5
+
6
+ def initialize(statement, transaction)
7
+ @statement = statement
8
+ @transaction = transaction
9
+ @active = true
10
+ @row_count = 0
11
+ @manage_statement = false
12
+ @manage_transaction = false
13
+ end
14
+
15
+ def initialize_copy(o)
16
+ raise FireRubyException.new("Object cloning is not supported");
17
+ end
18
+
19
+ def each
20
+ while row = fetch
21
+ yield row
22
+ end
23
+ ensure
24
+ close
25
+ end
26
+
27
+ def fetch
28
+ row = nil
29
+ case statement.fetch
30
+ when Statement::FETCH_MORE
31
+ @row_count += 1
32
+ row = Row.new(statement.metadata, statement.current_row(transaction), @row_count)
33
+ when Statement::FETCH_COMPLETED
34
+ close
35
+ when Statement::FETCH_ONE
36
+ @row_count = 1
37
+ row = Row.new(statement.metadata, statement.current_row(transaction), @row_count)
38
+ close
39
+ else
40
+ raise FireRubyException.new("Error fetching query row.");
41
+ end if active?
42
+
43
+ return row
44
+ end
45
+
46
+ def close
47
+ return unless active?
48
+
49
+ @active = false
50
+ statement.close_cursor
51
+ if @manage_statement && statement.prepared?
52
+ statement.close
53
+ end
54
+ if @manage_transaction && transaction.active?
55
+ transaction.commit
56
+ end
57
+ end
58
+
59
+ def connection
60
+ statement.connection
61
+ end
62
+
63
+ def sql
64
+ statement.sql
65
+ end
66
+
67
+ def dialect
68
+ statement.dialect
69
+ end
70
+
71
+ def column_name(index)
72
+ with_metadata(index){ |m| m.name }
73
+ end
74
+
75
+ def column_alias(index)
76
+ with_metadata(index){ |m| m.alias }
77
+ end
78
+
79
+ def column_scale(index)
80
+ with_metadata(index){ |m| m.scale }
81
+ end
82
+
83
+ def column_table(index)
84
+ with_metadata(index){ |m| m.relation }
85
+ end
86
+
87
+ def get_base_type(index)
88
+ with_metadata(index){ |m| m.type }
89
+ end
90
+
91
+ def column_count
92
+ statement.metadata.size
93
+ end
94
+
95
+ def active?
96
+ @active
97
+ end
98
+
99
+ def exhausted?
100
+ !active?
101
+ end
102
+ private
103
+ def with_metadata(index)
104
+ if index >= 0 and index < column_count
105
+ yield statement.metadata[index]
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,149 @@
1
+ module Rubyfb
2
+ class Row
3
+ include Enumerable
4
+ attr_reader :number
5
+
6
+ def initialize(metadata, data, row_number)
7
+ @number = row_number
8
+ @metadata = metadata
9
+ @data = data
10
+ end
11
+
12
+ def column_count
13
+ @metadata.size
14
+ end
15
+
16
+ def column_name(idx)
17
+ @metadata[get_index(idx)].name
18
+ end
19
+
20
+ def column_alias(idx)
21
+ @metadata[get_index(idx)].alias
22
+ end
23
+
24
+ def column_scale(idx)
25
+ @metadata[get_index(idx)].scale
26
+ end
27
+
28
+ def get_base_type(idx)
29
+ @metadata[get_index(idx)].type
30
+ end
31
+
32
+ def each
33
+ @metadata.each_with_index do |c, i|
34
+ yield c.key, @data[i]
35
+ end if block_given?
36
+ end
37
+
38
+ def each_key
39
+ @metadata.each do |c|
40
+ yield c.key
41
+ end if block_given?
42
+ end
43
+
44
+ def each_value
45
+ @data.each do |v|
46
+ yield v
47
+ end if block_given?
48
+ end
49
+
50
+ def [](idx)
51
+ @data[get_index(idx)]
52
+ end
53
+
54
+ def fetch(index, default=nil)
55
+ soft_value(index) || default || (
56
+ if block_given?
57
+ yield index
58
+ else
59
+ raise IndexError.new("Column identifier #{index} not found in row.")
60
+ end
61
+ )
62
+ end
63
+
64
+ def has_key?(key)
65
+ !!@metadata.find{ |col| col.key == key }
66
+ end
67
+
68
+ def has_column?(name)
69
+ !!@metadata.find{ |col| col.name == name }
70
+ end
71
+
72
+ def has_alias?(column_alias)
73
+ !!@metadata.find{ |col| col.alias == column_alias }
74
+ end
75
+
76
+ def has_value?(value)
77
+ !!@data.find{ |v| v == value }
78
+ end
79
+
80
+ def keys
81
+ @metadata.collect{ |col| col.key }
82
+ end
83
+
84
+ def names
85
+ @metadata.collect{ |col| col.name }
86
+ end
87
+
88
+ def aliases
89
+ @metadata.collect{ |col| col.alias }
90
+ end
91
+
92
+ def values
93
+ @data
94
+ end
95
+
96
+ def select
97
+ block_given? || (raise StandardError.new("No block specified in call to Row#select."))
98
+
99
+ [].tap do |a|
100
+ @metadata.each_with_index do |c, i|
101
+ a << [c.key, @data[i]] if yield c.key, @data[i]
102
+ end
103
+ end
104
+ end
105
+
106
+ def to_a
107
+ select{ true }
108
+ end
109
+
110
+ def to_hash
111
+ {}.tap do |map|
112
+ @metadata.each_with_index do |c, i|
113
+ map[c.key] = @data[i]
114
+ end
115
+ end
116
+ end
117
+
118
+ def values_at(*columns)
119
+ columns.collect{ |key| soft_value(key) }
120
+ end
121
+
122
+ alias_method :each_pair, :each
123
+ alias_method :include?, :has_key?
124
+ alias_method :key?, :has_key?
125
+ alias_method :member?, :has_key?
126
+ alias_method :value?, :has_value?
127
+ alias_method :length, :column_count
128
+ alias_method :size, :column_count
129
+ private
130
+ def soft_value(key)
131
+ if index = find_index(key)
132
+ @data[index]
133
+ end
134
+ end
135
+
136
+ def find_index(key)
137
+ if key.kind_of?(String)
138
+ @metadata.find_index{ |c| c.key == key }
139
+ else
140
+ key < 0 ? size + key : key
141
+ end
142
+ end
143
+
144
+ def get_index(key)
145
+ find_index(key) || (raise IndexError.new("Column identifier #{key} not found in row."))
146
+ end
147
+ end
148
+ end
149
+
@@ -1,6 +1,10 @@
1
+ require 'date'
1
2
  require 'rubyfb_options'
3
+ require 'result_set'
2
4
  require 'rubyfb_lib'
3
- require 'SQLType'
4
- require 'ProcedureCall'
5
- require 'Connection'
5
+ require 'sql_type'
6
+ require 'procedure_call'
7
+ require 'row'
8
+ require 'statement'
9
+ require 'connection'
6
10
 
Binary file
File without changes
@@ -0,0 +1,17 @@
1
+ module Rubyfb
2
+ class Statement
3
+ attr_reader :metadata
4
+
5
+ class ColumnMetadata
6
+ attr_reader :name, :alias, :key, :type, :scale, :relation
7
+ end
8
+
9
+ def create_column_metadata
10
+ ColumnMetadata.new
11
+ end
12
+
13
+ def create_result_set(transaction)
14
+ ResultSet.new(self, transaction)
15
+ end
16
+ end
17
+ end
@@ -2,16 +2,16 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "rubyfb"
5
- s.version = "0.6.4"
5
+ s.version = "0.6.7"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["George Georgiev"]
9
- s.date = "2012-03-23"
9
+ s.date = "2012-03-29"
10
10
  s.description = "Firebird SQL access library"
11
11
  s.email = "georgiev@heatbs.com"
12
12
  s.extensions = ["ext/extconf.rb"]
13
- s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README", "examples/example01.rb", "ext/extconf.rb", "lib/Connection.rb", "lib/ProcedureCall.rb", "lib/SQLType.rb", "lib/rubyfb.rb", "lib/rubyfb_options.rb", "lib/src.rb"]
14
- s.files = ["CHANGELOG", "LICENSE", "Manifest", "README", "Rakefile", "examples/example01.rb", "ext/AddUser.c", "ext/AddUser.h", "ext/Backup.c", "ext/Backup.h", "ext/Blob.c", "ext/Blob.h", "ext/Common.c", "ext/Common.h", "ext/Connection.c", "ext/Connection.h", "ext/DataArea.c", "ext/DataArea.h", "ext/Database.c", "ext/Database.h", "ext/FireRuby.c", "ext/FireRuby.h", "ext/FireRubyException.c", "ext/FireRubyException.h", "ext/Generator.c", "ext/Generator.h", "ext/RemoveUser.c", "ext/RemoveUser.h", "ext/Restore.c", "ext/Restore.h", "ext/ResultSet.c", "ext/ResultSet.h", "ext/Row.c", "ext/Row.h", "ext/ServiceManager.c", "ext/ServiceManager.h", "ext/Services.c", "ext/Services.h", "ext/Statement.c", "ext/Statement.h", "ext/Transaction.c", "ext/Transaction.h", "ext/TypeMap.c", "ext/TypeMap.h", "ext/extconf.rb", "ext/rfbint.h", "ext/rfbsleep.h", "ext/rfbstr.c", "ext/rfbstr.h", "ext/uncrustify.cfg", "lib/Connection.rb", "lib/ProcedureCall.rb", "lib/SQLType.rb", "lib/active_record/connection_adapters/rubyfb_adapter.rb", "lib/arel/visitors/fb15/rubyfb.rb", "lib/arel/visitors/rubyfb.rb", "lib/mkdoc", "lib/rubyfb.rb", "lib/rubyfb_lib.so", "lib/rubyfb_options.rb", "lib/src.rb", "mswin32fb/fbclient_mingw.def", "mswin32fb/fbclient_mingw.lib", "mswin32fb/fbclient_ms.lib", "mswin32fb/ibase.h", "mswin32fb/iberror.h", "test/AddRemoveUserTest.rb", "test/BackupRestoreTest.rb", "test/BlobTest.rb", "test/CharacterSetTest.rb", "test/ConnectionTest.rb", "test/DDLTest.rb", "test/DatabaseTest.rb", "test/FieldCharacterSetTest.rb", "test/GeneratorTest.rb", "test/KeyTest.rb", "test/ResultSetTest.rb", "test/RoleTest.rb", "test/RowCountTest.rb", "test/RowTest.rb", "test/SQLTest.rb", "test/SQLTypeTest.rb", "test/ServiceManagerTest.rb", "test/StatementTest.rb", "test/StoredProcedureTest.rb", "test/TestSetup.rb", "test/TransactionTest.rb", "test/TypeTest.rb", "rubyfb.gemspec"]
13
+ s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README", "examples/example01.rb", "ext/extconf.rb", "lib/connection.rb", "lib/procedure_call.rb", "lib/result_set.rb", "lib/row.rb", "lib/rubyfb.rb", "lib/rubyfb_options.rb", "lib/sql_type.rb", "lib/src.rb", "lib/statement.rb"]
14
+ s.files = ["CHANGELOG", "LICENSE", "Manifest", "README", "Rakefile", "examples/example01.rb", "ext/AddUser.c", "ext/AddUser.h", "ext/Backup.c", "ext/Backup.h", "ext/Blob.c", "ext/Blob.h", "ext/Common.c", "ext/Common.h", "ext/Connection.c", "ext/Connection.h", "ext/DataArea.c", "ext/DataArea.h", "ext/Database.c", "ext/Database.h", "ext/FireRuby.c", "ext/FireRuby.h", "ext/FireRubyException.c", "ext/FireRubyException.h", "ext/Generator.c", "ext/Generator.h", "ext/RemoveUser.c", "ext/RemoveUser.h", "ext/Restore.c", "ext/Restore.h", "ext/ServiceManager.c", "ext/ServiceManager.h", "ext/Services.c", "ext/Services.h", "ext/Statement.c", "ext/Statement.h", "ext/Transaction.c", "ext/Transaction.h", "ext/TypeMap.c", "ext/TypeMap.h", "ext/extconf.rb", "ext/rfbint.h", "ext/rfbsleep.h", "ext/rfbstr.c", "ext/rfbstr.h", "ext/uncrustify.cfg", "lib/active_record/connection_adapters/rubyfb_adapter.rb", "lib/arel/visitors/fb15/rubyfb.rb", "lib/arel/visitors/rubyfb.rb", "lib/connection.rb", "lib/mkdoc", "lib/procedure_call.rb", "lib/result_set.rb", "lib/row.rb", "lib/rubyfb.rb", "lib/rubyfb_lib.so", "lib/rubyfb_options.rb", "lib/sql_type.rb", "lib/src.rb", "lib/statement.rb", "mswin32fb/fbclient_mingw.def", "mswin32fb/fbclient_mingw.lib", "mswin32fb/fbclient_ms.lib", "mswin32fb/ibase.h", "mswin32fb/iberror.h", "test/AddRemoveUserTest.rb", "test/BackupRestoreTest.rb", "test/BlobTest.rb", "test/CharacterSetTest.rb", "test/ConnectionTest.rb", "test/DDLTest.rb", "test/DatabaseTest.rb", "test/FieldCharacterSetTest.rb", "test/GeneratorTest.rb", "test/KeyTest.rb", "test/ResultSetTest.rb", "test/RoleTest.rb", "test/RowCountTest.rb", "test/RowTest.rb", "test/SQLTest.rb", "test/SQLTypeTest.rb", "test/ServiceManagerTest.rb", "test/StatementTest.rb", "test/StoredProcedureTest.rb", "test/TestSetup.rb", "test/TransactionTest.rb", "test/TypeTest.rb", "rubyfb.gemspec"]
15
15
  s.homepage = "http://rubyforge.org/projects/rubyfb"
16
16
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Rubyfb", "--main", "README"]
17
17
  s.require_paths = ["lib", "ext"]
@@ -66,15 +66,18 @@ class ResultSetTest < Test::Unit::TestCase
66
66
  def test01
67
67
  r = @connections[0].execute("SELECT * FROM TEST_TABLE ORDER BY TESTID", @transactions[0])
68
68
  assert_equal(Rubyfb::ResultSet, r.class)
69
+ assert(r.kind_of?(Enumerable), "ResultSet should be Enumerable")
69
70
 
70
71
  assert(r.connection == @connections[0])
71
72
  assert(r.transaction == @transactions[0])
72
73
  assert(r.sql == "SELECT * FROM TEST_TABLE ORDER BY TESTID")
73
74
  assert(r.dialect == 3)
74
75
 
75
- assert(r.fetch != nil)
76
- assert(r.fetch.class == Row)
77
- assert(r.fetch[0] == 30)
76
+ first_row = r.fetch
77
+ assert(first_row != nil)
78
+ assert_equal(1, first_row.number)
79
+ assert_equal(Row, r.fetch.class)
80
+ assert_equal(30, r.fetch[0])
78
81
  assert(r.fetch[1] == 'Record Four.')
79
82
  r.fetch
80
83
  assert(r.fetch == nil)
@@ -83,7 +86,7 @@ class ResultSetTest < Test::Unit::TestCase
83
86
  r = @connections[0].execute("SELECT * FROM TEST_TABLE ORDER BY TESTID", @transactions[0])
84
87
  assert_equal(Rubyfb::ResultSet, r.class)
85
88
  assert(r.column_count == 2)
86
- assert(r.column_name(0) == 'TESTID')
89
+ assert_equal('TESTID', r.column_name(0))
87
90
  assert(r.column_name(1) == 'TESTINFO')
88
91
  assert(r.column_name(3) == nil)
89
92
  assert(r.column_name(-1) == nil)
@@ -58,6 +58,9 @@ class RowTest < Test::Unit::TestCase
58
58
  sql = 'select COL01 one, COL02 two, COL03 three from rowtest'
59
59
  rows = @connection.execute_immediate(sql)
60
60
  data = rows.fetch
61
+ assert_equal(Rubyfb::Row, data.class)
62
+ assert(data.kind_of?(Enumerable), "Row should be Enumerable")
63
+
61
64
 
62
65
  count = 0
63
66
  data.each do |name, value|
@@ -91,7 +94,7 @@ class RowTest < Test::Unit::TestCase
91
94
  end
92
95
 
93
96
  assert(data.has_key?('ONE'))
94
- assert(data.has_key?('TEN') == false)
97
+ assert_equal(false, data.has_key?('TEN'))
95
98
 
96
99
  assert(data.has_column?('COL02'))
97
100
  assert(data.has_column?('COL22') == false)
@@ -77,7 +77,7 @@ class TypeTest < Test::Unit::TestCase
77
77
  assert(row[1].instance_of?(Float))
78
78
  assert(row[2].instance_of?(Float))
79
79
  assert(row[3].instance_of?(Float))
80
- assert(row[4].instance_of?(Time))
80
+ assert(row[4].instance_of?(Time), "Time expected but #{row[4].class.name} found")
81
81
  assert(row[5].instance_of?(Time))
82
82
  assert(row[6].instance_of?(String))
83
83
  assert(row[7].instance_of?(Time))
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyfb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.6.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-23 00:00:00.000000000 Z
12
+ date: 2012-03-29 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Firebird SQL access library
15
15
  email: georgiev@heatbs.com
@@ -22,12 +22,15 @@ extra_rdoc_files:
22
22
  - README
23
23
  - examples/example01.rb
24
24
  - ext/extconf.rb
25
- - lib/Connection.rb
26
- - lib/ProcedureCall.rb
27
- - lib/SQLType.rb
25
+ - lib/connection.rb
26
+ - lib/procedure_call.rb
27
+ - lib/result_set.rb
28
+ - lib/row.rb
28
29
  - lib/rubyfb.rb
29
30
  - lib/rubyfb_options.rb
31
+ - lib/sql_type.rb
30
32
  - lib/src.rb
33
+ - lib/statement.rb
31
34
  files:
32
35
  - CHANGELOG
33
36
  - LICENSE
@@ -59,10 +62,6 @@ files:
59
62
  - ext/RemoveUser.h
60
63
  - ext/Restore.c
61
64
  - ext/Restore.h
62
- - ext/ResultSet.c
63
- - ext/ResultSet.h
64
- - ext/Row.c
65
- - ext/Row.h
66
65
  - ext/ServiceManager.c
67
66
  - ext/ServiceManager.h
68
67
  - ext/Services.c
@@ -79,17 +78,20 @@ files:
79
78
  - ext/rfbstr.c
80
79
  - ext/rfbstr.h
81
80
  - ext/uncrustify.cfg
82
- - lib/Connection.rb
83
- - lib/ProcedureCall.rb
84
- - lib/SQLType.rb
85
81
  - lib/active_record/connection_adapters/rubyfb_adapter.rb
86
82
  - lib/arel/visitors/fb15/rubyfb.rb
87
83
  - lib/arel/visitors/rubyfb.rb
84
+ - lib/connection.rb
88
85
  - lib/mkdoc
86
+ - lib/procedure_call.rb
87
+ - lib/result_set.rb
88
+ - lib/row.rb
89
89
  - lib/rubyfb.rb
90
90
  - lib/rubyfb_lib.so
91
91
  - lib/rubyfb_options.rb
92
+ - lib/sql_type.rb
92
93
  - lib/src.rb
94
+ - lib/statement.rb
93
95
  - mswin32fb/fbclient_mingw.def
94
96
  - mswin32fb/fbclient_mingw.lib
95
97
  - mswin32fb/fbclient_ms.lib