Skip to content

Commit

Permalink
feat: add table struct tab
Browse files Browse the repository at this point in the history
  • Loading branch information
invm authored and Michael Ionov committed Aug 22, 2023
1 parent 48b9534 commit f6d1207
Show file tree
Hide file tree
Showing 17 changed files with 521 additions and 216 deletions.
20 changes: 19 additions & 1 deletion src-tauri/Cargo.lock

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

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-wo
tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
sql_lexer = "0.9.4"
mysql = { version = "24.0.0", features = ["default"] }
futures = "0.3.28"

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
Expand Down
14 changes: 7 additions & 7 deletions src-tauri/src/database/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,55 +164,55 @@ impl ConnectedConnection {

pub async fn get_table_structure(&self, table_name: String) -> Result<Value> {
match &self.pool {
ConnectionPool::Mysql(pool) => engine::mysql::tables::get_table_structure(self, pool, table_name)
ConnectionPool::Mysql(pool) => engine::mysql::tables::get_table_structure(self, pool, table_name).await
// ConnectionPool::Postgres(_pool) => todo!(),
// ConnectionPool::Sqlite(_pool) => todo!(),
}
}

pub async fn get_indices(&self, table_name: Option<&str>) -> Result<Value> {
match &self.pool {
ConnectionPool::Mysql(pool) => engine::mysql::tables::get_indices(self, pool, table_name)
ConnectionPool::Mysql(pool) => engine::mysql::tables::get_indices(self, pool, table_name).await
// ConnectionPool::Postgres(_pool) => todo!(),
// ConnectionPool::Sqlite(_pool) => todo!(),
}
}

pub async fn get_columns(&self) -> Result<Value> {
match &self.pool {
ConnectionPool::Mysql(pool) => engine::mysql::tables::get_columns(self, pool, None)
ConnectionPool::Mysql(pool) => engine::mysql::tables::get_columns(self, pool, None).await
// ConnectionPool::Postgres(_pool) => todo!(),
// ConnectionPool::Sqlite(_pool) => todo!(),
}
}

pub async fn get_constraints(&self) -> Result<Value> {
match &self.pool {
ConnectionPool::Mysql(pool) => engine::mysql::tables::get_constraints(self, pool, None),
ConnectionPool::Mysql(pool) => engine::mysql::tables::get_constraints(self, pool, None).await,
// ConnectionPool::Postgres(_pool) => todo!(),
// ConnectionPool::Sqlite(_pool) => todo!(),
}
}

pub async fn get_functions(&self) -> Result<Value> {
match &self.pool {
ConnectionPool::Mysql(pool) => engine::mysql::tables::get_functions(self, pool),
ConnectionPool::Mysql(pool) => engine::mysql::tables::get_functions(self, pool).await,
// ConnectionPool::Postgres(_pool) => todo!(),
// ConnectionPool::Sqlite(_pool) => todo!(),
}
}

pub async fn get_procedures(&self) -> Result<Value> {
match &self.pool {
ConnectionPool::Mysql(pool) => engine::mysql::tables::get_procedures(self, pool),
ConnectionPool::Mysql(pool) => engine::mysql::tables::get_procedures(self, pool).await,
// ConnectionPool::Postgres(_pool) => todo!(),
// ConnectionPool::Sqlite(_pool) => todo!(),
}
}

pub async fn get_triggers(&self) -> Result<Value> {
match &self.pool {
ConnectionPool::Mysql(pool) => engine::mysql::tables::get_triggers(self, pool, None),
ConnectionPool::Mysql(pool) => engine::mysql::tables::get_triggers(self, pool, None).await,
// ConnectionPool::Postgres(_pool) => todo!(),
// ConnectionPool::Sqlite(_pool) => todo!(),
}
Expand Down
60 changes: 43 additions & 17 deletions src-tauri/src/database/engine/mysql/tables.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
use anyhow::Result;
use futures::try_join;
use mysql::Pool;
use serde_json::{json, Value};

use crate::database::connections::ConnectedConnection;

use super::query::raw_query;

pub fn get_table_structure(
pub async fn get_table_structure(
conn: &ConnectedConnection,
pool: &Pool,
table_name: String,
) -> Result<Value> {
let columns = get_columns(conn, pool, Some(&table_name))?;
let constraints = get_constraints(conn, pool, Some(&table_name))?;
let triggers = get_triggers(conn, pool, Some(&table_name))?;
let indices = get_indices(conn, pool, Some(&table_name))?;
let (columns, constraints, triggers, indices) = try_join!(
get_columns(conn, pool, Some(&table_name)),
get_constraints(conn, pool, Some(&table_name)),
get_triggers(conn, pool, Some(&table_name)),
get_indices(conn, pool, Some(&table_name)),
)?;

let result = json!({
"columns": columns,
"constraints": constraints,
"indices": indices,
"triggers": triggers,
"columns": columns["result"],
"constraints": constraints["result"],
"indices": indices["result"],
"triggers": triggers["result"],
});

Ok(result)
}

pub fn get_columns(
pub async fn get_columns(
conn: &ConnectedConnection,
pool: &Pool,
table_name: Option<&str>,
) -> Result<Value> {
let db_name = conn.config.get_db_name();
let mut _conn = pool.get_conn()?;
let query = format!(
"SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '{}'",
"SELECT
COLUMN_NAME,
COLUMN_TYPE,
DATA_TYPE,
CHARACTER_MAXIMUM_LENGTH,
IS_NULLABLE,
CHARACTER_OCTET_LENGTH,
CHARACTER_SET_NAME,
COLLATION_NAME,
COLUMN_COMMENT,
COLUMN_DEFAULT,
COLUMN_KEY
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '{}'",
db_name
);
let query = match table_name {
Expand All @@ -45,7 +60,7 @@ pub fn get_columns(
Ok(columns)
}

pub fn get_constraints(
pub async fn get_constraints(
conn: &ConnectedConnection,
pool: &Pool,
table_name: Option<&str>,
Expand All @@ -66,23 +81,27 @@ pub fn get_constraints(
Ok(columns)
}

pub fn get_functions(conn: &ConnectedConnection, pool: &Pool) -> Result<Value> {
pub async fn get_functions(conn: &ConnectedConnection, pool: &Pool) -> Result<Value> {
let db_name = conn.config.get_db_name();
let mut _conn = pool.get_conn()?;
let query = format!("SHOW FUNCTION STATUS WHERE DB = '{}';", db_name);
let functions = raw_query(_conn, query)?;
Ok(functions)
}

pub fn get_procedures(conn: &ConnectedConnection, pool: &Pool) -> Result<Value> {
pub async fn get_procedures(conn: &ConnectedConnection, pool: &Pool) -> Result<Value> {
let db_name = conn.config.get_db_name();
let mut _conn = pool.get_conn()?;
let query = format!("SHOW PROCEDURE STATUS WHERE DB = '{}';", db_name);
let procedures = raw_query(_conn, query)?;
Ok(procedures)
}

pub fn get_indices(conn: &ConnectedConnection, pool: &Pool, table_name: Option<&str>) -> Result<Value> {
pub async fn get_indices(
conn: &ConnectedConnection,
pool: &Pool,
table_name: Option<&str>,
) -> Result<Value> {
let db_name = conn.config.get_db_name();
let mut _conn = pool.get_conn()?;
let query = format!(
Expand All @@ -98,10 +117,17 @@ pub fn get_indices(conn: &ConnectedConnection, pool: &Pool, table_name: Option<&
Ok(procedures)
}

pub fn get_triggers(conn: &ConnectedConnection, pool: &Pool, table_name: Option<&str>) -> Result<Value> {
pub async fn get_triggers(
conn: &ConnectedConnection,
pool: &Pool,
table_name: Option<&str>,
) -> Result<Value> {
let mut _conn = pool.get_conn()?;
let db_name = conn.config.get_db_name();
let query = format!("SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE EVENT_OBJECT_SCHEMA = '{}'", db_name);
let query = format!(
"SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE EVENT_OBJECT_SCHEMA = '{}'",
db_name
);
let query = match table_name {
Some(table_name) => format!("{} AND EVENT_OBJECT_TABLE = '{}';", query, table_name),
None => format!("{};", query),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const QueryTextArea = () => {
const onInput = (q: string) => {
updateQueryText(q);
setCode(q);
updateStore();
};

const [code, setCode] = createSignal("");
Expand All @@ -69,7 +70,6 @@ export const QueryTextArea = () => {
const onFormat = () => {
const formatted = format(code());
onInput(formatted);
updateStore();
};

const onExecute = async () => {
Expand All @@ -82,9 +82,8 @@ export const QueryTextArea = () => {
});
setActiveContentQueryTabData({ query: code(), results: result });
} catch (error) {
setActiveContentQueryTabMessage("error", error as string);
setActiveContentQueryTabMessage("error", error);
}
updateStore();
};

createEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export const Table = (props: { data: Record<string, any>[] }) => {
console.log(props.data);
const columns = Object.keys(props.data[0]);
const rows = props.data.map((row) => Object.values(row));
return (
<div class="overflow-x-auto">
<table class="table">
<thead>
<tr>
<th></th>
{columns.map((column) => (
<th>{column}</th>
))}
</tr>
</thead>
<tbody>
{rows.map((row, idx) => (
<tr>
<th>{idx + 1}</th>
{row.map((cell) => (
<td>{cell}</td>
))}
</tr>
))}
</tbody>
</table>
</div>
);
};
Loading

0 comments on commit f6d1207

Please sign in to comment.