john 1 ano atrás
pai
commit
1aa870da06

+ 17 - 6
src-tauri/src/files/mod.rs

@@ -1,8 +1,19 @@
-pub mod file_struct;
-// pub use FileStruct;
+pub(crate) mod files;
 
-pub mod file_sqlite3;
-// pub use file_sqlite3::file_sqlite3;
+use tauri::{
+    plugin::{Builder, TauriPlugin}, Runtime,
+};
 
-pub mod file_tools;
-// pub mod file_tools::file_tools;
+use self::file_sqlite3::*;
+
+/// Initializes the plugin.
+pub fn init<R: Runtime>() -> TauriPlugin<R> {
+    Builder::new("st-files")
+        .invoke_handler(tauri::generate_handler![
+          is_create,create,inset
+        ])
+        .setup(|app| {
+            Ok(())
+        })
+        .build()
+}

+ 8 - 0
src-tauri/src/files/modCopy

@@ -0,0 +1,8 @@
+pub mod file_struct;
+// pub use FileStruct;
+
+pub mod file_sqlite3;
+// pub use file_sqlite3::file_sqlite3;
+
+pub mod file_tools;
+// pub mod file_tools::file_tools;

+ 3 - 0
src-tauri/src/main.rs

@@ -7,6 +7,7 @@ mod event_loop;
 mod self_plugin;
 mod common;
 mod utils;
+mod servics;
 
 
 use crate::menus::default::use_memu;
@@ -14,6 +15,7 @@ use crate::menus::event::m_event;
 use crate::event_loop::{greet, file_path, file_sort};
 use self_plugin::tauri_plugin_sqlite;
 use self_plugin::tauri_plugin_file;
+use servics::files_servics;
 
 
 use tauri::{Manager, generate_context};
@@ -59,6 +61,7 @@ fn main() {
     tauri::Builder::default()
         .plugin(tauri_plugin_sqlite::init())
         .plugin(tauri_plugin_file::init())
+        .plugin(files_servics::init())
         .plugin(
             tauri_plugin_sql::Builder::default()
             .add_migrations("sqlite:test.db", migrations)

+ 58 - 0
src-tauri/src/servics/files_servics/file_sqlite3.rs

@@ -0,0 +1,58 @@
+
+
+
+
+use tauri_plugin_sql::plugin::*;
+
+pub mod file_sqlite3 {
+    // 判断是否新建表
+    pub fn is_create(table_name: &str) -> bool {
+        if table_name.len() < 1 {
+            return false;
+        }
+        let connection = load("sqlite::files.db").unwrap();
+        let query: String = format!(
+            "SELECT tbl_name FROM sqlite_master WHERE tbl_name = '{}'",
+            table_name
+        );
+        let mut is_table: bool = false;
+        connection
+            .iterate(query, |pairs| {
+                for &(_tpl_name, value) in pairs.iter() {
+                    if value.unwrap() == table_name {
+                        is_table = true
+                    }
+                }
+                true
+            })
+            .unwrap();
+        // is_table
+        if !is_table {
+            return create(table_name);
+        }
+        is_table
+    }
+
+    pub fn create(table_name: &str) -> bool {
+        println!("28");
+        if table_name.len() < 1 {
+            return false;
+        }
+        let connection = sqlite::open("tauri.db").unwrap();
+        // CREATE TABLE users (name TEXT, age INTEGER)
+        let query: String = format!("CREATE TABLE '{}' ( name TEXT, path TEXT, history_path TEXT, uuid TEXT, parent_id TEXT, create_time INTEGER, update_time INTEGER, file_type TEXT, user TEXT, rule TEXT );", table_name);
+        // let mut isTable: bool = false;
+        println!("36");
+        connection.execute(query).unwrap();
+        true
+    }
+
+    use crate::files::file_struct::File;
+    pub fn inset(file: File) -> bool {
+        // INSERT INTO files (name, path, history_path, uuid, parent_id, create_time, update_time, file_type, user, rule) VALUES ('1', '1', '2', '3', '4', 5, null, null, null, null)
+        let query: String = format!("INSERT INTO files (name, path, history_path, uuid, parent_id, create_time, update_time, file_type, user, rule) VALUES ('{}', '{}', '{}', '{}', '{}', '{:?}', '{:?}', '{}', '{}', '{}')", file.name, file.path, file.history_path,  file.uuid, file.parent_id, file.create_time, file.update_time, file.file_type, file.user, file.rule);
+        let connection = sqlite::open("tauri.db").unwrap();
+        connection.execute(query).unwrap();
+        true
+    }
+}

+ 31 - 0
src-tauri/src/servics/files_servics/file_struct.rs

@@ -0,0 +1,31 @@
+// pub mod FileStruct {
+//     pub struct Site {
+//         pub domain: String,
+//         pub nation: String,
+//     }
+// }
+
+use std::time::Duration;
+
+#[derive(Debug)]
+pub struct Site {
+    pub domain: String,
+    pub nation: String,
+}
+
+
+// name TEXT, path TEXT, history_path TEXT, uuid TEXT, parent_id TEXT, create_time INTEGER, update_time INTEGER, file_type TEXT, user TEXT, rule TEXT
+
+#[derive(Debug)]
+pub struct File {
+    pub name: String,
+    pub path: String,
+    pub history_path: String,
+    pub uuid: String,
+    pub parent_id: String,
+    pub create_time: Duration,
+    pub update_time: Duration,
+    pub file_type: String,
+    pub user: String,
+    pub rule: String,
+}

+ 9 - 0
src-tauri/src/servics/files_servics/file_tools.rs

@@ -0,0 +1,9 @@
+pub mod file_tools {
+    use crate::files::file_struct::Site;
+    pub fn get_base_path () -> Site {
+        Site {
+            domain: String::from("domain"),
+            nation: String::from("nation")
+        }
+    }
+}

+ 19 - 0
src-tauri/src/servics/files_servics/mod.rs

@@ -0,0 +1,19 @@
+pub(crate) mod file_sqlite3;
+
+use tauri::{
+    plugin::{Builder, TauriPlugin}, Runtime,
+};
+
+use self::file_sqlite3::*;
+
+/// Initializes the plugin.
+pub fn init<R: Runtime>() -> TauriPlugin<R> {
+    Builder::new("ss-files")
+        .invoke_handler(tauri::generate_handler![
+          is_create,create,inset
+        ])
+        .setup(|app| {
+            Ok(())
+        })
+        .build()
+}

+ 8 - 0
src-tauri/src/servics/files_servics/modCopy

@@ -0,0 +1,8 @@
+pub mod file_struct;
+// pub use FileStruct;
+
+pub mod file_sqlite3;
+// pub use file_sqlite3::file_sqlite3;
+
+pub mod file_tools;
+// pub mod file_tools::file_tools;

+ 1 - 0
src-tauri/src/servics/mod.rs

@@ -0,0 +1 @@
+pub mod files_servics;

+ 0 - 0
src/databases/files/index.ts


+ 9 - 0
src/databases/files/sql.ts

@@ -0,0 +1,9 @@
+import Database from "tauri-plugin-sql-api";
+import { createSql } from "../createTableSql";
+
+export async function createTable() {
+
+  const db = await Database.load("sqlite:test.db");
+  // 创建表
+  await db.execute(createSql.select_history);
+}

+ 55 - 0
src/databases/index copy.ts

@@ -0,0 +1,55 @@
+import { SQLite } from "@/plugins/tauri-plugin-sqlite";
+import { createSql } from "./createTableSql";
+import { TableName } from "@/types/table";
+import {fileFileds} from './addFiledInTable'
+export const table_init = async (dbName: string, tableName: TableName) => {
+  const dbversion = await SQLite.open(dbName);
+  //查询是否有该表
+  const rows = await dbversion.queryWithArgs<Array<{ count: number }>>(
+    `SELECT count(1) count FROM sqlite_master WHERE type='table' and name = '${tableName}' `
+  );
+  if (!!rows && rows.length > 0 && rows[0].count > 0) {
+  } else {
+    //创建表
+    await dbversion.execute(createSql[tableName]);
+  }
+};
+
+export const table_add_filed = async (tableName: string, dbName: string, addFileFileds: any[]) =>  {
+  const dbversion = await SQLite.open(dbName);
+  // 依据版本增加
+  const addField = await Promise.allSettled(addFileFileds.map(async (item) => {
+    try {
+      const is_field = await is_field_in_table(item.key, tableName, dbName);
+      if(!is_field) {
+        return await dbversion.execute(item.sql.replace('{tableName}', tableName))
+      }
+    } catch (err) {
+      console.log('table_add_filed 出现bug::', err);
+    }
+    return true
+  }))
+  console.log(4545, addField);
+}
+
+export const is_field_in_table = async (fidld: string, tableName: string, dbName: string) => {
+  // 不能为空
+  if (!fidld || !tableName || !dbName) {
+    return Promise.reject('必填数据不能为空');
+  }
+  try {
+    const dbversion = await SQLite.open(dbName);
+    const fidldList: any[] = await dbversion.queryWithArgs(`SELECT * FROM sqlite_master WHERE type='table' AND name='${tableName}' AND sql LIKE '%${fidld}%'`);
+    console.log(5656, fidldList);
+    if(fidldList.length) {
+      // 字段存在
+      return Promise.resolve(true);
+    }
+    // 字段不存在
+    return Promise.resolve(false);
+  } catch (error) {
+    return Promise.reject('查询失败');
+  }
+}
+
+export const DB = async (dbName: string) => await SQLite.open(dbName);