Skip to content

Instantly share code, notes, and snippets.

@mfpiccolo
Created February 11, 2016 17:43
Show Gist options
  • Save mfpiccolo/52de7277ed0c9bbc190a to your computer and use it in GitHub Desktop.
Save mfpiccolo/52de7277ed0c9bbc190a to your computer and use it in GitHub Desktop.
use sqlite::Sqlite;
use types::{self, FromSql, FromSqlRow, HasSqlType, Numeric, Float};
use std::error::Error;
use sqlite::connection::SqliteValue;
use std::str::FromStr;
use query_source::Queryable;
use row::Row;
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum SqliteNumeric {
Positive {
number: f64
},
Negative {
number: f64
},
NaN
}
impl FromSql<types::Numeric, Sqlite> for SqliteNumeric {
fn from_sql(bytes: Option<&SqliteValue>) -> Result<Self, Box<Error>> {
let bytes = not_none!(bytes);
let number = f64::from_str(bytes.read_text()).unwrap();
if number.is_sign_positive() {
Ok(SqliteNumeric::Positive {number: number})
} else if number.is_sign_negative() {
Ok(SqliteNumeric::Negative {number: number})
} else {
Ok(SqliteNumeric::NaN)
}
}
}
impl HasSqlType<Numeric> for Sqlite {
fn metadata() -> Self::TypeMetadata {
<Sqlite as HasSqlType<Float>>::metadata()
}
}
impl Queryable<Numeric, Sqlite> for SqliteNumeric {
type Row = Self;
fn build(row: Self) -> Self {
row
}
}
impl FromSqlRow<Numeric, Sqlite> for SqliteNumeric {
fn build_from_row<R: Row<Sqlite>>(row: &mut R) -> Result<Self, Box<Error>> {
FromSql::<Numeric, Sqlite>::from_sql(row.take())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment