-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HCK-8611: connection string parser (#116)
* fix: connection string parser * feat: improved connection string detection of mssql format * fix: connection string parser * fix: aligned improvements between plugins * fix: unified helpers
- Loading branch information
1 parent
991c86d
commit 374e600
Showing
1 changed file
with
24 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
const { URL } = require('url'); | ||
const { ConnectionPool } = require('mssql'); | ||
const { URL } = require('url'); | ||
|
||
const mssqlPrefix = 'mssql://'; | ||
const sqlserverPrefix = 'jdbc:sqlserver://'; | ||
|
@@ -8,21 +8,21 @@ const sqlserverPrefix = 'jdbc:sqlserver://'; | |
const parseMssqlUrl = ({ url = '' }) => { | ||
const parsed = new URL(url); | ||
return { | ||
database: parsed.pathname.slice(1), | ||
host: parsed.hostname, | ||
port: parsed.port ? Number(parsed.port) : null, | ||
databaseName: parsed.pathname.slice(1), | ||
userName: parsed.username, | ||
userPassword: parsed.password, | ||
}; | ||
}; | ||
|
||
// example: jdbc:sqlserver://synapseworkspace.sql.azuresynapse.net:1433;databaseName=SampleDB;[email protected];password=myStrongPassword123;encrypt=true;trustServerCertificate=false;authentication=ActiveDirectoryPassword;loginTimeout=30; | ||
const parseSqlServerUrl = ({ url = '' }) => { | ||
const [_protocol, params] = url.split(sqlserverPrefix); | ||
const [server, ...paramParts] = params.split(';'); | ||
const [_protocol, urlParams] = url.split(sqlserverPrefix); | ||
const [server, ...paramParts] = urlParams.split(';'); | ||
const [host, port] = server.split(':'); | ||
|
||
const parsedParams = paramParts.reduce((acc, part) => { | ||
const params = paramParts.reduce((acc, part) => { | ||
const [key, value] = part.split('='); | ||
if (key && value) { | ||
acc[key] = value; | ||
|
@@ -31,33 +31,34 @@ const parseSqlServerUrl = ({ url = '' }) => { | |
}, {}); | ||
|
||
return { | ||
server: host, | ||
host, | ||
port: port ? Number(port) : null, | ||
database: parsedParams.databaseName, | ||
user: parsedParams.user, | ||
password: parsedParams.password, | ||
databaseName: params.databaseName, | ||
userName: params.user, | ||
userPassword: params.password, | ||
}; | ||
}; | ||
|
||
// example: Server=tcp:synapseworkspace.sql.azuresynapse.net,1433;Database=SampleDB;Authentication=Active Directory Password;User [email protected];Password=password;Encrypt=true;TrustServerCertificate=false;Connection Timeout=30; | ||
const parseBasicString = ({ string = '' }) => { | ||
const parsed = ConnectionPool.parseConnectionString(string); | ||
return { | ||
databaseName: parsed.database, | ||
host: parsed.server, | ||
port: parsed.port, | ||
userName: parsed.user, | ||
userPassword: parsed.password, | ||
}; | ||
}; | ||
|
||
// Default connection string example: | ||
// Server=host,1433;Database=DatabaseName;User Id=username;Password=password; | ||
const parseConnectionString = ({ string = '' }) => { | ||
let params; | ||
if (string.startsWith(sqlserverPrefix)) { | ||
params = parseSqlServerUrl({ url: string }); | ||
return parseSqlServerUrl({ url: string }); | ||
} else if (string.startsWith(mssqlPrefix)) { | ||
params = parseMssqlUrl({ url: string }); | ||
return parseMssqlUrl({ url: string }); | ||
} else { | ||
params = ConnectionPool.parseConnectionString(string); | ||
return parseBasicString({ string }); | ||
} | ||
|
||
return { | ||
databaseName: params.database, | ||
host: params.server, | ||
port: params.port, | ||
userName: params.user, | ||
userPassword: params.password, | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
|