Skip to content
This repository has been archived by the owner on May 10, 2020. It is now read-only.

Commit

Permalink
Make simple_* functions use packet writer, checking for packet size (#39
Browse files Browse the repository at this point in the history
)
  • Loading branch information
steffengy committed Jan 20, 2018
1 parent 5bd31ee commit 9fefc18
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
14 changes: 6 additions & 8 deletions tiberius/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,14 +858,7 @@ impl<I: BoxableIo + Sized + 'static> SqlConnection<I> {
S: Into<Cow<'a, str>>,
{
let sql = stmt.into();
let inner = &mut self.0;

let batch_packet = protocol::build_sql_batch(&mut inner.transport, &sql)?;
inner.transport.inner.queue_vec(batch_packet)?;

// attempt to send right now, if it works great, if not ready yet, it will be done later
// simply ensures that data is sent as fast as possible
let _ = inner.transport.inner.poll_complete()?;
protocol::write_sql_batch(&mut self.0.transport, &sql)?;
Ok(())
}

Expand Down Expand Up @@ -1219,6 +1212,11 @@ mod tests {
assert_eq!(row.get::<_, &str>(0), val.as_str());
Ok(())
});
let c1 = lp.run(future).unwrap();
let future = c1.simple_query(input.clone()).for_each_row(|row| {
assert_eq!(row.get::<_, &str>(0), val.as_str());
Ok(())
});
lp.run(future).unwrap();
}

Expand Down
28 changes: 12 additions & 16 deletions tiberius/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,27 +567,23 @@ pub fn write_trans_descriptor<W: Write>(mut wr: W, id: u64) -> io::Result<()> {
}

/// build an SQL batch packet
pub fn build_sql_batch<I: Io>(trans: &mut TdsTransport<I>, query: &str) -> io::Result<Vec<u8>> {
let vec = vec![0u8; HEADER_BYTES + ALL_HEADERS_LEN_TX as usize];
let mut cursor = Cursor::new(vec);
cursor.set_position(8);
pub fn write_sql_batch<I: Io>(trans: &mut TdsTransport<I>, query: &str) -> io::Result<()> {
let header = PacketHeader {
ty: PacketType::SQLBatch,
status: PacketStatus::NormalMessage,
..PacketHeader::new(0, 0)
};

write_trans_descriptor(&mut cursor, trans.transaction)?;
let mut writer = PacketWriter::new(&mut trans.inner, header);
write_trans_descriptor(&mut writer, trans.transaction)?;

// the SQL query (after ALL_HEADERS)
for byte in query.encode_utf16() {
cursor.write_u16::<LittleEndian>(byte)?;
writer.write_u16::<LittleEndian>(byte)?;
}
// build the packet header
let header = PacketHeader {
ty: PacketType::SQLBatch,
status: PacketStatus::EndOfMessage,
..PacketHeader::new(cursor.get_ref().len(), trans.next_id())
};
cursor.set_position(0);
let mut buf = cursor.into_inner();
header.serialize(&mut buf)?;
Ok(buf)

writer.finalize()?;
Ok(())
}

/// a writer that splits the written data across multiple packets
Expand Down

0 comments on commit 9fefc18

Please sign in to comment.