mirror of
https://github.com/fanchenio/DawnLauncher.git
synced 2025-07-14 05:12:11 +08:00
optimize code.
This commit is contained in:
parent
90594e7f21
commit
c73a2b3709
@ -14,7 +14,7 @@ import { getDataSqlite3 } from "../../commons/betterSqlite3";
|
||||
let db = getDataSqlite3();
|
||||
|
||||
// 分类表名
|
||||
let classificationTableName = "classification";
|
||||
let tableName = "classification";
|
||||
|
||||
// 查询字段
|
||||
let selectColumn =
|
||||
@ -41,7 +41,7 @@ function getClassification(row: any): Classification {
|
||||
*/
|
||||
function init() {
|
||||
// sql
|
||||
let sql = `CREATE TABLE IF NOT EXISTS ${classificationTableName} (
|
||||
let sql = `CREATE TABLE IF NOT EXISTS ${tableName} (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
parent_id INTEGER,
|
||||
name TEXT NOT NULL,
|
||||
@ -53,7 +53,7 @@ function init() {
|
||||
// 运行
|
||||
db.exec(sql);
|
||||
// 查询有多少条数据
|
||||
sql = `SELECT COUNT(id) count FROM ${classificationTableName}`;
|
||||
sql = `SELECT COUNT(id) count FROM ${tableName}`;
|
||||
let row: any = db.prepare(sql).get();
|
||||
let count = row.count as number;
|
||||
if (count === 0) {
|
||||
@ -70,7 +70,7 @@ function list(parentId: number | null = null) {
|
||||
// 参数
|
||||
let params = [];
|
||||
// sql
|
||||
let sql = `SELECT ${selectColumn} FROM ${classificationTableName}`;
|
||||
let sql = `SELECT ${selectColumn} FROM ${tableName}`;
|
||||
if (parentId) {
|
||||
sql += " WHERE parent_id = ?";
|
||||
params.push(parentId);
|
||||
@ -103,7 +103,7 @@ function add(
|
||||
// 获取序号
|
||||
let newOrder = getMaxOrder(parentId) + 1;
|
||||
// SQL
|
||||
let sql = `INSERT INTO ${classificationTableName} (parent_id, name, type, data, shortcut_key, global_shortcut_key, \`order\`) VALUES (?, ?, ?, ?, ?, ?, ?)`;
|
||||
let sql = `INSERT INTO ${tableName} (parent_id, name, type, data, shortcut_key, global_shortcut_key, \`order\`) VALUES (?, ?, ?, ?, ?, ?, ?)`;
|
||||
// 运行
|
||||
let id = db
|
||||
.prepare(sql)
|
||||
@ -146,7 +146,7 @@ function add(
|
||||
*/
|
||||
function update(classification: Classification) {
|
||||
// SQL
|
||||
let sql = `UPDATE ${classificationTableName} SET name = ?, type = ?, data = ?, shortcut_key = ?, global_shortcut_key = ? WHERE id = ?`;
|
||||
let sql = `UPDATE ${tableName} SET name = ?, type = ?, data = ?, shortcut_key = ?, global_shortcut_key = ? WHERE id = ?`;
|
||||
// 运行
|
||||
return (
|
||||
db
|
||||
@ -169,7 +169,7 @@ function update(classification: Classification) {
|
||||
*/
|
||||
function updateData(id: number, data: ClassificationData) {
|
||||
// SQL
|
||||
let sql = `UPDATE ${classificationTableName} SET data = ? WHERE id = ?`;
|
||||
let sql = `UPDATE ${tableName} SET data = ? WHERE id = ?`;
|
||||
return db.prepare(sql).run(JSON.stringify(data), id).changes > 0;
|
||||
}
|
||||
|
||||
@ -179,7 +179,7 @@ function updateData(id: number, data: ClassificationData) {
|
||||
*/
|
||||
function selectById(id: number): Classification | null {
|
||||
// SQL
|
||||
let sql = `SELECT ${selectColumn} FROM ${classificationTableName} WHERE id = ?`;
|
||||
let sql = `SELECT ${selectColumn} FROM ${tableName} WHERE id = ?`;
|
||||
// 运行
|
||||
let row = db.prepare(sql).get(id);
|
||||
// 返回
|
||||
@ -201,7 +201,7 @@ function del(id: number) {
|
||||
// 查询有无子分类
|
||||
let childList = list(classifictaion.id);
|
||||
// SQL
|
||||
let sql = `DELETE FROM ${classificationTableName} WHERE id = ? or parent_id = ?`;
|
||||
let sql = `DELETE FROM ${tableName} WHERE id = ? or parent_id = ?`;
|
||||
// 运行
|
||||
let res = db.prepare(sql).run(id, id).changes > 0;
|
||||
if (res) {
|
||||
@ -256,14 +256,14 @@ function updateOrder(
|
||||
newOrder = getMaxOrder(parentId) + 1;
|
||||
}
|
||||
// SQL
|
||||
let sql = `UPDATE ${classificationTableName} SET \`order\` = ? WHERE id = ?`;
|
||||
let sql = `UPDATE ${tableName} SET \`order\` = ? WHERE id = ?`;
|
||||
// 更新排序
|
||||
db.prepare(sql).run(newOrder, fromClassification.id);
|
||||
// 判断新序号和老序号之间的数据是+1还是-1
|
||||
if (newOrder > fromClassification.order) {
|
||||
// 新序号和老序号之间数据,序号-1
|
||||
let params = [fromClassification.order, newOrder, fromClassification.id];
|
||||
sql = `UPDATE ${classificationTableName} SET \`order\` = \`order\` - 1 WHERE \`order\` > ? AND \`order\` <= ? AND id != ?`;
|
||||
sql = `UPDATE ${tableName} SET \`order\` = \`order\` - 1 WHERE \`order\` > ? AND \`order\` <= ? AND id != ?`;
|
||||
if (parentId) {
|
||||
sql += " AND parent_id = ?";
|
||||
params.push(parentId);
|
||||
@ -274,7 +274,7 @@ function updateOrder(
|
||||
} else {
|
||||
// 新序号和老序号之间数据,序号+1
|
||||
let params = [newOrder, fromClassification.order, fromClassification.id];
|
||||
sql = `UPDATE ${classificationTableName} SET \`order\` = \`order\` + 1 WHERE \`order\` >= ? AND \`order\` < ? AND id != ?`;
|
||||
sql = `UPDATE ${tableName} SET \`order\` = \`order\` + 1 WHERE \`order\` >= ? AND \`order\` < ? AND id != ?`;
|
||||
if (parentId) {
|
||||
sql += " AND parent_id = ?";
|
||||
params.push(parentId);
|
||||
@ -298,7 +298,7 @@ function reorder(parentId: number | null) {
|
||||
// 开启事务
|
||||
db.transaction(() => {
|
||||
// SQL
|
||||
let sql = `UPDATE ${classificationTableName} SET \`order\` = ? WHERE id = ?`;
|
||||
let sql = `UPDATE ${tableName} SET \`order\` = ? WHERE id = ?`;
|
||||
// 更新序号
|
||||
for (let i = 0; i < classificationList.length; i++) {
|
||||
db.prepare(sql).run(i + 1, classificationList[i].id);
|
||||
@ -312,7 +312,7 @@ function reorder(parentId: number | null) {
|
||||
*/
|
||||
function getMaxOrder(parentId: number | null) {
|
||||
// SQL
|
||||
let sql = `SELECT MAX(\`order\`) \`order\` FROM ${classificationTableName}`;
|
||||
let sql = `SELECT MAX(\`order\`) \`order\` FROM ${tableName}`;
|
||||
if (parentId) {
|
||||
sql += " WHERE parent_id = ?";
|
||||
} else {
|
||||
@ -337,7 +337,7 @@ function updateIcon(id: number, icon: string | null) {
|
||||
let classification = selectById(id);
|
||||
if (classification) {
|
||||
// SQL
|
||||
let sql = `UPDATE ${classificationTableName} SET data = ? WHERE id = ?`;
|
||||
let sql = `UPDATE ${tableName} SET data = ? WHERE id = ?`;
|
||||
// 更新图标
|
||||
classification.data.icon = icon;
|
||||
return (
|
||||
|
@ -26,6 +26,7 @@ if (
|
||||
) {
|
||||
app.setPath("appData", join(dirname(process.execPath), "data"));
|
||||
app.setPath("userData", join(dirname(process.execPath), "data"));
|
||||
app.setPath("sessionData", join(dirname(process.execPath), "data"));
|
||||
}
|
||||
|
||||
process.env.DIST_ELECTRON = join(__dirname, "..");
|
||||
|
@ -7,7 +7,7 @@ import { getDataSqlite3 } from "../../commons/betterSqlite3";
|
||||
let db = getDataSqlite3();
|
||||
|
||||
// 项目表名
|
||||
let itemTableName = "item";
|
||||
let tableName = "item";
|
||||
|
||||
// 查询字段
|
||||
let selectColumn =
|
||||
@ -36,7 +36,7 @@ function getItem(row: any): Item {
|
||||
*/
|
||||
function init() {
|
||||
// sql
|
||||
let sql = `CREATE TABLE IF NOT EXISTS ${itemTableName} (
|
||||
let sql = `CREATE TABLE IF NOT EXISTS ${tableName} (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
classification_id INTEGER NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
@ -58,7 +58,7 @@ function add(item: Item, reuseId: boolean = false) {
|
||||
// 获取序号
|
||||
let newOrder = getMaxOrder(item.classificationId) + 1;
|
||||
// SQL
|
||||
let sql = `INSERT INTO ${itemTableName}
|
||||
let sql = `INSERT INTO ${tableName}
|
||||
(classification_id, name, type, data, shortcut_key, global_shortcut_key, \`order\`)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`;
|
||||
// 参数
|
||||
@ -73,7 +73,7 @@ function add(item: Item, reuseId: boolean = false) {
|
||||
];
|
||||
// 重复使用ID
|
||||
if (reuseId && item.id) {
|
||||
sql = `INSERT INTO ${itemTableName}
|
||||
sql = `INSERT INTO ${tableName}
|
||||
(id, classification_id, name, type, data, shortcut_key, global_shortcut_key, \`order\`)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`;
|
||||
params.unshift(item.id);
|
||||
@ -108,7 +108,7 @@ function batchAdd(
|
||||
// 循环添加
|
||||
for (let item of itemList) {
|
||||
// SQL
|
||||
let sql = `INSERT INTO ${itemTableName}
|
||||
let sql = `INSERT INTO ${tableName}
|
||||
(classification_id, name, type, data, shortcut_key, global_shortcut_key, \`order\`)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`;
|
||||
// 参数
|
||||
@ -123,7 +123,7 @@ function batchAdd(
|
||||
];
|
||||
// 重复使用ID
|
||||
if (reuseId && item.id) {
|
||||
sql = `INSERT INTO ${itemTableName}
|
||||
sql = `INSERT INTO ${tableName}
|
||||
(id, classification_id, name, type, data, shortcut_key, global_shortcut_key, \`order\`)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`;
|
||||
params.unshift(item.id);
|
||||
@ -147,7 +147,7 @@ function batchAdd(
|
||||
*/
|
||||
function update(item: Item) {
|
||||
// SQL
|
||||
let sql = `UPDATE ${itemTableName}
|
||||
let sql = `UPDATE ${tableName}
|
||||
SET name = ?,
|
||||
data = ?,
|
||||
shortcut_key = ?,
|
||||
@ -177,7 +177,7 @@ function updateClassificationId(
|
||||
newClassificationId: number
|
||||
) {
|
||||
// SQL
|
||||
let sql = `UPDATE ${itemTableName}
|
||||
let sql = `UPDATE ${tableName}
|
||||
SET classification_id = ?
|
||||
WHERE classification_id = ?`;
|
||||
// 运行
|
||||
@ -193,7 +193,7 @@ function updateClassificationId(
|
||||
*/
|
||||
function updateData(id: number, itemData: ItemData) {
|
||||
// SQL
|
||||
let sql = `UPDATE ${itemTableName}
|
||||
let sql = `UPDATE ${tableName}
|
||||
SET data = ?
|
||||
WHERE id = ?`;
|
||||
// 运行
|
||||
@ -206,7 +206,7 @@ function updateData(id: number, itemData: ItemData) {
|
||||
*/
|
||||
function getMaxOrder(classificationId: number) {
|
||||
// SQL
|
||||
let sql = `SELECT MAX(\`order\`) \`order\` FROM ${itemTableName} WHERE classification_id = ?`;
|
||||
let sql = `SELECT MAX(\`order\`) \`order\` FROM ${tableName} WHERE classification_id = ?`;
|
||||
// 运行
|
||||
let row: any = db.prepare(sql).get(classificationId);
|
||||
if (row && row.order) {
|
||||
@ -222,7 +222,7 @@ function getMaxOrder(classificationId: number) {
|
||||
*/
|
||||
function selectById(id: number): Item | null {
|
||||
// SQL
|
||||
let sql = `SELECT ${selectColumn} FROM ${itemTableName} WHERE id = ?`;
|
||||
let sql = `SELECT ${selectColumn} FROM ${tableName} WHERE id = ?`;
|
||||
// 运行
|
||||
let row = db.prepare(sql).get(id);
|
||||
// 返回
|
||||
@ -244,7 +244,7 @@ function list(simple: boolean = false, classificationId: number | null = null) {
|
||||
// sql
|
||||
let sql = `SELECT ${
|
||||
simple ? simpleSelectColumn : selectColumn
|
||||
} FROM ${itemTableName}`;
|
||||
} FROM ${tableName}`;
|
||||
if (classificationId) {
|
||||
sql += " WHERE classification_id = ?";
|
||||
params.push(classificationId);
|
||||
@ -269,7 +269,7 @@ function selectByIdList(simple: boolean, idList: Array<number>) {
|
||||
// sql
|
||||
let sql = `SELECT ${
|
||||
simple ? simpleSelectColumn : selectColumn
|
||||
} FROM ${itemTableName} WHERE id IN (`;
|
||||
} FROM ${tableName} WHERE id IN (`;
|
||||
for (let i = 0; i < idList.length; i++) {
|
||||
sql += "?";
|
||||
if (i !== idList.length - 1) {
|
||||
@ -307,7 +307,7 @@ function del(id: number) {
|
||||
let item = selectById(id);
|
||||
if (item) {
|
||||
// SQL
|
||||
let sql = `DELETE FROM ${itemTableName} WHERE id = ?`;
|
||||
let sql = `DELETE FROM ${tableName} WHERE id = ?`;
|
||||
// 运行
|
||||
let res = db.prepare(sql).run(id).changes > 0;
|
||||
if (res) {
|
||||
@ -328,7 +328,7 @@ function del(id: number) {
|
||||
*/
|
||||
function deleteByClassificationId(classificationId: number) {
|
||||
// SQL
|
||||
let sql = `DELETE FROM ${itemTableName} WHERE classification_id = ?`;
|
||||
let sql = `DELETE FROM ${tableName} WHERE classification_id = ?`;
|
||||
// 运行
|
||||
return db.prepare(sql).run(classificationId).changes > 0;
|
||||
}
|
||||
@ -343,7 +343,7 @@ function reorder(classification_id: number) {
|
||||
// 开启事务
|
||||
db.transaction(() => {
|
||||
// SQL
|
||||
let sql = `UPDATE ${itemTableName} SET \`order\` = ? WHERE id = ?`;
|
||||
let sql = `UPDATE ${tableName} SET \`order\` = ? WHERE id = ?`;
|
||||
// 更新序号
|
||||
for (let i = 0; i < itemList.length; i++) {
|
||||
db.prepare(sql).run(i + 1, itemList[i].id);
|
||||
@ -396,7 +396,7 @@ function updateOrder(
|
||||
// 开启事务
|
||||
db.transaction(() => {
|
||||
// SQL
|
||||
let sql = `UPDATE ${itemTableName} SET \`order\` = ?, classification_id = ? WHERE id = ?`;
|
||||
let sql = `UPDATE ${tableName} SET \`order\` = ?, classification_id = ? WHERE id = ?`;
|
||||
// 更新序号
|
||||
for (let i = 0; i < toItemList.length; i++) {
|
||||
db.prepare(sql).run(i + 1, toClassificationId, toItemList[i].id);
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { BrowserWindow, shell } from "electron";
|
||||
import { join } from "node:path";
|
||||
import { getMainBackgorunColor, sendToWebContent } from "../commons";
|
||||
import { sendToWebContent } from "../commons";
|
||||
import cacheData from "../commons/cacheData";
|
||||
|
||||
// 窗口
|
||||
|
Loading…
Reference in New Issue
Block a user