| 1 |
|
| 2 |
#simulation of the real icondb.icn |
| 3 |
#using the C mysql and postgresql interfaces for Icon 9.4. |
| 4 |
#use with cgi.icn (instead of web.icn) |
| 5 |
#until loadfuncpp becomes reliable |
| 6 |
|
| 7 |
#WARNING: can only connect to one mysql and one postgresql database at a time |
| 8 |
|
| 9 |
#CS 2008/7/27 |
| 10 |
|
| 11 |
|
| 12 |
link io |
| 13 |
|
| 14 |
#the C interface |
| 15 |
procedure _mysqldb(arg[]) |
| 16 |
return ( _mysqldb := pathload("mysqldb.so","mysqldb") )!arg |
| 17 |
end |
| 18 |
|
| 19 |
procedure _postgresqldb(arg[]) |
| 20 |
return ( _postgresqldb := pathload("postgresqldb.so","postgresqldb") )!arg |
| 21 |
end |
| 22 |
|
| 23 |
#simulated external value |
| 24 |
record database_handle(connection, c_interface) |
| 25 |
|
| 26 |
#simulated mysql connection procedure |
| 27 |
procedure _connectmysql(dbname, user, pwd, host, port) |
| 28 |
local connection, result |
| 29 |
icondb_error := &null |
| 30 |
connection := [dbname, user, pwd] |
| 31 |
if put(connection, \host) then put(connection, \port) |
| 32 |
result := _mysqldb(connection) |
| 33 |
if /result then return database_handle(connection, _mysqldb) |
| 34 |
icondb_error := result |
| 35 |
fail |
| 36 |
end |
| 37 |
|
| 38 |
#simulated postgresql connection procedure |
| 39 |
procedure _connectpostgresql(dbname, user, pwd, host, port) |
| 40 |
local connection, result |
| 41 |
icondb_error := &null |
| 42 |
connection := [dbname, user, pwd] |
| 43 |
if put(connection, \host) then put(connection, \port) |
| 44 |
result := _postgresqldb(connection) |
| 45 |
if /result then return database_handle(connection, _postgresqldb) |
| 46 |
icondb_error := result |
| 47 |
fail |
| 48 |
end |
| 49 |
|
| 50 |
global icondb_error |
| 51 |
|
| 52 |
#icondb returns a connection procedure for a known kind of dbms |
| 53 |
#which may then be called following the pattern |
| 54 |
#dbhandle := connect(dbname, user, pwd, host, port) |
| 55 |
#where host and port are optional |
| 56 |
|
| 57 |
procedure icondb(kind) |
| 58 |
case kind of { |
| 59 |
"mysql" : return _connectmysql |
| 60 |
"postgresql" : return _connectpostgresql |
| 61 |
default : stop("icondb: unknown dbms\nerror: ", image(kind)) | fail |
| 62 |
} |
| 63 |
end |
| 64 |
|
| 65 |
procedure dbclose(db) |
| 66 |
if type(db) ~== "database_handle" then |
| 67 |
stop("dbclose: not a database handle: ", image(db)) |
| 68 |
icondb_error := &null |
| 69 |
db.c_interface() |
| 70 |
return |
| 71 |
end |
| 72 |
|
| 73 |
procedure dbquery(db, query, constructor) |
| 74 |
local result, rec |
| 75 |
if type(db) ~== "database_handle" then |
| 76 |
stop("dbquery: not a database handle: ", image(db)) |
| 77 |
case type(constructor) of { |
| 78 |
"null" : |
| 79 |
&null |
| 80 |
"procedure" : |
| 81 |
image(constructor) ? { |
| 82 |
="record constructor" | |
| 83 |
stop("dbquery: not a record constructor: ", image(constructor)) |
| 84 |
} |
| 85 |
default : |
| 86 |
stop("dbquery: not a record constructor: ", image(constructor)) |
| 87 |
} |
| 88 |
icondb_error := &null |
| 89 |
result := db.c_interface(query) |
| 90 |
case type(result) of { |
| 91 |
"integer" | "null" : return result |
| 92 |
"list" : case type(result[1]) of { |
| 93 |
"list": |
| 94 |
if /constructor then |
| 95 |
suspend !result |
| 96 |
else { |
| 97 |
if result[1] & *constructor() ~= *result[1] then |
| 98 |
stop("dbquery: ",image(constructor)," needs at least ",*rec[1]," fields." ) |
| 99 |
suspend constructor!!result |
| 100 |
} |
| 101 |
"integer" : icondb_error := result |
| 102 |
} |
| 103 |
} |
| 104 |
end |
| 105 |
|