Skip to content

Commit

Permalink
cater for sqlserver and oracle being defined in connection string (bo…
Browse files Browse the repository at this point in the history
…th will be ODBC driver)
  • Loading branch information
SingingBush committed Feb 12, 2021
1 parent 1dd5624 commit d5a8a1f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ DDBC

[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/buggins/ddbc?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

![CI](https://github.com/buggins/ddbc/workflows/CI/badge.svg)
[![CI](https://github.com/buggins/ddbc/workflows/CI/badge.svg)](https://github.com/buggins/ddbc/actions?query=workflow%3ACI)

[![Build Status](https://travis-ci.org/buggins/ddbc.svg?branch=master)](https://travis-ci.org/buggins/ddbc)

Expand Down Expand Up @@ -148,9 +148,23 @@ ddbc:postgresql://127.0.0.1:5432
### Microsoft SQL Server (via ODBC)

```
ddbc:sqlserver://localhost,1433?user=sa,password=bbk4k77JKH88g54,driver=FreeTDS
or
ddbc:odbc://localhost,1433?user=sa,password=bbk4k77JKH88g54,driver=FreeTDS
```

### Oracle (via ODBC) **experimental**

```
ddbc:oracle://localhost:1521?user=sa,password=bbk4k77JKH88g54,driver=FreeTDS
or
ddbc:odbc://localhost:1521?user=sa,password=bbk4k77JKH88g54,driver=FreeTDS
```

### DSN connections for Microsoft SQL Server
The correct format to use for a dsn connection string is `odbc://?dsn=<DSN name>`.
Note that the server portion before the `?` is empty, so the default server for
Expand Down
6 changes: 3 additions & 3 deletions source/ddbc/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,9 @@ string extractDriverNameFromURL(string url) {
url = stripDdbcPrefix(url);
import std.string;
int colonPos = cast(int)url.indexOf(":");
if (colonPos < 0)
return url;
return url[0 .. colonPos];

string dbName = colonPos < 0 ? url : url[0 .. colonPos];
return dbName == "sqlserver" || dbName == "oracle" ? "odbc" : dbName;
}

/// extract parameters from URL string to string[string] map, update url to strip params
Expand Down
17 changes: 16 additions & 1 deletion source/ddbc/core.d
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ string makeDDBCUrl(string driverName, string[string] params) {
string makeDDBCUrl(string driverName, string host = null, int port = 0,
string dbName = null, string[string] params = null) {
import std.algorithm.searching : canFind;
enforce(canFind(["sqlite", "postgresql", "mysql", "odbc"], driverName), "driver must be one of sqlite|postgresql|mysql|odbc");
enforce(canFind(["sqlite", "postgresql", "mysql", "sqlserver", "oracle", "odbc"], driverName), "driver must be one of sqlite|postgresql|mysql|sqlserver|oracle|odbc");
import std.conv : to;
char[] res;
res.assumeSafeAppend;
Expand Down Expand Up @@ -462,6 +462,21 @@ private unittest {
assert(url == "ddbc:mysql://127.0.0.1:3306/mydb", "MySQL URL is not correct: "~url);
}

private unittest {
string url = makeDDBCUrl("sqlserver", "127.0.0.1", 1433, "mydb");
assert(url == "ddbc:sqlserver://127.0.0.1:1433/mydb", "SQL Server URL is not correct: "~url);
}

private unittest {
string url = makeDDBCUrl("oracle", "127.0.0.1", 1521, "mydb");
assert(url == "ddbc:oracle://127.0.0.1:1521/mydb", "Oracle URL is not correct: "~url);
}

private unittest {
string url = makeDDBCUrl("odbc", "127.0.0.1", 3306, "mydb");
assert(url == "ddbc:odbc://127.0.0.1:3306/mydb", "ODBC URL is not correct: "~url);
}

private unittest {
string[string] params;
params["user"] = "sa";
Expand Down

0 comments on commit d5a8a1f

Please sign in to comment.