Class: DB::MySQL::Native::Connection

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, io) ⇒ Connection

Returns a new instance of Connection.



97
98
99
100
101
102
# File 'lib/db/mysql/native/connection.rb', line 97

def initialize(address, io)
	super(address)
	
	@io = io
	@result = nil
end

Class Method Details

.connect(connection_string = "", io: ::IO) ⇒ Object



57
58
59
60
61
62
63
64
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
# File 'lib/db/mysql/native/connection.rb', line 57

def self.connect(connection_string = "", io: ::IO)
	pointer = Native.mysql_init(nil)
	Native.mysql_options(pointer, MYSQL_OPT_NONBLOCK, nil)
	
	uri = URI(connection_string)
	host = uri.host
	user = uri.user
	password = uri.password
	database = uri.path.gsub(/^\//, '')
	port = uri.port || 0
	unix_socket = nil
	client_flags = CLIENT_MULTI_STATEMENT | CLIENT_MULTI_RESULTS
	
	result = FFI::MemoryPointer.new(:pointer)
	
	status = Native.mysql_real_connect_start(result, pointer, host, user, password, database, port, unix_socket, client_flags);
	
	if status > 0
		io = io.new(Native.mysql_get_socket(pointer), "r+")
		
		while status > 0
			if status & MYSQL_WAIT_READ
				io.wait_readable
			elsif status & MYSQL_WAIT_WRITE
				io.wait_writable
			else
				io.wait_any
			end
			
			status = Native.mysql_real_connect_cont(result, pointer, status)
		end
	end
	
	if result.read_pointer.null?
		raise "Could not connect: #{Native.mysql_error(pointer)}!"
	end
	
	return self.new(pointer, io)
end

Instance Method Details

#check_error!(message) ⇒ Object



112
113
114
115
116
# File 'lib/db/mysql/native/connection.rb', line 112

def check_error!(message)
	if Native.mysql_errno(self) != 0
		raise "#{message}: #{Native.mysql_error(self)}!"
	end
end

#closeObject



130
131
132
133
134
135
136
# File 'lib/db/mysql/native/connection.rb', line 130

def close
	self.free_result
	
	Native.mysql_close(self)
	
	@io.close
end

#free_resultObject



122
123
124
125
126
127
128
# File 'lib/db/mysql/native/connection.rb', line 122

def free_result
	if @result
		Native.mysql_free_result(@result)
		
		@result = nil
	end
end

#next_resultObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/db/mysql/native/connection.rb', line 156

def next_result
	if @result
		self.free_result
		
		# Successful and there are no more results:
		return if Native.mysql_next_result(self) == -1
		
		check_error!("Next result")
	end
	
	@result = Native.mysql_use_result(self)
	
	if @result.null?
		check_error!("Next result")
		
		return nil
	else
		return Result.new(self, @result)
	end
end

#send_query(statement) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/db/mysql/native/connection.rb', line 138

def send_query(statement)
	self.free_result
	
	error = FFI::MemoryPointer.new(:int)
	
	status = Native.mysql_real_query_start(error, self, statement, statement.bytesize)
	
	while status != 0
		self.wait_for(status)
		
		status = Native.mysql_real_query_cont(error, self, status)
	end
	
	if error.read_int != 0
		raise "Could not send query: #{Native.mysql_error(self)}!"
	end
end

#statusObject



118
119
120
# File 'lib/db/mysql/native/connection.rb', line 118

def status
	Native.mysql_stat(self)
end

#wait_for(status) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/db/mysql/native/connection.rb', line 104

def wait_for(status)
	if status & MYSQL_WAIT_READ
		@io.wait_readable
	elsif status & MYSQL_WAIT_WRITE
		@io.wait_writable
	end
end