From 518f952c24a65faa687e4c111038ffc5026769ff Mon Sep 17 00:00:00 2001 From: CrescentLeaf Date: Tue, 5 Aug 2025 17:03:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=88=9B=E5=BB=BA=E6=97=B6=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E7=94=A8=E6=88=B7=E5=90=8D=E5=A4=B1=E6=95=88=20=20=20?= =?UTF-8?q?-=20=E6=9B=B4=E6=94=B9=E4=BA=86=E6=9F=A5=E8=AF=A2=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E8=BF=87=E5=A4=9A=E6=97=A0=E6=B3=95=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E5=A4=84=E7=90=86=E7=9A=84=E6=83=85=E5=86=B5=20(=E5=BD=93?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E7=BB=93=E6=9E=9C=20>=201=20=E6=97=B6,=20?= =?UTF-8?q?=E4=B9=9F=E5=88=A4=E5=AE=9A=E4=B8=BA=E7=94=A8=E6=88=B7=E4=B8=8D?= =?UTF-8?q?=E5=AD=98=E5=9C=A8)=20=20=20-=20=E6=B7=BB=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=90=8D,=20=E7=94=A8=E6=88=B7ID=20=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E6=97=B6=E7=9A=84=E6=8E=A7=E5=88=B6=E5=8F=B0=E8=AD=A6?= =?UTF-8?q?=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit chore: 为 SQL 语句添加分号 --- src/data/User.ts | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/data/User.ts b/src/data/User.ts index c055d80..8d93ea5 100644 --- a/src/data/User.ts +++ b/src/data/User.ts @@ -3,6 +3,8 @@ import { Buffer } from "node:buffer" import path from 'node:path' import crypto from 'node:crypto' +import chalk from 'chalk' + import config from '../config.ts' import UserBean from './UserBean.ts' @@ -15,7 +17,7 @@ export default class User { static table_name: string = "Users" private static database: DatabaseSync = User.init() private static init(): DatabaseSync { - const db: DatabaseSync = new DatabaseSync(path.join(config.data_path, 'Users.db')) + const db: DatabaseSync = new DatabaseSync(path.join(config.data_path, User.table_name + '.db')) db.exec(` CREATE TABLE IF NOT EXISTS ${User.table_name} ( /* 序号 */ count INTEGER PRIMARY KEY AUTOINCREMENT, @@ -31,18 +33,13 @@ export default class User { } static createWithUserNameChecked(userName: string | null, nickName: string, avatar: Buffer | null): User { - try { - User.findByUserName(userName) + if (User.findAllBeansByCondition('username = ?', userName).length > 0) throw new Error(`用户名 ${userName} 已存在`) - } catch (e) { - if (e.message.indexOf("找不到") == -1) - return User.create( - userName, - nickName, - avatar - ) - throw e - } + return User.create( + userName, + nickName, + avatar + ) } static create(userName: string | null, nickName: string, avatar: Buffer | null): User { @@ -56,7 +53,7 @@ export default class User { nickname, avatar, settings - ) VALUES (?, ?, ?, ?, ?, ?)`).run( + ) VALUES (?, ?, ?, ?, ?, ?);`).run( crypto.randomUUID(), Date.now(), userName, @@ -69,17 +66,23 @@ export default class User { } private static findAllBeansByCondition(condition: string, ...args: unknown[]): UserBean[] { - return User.database.prepare(`SELECT * FROM ${User.table_name} WHERE ${condition}`).all(...args) - } - private static checkLengthOrThrow(array: Array, leng: number, errMsg: string): Array { - if (array.length != leng) throw new Error(errMsg) - return array + return User.database.prepare(`SELECT * FROM ${User.table_name} WHERE ${condition};`).all(...args) } static findById(id: string): User { - return new User(checkLengthOrThrow(User.findAllBeansByCondition('id = ?', id), 1, `找不到用户 ID 为 ${id} 的用户`)[0]) + const beans = User.findAllBeansByCondition('id = ?', id) + if (beans.length == 0) + throw new Error(`找不到用户 ID 为 ${id} 的用户`) + else if (beans.length > 1) + console.error(chalk.red(`警告: 查询 id = ${id} 时, 查询到多个相同用户 ID 的用户`)) + return new User(beans[0]) } static findByUserName(userName: string): User { - return new User(checkLengthOrThrow(User.findAllBeansByCondition('username = ?', userName), 1, `找不到用户名为 ${userName} 的用户`)[0]) + const beans = User.findAllBeansByCondition('username = ?', userName) + if (beans.length == 0) + throw new Error(`找不到用户名为 ${userName} 的用户`) + else if (beans.length > 1) + console.error(chalk.red(`警告: 查询 username = ${userName} 时, 查询到多个相同用户名的用户`)) + return new User(beans[0]) } declare bean: UserBean