Support BOOL[]

This commit is contained in:
Steven Fackler 2013-12-07 15:39:44 -08:00
parent 302d782ae9
commit 9ce4df68f4
3 changed files with 34 additions and 11 deletions

View File

@ -258,6 +258,10 @@ types. The driver currently supports the following conversions:
<td>types::range::Range&lt;Timespec&gt;</td> <td>types::range::Range&lt;Timespec&gt;</td>
<td>TSRANGE, TSTZRANGE</td> <td>TSRANGE, TSTZRANGE</td>
</tr> </tr>
<tr>
<td>types::array::ArrayBase&lt;Option&lt;bool&gt;&gt;</td>
<td>BOOL[], BOOL[][], ...</td>
</tr>
<tr> <tr>
<td>types::array::ArrayBase&lt;Option&lt;i32&gt;&gt;</td> <td>types::array::ArrayBase&lt;Option&lt;i32&gt;&gt;</td>
<td>INT4[], INT4[][], ...</td> <td>INT4[], INT4[][], ...</td>

3
lib.rs
View File

@ -520,8 +520,7 @@ impl InnerPostgresConnection {
let mut param_types: ~[PostgresType] = match self.read_message() { let mut param_types: ~[PostgresType] = match self.read_message() {
ParameterDescription { types } => ParameterDescription { types } =>
types.iter().map(|ty| { PostgresType::from_oid(*ty) }) types.iter().map(|ty| PostgresType::from_oid(*ty)).collect(),
.collect(),
_ => unreachable!() _ => unreachable!()
}; };

View File

@ -33,6 +33,8 @@ static TEXTOID: Oid = 25;
static JSONOID: Oid = 114; static JSONOID: Oid = 114;
static FLOAT4OID: Oid = 700; static FLOAT4OID: Oid = 700;
static FLOAT8OID: Oid = 701; static FLOAT8OID: Oid = 701;
static BOOLARRAYOID: Oid = 1000;
static BYTEAARRAYOID: Oid = 1001;
static INT4ARRAYOID: Oid = 1007; static INT4ARRAYOID: Oid = 1007;
static INT8ARRAYOID: Oid = 1016; static INT8ARRAYOID: Oid = 1016;
static FLOAT4ARRAYOID: Oid = 1021; static FLOAT4ARRAYOID: Oid = 1021;
@ -82,6 +84,8 @@ pub enum PostgresType {
PgFloat4, PgFloat4,
/// FLOAT8/DOUBLE PRECISION /// FLOAT8/DOUBLE PRECISION
PgFloat8, PgFloat8,
/// BOOL[]
PgBoolArray,
/// INT4[] /// INT4[]
PgInt4Array, PgInt4Array,
/// INT8[] /// INT8[]
@ -131,6 +135,7 @@ impl PostgresType {
JSONOID => PgJson, JSONOID => PgJson,
FLOAT4OID => PgFloat4, FLOAT4OID => PgFloat4,
FLOAT8OID => PgFloat8, FLOAT8OID => PgFloat8,
BOOLARRAYOID => PgBoolArray,
INT4ARRAYOID => PgInt4Array, INT4ARRAYOID => PgInt4Array,
INT8ARRAYOID => PgInt8Array, INT8ARRAYOID => PgInt8Array,
FLOAT4ARRAYOID => PgFloat4Array, FLOAT4ARRAYOID => PgFloat4Array,
@ -202,6 +207,12 @@ macro_rules! raw_from_impl(
) )
) )
impl RawFromSql for bool {
fn raw_from_sql<R: Reader>(raw: &mut R) -> bool {
raw.read_u8() != 0
}
}
raw_from_impl!(i32, read_be_i32) raw_from_impl!(i32, read_be_i32)
raw_from_impl!(i64, read_be_i64) raw_from_impl!(i64, read_be_i64)
raw_from_impl!(f32, read_be_f32) raw_from_impl!(f32, read_be_f32)
@ -263,9 +274,8 @@ macro_rules! from_option_impl(
) )
) )
from_map_impl!(PgBool, bool, |buf| { buf[0] != 0 }) from_raw_from_impl!(PgBool, bool)
from_option_impl!(bool) from_option_impl!(bool)
from_raw_from_impl!(PgInt4, i32) from_raw_from_impl!(PgInt4, i32)
from_option_impl!(i32) from_option_impl!(i32)
from_raw_from_impl!(PgInt8, i64) from_raw_from_impl!(PgInt8, i64)
@ -385,6 +395,9 @@ macro_rules! from_array_impl(
) )
) )
from_array_impl!(PgBoolArray, bool)
from_option_impl!(ArrayBase<Option<bool>>)
from_array_impl!(PgInt4Array, i32) from_array_impl!(PgInt4Array, i32)
from_option_impl!(ArrayBase<Option<i32>>) from_option_impl!(ArrayBase<Option<i32>>)
@ -454,6 +467,16 @@ macro_rules! raw_to_impl(
) )
) )
impl RawToSql for bool {
fn raw_to_sql<W: Writer>(&self, w: &mut W) {
w.write_u8(*self as u8)
}
fn raw_size(&self) -> uint {
1
}
}
raw_to_impl!(i32, write_be_i32) raw_to_impl!(i32, write_be_i32)
raw_to_impl!(i64, write_be_i64) raw_to_impl!(i64, write_be_i64)
raw_to_impl!(f32, write_be_f32) raw_to_impl!(f32, write_be_f32)
@ -529,14 +552,8 @@ macro_rules! to_conversions_impl(
) )
) )
impl ToSql for bool { to_raw_to_impl!(PgBool, bool)
fn to_sql(&self, ty: &PostgresType) -> (Format, Option<~[u8]>) {
check_types!(PgBool, ty)
(Binary, Some(~[*self as u8]))
}
}
to_option_impl!(PgBool, bool) to_option_impl!(PgBool, bool)
to_raw_to_impl!(PgInt4, i32) to_raw_to_impl!(PgInt4, i32)
to_option_impl!(PgInt4, i32) to_option_impl!(PgInt4, i32)
to_raw_to_impl!(PgInt8, i64) to_raw_to_impl!(PgInt8, i64)
@ -695,6 +712,9 @@ macro_rules! to_array_impl(
) )
) )
to_array_impl!(PgBoolArray, BOOLOID, bool)
to_option_impl!(PgBoolArray, ArrayBase<Option<bool>>)
to_array_impl!(PgInt4Array, INT4OID, i32) to_array_impl!(PgInt4Array, INT4OID, i32)
to_option_impl!(PgInt4Array, ArrayBase<Option<i32>>) to_option_impl!(PgInt4Array, ArrayBase<Option<i32>>)