|
1 | 1 | import getErrorMessage from '$lib/util/getErrorMsg'; |
2 | | -import type { DataRow, QueryRequestData, QueryResponseData, WorkerMessage } from '../types'; |
| 2 | +import type { |
| 3 | + DataRow, |
| 4 | + QueryRequestData, |
| 5 | + QueryResponseData, |
| 6 | + QueryStorageRequestData, |
| 7 | + QueryStorageResponseData, |
| 8 | + WorkerMessage |
| 9 | +} from '../types'; |
3 | 10 | import { db } from './initDb'; |
4 | 11 |
|
5 | 12 | export function handleQuery(msg: WorkerMessage<QueryRequestData>): QueryResponseData { |
@@ -32,3 +39,73 @@ export function handleQuery(msg: WorkerMessage<QueryRequestData>): QueryResponse |
32 | 39 | }; |
33 | 40 | } |
34 | 41 | } |
| 42 | + |
| 43 | +type SQLiteColInfo = { |
| 44 | + cid: number; |
| 45 | + dflt_value: string | null; |
| 46 | + name: string; |
| 47 | + notnull: 0 | 1; |
| 48 | + pk: 0 | 1; |
| 49 | + type: string; |
| 50 | +}; |
| 51 | + |
| 52 | +function prepareSearchTerm(searchTerm: string, storageId: string) { |
| 53 | + const colInfo = db.selectObjects(`PRAGMA table_info(${storageId});`) as SQLiteColInfo[]; |
| 54 | + |
| 55 | + const coalescedCols = colInfo.map((c) => `coalesce(${c.name}, '')`).join(' || '); |
| 56 | + |
| 57 | + return { |
| 58 | + where: `where (lower(${coalescedCols}) LIKE $searchTerm)`, |
| 59 | + bindVal: `%${searchTerm.replaceAll('%', '/%').toLowerCase()}%` |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +export function handleStorageQuery( |
| 64 | + msg: WorkerMessage<QueryStorageRequestData> |
| 65 | +): QueryStorageResponseData { |
| 66 | + try { |
| 67 | + let sql = `select * from ${msg.storageId} #WHERE# #ORDER_BY# #LIMIT#`; |
| 68 | + |
| 69 | + if (msg.data.orderByCol) { |
| 70 | + const dir = msg.data.orderByDir?.toLowerCase() === 'desc' ? 'desc' : 'asc'; |
| 71 | + |
| 72 | + sql = sql.replace('#ORDER_BY#', `order by ${msg.data.orderByCol} ${dir}`); |
| 73 | + } else { |
| 74 | + sql = sql.replace('#ORDER_BY#', 'order by 1'); |
| 75 | + } |
| 76 | + |
| 77 | + const binds: { [key: string]: number | string } = { |
| 78 | + $limit: msg.data.limit ?? 999999999 |
| 79 | + }; |
| 80 | + |
| 81 | + if (msg.data.offset) { |
| 82 | + sql = sql.replace('#LIMIT#', `limit $limit offset $offset`); |
| 83 | + binds['$offset'] = msg.data.offset; |
| 84 | + } else { |
| 85 | + sql = sql.replace('#LIMIT#', 'limit $limit'); |
| 86 | + } |
| 87 | + |
| 88 | + if (msg.data.searchTerm) { |
| 89 | + const { where, bindVal } = prepareSearchTerm(msg.data.searchTerm, msg.storageId); |
| 90 | + sql = sql.replace('#WHERE#', where); |
| 91 | + binds['$searchTerm'] = bindVal; |
| 92 | + } else { |
| 93 | + sql = sql.replace('#WHERE#', ``); |
| 94 | + } |
| 95 | + |
| 96 | + console.log('handleStorageQuery sql:', sql, binds); |
| 97 | + |
| 98 | + const data = db.selectObjects(sql, binds); |
| 99 | + |
| 100 | + return { |
| 101 | + rows: data |
| 102 | + }; |
| 103 | + } catch (err) { |
| 104 | + const errMsg = `Error getting rows for ${msg.storageId}: ${err}`; |
| 105 | + console.error(errMsg); |
| 106 | + return { |
| 107 | + rows: [], |
| 108 | + errorMsg: errMsg |
| 109 | + }; |
| 110 | + } |
| 111 | +} |
0 commit comments