Fixed several features

This commit is contained in:
Johannes Randerath
2024-08-28 19:49:12 +02:00
parent c06ddc9498
commit 45b34f5fc8
13 changed files with 650 additions and 452 deletions

View File

@@ -17,5 +17,5 @@ fn main() {
.expect("Usage: add_column <tblid> <name> <uid>.");
let conn = &mut establish_connection();
println!("{}", add_column(conn, table_id, name, uid).expect("Error"));
println!("{}", add_column(conn, table_id, name, crate::FIELDTYPE::TEXT, uid).expect("Error"));
}

View File

@@ -34,5 +34,5 @@ fn main() {
}
println!("{}", create_table(conn, name.to_string(), fields, uid).unwrap());
println!("{}", create_table(conn, name.to_string(), fields.clone(), vec![crate::FIELDTYPE::TEXT; fields.len()], uid).unwrap());
}

View File

@@ -24,7 +24,7 @@ pub fn create_jrcell_relative(conn: &mut MysqlConnection, tblid: i32, rowpos: i3
create_jrcell(conn, ntrid, clmid, &value)
}
pub fn change_jrcell_value_relative(conn: &mut MysqlConnection, tblid: i32, rowpos: i32, idintbl: i32, new_value: String) -> Result<usize, diesel::result::Error> {
pub fn change_jrcell_value_relative(conn: &mut MysqlConnection, tblid: i32, rowpos: i32, idintbl: i32, new_value: &String) -> Result<usize, diesel::result::Error> {
let ntrid = jrentries::get_jrentry_id(conn, tblid, rowpos);
if ntrid.is_err() {
return Err(ntrid.err().unwrap());
@@ -78,7 +78,7 @@ pub fn create_jrcell(conn: &mut MysqlConnection, entryid: i32, columnid: i32, va
}
pub fn change_jrcell_value(conn: &mut MysqlConnection, entryid: i32, columnid: i32, new_value: String) -> Result<usize, diesel::result::Error> {
pub fn change_jrcell_value(conn: &mut MysqlConnection, entryid: i32, columnid: i32, new_value: &String) -> Result<usize, diesel::result::Error> {
diesel::update(jrcells.filter(jrentry_id.eq(entryid)).filter(jrcolumn_id.eq(columnid)))
.set(cell_value.eq(new_value))
.execute(conn)
@@ -96,3 +96,10 @@ pub fn get_entry_cells(conn: &mut MysqlConnection, entryid: i32) -> Result<Vec<J
.select(Jrcell::as_select())
.load(conn)
}
pub fn get_entry_cell_ids(conn: &mut MysqlConnection, entryid: i32) -> Result<Vec<i32>, diesel::result::Error> {
jrcells
.filter(jrentry_id.eq(entryid))
.select(id)
.load(conn)
}

View File

@@ -7,7 +7,7 @@ use models::{ Jrcolumn, NewJrcolumn };
use schema::jrcolumns::dsl::{ jrcolumns, id, name, jrtable_id, id_in_table };
pub fn create_jrcolumn(conn: &mut MysqlConnection, tblid: i32, clmnname: String) -> Result<usize, diesel::result::Error> {
pub fn create_jrcolumn(conn: &mut MysqlConnection, tblid: i32, clmnname: String, clmtype: crate::FIELDTYPE) -> Result<usize, diesel::result::Error> {
use schema::jrentries::dsl::{ jrentries, jrtable_id };
use models::{ Jrentry, NewJrcell };
@@ -16,7 +16,7 @@ pub fn create_jrcolumn(conn: &mut MysqlConnection, tblid: i32, clmnname: String)
return ncols;
}
let ncols = ncols.unwrap() as i32;
let jrcolumn = NewJrcolumn { name: clmnname, jrtable_id: tblid, id_in_table: ncols + 1 };
let jrcolumn = NewJrcolumn { name: clmnname.clone(), jrtable_id: tblid, column_type: clmtype as i32, id_in_table: (ncols + 1) as i32 };
let res = diesel::insert_into(crate::schema::jrcolumns::table)
.values(&jrcolumn)
.execute(conn);
@@ -48,16 +48,11 @@ pub fn create_jrcolumn(conn: &mut MysqlConnection, tblid: i32, clmnname: String)
}
pub fn create_jrcolumns_empty_tbl(conn: &mut MysqlConnection, tblid: i32, names: Vec<String>) -> Result<usize, diesel::result::Error> {
pub fn create_jrcolumns_empty_tbl(conn: &mut MysqlConnection, tblid: i32, names: Vec<String>, types: Vec<crate::FIELDTYPE>) -> Result<usize, diesel::result::Error> {
let mut cols : Vec<NewJrcolumn> = Vec::new();
let ncols = jrtables::get_ncols(conn, tblid);
if ncols.is_err() {
return ncols;
}
let mut ncols = ncols.unwrap() as i32;
for clmnname in names {
ncols += 1;
cols.push(NewJrcolumn { name: clmnname, jrtable_id: tblid, id_in_table: ncols});
for i in 0..names.len() {
cols.push(NewJrcolumn { name: names[i].clone(), column_type: types[i] as i32, jrtable_id: tblid, id_in_table: (i+1) as i32});
}
let cols = cols;
@@ -169,8 +164,8 @@ pub fn move_column(conn: &mut MysqlConnection, clmnid: i32, new_id_in_table: i32
.execute(conn)
}
pub fn delete_column_relative(conn: &mut MysqlConnection, tblid: i32, idinclm: i32) -> Result<usize, diesel::result::Error> {
let clmid = get_clmid_relative(conn, tblid, idinclm);
pub fn delete_column_relative(conn: &mut MysqlConnection, tblid: i32, idintbl: i32) -> Result<usize, diesel::result::Error> {
let clmid = get_clmid_relative(conn, tblid, idintbl);
if clmid.is_err() {
return Err(clmid.err().unwrap());
}

View File

@@ -76,7 +76,6 @@ pub fn delete_jrentry_relative(conn: &mut MysqlConnection, tblid: i32, rowpos:
return Err(entryid.err().unwrap());
}
delete_jrentry(conn, entryid.unwrap())
}
pub fn delete_jrentry(conn: &mut MysqlConnection, entryid: i32) -> Result<usize, diesel::result::Error> {

View File

@@ -6,7 +6,7 @@ use crate::jrcolumns;
use models::{ Jrtable, NewJrtable };
use schema::jrtables::dsl::{ jrtables, id, name, owner_id };
pub fn create_jrtable(conn: &mut MysqlConnection, tblname: &String, field_names: Vec<String>, uid: i32) -> Result<i32, diesel::result::Error> {
pub fn create_jrtable(conn: &mut MysqlConnection, tblname: &String, field_names: Vec<String>, field_types: Vec<crate::FIELDTYPE>, uid: i32) -> Result<i32, diesel::result::Error> {
let jrtable = NewJrtable { name: tblname.to_string(), owner_id: uid };
let tbl = diesel::insert_into(crate::schema::jrtables::table)
@@ -24,7 +24,7 @@ pub fn create_jrtable(conn: &mut MysqlConnection, tblname: &String, field_names:
return Err(tblid.err().unwrap());
}
let tblid = tblid.unwrap();
let cols = jrcolumns::create_jrcolumns_empty_tbl(conn, tblid, field_names);
let cols = jrcolumns::create_jrcolumns_empty_tbl(conn, tblid, field_names, field_types);
if cols.is_err() {
return Err(cols.err().unwrap());
}

View File

@@ -1,3 +1,4 @@
#![recursion_limit = "512"]
//! Database API using the table style system provided by this crate
mod models;
mod schema;
@@ -17,6 +18,8 @@ use diesel::deserialize::FromSql;
use diesel::deserialize;
use diesel::backend::Backend;
use diesel::sql_types::Integer;
use diesel::expression::Expression;
#[derive(PartialEq, Clone, Copy, diesel::FromSqlRow)]
#[repr(i32)]
@@ -25,6 +28,10 @@ pub enum FIELDTYPE {
NUMBER = 1,
}
impl Expression for FIELDTYPE {
type SqlType = Integer;
}
impl<DB> FromSql<Integer, DB> for FIELDTYPE
where DB: Backend, i32: FromSql<Integer,DB> {
fn from_sql(bytes: DB::RawValue<'_>) -> deserialize::Result<Self> {
@@ -35,6 +42,15 @@ where DB: Backend, i32: FromSql<Integer,DB> {
}
}
}
impl From<isize> for FIELDTYPE {
fn from(value: isize) -> Self {
match value {
1 => FIELDTYPE::NUMBER,
x => FIELDTYPE::TEXT,
}
}
}
/// represents and summarised all relevant data of a table.
/// Standard return type if whole tables should be returned
@@ -169,7 +185,7 @@ pub fn search_table(tbl: Tbl, search_fields: Vec<i32>, search_value: String) ->
let mut field_sets = HashSet::new();
for field in search_fields {
for row in &rows {
if row.cells[field as usize].contains(&search_value) {
if row.cells[field as usize].to_lowercase().contains(&search_value) {
field_sets.insert(row.clone());
}
}
@@ -241,11 +257,11 @@ pub fn delete_table(conn: &mut MysqlConnection, tblid: i32, uid: i32) -> Option<
/// Returns the id of the newly created table if the action seems to have been successful and the requesting user is the
/// table's owner.
/// None otherwise.
pub fn create_table(conn: &mut MysqlConnection, name: String, field_names: Vec<String>, uid: i32) -> Option<i32> {
pub fn create_table(conn: &mut MysqlConnection, name: String, field_names: Vec<String>, field_types: Vec<crate::FIELDTYPE>, uid: i32) -> Option<i32> {
if jrtables::get_tbl_by_name_uid(conn, &name, uid).is_ok() {
return None;
}
if jrtables::create_jrtable(conn, &name, field_names, uid).is_err() {
if jrtables::create_jrtable(conn, &name, field_names, field_types, uid).is_err() {
return None;
}
let tblid = jrtables::get_tbl_by_name_uid(conn, &name, uid);
@@ -327,8 +343,21 @@ pub fn move_row(conn: &mut MysqlConnection, tblid: i32, rowpos: i32, newpos: i32
Some(true)
}
pub fn edit_row(conn: &mut MysqlConnection, tblid: i32, cells: Vec<String>, row_pos: i32, uid: i32) -> Option<bool> {
let owner = jrtables::get_owner_id(conn, tblid);
if owner.is_err() || owner.unwrap() != uid {
return None;
}
for (i, cell) in cells.iter().enumerate() {
if jrcells::change_jrcell_value_relative(conn, tblid, row_pos, (i+1) as i32, cell).is_err() {
return None;
}
}
Some(true)
}
pub fn edit_cell(conn: &mut MysqlConnection, tblid: i32, row_pos: i32, column_pos: i32, new_value: String, uid: i32) -> Option<bool> {
pub fn edit_cell(conn: &mut MysqlConnection, tblid: i32, row_pos: i32, column_pos: i32, new_value: &String, uid: i32) -> Option<bool> {
let owner = jrtables::get_owner_id(conn, tblid);
if owner.is_err() ||
owner.unwrap() != uid ||
@@ -350,11 +379,11 @@ pub fn move_column(conn: &mut MysqlConnection, tblid:i32, column_pos: i32, new_c
Some(true)
}
pub fn add_column(conn: &mut MysqlConnection, tblid: i32, name: String, uid: i32) -> Option<i32> {
pub fn add_column(conn: &mut MysqlConnection, tblid: i32, name: String, clmtype: FIELDTYPE, uid: i32) -> Option<i32> {
let owner = jrtables::get_owner_id(conn, tblid);
if owner.is_err() ||
owner.unwrap() != uid ||
jrcolumns::create_jrcolumn(conn, tblid, name).is_err()
jrcolumns::create_jrcolumn(conn, tblid, name, clmtype).is_err()
{
return None;
}

View File

@@ -37,6 +37,7 @@ pub struct Jrcolumn {
pub struct NewJrcolumn {
pub name: String,
pub jrtable_id: i32,
pub column_type: i32,
pub id_in_table: i32,
}