main() { CONDITION cond; char tab[50], db[50]; TBL_HANDLE * handle; void *foo = NULL; printf("Input database: "); scanf("%s", db); printf("Input table: "); scanf("%s", tab); THR_Init(); cond = TBL_Open(db, tab, &handle); if (cond != TBL_NORMAL) { COND_DumpConditions(); THR_Shutdown(); exit(1); } cond = TBL_Layout(db, tab, callback, (void *) foo); if (cond != TBL_NORMAL) { COND_DumpConditions(); THR_Shutdown(); exit(2); } THR_Shutdown(); exit(0); }
CONDITION DMAN_Open(char *databaseName, char *requestingTitle, char *respondingTitle, DMAN_HANDLE ** handle) { CONDITION cond; PRIVATE_HANDLE * prv; *handle = NULL; prv = malloc(sizeof(*prv)); if (prv == NULL) return COND_PushCondition(DMAN_ERROR(DMAN_MALLOCFAILED), sizeof(*prv), "DMAN_Open"); (void) strcpy(prv->requestingTitle, requestingTitle); (void) strcpy(prv->respondingTitle, respondingTitle); prv->storage = NULL; prv->storageAccess = FALSE; prv->readAccess = FALSE; prv->writeAccess = FALSE; cond = TBL_Open(databaseName, "ApplicationEntity", &prv->applicationEntityHandle); if (cond != TBL_NORMAL) return COND_PushCondition(DMAN_ERROR(DMAN_TABLEOPENFAILED), "ApplicationEntity", "DMAN_Open"); cond = TBL_Open(databaseName, "GroupNames", &prv->groupNamesHandle); if (cond != TBL_NORMAL) return COND_PushCondition(DMAN_ERROR(DMAN_TABLEOPENFAILED), "GroupNames", "DMAN_Open"); cond = TBL_Open(databaseName, "StorageAccess", &prv->storageAccessHandle); if (cond != TBL_NORMAL) return COND_PushCondition(DMAN_ERROR(DMAN_TABLEOPENFAILED), "StorageAccess", "DMAN_Open"); cond = TBL_Open(databaseName, "StorageControl", &prv->storageControlHandle); if (cond != TBL_NORMAL) return COND_PushCondition(DMAN_ERROR(DMAN_TABLEOPENFAILED), "StorageControl", "DMAN_Open"); cond = TBL_Open(databaseName, "SecurityMatrix", &prv->securityMatrixHandle); if (cond != TBL_NORMAL) return COND_PushCondition(DMAN_ERROR(DMAN_TABLEOPENFAILED), "SecurityMatrix", "DMAN_Open"); #ifdef FIS cond = TBL_Open(databaseName, "FISAccess", &prv->FISAccessHandle); if (cond != TBL_NORMAL) return COND_PushCondition(DMAN_ERROR(DMAN_TABLEOPENFAILED), "FISAccess", "DMAN_Open"); #endif /*ZT cond = TBL_Open(databaseName, "PrintServerCFG", &prv->printServerCFGHandle); if (cond != TBL_NORMAL) return COND_PushCondition(DMAN_ERROR(DMAN_TABLEOPENFAILED), "PrintServerCFG", "DMAN_Open"); cond = TBL_Open(databaseName, "VideoImageDest", &prv->VideoImageDestHandle); if (cond != TBL_NORMAL) return COND_PushCondition(DMAN_ERROR(DMAN_TABLEOPENFAILED), "VideoImageDest", "DMAN_Open"); */ *handle = (DMAN_HANDLE *) prv; return DMAN_NORMAL; }
main() { CONDITION cond; TBL_HANDLE * handle; /* TBL_CRITERIA criteria[10]; */ long count; void *foo = NULL; static TBL_FIELD fields[] = { {"FNAME", TBL_STRING, 50, 0, 1, (void *) fname}, {"LNAME", TBL_STRING, 50, 0, 1, (void *) lname}, {"AGE", TBL_SIGNED4, 4, 0, 1, &age}, {"ZIP", TBL_SIGNED4, 4, 0, 1, &zip}, {"WEIGHT", TBL_FLOAT4, 4, 0, 1, &weight}, {NULL} }; static TBL_FIELD fields1[] = { {"T_TEXT", TBL_TEXT, 50, 0, 1, (void *) textTest}, {NULL} }; THR_Init(); cond = TBL_Open("TBLTest", "TBL_Persons", &handle); if (cond != TBL_NORMAL) { COND_DumpConditions(); exit(1); } /* criteria[0].FieldName = "LNAME"; criteria[0].Operator = TBL_EQUAL; TBL_LOAD_STRING(&criteria[0].Value, "JONES"); criteria[1].FieldName = 0; */ cond = TBL_Select(&handle, NULL, fields, &count, callback, (void *) foo); if (cond != TBL_NORMAL) { COND_DumpConditions(); THR_Shutdown(); exit(2); } printf("ALL DONE--Count: %d\n", count); (void) TBL_Close(&handle); #ifdef _MSC_VER cond = TBL_Open("TBLTest", "TBL_DataTypes", &handle); if (cond != TBL_NORMAL) { COND_DumpConditions(); THR_Shutdown(); exit(1); } count = 0; cond = TBL_Select(&handle, NULL, fields1, &count, callback, (void *) foo); if (cond != TBL_NORMAL) { COND_DumpConditions(); THR_Shutdown(); exit(3); } printf("ALL DONE--Count: %d\n", count); #endif THR_Shutdown(); exit(0); }
main() { CONDITION cond; TBL_HANDLE * handle; TBL_CRITERIA criteria[10]; int age; static TBL_UPDATE fields[] = { {"LNAME", TBL_SET, TBL_STRING, 50, 0, 1, (void *) "WOODROW"}, {NULL} }; #ifdef _MSC_VER static TBL_UPDATE fields1[] = { {"T_TEXT", TBL_SET, TBL_STRING, 50, 0, 1, (void *) "Updated Text Data"}, {NULL} }; #endif THR_Init(); cond = TBL_Open("TBLTest", "TBL_Persons", &handle); if (cond != TBL_NORMAL) { COND_DumpConditions(); THR_Shutdown(); exit(1); } criteria[0].FieldName = "AGE"; criteria[0].Operator = TBL_EQUAL; criteria[0].Value.Type = TBL_SIGNED4; age = 50; criteria[0].Value.Value.Signed4 = &age; criteria[0].Value.IsNull = 0; criteria[0].Value.AllocatedSize = 4; criteria[1].FieldName = 0; cond = TBL_Update(&handle, criteria, fields); if (cond != TBL_NORMAL) { COND_DumpConditions(); THR_Shutdown(); exit(2); } (void) TBL_Close(&handle); #ifdef _MSC_VER cond = TBL_Open("TBLTest", "TBL_DataTypes", &handle); if (cond != TBL_NORMAL) { COND_DumpConditions(); THR_Shutdown(); exit(1); } cond = TBL_Update(&handle, NULL, fields1); if (cond != TBL_NORMAL) { COND_DumpConditions(); THR_Shutdown(); exit(3); } (void) TBL_Close(&handle); #endif printf("\n\nUpdate operation succeeded\n\n"); THR_Shutdown(); exit(0); }
/* TBL_Layout ** ** Purpose: ** This function returns the columns and types of a particular ** table specified by handle. ** ** Parameter Dictionary: ** char *databaseName: The name of the database to use. ** char *tableName: The name of the table to access. ** CONDITION (*callback)(): The callback function invoked whenever ** a new record is retreived from the database. It is ** invoked with parameters as described below. ** void *ctx: Ancillary data passed through to the callback function ** and untouched by this routine. ** ** Return Values: ** TBL_NORMAL: normal termination. ** TBL_NOCALLBACK: No callback function was specified. ** TBL_DBNOEXIST: The database specified does not exist. ** TBL_TBLNOEXIST: The table specified did not exist in the correct ** internal database table...this may indicate some sort ** of consistency problem withing the database. ** TBL_NOCOLUMNS: The table specified contains no columnns. ** TBL_EARLYEXIT: The callback routine returned something other than ** TBL_NORMAL which caused this routine to cancel the remainder ** of the database operation and return early. ** ** Notes: ** It is an error to specify a null callback function. ** ** Algorithm: ** As each column is retrieved from the specified table, the ** callback function is invoked as follows: ** ** callback(fieldList *fieldList, void *ctx) ** ** fieldList contains the field name and the type of the column from ** the table specified. ** ctx contains any additional information the user originally passed ** to this layout function. If callback returns any value other ** than TBL_NORMAL, it is assumed that this function should terminate ** (i.e. cancel the current db operation), and return an abnormal ** termination message (TBL_EARLYEXIT) to the routine which ** originally invoked TBL_Layout. */ CONDITION TBL_Layout(char *databaseName, char *tableName, CONDITION(*callback) (), void *ctx) { TBL_FIELD field; int i; char descbuf[512]; TBL_HANDLE* handle; TBL_CONTEXT* tc; CONDITION cond; PGresult *res; PGconn* conn; int nTuples; char lcTable[512]; if (callback == NULL) { return COND_PushCondition(TBL_ERROR(TBL_NOCALLBACK), "TBL_Layout"); } remapLower(tableName, lcTable); cond = TBL_Open(databaseName, tableName, &handle); if (cond != TBL_NORMAL) return cond; tc = (TBL_CONTEXT*)handle; conn = (PGconn*)tc->dbSpecific; strcpy(descbuf, "SELECT a.attname, t.typname, a.attlen "); strcat(descbuf, "FROM pg_class c, pg_attribute a, pg_type t "); strcat(descbuf, "WHERE c.relname = '"); strcat(descbuf, lcTable); strcat(descbuf, "'"); strcat(descbuf, " and a.attnum > 0 "); strcat(descbuf, " and a.attrelid = c.oid "); strcat(descbuf, " and a.atttypid = t.oid "); res = PQexec(conn, descbuf); if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, PQresultErrorMessage(res)); exit(1); } nTuples = PQntuples(res); for (i = 0; i < nTuples; i++) { char *c; field.FieldName = PQgetvalue(res, i, 0); field.Value.AllocatedSize = atoi(PQgetvalue(res, i, 2)); field.Value.Type = TBL_SIGNED4; c = PQgetvalue(res, i, 1); if (strcmp(c, "bpchar") == 0) field.Value.Type = TBL_STRING; else if (strcmp(c, "int4") == 0) field.Value.Type = TBL_SIGNED4; else if (strcmp(c, "float8") == 0) field.Value.Type = TBL_FLOAT8; else printf ("%s\n", c); if (callback != NULL) { if (callback(&field, ctx) != TBL_NORMAL) { return COND_PushCondition(TBL_ERROR(TBL_EARLYEXIT), "TBL_Layout"); } } PQclear(res); } return TBL_NORMAL; }