Skip to content

Commit

Permalink
feat: added sql configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb committed Mar 10, 2023
1 parent 9da34e0 commit 87961ae
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 11 deletions.
7 changes: 6 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
import Theme from './components/Theme/index.vue'
import Avatar from './components/Avatar/index.vue'
import { useThemeStore, useUuidStore } from '@/stores'
import { initSQL } from '@/sqls'
const { themeClass } = storeToRefs(useThemeStore())
const { uuid } = storeToRefs(useUuidStore())
onMounted(async () => {
initSQL()
})
</script>

<template>
Expand All @@ -15,7 +20,7 @@ const { uuid } = storeToRefs(useUuidStore())
<Theme />

<!-- 内容区 -->
<div class="h-[calc(100%-49px)]" data-tauri-drag-region>
<div class="h-[calc(100%-48px)]" data-tauri-drag-region>
<Avatar />
<Avatar :value="uuid" />
</div>
Expand Down
10 changes: 0 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import { createApp } from 'vue'
import { message } from '@tauri-apps/api/dialog'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
import App from './App.vue'
import 'uno.css'
import '@kidonng/daisyui/index.css'
import './assets/css/global.scss'

window.onerror = (errorMessage) => {
message((errorMessage as string).replace('Error: ', ''), {
title: import.meta.env.VITE_APP_NAME,
type: 'error'
})

return true
}

createApp(App).use(createPinia().use(piniaPluginPersistedstate)).mount('#app')
82 changes: 82 additions & 0 deletions src/sqls/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { configDir } from '@tauri-apps/api/path'
import Database from 'tauri-plugin-sql-api'
import { dialogErrorMessage } from '@/utils'

const dbFile = import.meta.env.DEV ? 'sql.dev.db' : 'sql.db'
const db = await Database.load(
`sqlite:${await configDir()}/${import.meta.env.VITE_APP_NAME}/${dbFile}`
)
const tableName = 'history'

/**
* 执行 sql 语句
* @param sql sql 语句
*/
export const executeSQL = async (sql: string) => {
try {
await db.execute(sql)
} catch (error) {
let action = '创建'

if (sql.includes('INSERT')) {
action = '添加'
} else if (sql.includes('UPDATE')) {
action = '更新'
} else if (sql.includes('DELETE')) {
action = '删除'
}

dialogErrorMessage(`${action}数据时遇到了问题,请重试~`)
}
}

/**
* 初始化 sql 配置
*/
export const initSQL = () => {
executeSQL(
`CREATE TABLE IF NOT EXISTS ${tableName} (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, data TEXT, time TIMESTAMP DEFAULT CURRENT_TIMESTAMP);`
)
}

/**
* 查找的 sql 语句
*/
export const selectSQL = async () => {
try {
return await db.select(`SELECT * FROM ${tableName} ORDER BY id;`)
} catch (error) {
dialogErrorMessage('数据查找失败,请重试~')
}
}

/**
* 添加的 sql 语句
* @param data 聊天内容
*/
export const insertSQL = async (data: any[]) => {
await executeSQL(
`INSERT INTO ${tableName} (data) VALUES ('${JSON.stringify(data)}');`
)
}

/**
* 更新的 sql 语句
* @param id 更新数据的 id
* @param title 聊天内容标题
*/
export const updateSQL = async (id: number, title: string) => {
await executeSQL(`UPDATE ${tableName} SET title='${title}' WHERE id=${id};`)
}

/**
* 删除的 sql 语句
* @param id 删除数据的 id
*/
export const deleteSQL = async (id?: number) => {
if (id) {
await executeSQL(`DELETE FROM ${tableName} WHERE id=${id};`)
} else {
await executeSQL(`DELETE FROM ${tableName};`)
}
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './shared'
export * from './tauri'
8 changes: 8 additions & 0 deletions src/utils/tauri.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { message } from '@tauri-apps/api/dialog'

export const dialogErrorMessage = (errorMessage: string) => {
message(errorMessage, {
title: import.meta.env.VITE_APP_NAME,
type: 'error'
})
}

0 comments on commit 87961ae

Please sign in to comment.