Skip to content

Commit

Permalink
feat: change id to uuid, fix handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ionov committed Jun 27, 2023
1 parent 8ab6895 commit ba07739
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 100 deletions.
1 change: 1 addition & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ tauri-build = { version = "1.4", features = [] }
tauri = { version = "1.4", features = ["shell-open"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
rusqlite = { version = "0.29.0", features = ["bundled"] }
rusqlite = { version = "0.29.0", features = ["bundled", "uuid"] }
simplecrypt = "1.0.2"
anyhow = "1.0.71"
log = "0.4.19"
Expand Down
171 changes: 102 additions & 69 deletions src-tauri/src/database/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,94 +100,127 @@ impl ConnectionConfig {
}

pub fn add_connection(db: &Connection, conn: &ConnectionConfig) -> Result<()> {
// let mut statement = db.prepare("INSERT INTO connections (connection_name, color, credentials, default_db, save_password, metadata) VALUES (:connection_name, :color, :credentials, :default_db, :save_password, :metadata)")?;
// let credentials = serde_json::to_string(&conn.credentials)?;
// let metadata = serde_json::to_string(&conn.metadata)?;
// statement.execute(named_params! {
// ":connection_name": conn.connection_name,
// ":color": conn.color,
// ":credentials": credentials,
// ":default_db": conn.default_db,
// ":save_password": conn.save_password,
// ":metadata": metadata,
// })?;
let mut statement = db.prepare(
"INSERT INTO connections (
id,
scheme,
connection_name,
color,
default_db,
save_password,
metadata
) VALUES (
:id,
:connection_name,
:scheme,
:color,
:default_db,
:save_password,
:metadata
)",
)?;
let scheme = serde_json::to_string(&conn.scheme)?;
let metadata = serde_json::to_string(&conn.metadata)?;
statement.execute(named_params! {
":id": conn.id,
":connection_name": conn.connection_name,
":scheme": scheme,
":color": conn.color,
":default_db": conn.default_db,
":save_password": conn.save_password,
":metadata": metadata,
})?;

Ok(())
}

pub fn update_connection(db: &Connection, conn: &ConnectionConfig) -> Result<()> {
// let mut statement = db.prepare("INSERT INTO connections (connection_name, color, credentials, default_db, save_password, metadata) VALUES (:connection_name, :color, :credentials, :default_db, :save_password, :metadata) where id = :id")?;
// let credentials = serde_json::to_string(&conn.credentials)?;
// let metadata = serde_json::to_string(&conn.metadata)?;
// statement.execute(named_params! {
// ":connection_name": conn.connection_name,
// ":color": conn.color,
// ":credentials": credentials,
// ":default_db": conn.default_db,
// ":save_password": conn.save_password,
// ":metadata": metadata,
// ":id": conn.id,
// })?;
let mut statement = db.prepare(
"INSERT INTO connections (
connection_name,
color,
credentials,
default_db,
save_password,
metadata
) VALUES (
:connection_name,
:color,
:credentials,
:default_db,
:save_password,
:metadata
) where id = :id",
)?;
let scheme = serde_json::to_string(&conn.scheme)?;
let metadata = serde_json::to_string(&conn.metadata)?;
statement.execute(named_params! {
":connection_name": conn.connection_name,
":color": conn.color,
":scheme": scheme,
":default_db": conn.default_db,
":save_password": conn.save_password,
":metadata": metadata,
":id": conn.id,
})?;

Ok(())
}

// pub fn delete_connection(db: &Connection, conn: &DBConnection) -> Result<()> {
// let mut statement = db.prepare("DELETE FROM connections where id = :id")?;
// statement.execute(named_params! {":id": conn.id})?;
//
// Ok(())
// }
pub fn delete_connection(db: &Connection, id: &Uuid) -> Result<()> {
let mut statement = db.prepare("DELETE FROM connections where id = :id")?;
statement.execute(named_params! {":id": id})?;
Ok(())
}

pub fn get_all_connections(
db: &Connection,
limit: usize,
offset: usize,
) -> Result<Vec<ConnectionConfig>> {
// let mut statement = db.prepare("SELECT * FROM connections LIMIT ? OFFSET ?")?;
// let mut rows = statement.query([limit, offset])?;
// let mut items = Vec::new();
// while let Some(row) = rows.next()? {
// let credentials: String = row.get("credentials")?;
// let credentials: HostCredentials = serde_json::from_str(&credentials)?;
// let metadata: String = row.get("metadata")?;
// let metadata: Option<String> = serde_json::from_str(&metadata).ok();
//
// items.push(DBConnection {
// id: row.get("id")?,
// connection_name: row.get("name")?,
// color: row.get("color")?,
// credentials,
// default_db: row.get("default_db")?,
// save_password: row.get("save_password")?,
// metadata,
// });
// }
let mut statement = db.prepare("SELECT * FROM connections LIMIT ? OFFSET ?")?;
let mut rows = statement.query([limit, offset])?;
let mut items = Vec::new();
while let Some(row) = rows.next()? {
let scheme: String = row.get("scheme")?;
let scheme: Scheme = serde_json::from_str(&scheme)?;
let metadata: String = row.get("metadata")?;
let metadata: Option<String> = serde_json::from_str(&metadata).ok();

items.push(ConnectionConfig {
id: row.get("id")?,
connection_name: row.get("name")?,
color: row.get("color")?,
scheme,
default_db: row.get("default_db")?,
save_password: row.get("save_password")?,
metadata,
});
}

Ok(vec![])
}

// pub fn get_connection_by_id(db: &Connection, id: u32) -> Result<DBConnection> {
// let mut statement = db.prepare("SELECT * FROM connections WHERE id = ?")?;
// let mut rows = statement.query([id])?;
// let row = rows.next()?;
// let row = row.ok_or(anyhow::anyhow!("No connection found"))?;
// let credentials: String = row.get("credentials")?;
// let credentials: HostCredentials = serde_json::from_str(&credentials)?;
// let metadata: String = row.get("metadata")?;
// let metadata: Option<String> = serde_json::from_str(&metadata).ok();
//
// return Ok(DBConnection {
// id: row.get("id")?,
// connection_name: row.get("name")?,
// color: row.get("color")?,
// credentials,
// default_db: row.get("default_db")?,
// save_password: row.get("save_password")?,
// metadata,
// });
// return Ok(())
// }
pub fn get_connection_by_id(db: &Connection, id: Uuid) -> Result<ConnectionConfig> {
let mut statement = db.prepare("SELECT * FROM connections WHERE id = ?")?;
let mut rows = statement.query([id])?;
let row = rows.next()?;
let row = row.ok_or(anyhow::anyhow!("No connection found"))?;
let scheme: String = row.get("scheme")?;
let scheme: Scheme = serde_json::from_str(&scheme)?;
let metadata: String = row.get("metadata")?;
let metadata: Option<String> = serde_json::from_str(&metadata).ok();

return Ok(ConnectionConfig {
id: row.get("id")?,
connection_name: row.get("name")?,
color: row.get("color")?,
scheme,
default_db: row.get("default_db")?,
save_password: row.get("save_password")?,
metadata,
});
}

#[cfg(test)]
mod test {
Expand Down
4 changes: 2 additions & 2 deletions src-tauri/src/database/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ pub fn create_app_db(app_path: &str) -> Result<()> {
db.execute(
"create table `connections` (
`id` integer not null primary key autoincrement,
`name` varchar(255) not null,
`scheme` TEXT not null,
`connection_name` varchar(255) not null,
`color` varchar(255) not null,
`credentials` TEXT not null,
`default_db` VARCHAR(255) not null,
`save_password` TINYINT not null,
`metadata` TEXT null
Expand Down
56 changes: 28 additions & 28 deletions src-tauri/src/handlers/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,38 @@ pub fn add_connection(
save_password: bool,
color: &str,
) -> CommandResult<()> {
// let conn: DBConnection = conn.try_into()?;
let scheme = Scheme::try_from(scheme.as_str())?;
let conn = ConnectionConfig::new(name, scheme, save_password, color)?;
return app_handle
.db(|db| connections::add_connection(db, &conn))
.map_err(Error::from);
}
//
// #[command]
// pub fn update_connection(app_handle: AppHandle, conn: DBConnection) -> CommandResult<()> {
// return app_handle
// .db(|db| queries::update_connection(db, &conn))
// .map_err(Error::from);
// }
//
// #[command]
// pub fn delete_connection(app_handle: AppHandle, conn: DBConnection) -> CommandResult<()> {
// return app_handle
// .db(|db| queries::delete_connection(db, &conn))
// .map_err(Error::from);
// }
//
// #[command]
// pub fn get_all_connections(app_handle: AppHandle) -> CommandResult<Vec<DBConnection>> {
// return app_handle
// .db(|db| queries::get_all_connections(db, 10, 0))
// .map_err(Error::from);
// }

// #[command]
// pub fn get_connection_by_id(app_handle: AppHandle, id: u32) -> CommandResult<DBConnection> {
// return app_handle
// .db(|db| queries::get_connection_by_id(db, id))
// .map_err(Error::from);
// }
#[command]
pub fn update_connection(app_handle: AppHandle, conn: ConnectionConfig) -> CommandResult<()> {
return app_handle
.db(|db| connections::update_connection(db, &conn))
.map_err(Error::from);
}

pub fn delete_connection(app_handle: AppHandle, id: String) -> CommandResult<()> {
let id = uuid::Uuid::parse_str(id.as_str()).map_err(Error::from)?;
return app_handle
.db(|db| connections::delete_connection(db, &id))
.map_err(Error::from);
}

#[command]
pub fn get_all_connections(app_handle: AppHandle) -> CommandResult<Vec<ConnectionConfig>> {
return app_handle
.db(|db| connections::get_all_connections(db, 10, 0))
.map_err(Error::from);
}

#[command]
pub fn get_connection_by_id(app_handle: AppHandle, id: String) -> CommandResult<ConnectionConfig> {
let id = uuid::Uuid::parse_str(id.as_str()).map_err(Error::from)?;
return app_handle
.db(|db| connections::get_connection_by_id(db, id))
.map_err(Error::from);
}
2 changes: 2 additions & 0 deletions src-tauri/src/utils/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub enum Error {
Utf8(#[from] std::string::FromUtf8Error),
#[error("General error")]
General(#[from] anyhow::Error),
#[error("Uuid parse error")]
UUIDError(#[from] uuid::Error),
}

// we must also implement serde::Serialize
Expand Down

0 comments on commit ba07739

Please sign in to comment.