int main() { int res; struct SednaConnection conn = SEDNA_CONNECTION_INITIALIZER; const char* url = "localhost"; const char* db_name = "testdb"; const char* login = "******"; const char* password = "******"; printf("01_connect started.\n"); //connecting to database "testdb" with login "SYSTEM", password "MANAGER" res = SEconnect(&conn, url, db_name, login, password); if(res != SEDNA_SESSION_OPEN) { printf("Session starting failed: \n%s\n", SEgetLastErrorMsg(&conn)); return -1; } printf("Session opened successfully.\n"); //closing session res = SEclose(&conn); if(res != SEDNA_SESSION_CLOSED) { printf("A error occured while trying to close session: \n%s\n", SEgetLastErrorMsg(&conn)); return -1; } printf("Session closed successfully.\n"); return 0; }
int main() { int res; struct SednaConnection conn = SEDNA_CONNECTION_INITIALIZER; const char* url = "localhost"; const char* db_name = "testdb"; const char* login = "******"; const char* password = "******"; printf("02_load started.\n"); //connecting to database "testdb" with login "SYSTEM", password "MANAGER" res = SEconnect(&conn, url, db_name, login, password); if(res != SEDNA_SESSION_OPEN) { printf("Session starting failed: \n%s\n", SEgetLastErrorMsg(&conn)); return -1; } // load data from file "categories.xml" into the document "categories" res = SEexecute(&conn, "LOAD \"../data/categories.xml\" \"categories\""); if(res != SEDNA_BULK_LOAD_SUCCEEDED) { printf("Bulk load failed: \n%s\n", SEgetLastErrorMsg(&conn)); // closing session SEclose(&conn); return -1; } printf("Document 'categories.xml' has been successfully loaded into 'testdb' database.\n"); //closing session res = SEclose(&conn); if(res != SEDNA_SESSION_CLOSED) { printf("An error occured while trying to close session: \n%s\n", SEgetLastErrorMsg(&conn)); return -1; } return 0; }
// Test the last error message for conn, and raise an exception if there is one. // The type of the exception is based on the result of the function that was // called that generated the error and should be passed as res. static void sedna_err(SC *conn, int res) { VALUE exc_class, exc; const char *msg; char *code, *err, *details, *p, exc_message[BUFSIZ]; switch(res) { case SEDNA_AUTHENTICATION_FAILED: exc_class = cSednaAuthError; break; case SEDNA_OPEN_SESSION_FAILED: case SEDNA_CLOSE_SESSION_FAILED: exc_class = cSednaConnError; break; case SEDNA_BEGIN_TRANSACTION_FAILED: case SEDNA_ROLLBACK_TRANSACTION_FAILED: case SEDNA_COMMIT_TRANSACTION_FAILED: exc_class = cSednaTrnError; break; case SEDNA_ERROR: default: exc_class = cSednaException; } msg = SEgetLastErrorMsg(conn); // SEgetLastErrorCode(conn) is useless, because it varies if the order of // errors changes. The actual code is a string which is defined in error_codes.h // in the Sedna source code. code = strstr(msg, "ERROR "); err = strstr(msg, "\n"); details = strstr(err, "\nDetails: "); if(code != NULL) { code += 6; // Advance beyond "ERROR " if((p = strstr(code, "\n")) != NULL) strncpy(p, "\0", 1); } if(err != NULL) { err++; // Advance beyond "\n" if((p = strstr(err, "\n")) != NULL) strncpy(p, "\0", 1); } else { err = strdup("Unknown error."); } if(details != NULL) { details += 10; // Advance beyond "\nDetails: " while((p = strstr(details, "\n")) != NULL) strncpy(p, " ", 1); snprintf(exc_message, BUFSIZ, "%s (%s)", err, details); } else { snprintf(exc_message, BUFSIZ, "%s", err); } exc = rb_exc_new2(exc_class, exc_message); if(code != NULL) { rb_iv_set(exc, IV_EXC_CODE, rb_str_new2(code)); } rb_exc_raise(exc); }
int main() { struct SednaConnection conn = SEDNA_CONNECTION_INITIALIZER; int bytes_read, res, value; char buf[1024]; const char* url = "localhost"; const char* db_name = "testdb"; const char* login = "******"; const char* password = "******"; printf("Client started.\n"); value = SEDNA_AUTOCOMMIT_OFF; res = SEsetConnectionAttr(&conn, SEDNA_ATTR_AUTOCOMMIT, (void*)&value, sizeof(int)); //connecting to database "testdb" with login "SYSTEM", password "MANAGER" res = SEconnect(&conn, url, db_name, login, password); if(res != SEDNA_SESSION_OPEN) { printf("Session starting failed: \n%s\n", SEgetLastErrorMsg(&conn)); return -1; } //begin transaction res = SEbegin(&conn); if(res != SEDNA_BEGIN_TRANSACTION_SUCCEEDED) { printf("Begin transaction failed: \n%s\n", SEgetLastErrorMsg(&conn)); //closing session SEclose(&conn); return -1; } // load data from file "region.xml" into the document "region" res = SEexecute(&conn, "LOAD \"..\\data\\region.xml\" \"region\""); if(res != SEDNA_BULK_LOAD_SUCCEEDED) { printf("Bulk load failed: \n%s\n", SEgetLastErrorMsg(&conn)); // closing session SEclose(&conn); return -1; } // execute XQuery query res = SEexecute(&conn, "doc(\"region\")/*/*"); if(res != SEDNA_QUERY_SUCCEEDED) { printf("Query failed: \n%s\n", SEgetLastErrorMsg(&conn)); //closing session SEclose(&conn); return -1; } // iterate over the result sequence and retrieve the result data while((res = SEnext(&conn)) != SEDNA_RESULT_END) { if (res == SEDNA_ERROR) { printf("Failed to get next result item from server: \n%s\n", SEgetLastErrorMsg(&conn)); //closing session SEclose(&conn); return -1; } printf("\nresult item:\n"); do { bytes_read = SEgetData(&conn, buf, 1024 - 1); if(bytes_read == SEDNA_ERROR) { printf("Failed to get result data from server: \n%s\n", SEgetLastErrorMsg(&conn)); //closing session SEclose(&conn); return -1; } buf[bytes_read] = '\0'; printf("%s", buf); }while(bytes_read > 0); printf("\n"); } // Drop document "region" res = SEexecute(&conn, "DROP DOCUMENT \"region\""); if(res != SEDNA_UPDATE_SUCCEEDED) { printf("Drop document failed: \n%s\n", SEgetLastErrorMsg(&conn)); // closing session SEclose(&conn); return -1; } //commiting transaction res = SEcommit(&conn); if(res != SEDNA_COMMIT_TRANSACTION_SUCCEEDED) { printf("Commit transaction failed: \n%s\n", SEgetLastErrorMsg(&conn)); //closing session SEclose(&conn); return -1; } //closing session res = SEclose(&conn); if(res != SEDNA_SESSION_CLOSED) { printf("An error occured while trying to close session: \n%s\n", SEgetLastErrorMsg(&conn)); return -1; } return 0; }