Class: DB::MariaDB::Native::Result

Inherits:
FFI::Pointer
  • Object
show all
Defined in:
lib/db/mariadb/native/result.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection, types = {}, address) ⇒ Result

Returns a new instance of Result.



20
21
22
23
24
25
26
27
# File 'lib/db/mariadb/native/result.rb', line 20

def initialize(connection, types = {}, address)
	super(address)
	
	@connection = connection
	@fields = nil
	@types = types
	@casts = nil
end

Instance Method Details

#cast!(row) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/db/mariadb/native/result.rb', line 61

def cast!(row)
	@casts ||= self.field_types
	
	row.size.times do |index|
		if cast = @casts[index]
			row[index] = cast.parse(row[index])
		end
	end
	
	return row
end

#eachObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/db/mariadb/native/result.rb', line 73

def each
	row = FFI::MemoryPointer.new(:pointer)
	field_count = self.field_count
	
	while true
		status = Native.mysql_fetch_row_start(row, self)
		
		while status != 0
			@connection.wait_for(status)
			
			status = Native.mysql_fetch_row_cont(row, self, status)
		end
		
		pointer = row.read_pointer
		
		if pointer.null?
			break
		else
			yield cast!(pointer.get_array_of_string(0, field_count))
		end
	end
	
	@connection.check_error!("Reading recordset")
end

#field_countObject



29
30
31
# File 'lib/db/mariadb/native/result.rb', line 29

def field_count
	Native.mysql_num_fields(self)
end

#field_namesObject Also known as: keys



45
46
47
# File 'lib/db/mariadb/native/result.rb', line 45

def field_names
	fields.map(&:name)
end

#field_typesObject



49
50
51
# File 'lib/db/mariadb/native/result.rb', line 49

def field_types
	fields.map{|field| @types[field.type]}
end

#fieldsObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/db/mariadb/native/result.rb', line 33

def fields
	unless @fields
		pointer = Native.mysql_fetch_fields(self)
		
		@fields = field_count.times.map do |index|
			Field.new(pointer +  index * Field.size)
		end
	end
	
	return @fields
end

#map(&block) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/db/mariadb/native/result.rb', line 98

def map(&block)
	results = []
	
	self.each do |row|
		results << yield(row)
	end
	
	return results
end

#row_countObject Also known as: count

In the context of unbuffered queries, this is the number of rows that have been fetched so far.



54
55
56
# File 'lib/db/mariadb/native/result.rb', line 54

def row_count
	Native.mysql_num_rows(self)
end

#to_aObject



108
109
110
111
112
113
114
115
116
# File 'lib/db/mariadb/native/result.rb', line 108

def to_a
	rows = []
	
	self.each do |row|
		rows << row
	end
	
	return rows
end