mirror of
https://github.com/executeautomation/mcp-database-server.git
synced 2025-12-09 21:12:57 +08:00
Added support for SQL Server
This commit is contained in:
74
src/db/adapter.ts
Normal file
74
src/db/adapter.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* Database adapter interface
|
||||
* Defines the contract for all database implementations (SQLite, SQL Server)
|
||||
*/
|
||||
export interface DbAdapter {
|
||||
/**
|
||||
* Initialize database connection
|
||||
*/
|
||||
init(): Promise<void>;
|
||||
|
||||
/**
|
||||
* Close database connection
|
||||
*/
|
||||
close(): Promise<void>;
|
||||
|
||||
/**
|
||||
* Execute a query and return all results
|
||||
* @param query SQL query to execute
|
||||
* @param params Query parameters
|
||||
*/
|
||||
all(query: string, params?: any[]): Promise<any[]>;
|
||||
|
||||
/**
|
||||
* Execute a query that modifies data
|
||||
* @param query SQL query to execute
|
||||
* @param params Query parameters
|
||||
*/
|
||||
run(query: string, params?: any[]): Promise<{ changes: number, lastID: number }>;
|
||||
|
||||
/**
|
||||
* Execute multiple SQL statements
|
||||
* @param query SQL statements to execute
|
||||
*/
|
||||
exec(query: string): Promise<void>;
|
||||
|
||||
/**
|
||||
* Get database metadata
|
||||
*/
|
||||
getMetadata(): { name: string, type: string, path?: string, server?: string, database?: string };
|
||||
|
||||
/**
|
||||
* Get database-specific query for listing tables
|
||||
*/
|
||||
getListTablesQuery(): string;
|
||||
|
||||
/**
|
||||
* Get database-specific query for describing a table
|
||||
* @param tableName Table name
|
||||
*/
|
||||
getDescribeTableQuery(tableName: string): string;
|
||||
}
|
||||
|
||||
// Import adapters using dynamic imports
|
||||
import { SqliteAdapter } from './sqlite-adapter.js';
|
||||
import { SqlServerAdapter } from './sqlserver-adapter.js';
|
||||
|
||||
/**
|
||||
* Factory function to create the appropriate database adapter
|
||||
*/
|
||||
export function createDbAdapter(type: string, connectionInfo: any): DbAdapter {
|
||||
switch (type.toLowerCase()) {
|
||||
case 'sqlite':
|
||||
// For SQLite, if connectionInfo is a string, use it directly as path
|
||||
if (typeof connectionInfo === 'string') {
|
||||
return new SqliteAdapter(connectionInfo);
|
||||
} else {
|
||||
return new SqliteAdapter(connectionInfo.path);
|
||||
}
|
||||
case 'sqlserver':
|
||||
return new SqlServerAdapter(connectionInfo);
|
||||
default:
|
||||
throw new Error(`Unsupported database type: ${type}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user