Skip to content

Commit

Permalink
upgrade rxqlite to make it usable as a casbin adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Apr 26, 2024
1 parent 4f6a04f commit 6967682
Show file tree
Hide file tree
Showing 8 changed files with 392 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rxqlite"
version = "0.1.21"
version = "0.1.22"
readme = "README.md"
edition = "2021"
authors = [
Expand Down
3 changes: 2 additions & 1 deletion crates/rxqlite-common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rxqlite-common"
version = "0.1.3"
version = "0.1.4"


edition = "2021"
Expand All @@ -18,4 +18,5 @@ repository = "https://github.com/HaHa421/rxqlite"
[dependencies]
serde = { version = "1" , features = [ "derive" ] }
chrono = { version = "0.4" , features = [ "serde" ] }
either = { version = "1.11" , features = [ "serde" ] }

129 changes: 125 additions & 4 deletions crates/rxqlite-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
pub use either::Either;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Value {
Expand Down Expand Up @@ -184,10 +185,46 @@ impl FromValueRef for DateTime<Utc> {
}
}

pub type Col = Value;
#[derive(Serialize, Deserialize, Debug)]
pub struct Col {
pub value: Value,
pub ordinal: u64,
}

impl Col {
pub fn new(value: Value, ordinal: u64) -> Self {
Self {
value,
ordinal,
}
}
}

impl From<(Value,u64)> for Col {
fn from((value,ordinal): (Value,u64))->Self {
Self {
value,
ordinal,
}
}
}

impl std::ops::Deref for Col {
type Target = Value;
fn deref(&self) -> &Self::Target {
&self.value
}
}

impl std::ops::DerefMut for Col {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.value
}
}

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Row {
pub columns: Vec<Column>,
pub inner: Vec<Col>,
}

Expand All @@ -209,20 +246,101 @@ impl Row {
T::from_value_ref(&self[idx])
}
}
impl From<Vec<Value>> for Row {
fn from(inner: Vec<Value>) -> Self {
Self { inner }

impl From<(Vec<Column>,Vec<Col>)> for Row {
fn from((columns,inner): (Vec<Column>,Vec<Col>)) -> Self {
Self { columns, inner }
}
}


pub type Rows = Vec<Row>;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum TypeInfo {
Null,
Int,
Float,
Text,
Blob,
Numeric,
Bool,
Int64,
Date,
Time,
DateTime,
}
/*
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TypeInfo {
pub is_null: bool,
pub name: String,
}
*/
impl TypeInfo {
pub fn is_null(&self) -> bool {
match self {
TypeInfo::Null=>true,
_=>false,
}
}
pub fn name(&self) -> &'static str {
match self {
Self::Null => "NULL",
Self::Int => "INT",
Self::Float => "FLOAT",
Self::Text => "FLOAT",
Self::Blob => "BLOB",
Self::Numeric => "NUMERIC",
Self::Bool => "BOOL",
Self::Int64 => "INTEGER",
Self::Date => "DATE",
Self::Time => "TIME",
Self::DateTime => "DATETIME",
}
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Column {
pub ordinal: usize,
pub name: String,
pub type_info: TypeInfo,
}
/*
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Rows {
pub description: ColumnsDescription,
pub inner: Vec<Row>,
}
impl std::ops::Deref for Rows {
type Target = Vec<Row>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl std::ops::DerefMut for Rows {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
*/
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct QueryResult {
pub last_insert_rowid: i64,
pub changes: u64,
}


#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Message {
Execute(String, Vec<Value>),
Fetch(String, Vec<Value>),
FetchOne(String, Vec<Value>),
FetchOptional(String, Vec<Value>),
FetchMany(String, Vec<Value>),
}

impl Message {
Expand All @@ -232,13 +350,16 @@ impl Message {
Self::Fetch(s, _) => s.as_str(),
Self::FetchOne(s, _) => s.as_str(),
Self::FetchOptional(s, _) => s.as_str(),
Self::FetchMany(s, _) => s.as_str(),
}
}
}

#[derive(Serialize, Deserialize, Debug)]
pub enum MessageResponse {
Rows(Rows),
QueryResult(QueryResult),
QueryResultsAndRows(Vec<Result<Either<QueryResult,Row>,String>>),
Error(String),
}

Expand Down
3 changes: 2 additions & 1 deletion crates/rxqlite-sqlx-common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rxqlite-sqlx-common"
version = "0.1.6"
version = "0.1.7"

edition = "2021"
authors = [
Expand All @@ -19,6 +19,7 @@ rxqlite-common = { version = "0.1.3" , path = "../rxqlite-common" }
sqlparser= "0.44"
anyhow = "1"
tracing = "0.1"
futures-util = "0.3"

[dependencies.sqlx]
version = "0.7"
Expand Down
Loading

0 comments on commit 6967682

Please sign in to comment.