sqlx/sqlx-core/src/postgres/arguments.rs
Ryan Leckey 6b22fb7489 Refactor HasSqlType to provide an array of compatible types.
* Intending to use in a new Row type to check types at runtime for
   dynamic queries and to guard against schema changes

 * Hoping the query! macro can utilize this to allow accepting N
   rust types for 1 sql type and returning N rust types for 1 sql
   type.
2020-01-14 23:27:42 -08:00

62 lines
1.7 KiB
Rust

use byteorder::{ByteOrder, NetworkEndian};
use crate::arguments::Arguments;
use crate::encode::{Encode, IsNull};
use crate::io::BufMut;
use crate::types::HasSqlType;
use crate::Postgres;
#[derive(Default)]
pub struct PgArguments {
// OIDs of the bind parameters
pub(super) types: Vec<u32>,
// Write buffer for serializing bind values
pub(super) values: Vec<u8>,
}
impl Arguments for PgArguments {
type Database = super::Postgres;
fn len(&self) -> usize {
self.types.len()
}
fn size(&self) -> usize {
self.values.len()
}
fn reserve(&mut self, len: usize, size: usize) {
self.types.reserve(len);
self.values.reserve(size);
}
fn add<T>(&mut self, value: T)
where
Self::Database: HasSqlType<T>,
T: Encode<Self::Database>,
{
// TODO: When/if we receive types that do _not_ support BINARY, we need to check here
// TODO: There is no need to be explicit unless we are expecting mixed BINARY / TEXT
self.types
.push(<Postgres as HasSqlType<T>>::type_info().id.0);
let pos = self.values.len();
self.values.put_i32::<NetworkEndian>(0);
let len = if let IsNull::No = value.encode_nullable(&mut self.values) {
(self.values.len() - pos - 4) as i32
} else {
// Write a -1 for the len to indicate NULL
// TODO: It is illegal for [encode] to write any data
// if IsSql::No; fail a debug assertion
-1
};
// Write-back the len to the beginning of this frame (not including the len of len)
NetworkEndian::write_i32(&mut self.values[pos..], len as i32);
}
}