updates
This commit is contained in:
+58
-38
@@ -28,41 +28,52 @@ export function select<T extends {}>(db: Database, table: string, options?: Quer
|
||||
let query = `SELECT ${select} FROM ${table}\n`;
|
||||
const values = [];
|
||||
if (where) {
|
||||
query += `WHERE `;
|
||||
|
||||
const orConditions: string[] = [];
|
||||
if (where.or) {
|
||||
query += `${Object.keys(where.or)
|
||||
.map((k) => {
|
||||
values.push(where.or[k]);
|
||||
return `${k} = ?\n`;
|
||||
})
|
||||
.join('OR ')}`;
|
||||
where.or && Object.keys(where.or).forEach((k) => {
|
||||
values.push(where.or[k]);
|
||||
orConditions.push(`${k} = ?`);
|
||||
});
|
||||
delete where.or;
|
||||
}
|
||||
|
||||
if (Object.keys(where).length > 0) {
|
||||
query += `${Object.keys(where)
|
||||
.map((k) => {
|
||||
values.push(where[k]);
|
||||
return `${k} = ?\n`;
|
||||
})
|
||||
.join('AND ')}`;
|
||||
const andConditions: string[] = [];
|
||||
Object.keys(where).forEach((k) => {
|
||||
values.push(where[k]);
|
||||
andConditions.push(`${k} = ?`);
|
||||
});
|
||||
|
||||
if (orConditions.length > 0 || andConditions.length > 0) {
|
||||
query += 'WHERE ';
|
||||
const conditions: string[] = [];
|
||||
|
||||
if (orConditions.length > 0) {
|
||||
conditions.push(`(${orConditions.join(' OR ')})`);
|
||||
}
|
||||
|
||||
if (andConditions.length > 0) {
|
||||
conditions.push(andConditions.join(' AND '));
|
||||
}
|
||||
|
||||
query += conditions.join(' AND ');
|
||||
}
|
||||
}
|
||||
|
||||
if (options?.orderBy) {
|
||||
query += `ORDER BY\n${Object.keys(options.orderBy)
|
||||
query += `ORDER BY ${Object.keys(options.orderBy)
|
||||
.map((k) => {
|
||||
values.push(options.orderBy[k]);
|
||||
return `${k} ?`;
|
||||
return `${k} ${options.orderBy[k]}`;
|
||||
})
|
||||
.join(',\n')}\n`;
|
||||
.join(', ')}\n`;
|
||||
}
|
||||
|
||||
if (options?.skip) {
|
||||
values.push(options.limit || -1);
|
||||
values.push(options.skip);
|
||||
query += `LIMIT ? OFFSET ?`;
|
||||
if (options?.limit !== undefined) {
|
||||
values.push(options.limit);
|
||||
query += `LIMIT ?`;
|
||||
if (options?.skip) {
|
||||
values.push(options.skip);
|
||||
query += ` OFFSET ?`;
|
||||
}
|
||||
}
|
||||
|
||||
return db.prepare(query).all(...values) as T[];
|
||||
@@ -73,25 +84,34 @@ export function remove<T>(db: Database, table: string, where?: Where<T>) {
|
||||
|
||||
const values = [];
|
||||
if (where) {
|
||||
query += `WHERE `;
|
||||
|
||||
const orConditions: string[] = [];
|
||||
if (where.or) {
|
||||
query += `${Object.keys(where.or)
|
||||
.map((k) => {
|
||||
values.push(where.or[k]);
|
||||
return `${k} = ?\n`;
|
||||
})
|
||||
.join('OR ')}`;
|
||||
where.or && Object.keys(where.or).forEach((k) => {
|
||||
values.push(where.or[k]);
|
||||
orConditions.push(`${k} = ?`);
|
||||
});
|
||||
delete where.or;
|
||||
}
|
||||
|
||||
if (Object.keys(where).length > 0) {
|
||||
query += `${Object.keys(where)
|
||||
.map((k) => {
|
||||
values.push(where[k]);
|
||||
return `${k} = ?\n`;
|
||||
})
|
||||
.join('AND ')}`;
|
||||
const andConditions: string[] = [];
|
||||
Object.keys(where).forEach((k) => {
|
||||
values.push(where[k]);
|
||||
andConditions.push(`${k} = ?`);
|
||||
});
|
||||
|
||||
if (orConditions.length > 0 || andConditions.length > 0) {
|
||||
query += ' WHERE ';
|
||||
const conditions: string[] = [];
|
||||
|
||||
if (orConditions.length > 0) {
|
||||
conditions.push(`(${orConditions.join(' OR ')})`);
|
||||
}
|
||||
|
||||
if (andConditions.length > 0) {
|
||||
conditions.push(andConditions.join(' AND '));
|
||||
}
|
||||
|
||||
query += conditions.join(' AND ');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user