Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/plugins/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const packagePluginList = [
'drawing/baseChart',
'wiki',
'databaseConnection',
'databaseQuery',
'Doc2X',
'Doc2X/PDF2text',
'searchXNG',
Expand Down
51 changes: 51 additions & 0 deletions packages/plugins/src/databaseQuery/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Client as PgClient } from 'pg'; // PostgreSQL 客户端
import mysql from 'mysql2/promise'; // MySQL 客户端
import mssql from 'mssql'; // SQL Server 客户端

type Props = {
databaseType: string;
connectionString: string;
sql: string;
};

type Response = Promise<{
result: any; // 根据你的 SQL 查询结果类型调整
}>;

const main = async ({ databaseType, connectionString, sql }: Props): Response => {
let result;
try {
if (databaseType === 'PostgreSQL') {
const client = new PgClient(connectionString);

await client.connect();
const res = await client.query(sql);
result = res.rows;
await client.end();
} else if (databaseType === 'MySQL') {
const connection = await mysql.createConnection(connectionString);

const [rows] = await connection.execute(sql);
result = rows;
await connection.end();
} else if (databaseType === 'Microsoft SQL Server') {
const pool = await mssql.connect(connectionString);

result = await pool.query(sql);
await pool.close();
}
return {
result
};
} catch (error: unknown) {
// 使用类型断言来处理错误
if (error instanceof Error) {
console.error('Database query error:', error.message);
return Promise.reject(error.message);
}
console.error('Database query error:', error);
return Promise.reject('An unknown error occurred');
}
};

export default main;
Loading
Loading