Portál AbcLinuxu, 30. dubna 2025 12:41
CREATE OR REPLACE TYPE parent_type AUTHID CURRENT_USER AS OBJECT (
id NUMBER(38)
) NOT FINAL NOT INSTANTIABLE;
/
CREATE OR REPLACE TYPE subtype_a AUTHID CURRENT_USER UNDER parent_type (
);
/
CREATE OR REPLACE TYPE subtype_b AUTHID CURRENT_USER UNDER parent_type (
);
/
CREATE TABLE t_parents (
parent parent_type
);
INSERT INTO t_parents VALUES (subtype_a(1));
INSERT INTO t_parents VALUES (NULL);
INSERT INTO t_parents VALUES (subtype_b(2));
COMMIT;
/
a smrtici Perl kod:
use DBI;
use DBD::Oracle qw(:ora_types);
my $dbh = DBI->connect("dbi:Oracle:SID","jmeno","heslo");
my $sth = $dbh->prepare("SELECT * FROM t_parents");
$sth->execute();
while (my @r = $sth->fetchrow_array) {}
$dbh->disconnect;
A co jsme to uvarili?
Tiskni
Sdílej:
%new-style
Datasource ds("oracle:omquser/omquser@xbox");
any res = ds.selectRows("SELECT * FROM t_parents");
printf("%N\n", res);
list: (3 elements)
[0]=hash: (1 member)
parent : hash: (1 member)
ID : 1
[1]=hash: (1 member)
parent : NULL
[2]=hash: (1 member)
parent : hash: (1 member)
ID : 2
Tím nechci hájit Oracle, podobných interních chyb jsem už viděl desítky. Ale většinou to bylo chybně použitým OCI.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <oci.h>
struct parent_type {
OCINumber id;
};
typedef struct parent_type parent_type;
struct null_parent_type {
OCIInd null_parent_type;
OCIInd null_id;
};
typedef struct null_parent_type null_parent_type;
static text *username = (text *) "username";
static text *password = (text *) "password";
static text *sid = (text *) "sid";
static text *selall = (text *) "SELECT PARENT FROM t_parents";
OCIDefine *defnp = (OCIDefine *) NULL;
OCIType * parent_type_tdo;
static parent_type *parentp[3];
static null_parent_type *null_parentp[3];
static OCIEnv *envhp;
static OCIError *errhp;
static void checkerr(/*_ OCIError *errhp, sword status _*/);
static void cleanup(/*_ void _*/);
static void myfflush(/*_ void _*/);
int main(/*_ int argc, char *argv[] _*/);
static sword status;
int main(argc, argv)
int argc;
char *argv[];
{
sword errcode = 0;
OCISession *authp = (OCISession *) 0;
OCIServer *srvhp;
OCISvcCtx *svchp;
OCIStmt *stmthp;
errcode = OCIEnvCreate((OCIEnv **) &envhp, (ub4) OCI_DEFAULT | OCI_OBJECT,
(dvoid *) 0, (dvoid * (*)(dvoid *,size_t)) 0,
(dvoid * (*)(dvoid *, dvoid *, size_t)) 0,
(void (*)(dvoid *, dvoid *)) 0, (size_t) 0, (dvoid **) 0);
if (errcode != 0) {
(void) printf("OCIEnvCreate failed with errcode = %d.\n", errcode);
exit(1);
}
(void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &errhp, OCI_HTYPE_ERROR,
(size_t) 0, (dvoid **) 0);
/* server contexts */
(void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &srvhp, OCI_HTYPE_SERVER,
(size_t) 0, (dvoid **) 0);
(void) OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &svchp, OCI_HTYPE_SVCCTX,
(size_t) 0, (dvoid **) 0);
(void) OCIServerAttach( srvhp, errhp, (text *)sid, strlen(sid), 0);
/* set attribute server context in the service context */
(void) OCIAttrSet( (dvoid *) svchp, OCI_HTYPE_SVCCTX, (dvoid *)srvhp,
(ub4) 0, OCI_ATTR_SERVER, (OCIError *) errhp);
(void) OCIHandleAlloc((dvoid *) envhp, (dvoid **)&authp,
(ub4) OCI_HTYPE_SESSION, (size_t) 0, (dvoid **) 0);
(void) OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION,
(dvoid *) username, (ub4) strlen((char *)username),
(ub4) OCI_ATTR_USERNAME, errhp);
(void) OCIAttrSet((dvoid *) authp, (ub4) OCI_HTYPE_SESSION,
(dvoid *) password, (ub4) strlen((char *)password),
(ub4) OCI_ATTR_PASSWORD, errhp);
checkerr(errhp, OCISessionBegin ( svchp, errhp, authp, OCI_CRED_RDBMS,
(ub4) OCI_DEFAULT));
(void) OCIAttrSet((dvoid *) svchp, (ub4) OCI_HTYPE_SVCCTX,
(dvoid *) authp, (ub4) 0,
(ub4) OCI_ATTR_SESSION, errhp);
checkerr(errhp, OCIHandleAlloc( (dvoid *) envhp, (dvoid **) &stmthp,
OCI_HTYPE_STMT, (size_t) 0, (dvoid **) 0));
checkerr(errhp, OCIStmtPrepare(stmthp, errhp, selall,
(ub4) strlen((char *) selall),
(ub4) OCI_NTV_SYNTAX, (ub4) OCI_DEFAULT));
checkerr(errhp, OCIDefineByPos(stmthp, &defnp, errhp, (ub4) 1, (dvoid *) 0,
(sb4) 0, SQLT_NTY, (dvoid *) 0, (ub2 *)0,
(ub2 *)0, (ub4) OCI_DEFAULT));
checkerr(errhp, OCITypeByName(envhp, errhp, svchp, (const text *) 0,
(ub4) 0, (const text *) "PARENT_TYPE",
(ub4) strlen((const char *) "PARENT_TYPE"),
(CONST text *) 0, (ub4) 0,
OCI_DURATION_SESSION, OCI_TYPEGET_ALL,
&parent_type_tdo));
checkerr(errhp, OCIDefineObject(defnp, errhp,
(OCIType *) parent_type_tdo,
(dvoid **)&parentp, (ub4 *) 0,
(dvoid **)&null_parentp, (ub4 *) 0));
/* execute and fetch */
if (status = OCIStmtExecute(svchp, stmthp, errhp, (ub4) 1, (ub4) 0,
(CONST OCISnapshot *) NULL, (OCISnapshot *) NULL, OCI_DEFAULT))
{
if (status == OCI_NO_DATA)
{
}
else
{
checkerr(errhp, status);
cleanup();
return OCI_ERROR;
}
}
while ((status = OCIStmtFetch(stmthp, errhp, (ub4) 1, (ub4) OCI_FETCH_NEXT,
(ub4) OCI_DEFAULT)) == 0) {
}
}
void checkerr(errhp, status)
OCIError *errhp;
sword status;
{
text errbuf[512];
sb4 errcode = 0;
switch (status)
{
case OCI_SUCCESS:
break;
case OCI_SUCCESS_WITH_INFO:
(void) printf("Error - OCI_SUCCESS_WITH_INFO\n");
break;
case OCI_NEED_DATA:
(void) printf("Error - OCI_NEED_DATA\n");
break;
case OCI_NO_DATA:
(void) printf("Error - OCI_NODATA\n");
break;
case OCI_ERROR:
(void) OCIErrorGet((dvoid *)errhp, (ub4) 1, (text *) NULL, &errcode,
errbuf, (ub4) sizeof(errbuf), OCI_HTYPE_ERROR);
(void) printf("Error - %.*s\n", 512, errbuf);
break;
case OCI_INVALID_HANDLE:
(void) printf("Error - OCI_INVALID_HANDLE\n");
break;
case OCI_STILL_EXECUTING:
(void) printf("Error - OCI_STILL_EXECUTE\n");
break;
case OCI_CONTINUE:
(void) printf("Error - OCI_CONTINUE\n");
break;
default:
break;
}
}
/*
* Exit program with an exit code.
*/
void cleanup()
{
if (envhp)
(void) OCIHandleFree((dvoid *) envhp, OCI_HTYPE_ENV);
return;
}
void myfflush()
{
eb1 buf[50];
fgets((char *) buf, 50, stdin);
}
Plus obligatni gcc pro InstantClient:uff a to jim nestačí něco v Javě? Použít existujícího klienta a jen napsat SQL? JDBC ovladače snad podporují ne? (i když co si pamatuji, kdysi jsme s nimi bojovali a jejich „podpora“ byla dost na nic a to jsme byli celkem dost platící zákazník…)
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.