Skip to content

Commit

Permalink
Fix compile-time compatibility with other database drivers, resolves #19
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlan authored and wonderix committed May 13, 2024
1 parent c348e31 commit 4c4a177
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
20 changes: 12 additions & 8 deletions src/tds/prepared_statement.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,36 @@ class TDS::PreparedStatement < DB::Statement
end
end
raise DB::Error.new("Too many arguments specified for statement: #{command}") if index != arguments.size - 1
Parameter.new(connection.sp_prepare(params.join(","), cmd))
Parameter.new(conn.sp_prepare(params.join(","), cmd))
}
[handle] + arguments
end

protected def perform_query(args : Enumerable) : DB::ResultSet
parameters = ensure_prepared(args)
connection.send(PacketIO::Type::RPC) do |io|
conn.send(PacketIO::Type::RPC) do |io|
RpcRequest.new(id: RpcRequest::Type::EXECUTE, parameters: parameters).write(io)
end
result = nil
connection.recv(PacketIO::Type::REPLY) do |io|
conn.recv(PacketIO::Type::REPLY) do |io|
result = ResultSet.new(self, Token.each(io))
end
result.not_nil!
rescue ex : IO::Error
raise DB::ConnectionLost.new(connection, ex)
raise DB::ConnectionLost.new(conn, ex)
end

protected def perform_exec(args : Enumerable) : DB::ExecResult
parameters = ensure_prepared(args)
connection.send(PacketIO::Type::RPC) do |io|
conn.send(PacketIO::Type::RPC) do |io|
RpcRequest.new(id: RpcRequest::Type::EXECUTE, parameters: parameters).write(io)
end
connection.recv(PacketIO::Type::REPLY) do |io|
conn.recv(PacketIO::Type::REPLY) do |io|
Token.each(io) { |t| }
end
DB::ExecResult.new 0, 0
rescue ex : IO::Error
raise DB::ConnectionLost.new(connection, ex)
raise DB::ConnectionLost.new(conn, ex)
rescue ex
raise DB::Error.new("#{ex.to_s} in \"#{command}\"", ex)
end
Expand All @@ -67,11 +67,15 @@ class TDS::PreparedStatement < DB::Statement
super
@handles.each_value do |handle|
begin
connection.sp_unprepare handle.value.as(Int32)
conn.sp_unprepare handle.value.as(Int32)
rescue ex : DB::Error
# ignore errors when unpreparing to not affect the connection being closed
end
end
@handles.clear
end

protected def conn
@connection.as(Connection)
end
end
16 changes: 10 additions & 6 deletions src/tds/unprepared_statement.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,29 @@ class TDS::UnpreparedStatement < DB::Statement
end

protected def perform_query(args : Enumerable) : DB::ResultSet
connection.send(PacketIO::Type::QUERY) do |io|
conn.send(PacketIO::Type::QUERY) do |io|
UTF16_IO.write(io, expanded_command(args), ENCODING)
end
result = nil
connection.recv(PacketIO::Type::REPLY) do |io|
conn.recv(PacketIO::Type::REPLY) do |io|
result = ResultSet.new(self, Token.each(io))
end
result.not_nil!
rescue ex : IO::Error
raise DB::ConnectionLost.new(connection, ex)
raise DB::ConnectionLost.new(conn, ex)
end

protected def perform_exec(args : Enumerable) : DB::ExecResult
statement = expanded_command(args)
connection.send(PacketIO::Type::QUERY) do |io|
conn.send(PacketIO::Type::QUERY) do |io|
UTF16_IO.write(io, statement, ENCODING)
end
connection.recv(PacketIO::Type::REPLY) do |io|
conn.recv(PacketIO::Type::REPLY) do |io|
Token.each(io) { |t| }
end
DB::ExecResult.new 0, 0
rescue ex : IO::Error
raise DB::ConnectionLost.new(connection, ex)
raise DB::ConnectionLost.new(conn, ex)
rescue ex
raise StatementError.new(ex, statement.to_s)
end
Expand All @@ -68,4 +68,8 @@ class TDS::UnpreparedStatement < DB::Statement
protected def self.encode(value : Nil)
"NULL"
end

protected def conn
@connection.as(Connection)
end
end

0 comments on commit 4c4a177

Please sign in to comment.