Skip to content
Snippets Groups Projects
Commit b7a44c7d authored by Henrik (Grubba) Grubbström's avatar Henrik (Grubba) Grubbström
Browse files

The host-argument can now have the following syntax:

[dbtype://][user[:password]@]hostname[:port][/database]

Rev: lib/modules/Sql.pmod/sql.pike:1.11
parent 35005499
Branches
Tags
No related merge requests found
/*
* $Id: sql.pike,v 1.10 1997/06/20 12:59:31 grubba Exp $
* $Id: sql.pike,v 1.11 1997/06/27 17:30:32 grubba Exp $
*
* Implements the generic parts of the SQL-interface
*
......@@ -8,7 +8,7 @@
//.
//. File: sql.pike
//. RCSID: $Id: sql.pike,v 1.10 1997/06/20 12:59:31 grubba Exp $
//. RCSID: $Id: sql.pike,v 1.11 1997/06/27 17:30:32 grubba Exp $
//. Author: Henrik Grubbstrm (grubba@infovav.se)
//.
//. Synopsis: Implements the generic parts of the SQL-interface.
......@@ -39,8 +39,14 @@ int case_convert;
//. Create a new generic SQL object.
//. > host
//. object - Use this object to access the SQL-database.
//. string - Use any available database server on this host.
//. If "" or 0, access through UNIX-domain socket or similar.
//. string - Connect to the server specified.
//. The string should be on the format:
//. [dbtype://][user[:password]@]hostname[:port][/database]
//. If dbtype isn't specified, use any available database server
//. on the specified host.
//. If the hostname is "", access through a UNIX-domain socket or
//. similar.
//. zero - Access through a UNIX-domain socket or similar.
//. > database
//. Select this database.
//. > user
......@@ -53,15 +59,66 @@ void create(void|string|object host, void|string db,
if (objectp(host)) {
master_sql = host;
if ((user && user != "") || (password && password != "")) {
throw_error("Sql(): Only the database argument is supported when "
throw_error("Sql.sql(): Only the database argument is supported when "
"first argument is an object\n");
}
if (db && db != "") {
master_sql->select_db(db);
}
return;
} else {
foreach(get_dir(Sql->dirname), string program_name) {
}
if (db == "") {
db = 0;
}
if (user == "") {
user = 0;
}
if (password == "") {
password = 0;
}
array(string) program_names;
if (host != replace(host, ({ ":", "/", "@" }), ({ "", "", "" }))) {
// The hostname is on the format:
//
// [dbtype://][user[:password]@]hostname[:port][/database]
array(string) arr = host/"://";
if ((sizeof(arr) > 1) && (arr[0] !="")) {
program_names = ({ arr[0] });
host = arr[1..] * "://";
}
arr = host/"@";
if (sizeof(arr) > 1) {
// User and/or password specified
host = arr[-1];
arr = (arr[0..sizeof(arr)-2]*"@")/":";
if (!user && sizeof(arr[0])) {
user = arr[0];
}
if (!password && (sizeof(arr) > 1)) {
password = arr[1..]*":";
if (password == "") {
password = 0;
}
}
}
arr = host/"/";
if (sizeof(arr) > 1) {
if (!db) {
db = arr[1..]*"/";
host = arr[0];
}
}
}
if (host == "") {
host = 0;
}
foreach(program_names || get_dir(Sql->dirname), string program_name) {
if ((sizeof(program_name / "_result") == 1) &&
(sizeof(program_name / ".pike") > 1) &&
(program_name != "sql.pike")) {
......@@ -88,13 +145,13 @@ void create(void|string|object host, void|string db,
if (p) {
err = catch {
if (password && password != "") {
if (password) {
master_sql = p(host||"", db||"", user||"", password);
} else if (user && user != "") {
} else if (user) {
master_sql = p(host||"", db||"", user);
} else if (db && db != "") {
} else if (db) {
master_sql = p(host||"", db);
} else if (host && host != "") {
} else if (host) {
master_sql = p(host);
} else {
master_sql = p();
......@@ -120,9 +177,13 @@ void create(void|string|object host, void|string db,
#endif /* PIKE_SQL_DEBUG */
}
}
}
throw_error("Sql(): Couldn't connect to database\n");
if (sizeof(program_names) > 1) {
throw_error("Sql.sql(): Couldn't connect using any of the databases\n");
} else {
throw_error("Sql.sql(): Couldn't connect using the " +
program_names[0] + " database\n");
}
}
static private array(mapping(string:mixed)) res_obj_to_array(object res_obj)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment