v2.0.0
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
import { getDB } from '../../db'
|
||||
import {
|
||||
createLyricQueryStatement,
|
||||
createRawLyricQueryStatement,
|
||||
createRawLyricInsertStatement,
|
||||
createRawLyricDeleteStatement,
|
||||
createRawLyricUpdateStatement,
|
||||
createRawLyricClearStatement,
|
||||
createEditedLyricQueryStatement,
|
||||
createEditedLyricInsertStatement,
|
||||
createEditedLyricDeleteStatement,
|
||||
createEditedLyricUpdateStatement,
|
||||
createEditedLyricClearStatement,
|
||||
createEditedLyricCountStatement,
|
||||
createRawLyricCountStatement,
|
||||
} from './statements'
|
||||
|
||||
/**
|
||||
* 查询原始歌词
|
||||
* @param id 歌曲id
|
||||
* @returns 歌词信息
|
||||
*/
|
||||
export const queryLyric = (id: string): LX.DBService.Lyricnfo[] => {
|
||||
const lyricQueryStatement = createLyricQueryStatement()
|
||||
return lyricQueryStatement.all(id)
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询原始歌词
|
||||
* @param id 歌曲id
|
||||
* @returns 歌词信息
|
||||
*/
|
||||
export const queryRawLyric = (id: string): LX.DBService.Lyricnfo[] => {
|
||||
const rawLyricQueryStatement = createRawLyricQueryStatement()
|
||||
return rawLyricQueryStatement.all(id)
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量插入原始歌词
|
||||
* @param lyrics 列表
|
||||
*/
|
||||
export const inertRawLyric = (lyrics: LX.DBService.Lyricnfo[]) => {
|
||||
const db = getDB()
|
||||
const rawLyricInsertStatement = createRawLyricInsertStatement()
|
||||
db.transaction((lyrics: LX.DBService.Lyricnfo[]) => {
|
||||
for (const lyric of lyrics) rawLyricInsertStatement.run(lyric)
|
||||
})(lyrics)
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除原始歌词
|
||||
* @param ids 列表
|
||||
*/
|
||||
export const deleteRawLyric = (ids: string[]) => {
|
||||
const db = getDB()
|
||||
const rawLyricDeleteStatement = createRawLyricDeleteStatement()
|
||||
db.transaction((ids: string[]) => {
|
||||
for (const id of ids) rawLyricDeleteStatement.run(id)
|
||||
})(ids)
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新原始歌词
|
||||
* @param lyrics 列表
|
||||
*/
|
||||
export const updateRawLyric = (lyrics: LX.DBService.Lyricnfo[]) => {
|
||||
const db = getDB()
|
||||
const rawLyricUpdateStatement = createRawLyricUpdateStatement()
|
||||
db.transaction((lyrics: LX.DBService.Lyricnfo[]) => {
|
||||
for (const lyric of lyrics) rawLyricUpdateStatement.run(lyric)
|
||||
})(lyrics)
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空原始歌词
|
||||
*/
|
||||
export const clearRawLyric = () => {
|
||||
const rawLyricClearStatement = createRawLyricClearStatement()
|
||||
rawLyricClearStatement.run()
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计已编辑歌词数量
|
||||
*/
|
||||
export const countRawLyric = () => {
|
||||
const countStatement = createRawLyricCountStatement()
|
||||
return countStatement.get().count
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查询已编辑歌词
|
||||
* @param id 歌曲id
|
||||
* @returns 歌词信息
|
||||
*/
|
||||
export const queryEditedLyric = (id: string): LX.DBService.Lyricnfo[] => {
|
||||
const rawLyricQueryStatement = createEditedLyricQueryStatement()
|
||||
return rawLyricQueryStatement.all(id)
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量插入已编辑歌词
|
||||
* @param lyrics 列表
|
||||
*/
|
||||
export const inertEditedLyric = (lyrics: LX.DBService.Lyricnfo[]) => {
|
||||
const db = getDB()
|
||||
const rawLyricInsertStatement = createEditedLyricInsertStatement()
|
||||
db.transaction((lyrics: LX.DBService.Lyricnfo[]) => {
|
||||
for (const lyric of lyrics) rawLyricInsertStatement.run(lyric)
|
||||
})(lyrics)
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除已编辑歌词
|
||||
* @param ids 列表
|
||||
*/
|
||||
export const deleteEditedLyric = (ids: string[]) => {
|
||||
const db = getDB()
|
||||
const rawLyricDeleteStatement = createEditedLyricDeleteStatement()
|
||||
db.transaction((ids: string[]) => {
|
||||
for (const id of ids) rawLyricDeleteStatement.run(id)
|
||||
})(ids)
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新已编辑歌词
|
||||
* @param lyrics 列表
|
||||
*/
|
||||
export const updateEditedLyric = (lyrics: LX.DBService.Lyricnfo[]) => {
|
||||
const db = getDB()
|
||||
const rawLyricUpdateStatement = createEditedLyricUpdateStatement()
|
||||
db.transaction((lyrics: LX.DBService.Lyricnfo[]) => {
|
||||
for (const lyric of lyrics) rawLyricUpdateStatement.run(lyric)
|
||||
})(lyrics)
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空已编辑歌词
|
||||
*/
|
||||
export const clearEditedLyric = () => {
|
||||
const rawLyricClearStatement = createEditedLyricClearStatement()
|
||||
rawLyricClearStatement.run()
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 统计已编辑歌词数量
|
||||
*/
|
||||
export const countEditedLyric = () => {
|
||||
const countStatement = createEditedLyricCountStatement()
|
||||
return countStatement.get().count
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
import {
|
||||
queryLyric,
|
||||
queryRawLyric,
|
||||
inertRawLyric,
|
||||
deleteRawLyric,
|
||||
updateRawLyric,
|
||||
clearRawLyric,
|
||||
queryEditedLyric,
|
||||
inertEditedLyric,
|
||||
deleteEditedLyric,
|
||||
updateEditedLyric,
|
||||
clearEditedLyric,
|
||||
countEditedLyric,
|
||||
countRawLyric,
|
||||
} from './dbHelper'
|
||||
|
||||
const keys = ['lyric', 'tlyric', 'rlyric', 'lxlyric'] as const
|
||||
|
||||
const toDBLyric = (id: string, source: LX.DBService.Lyricnfo['source'], lyricInfo: LX.Music.LyricInfo): LX.DBService.Lyricnfo[] => {
|
||||
return (keys.map(k => [k, lyricInfo[k]])
|
||||
.filter(([k, t]) => t != null) as Array<[LX.DBService.Lyricnfo['type'], string]>)
|
||||
.map(([k, t]) => {
|
||||
return {
|
||||
id,
|
||||
type: k,
|
||||
text: Buffer.from(t).toString('base64'),
|
||||
source,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取歌词
|
||||
* @param id 歌曲id
|
||||
* @returns 歌词信息
|
||||
*/
|
||||
export const getPlayerLyric = (id: string): LX.Player.LyricInfo => {
|
||||
const lyrics = queryLyric(id)
|
||||
|
||||
let lyricInfo: LX.Music.LyricInfo = {
|
||||
lyric: '',
|
||||
}
|
||||
let rawLyricInfo: LX.Music.LyricInfo = {
|
||||
lyric: '',
|
||||
}
|
||||
for (const lyric of lyrics) {
|
||||
switch (lyric.source) {
|
||||
case 'edited':
|
||||
if (lyric.type == 'lyric') lyricInfo.lyric = Buffer.from(lyric.text, 'base64').toString()
|
||||
else if (lyric.text != null) lyricInfo[lyric.type] = Buffer.from(lyric.text, 'base64').toString()
|
||||
break
|
||||
default:
|
||||
if (lyric.type == 'lyric') rawLyricInfo.lyric = Buffer.from(lyric.text, 'base64').toString()
|
||||
else if (lyric.text != null) rawLyricInfo[lyric.type] = Buffer.from(lyric.text, 'base64').toString()
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return lyricInfo.lyric ? {
|
||||
...lyricInfo,
|
||||
rawlrcInfo: rawLyricInfo,
|
||||
} : {
|
||||
...rawLyricInfo,
|
||||
rawlrcInfo: rawLyricInfo,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取原始歌词
|
||||
* @param id 歌曲id
|
||||
* @returns 歌词信息
|
||||
*/
|
||||
export const getRawLyric = (id: string): LX.Music.LyricInfo => {
|
||||
const lyrics = queryRawLyric(id)
|
||||
|
||||
let lyricInfo: LX.Music.LyricInfo = {
|
||||
lyric: '',
|
||||
}
|
||||
for (const lyric of lyrics) {
|
||||
if (lyric.type == 'lyric') lyricInfo.lyric = Buffer.from(lyric.text, 'base64').toString()
|
||||
else if (lyric.text != null) lyricInfo[lyric.type] = Buffer.from(lyric.text, 'base64').toString()
|
||||
}
|
||||
|
||||
return lyricInfo
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存原始歌词信息
|
||||
* @param id 歌曲id
|
||||
* @param lyricInfo 歌词信息
|
||||
*/
|
||||
export const rawLyricAdd = (id: string, lyricInfo: LX.Music.LyricInfo) => {
|
||||
inertRawLyric(toDBLyric(id, 'raw', lyricInfo))
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除原始歌词信息
|
||||
* @param ids 歌曲id
|
||||
*/
|
||||
export const rawLyricRemove = (ids: string[]) => {
|
||||
deleteRawLyric(ids)
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新原始歌词信息
|
||||
* @param id 歌曲id
|
||||
* @param lyricInfo 歌词信息
|
||||
*/
|
||||
export const rawLyricUpdate = (id: string, lyricInfo: LX.Music.LyricInfo) => {
|
||||
updateRawLyric(toDBLyric(id, 'raw', lyricInfo))
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空原始歌词信息
|
||||
*/
|
||||
export const rawLyricClear = () => {
|
||||
clearRawLyric()
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计原始歌词数量
|
||||
*/
|
||||
export const rawLyricCount = () => {
|
||||
return countRawLyric()
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取已编辑歌词
|
||||
* @param id 歌曲id
|
||||
* @returns 歌词信息
|
||||
*/
|
||||
export const getEditedLyric = (id: string): LX.Music.LyricInfo => {
|
||||
const lyrics = queryEditedLyric(id)
|
||||
|
||||
let lyricInfo: LX.Music.LyricInfo = {
|
||||
lyric: '',
|
||||
}
|
||||
for (const lyric of lyrics) {
|
||||
if (lyric.type == 'lyric') lyricInfo.lyric = Buffer.from(lyric.text, 'base64').toString()
|
||||
else if (lyric.text != null) lyricInfo[lyric.type] = Buffer.from(lyric.text, 'base64').toString()
|
||||
}
|
||||
|
||||
return lyricInfo
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存已编辑歌词信息
|
||||
* @param id 歌曲id
|
||||
* @param lyricInfo 歌词信息
|
||||
*/
|
||||
export const editedLyricAdd = (id: string, lyricInfo: LX.Music.LyricInfo) => {
|
||||
inertEditedLyric(toDBLyric(id, 'edited', lyricInfo))
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除已编辑歌词信息
|
||||
* @param ids 歌曲id
|
||||
*/
|
||||
export const editedLyricRemove = (ids: string[]) => {
|
||||
deleteEditedLyric(ids)
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新已编辑歌词信息
|
||||
* @param id 歌曲id
|
||||
* @param lyricInfo 歌词信息
|
||||
*/
|
||||
export const editedLyricUpdate = (id: string, lyricInfo: LX.Music.LyricInfo) => {
|
||||
updateEditedLyric(toDBLyric(id, 'edited', lyricInfo))
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空已编辑歌词信息
|
||||
*/
|
||||
export const editedLyricClear = () => {
|
||||
clearEditedLyric()
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增或更新已编辑歌词信息
|
||||
* @param id 歌曲id
|
||||
* @param lyricInfo 歌词信息
|
||||
*/
|
||||
export const editedLyricUpdateAddAndUpdate = (id: string, lyricInfo: LX.Music.LyricInfo) => {
|
||||
const lyrics = queryEditedLyric(id)
|
||||
if (lyrics.length) updateEditedLyric(toDBLyric(id, 'edited', lyricInfo))
|
||||
else inertEditedLyric(toDBLyric(id, 'edited', lyricInfo))
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计已编辑歌词数量
|
||||
*/
|
||||
export const editedLyricCount = () => {
|
||||
return countEditedLyric()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
import { getDB } from '../../db'
|
||||
|
||||
const RAW_LYRIC = 'raw'
|
||||
const EDITED_LYRIC = 'edited'
|
||||
|
||||
/**
|
||||
* 创建歌词查询语句
|
||||
* @returns 查询语句
|
||||
*/
|
||||
export const createLyricQueryStatement = () => {
|
||||
const db = getDB()
|
||||
return db.prepare<[string]>(`
|
||||
SELECT "type", "text", "source"
|
||||
FROM "main"."lyric"
|
||||
WHERE "id"=?
|
||||
`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建原始歌词查询语句
|
||||
* @returns 查询语句
|
||||
*/
|
||||
export const createRawLyricQueryStatement = () => {
|
||||
const db = getDB()
|
||||
return db.prepare<[string]>(`
|
||||
SELECT "type", "text"
|
||||
FROM "main"."lyric"
|
||||
WHERE "id"=? AND "source"='${RAW_LYRIC}'
|
||||
`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建原始歌词插入语句
|
||||
* @returns 插入语句
|
||||
*/
|
||||
export const createRawLyricInsertStatement = () => {
|
||||
const db = getDB()
|
||||
return db.prepare<[LX.DBService.Lyricnfo]>(`
|
||||
INSERT INTO "main"."lyric" ("id", "type", "text", "source")
|
||||
VALUES (@id, @type, @text, '${RAW_LYRIC}')`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建原始歌词清空语句
|
||||
* @returns 清空语句
|
||||
*/
|
||||
export const createRawLyricClearStatement = () => {
|
||||
const db = getDB()
|
||||
return db.prepare(`
|
||||
DELETE FROM "main"."lyric"
|
||||
WHERE "source"='${RAW_LYRIC}'
|
||||
`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建原始歌词删除语句
|
||||
* @returns 删除语句
|
||||
*/
|
||||
export const createRawLyricDeleteStatement = () => {
|
||||
const db = getDB()
|
||||
return db.prepare<[string]>(`
|
||||
DELETE FROM "main"."lyric"
|
||||
WHERE "id"=? AND "source"='${RAW_LYRIC}'
|
||||
`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建原始歌词更新语句
|
||||
* @returns 更新语句
|
||||
*/
|
||||
export const createRawLyricUpdateStatement = () => {
|
||||
const db = getDB()
|
||||
return db.prepare<[LX.DBService.Lyricnfo]>(`
|
||||
UPDATE "main"."lyric"
|
||||
SET "text"=@text
|
||||
WHERE "id"=@id AND "source"='${RAW_LYRIC}' AND "type"=@type`)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建原始歌词数量统计语句
|
||||
* @returns 统计语句
|
||||
*/
|
||||
export const createRawLyricCountStatement = () => {
|
||||
const db = getDB()
|
||||
return db.prepare(`SELECT COUNT(*) as count FROM "main"."lyric" WHERE "source"='${RAW_LYRIC}'`)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建已编辑歌词查询语句
|
||||
* @returns 查询语句
|
||||
*/
|
||||
export const createEditedLyricQueryStatement = () => {
|
||||
const db = getDB()
|
||||
return db.prepare<[string]>(`
|
||||
SELECT "type", "text"
|
||||
FROM "main"."lyric"
|
||||
WHERE "id"=? AND "source"='${EDITED_LYRIC}'
|
||||
`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建已编辑歌词插入语句
|
||||
* @returns 插入语句
|
||||
*/
|
||||
export const createEditedLyricInsertStatement = () => {
|
||||
const db = getDB()
|
||||
return db.prepare<[LX.DBService.Lyricnfo]>(`
|
||||
INSERT INTO "main"."lyric" ("id", "type", "text", "source")
|
||||
VALUES (@id, @type, @text, '${EDITED_LYRIC}')`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建已编辑歌词清空语句
|
||||
* @returns 清空语句
|
||||
*/
|
||||
export const createEditedLyricClearStatement = () => {
|
||||
const db = getDB()
|
||||
return db.prepare(`
|
||||
DELETE FROM "main"."lyric"
|
||||
WHERE "source"='${EDITED_LYRIC}'
|
||||
`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建已编辑歌词删除语句
|
||||
* @returns 删除语句
|
||||
*/
|
||||
export const createEditedLyricDeleteStatement = () => {
|
||||
const db = getDB()
|
||||
return db.prepare<[string]>(`
|
||||
DELETE FROM "main"."lyric"
|
||||
WHERE "id"=? AND "source"='${EDITED_LYRIC}'
|
||||
`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建已编辑歌词更新语句
|
||||
* @returns 更新语句
|
||||
*/
|
||||
export const createEditedLyricUpdateStatement = () => {
|
||||
const db = getDB()
|
||||
return db.prepare<[LX.DBService.Lyricnfo]>(`
|
||||
UPDATE "main"."lyric"
|
||||
SET "text"=@text
|
||||
WHERE "id"=@id AND "source"='${EDITED_LYRIC}' AND "type"=@type`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建已编辑歌词数量统计语句
|
||||
* @returns 统计语句
|
||||
*/
|
||||
export const createEditedLyricCountStatement = () => {
|
||||
const db = getDB()
|
||||
return db.prepare(`SELECT COUNT(*) as count FROM "main"."lyric" WHERE "source"='${EDITED_LYRIC}'`)
|
||||
}
|
||||
Reference in New Issue
Block a user