Skip to content

Commit 0eee5a4

Browse files
refactor: streamline error handling and improve code readability in database connectors
- Consolidated error message formatting for DSN parsing across all database connectors. - Simplified schema clause definitions in MySQL, MariaDB, and SQLite connectors for better readability. - Enhanced consistency in code structure by reducing line breaks in relevant sections.
1 parent 6a2678a commit 0eee5a4

File tree

5 files changed

+15
-41
lines changed

5 files changed

+15
-41
lines changed

src/connectors/mariadb/index.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ class MariadbDSNParser implements DSNParser {
4242

4343
return config;
4444
} catch (error) {
45-
throw new Error(
46-
`Failed to parse MariaDB DSN: ${error instanceof Error ? error.message : String(error)}`
47-
);
45+
throw new Error(`Failed to parse MariaDB DSN: ${error instanceof Error ? error.message : String(error)}`);
4846
}
4947
}
5048

@@ -295,9 +293,7 @@ export class MariaDBConnector implements Connector {
295293

296294
try {
297295
// In MariaDB, if no schema is provided, use the current database context
298-
const schemaClause = schema
299-
? 'WHERE ROUTINE_SCHEMA = ?'
300-
: 'WHERE ROUTINE_SCHEMA = DATABASE()';
296+
const schemaClause = schema ? 'WHERE ROUTINE_SCHEMA = ?' : 'WHERE ROUTINE_SCHEMA = DATABASE()';
301297

302298
const queryParams = schema ? [schema] : [];
303299

@@ -326,9 +322,7 @@ export class MariaDBConnector implements Connector {
326322

327323
try {
328324
// In MariaDB, if no schema is provided, use the current database context
329-
const schemaClause = schema
330-
? 'WHERE r.ROUTINE_SCHEMA = ?'
331-
: 'WHERE r.ROUTINE_SCHEMA = DATABASE()';
325+
const schemaClause = schema ? 'WHERE r.ROUTINE_SCHEMA = ?' : 'WHERE r.ROUTINE_SCHEMA = DATABASE()';
332326

333327
const queryParams = schema ? [schema, procedureName] : [procedureName];
334328

src/connectors/mysql/index.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ class MySQLDSNParser implements DSNParser {
4242

4343
return config;
4444
} catch (error) {
45-
throw new Error(
46-
`Failed to parse MySQL DSN: ${error instanceof Error ? error.message : String(error)}`
47-
);
45+
throw new Error(`Failed to parse MySQL DSN: ${error instanceof Error ? error.message : String(error)}`);
4846
}
4947
}
5048

@@ -293,9 +291,7 @@ export class MySQLConnector implements Connector {
293291

294292
try {
295293
// In MySQL, if no schema is provided, use the current database context
296-
const schemaClause = schema
297-
? 'WHERE ROUTINE_SCHEMA = ?'
298-
: 'WHERE ROUTINE_SCHEMA = DATABASE()';
294+
const schemaClause = schema ? 'WHERE ROUTINE_SCHEMA = ?' : 'WHERE ROUTINE_SCHEMA = DATABASE()';
299295

300296
const queryParams = schema ? [schema] : [];
301297

@@ -324,9 +320,7 @@ export class MySQLConnector implements Connector {
324320

325321
try {
326322
// In MySQL, if no schema is provided, use the current database context
327-
const schemaClause = schema
328-
? 'WHERE r.ROUTINE_SCHEMA = ?'
329-
: 'WHERE r.ROUTINE_SCHEMA = DATABASE()';
323+
const schemaClause = schema ? 'WHERE r.ROUTINE_SCHEMA = ?' : 'WHERE r.ROUTINE_SCHEMA = DATABASE()';
330324

331325
const queryParams = schema ? [schema, procedureName] : [procedureName];
332326

src/connectors/postgres/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ class PostgresDSNParser implements DSNParser {
4545

4646
return config;
4747
} catch (error) {
48-
throw new Error(
49-
`Failed to parse PostgreSQL DSN: ${error instanceof Error ? error.message : String(error)}`
50-
);
48+
throw new Error(`Failed to parse PostgreSQL DSN: ${error instanceof Error ? error.message : String(error)}`);
5149
}
5250
}
5351

src/connectors/sqlite/index.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ class SQLiteDSNParser implements DSNParser {
5555

5656
return { dbPath };
5757
} catch (error) {
58-
throw new Error(
59-
`Failed to parse SQLite DSN: ${error instanceof Error ? error.message : String(error)}`
60-
);
58+
throw new Error(`Failed to parse SQLite DSN: ${error instanceof Error ? error.message : String(error)}`);
6159
}
6260
}
6361

@@ -212,9 +210,7 @@ export class SQLiteConnector implements Connector {
212210
.all(tableName) as { index_name: string; is_unique: number }[];
213211

214212
// Get the primary key info
215-
const tableInfo = this.db
216-
.prepare(`PRAGMA table_info(${tableName})`)
217-
.all() as SQLiteTableInfo[];
213+
const tableInfo = this.db.prepare(`PRAGMA table_info(${tableName})`).all() as SQLiteTableInfo[];
218214

219215
// Find primary key columns
220216
const pkColumns = tableInfo.filter((col) => col.pk > 0).map((col) => col.name);
@@ -224,9 +220,9 @@ export class SQLiteConnector implements Connector {
224220
// Add regular indexes
225221
for (const indexInfo of indexInfoRows) {
226222
// Get the columns for this index
227-
const indexDetailRows = this.db
228-
.prepare(`PRAGMA index_info(${indexInfo.index_name})`)
229-
.all() as { name: string }[];
223+
const indexDetailRows = this.db.prepare(`PRAGMA index_info(${indexInfo.index_name})`).all() as {
224+
name: string;
225+
}[];
230226
const columnNames = indexDetailRows.map((row) => row.name);
231227

232228
results.push({

src/connectors/sqlserver/index.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ export class SQLServerDSNParser implements DSNParser {
1919
async parse(dsn: string): Promise<sql.config> {
2020
// Remove the protocol prefix
2121
if (!this.isValidDSN(dsn)) {
22-
throw new Error(
23-
'Invalid SQL Server DSN format. Expected: sqlserver://username:password@host:port/database'
24-
);
22+
throw new Error('Invalid SQL Server DSN format. Expected: sqlserver://username:password@host:port/database');
2523
}
2624

2725
// Parse the DSN
@@ -402,14 +400,8 @@ export class SQLServerConnector implements Connector {
402400
if (parameterResult.recordset.length > 0) {
403401
parameterList = parameterResult.recordset
404402
.map(
405-
(param: {
406-
CHARACTER_MAXIMUM_LENGTH: number;
407-
PARAMETER_NAME: any;
408-
PARAMETER_MODE: any;
409-
DATA_TYPE: any;
410-
}) => {
411-
const lengthStr =
412-
param.CHARACTER_MAXIMUM_LENGTH > 0 ? `(${param.CHARACTER_MAXIMUM_LENGTH})` : '';
403+
(param: { CHARACTER_MAXIMUM_LENGTH: number; PARAMETER_NAME: any; PARAMETER_MODE: any; DATA_TYPE: any }) => {
404+
const lengthStr = param.CHARACTER_MAXIMUM_LENGTH > 0 ? `(${param.CHARACTER_MAXIMUM_LENGTH})` : '';
413405
return `${param.PARAMETER_NAME} ${param.PARAMETER_MODE} ${param.DATA_TYPE}${lengthStr}`;
414406
}
415407
)

0 commit comments

Comments
 (0)