Class: Drizzle::Connection
- Inherits:
-
Object
- Object
- Drizzle::Connection
- Defined in:
- lib/drizzle/connection.rb
Overview
A drizzle connection
Instance Attribute Summary collapse
-
#db ⇒ Object
Returns the value of attribute db.
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
Instance Method Summary collapse
- #check_return_code ⇒ Object
-
#initialize(host = "localhost", port = 4427, db = nil, opts = [], drizzle_ptr = nil) ⇒ Connection
constructor
Creates a connection instance.
-
#query(query) ⇒ Object
execute a query and construct a result object.
-
#randomize_query(query) ⇒ Object
tokenize the input query and append the randomization key to keywords.
-
#set_db(db_name) ⇒ Object
set the database name for the connection.
-
#set_tcp(host, port) ⇒ Object
set the host and port for the connection.
Constructor Details
#initialize(host = "localhost", port = 4427, db = nil, opts = [], drizzle_ptr = nil) ⇒ Connection
Creates a connection instance
== parameters
* host the hostname for the connection
* port the port number for the connection
* db the database name for the connection
* opts connection options
* drizzle_ptr FFI pointer to a drizzle_st object
Some examples :
c = Drizzle::Connection.new
c = Drizzle::Connection.new("my_host", 4427)
c = Drizzle::Connection.new("my_host", 4427, "my_db")
c = Drizzle::Connection.new("my_host", 4427, "my_db", [Drizzle::NONE])
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 109 110 111 |
# File 'lib/drizzle/connection.rb', line 78 def initialize(host = "localhost", port = 4427, db = nil, opts = [], drizzle_ptr = nil) @host = host @port = port @db = db @drizzle_handle = drizzle_ptr || DrizzlePtr.new(LibDrizzle.drizzle_create(nil)) @con_ptr = ConnectionPtr.new(LibDrizzle.drizzle_con_create(@drizzle_handle, nil)) @rand_key = "" opts.each do |opt| if opt == INJECTION_PREVENTION @randomize_queries = true next end LibDrizzle.(@con_ptr, LibDrizzle::ConnectionOptions[opt]) end LibDrizzle.drizzle_con_set_tcp(@con_ptr, @host, @port) LibDrizzle.drizzle_con_set_db(@con_ptr, @db) if @db @ret_ptr = FFI::MemoryPointer.new(:int) # # if SQL injection prevention is enabled, we need to retrieve # the key to use for randomization from the server # if @randomize_queries == true query = "show variables like '%stad_key%'" res = LibDrizzle.drizzle_query_str(@con_ptr, nil, query, @ret_ptr) check_return_code result = Result.new(res) result.buffer_result result.each do |row| @rand_key = row[1] end end end |
Instance Attribute Details
#db ⇒ Object
Returns the value of attribute db.
58 59 60 |
# File 'lib/drizzle/connection.rb', line 58 def db @db end |
#host ⇒ Object
Returns the value of attribute host.
58 59 60 |
# File 'lib/drizzle/connection.rb', line 58 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
58 59 60 |
# File 'lib/drizzle/connection.rb', line 58 def port @port end |
Instance Method Details
#check_return_code ⇒ 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 |
# File 'lib/drizzle/connection.rb', line 158 def check_return_code() case LibDrizzle::ReturnCode[@ret_ptr.get_int(0)] when :DRIZZLE_RETURN_IO_WAIT raise IoWait.new(LibDrizzle.drizzle_error(@drizzle_handle)) when :DRIZZLE_RETURN_PAUSE raise Pause.new(LibDrizzle.drizzle_error(@drizzle_handle)) when :DRIZZLE_RETURN_ROW_BREAK raise RowBreak.new(LibDrizzle.drizzle_error(@drizzle_handle)) when :DRIZZLE_RETURN_MEMORY raise Memory.new(LibDrizzle.drizzle_error(@drizzle_handle)) when :DRIZZLE_RETURN_INTERNAL_ERROR raise InternalError.new(LibDrizzle.drizzle_error(@drizzle_handle)) when :DRIZZLE_RETURN_NOT_READY raise NotReady.new(LibDrizzle.drizzle_error(@drizzle_handle)) when :DRIZZLE_RETURN_BAD_PACKET_NUMBER raise BadPacketNumber.new(LibDrizzle.drizzle_error(@drizzle_handle)) when :DRIZZLE_RETURN_BAD_HANDSHAKE_PACKET raise BadHandshake.new(LibDrizzle.drizzle_error(@drizzle_handle)) when :DRIZZLE_RETURN_BAD_PACKET raise BadPacket.new(LibDrizzle.drizzle_error(@drizzle_handle)) when :DRIZZLE_RETURN_PROTOCOL_NOT_SUPPORTED raise ProtocolNotSupported.new(LibDrizzle.drizzle_error(@drizzle_handle)) when :DRIZZLE_RETURN_UNEXPECTED_DATA raise UnexpectedData.new(LibDrizzle.drizzle_error(@drizzle_handle)) when :DRIZZLE_RETURN_NO_SCRAMBLE raise NoScramble.new(LibDrizzle.drizzle_error(@drizzle_handle)) when :DRIZZLE_RETURN_AUTH_FAILED raise AuthFailed.new(LibDrizzle.drizzle_error(@drizzle_handle)) when :DRIZZLE_RETURN_NULL_SIZE raise NullSize.new(LibDrizzle.drizzle_error(@drizzle_handle)) when :DRIZZLE_RETURN_TOO_MANY_COLUMNS raise TooManyColumns.new(LibDrizzle.drizzle_error(@drizzle_handle)) when :DRIZZLE_RETURN_ROW_END raise RowEnd.new(LibDrizzle.drizzle_error(@drizzle_handle)) when :DRIZZLE_RETURN_LOST_CONNECTION raise LostConnection.new(LibDrizzle.drizzle_error(@drizzle_handle)) when :DRIZZLE_RETURN_COULD_NOT_CONNECT raise CouldNotConnect.new(LibDrizzle.drizzle_error(@drizzle_handle)) when :DRIZZLE_RETURN_NO_ACTIVE_CONNECTIONS raise NoActiveConnections.new(LibDrizzle.drizzle_error(@drizzle_handle)) when :DRIZZLE_RETURN_HANDSHAKE_FAILED raise HandshakeFailed.new(LibDrizzle.drizzle_error(@drizzle_handle)) when :DRIZZLE_RETURN_TIMEOUT raise ReturnTimeout.new(LibDrizzle.drizzle_error(@drizzle_handle)) end end |
#query(query) ⇒ Object
execute a query and construct a result object
133 134 135 136 137 |
# File 'lib/drizzle/connection.rb', line 133 def query(query) res = LibDrizzle.drizzle_query_str(@con_ptr, nil, query, @ret_ptr) check_return_code Result.new(res) end |
#randomize_query(query) ⇒ Object
tokenize the input query and append the randomization key to keywords
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/drizzle/connection.rb', line 143 def randomize_query(query) if @rand_key.empty? return query end toks = query.split(" ") new_query = "" toks.each do |token| if Keywords[token.downcase] == true token << @rand_key end new_query << token << " " end new_query end |
#set_db(db_name) ⇒ Object
set the database name for the connection
125 126 127 128 |
# File 'lib/drizzle/connection.rb', line 125 def set_db(db_name) @db = db_name LibDrizzle.drizzle_con_set_db(@con_ptr, @db) end |
#set_tcp(host, port) ⇒ Object
set the host and port for the connection
116 117 118 119 120 |
# File 'lib/drizzle/connection.rb', line 116 def set_tcp(host, port) @host = host @port = port LibDrizzle.drizzle_con_set_tcp(@con_ptr, @host, @port) end |