/*---------------------------------------------------------------------------------*/ /* $RCSfile: testInsert.c,v $ */ /*---------------------------------------------------------------------------------*/ /* $Revision: 1.1 $ */ /* $Name: $ */ /* $Date: 2006/02/28 23:28:21 $ */ /* $Author: agibert $ */ /*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/ /* This file is part of LibDataBase */ /* */ /* LibDataBase is free software; you can redistribute it and/or modify */ /* it under the terms of the GNU Lesser General Public Licence as published by */ /* the Free Software Foundation; either version 2.1 of the License, or */ /* (at your option) any later version. */ /* */ /* LibDataBase is distributed in the hope that it will be useful, */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ /* GNU Lesser General Public License for more details. */ /* */ /* You should have received a copy of the GNU Lesser General Public License */ /* along with LibDataBase; if not, write to the Free Software */ /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /*---------------------------------------------------------------------------------*/ #include #include #include #include "../lib/database.h" /* Globals */ #define DEFAULT_REQUEST "INSERT INTO dummy (c1, c2, c3) VALUES (:1, :2, :3)" #define TRUE 1 #define FALSE 0 const char *USAGE = "Usage: %s [--req ] [--from ] [--count ] [--db ] [--size ] [--baseIndex ]\n\n\ Executes an INSERT SQL request in a table which must have the following format:\n\ \"CREATE TABLE dummy (c1 NUMBER(35) primary key, c2 VARCHAR2(16), c3 NUMBER(10))\".\n\ Data to insert are taken from an array which elements are generated by the program (see\n\ option --baseIndex).\n\ \n\ Options:\n\ --req:\tExecutes specified request. If not specified, a default request is used:\n\ \t\"INSERT INTO dummy (c1, c2, c3) VALUES (:1, :2, :3)\".\n\ --from:\tStarting row in the array of input data (default: 0).\n\ --count:\tRows count to use in the input array (1 to 65536) (Default: 0).\n\ --size:\tTotal number of rows of the input array generated by the program (default: 1).\n\ --db:\t\tDatabase specification in the format: login/password@oracleSID.\n\ --baseIndex:\tStart index to use to create input data. Ex: if baseIndex = 10, first row\n\ to insert will have columns values: 10, 'A10', 110; second row will have\n\ 11, 'A11', 111; etc. (default: 1).\n\ \n\ Examples:\n\ _ '%s --db \"login/pwd@srv\" --size 100 --count 20' will insert 5 times 20 rows in table dummy.\n\ _ '%s --db \"login/pwd@srv\" --size 10 --count 4' will insert 2 times 4 rows then the 2\n\ remaining rows.\n"; extern int strcasecmp(const char *, const char *); extern char *strdup(const char *); void parseArgs(int argc, char **argv); void usage(char **argv); void say (const char *format, ...); int setupHostVars(void); void setupData(int n); void splitDBSpec(char *spec, const char **login, const char **pwd, const char **server); int arraySize = 1; int baseIndex = 0; int from = 0; int count = 0; int debugLevel = 0; char *req = NULL; const char *login = ""; const char *pwd = ""; const char *server = ""; DBT_Connection conn; DBT_Statement st; DBT_Result res; DBT_HostVar hv1; DBT_HostVar hv2; DBT_HostVar hv3; int *dynInt1 = NULL; char *dynBuf = NULL; int *dynInt3 = NULL; DBT_Indicator *dynIndic1 = NULL; DBT_Indicator *dynIndic2 = NULL; DBT_Indicator *dynIndic3 = NULL; /** * Main. */ int main(int argc, char **argv) { int totalRowsProcessed = 0; DBT_Status rc; TOOLT_Counter *counter = NULL; /* Parse arguments */ parseArgs(argc, argv); if (req == NULL) { req = strdup(DEFAULT_REQUEST); } counter = TOOL_Counter_Alloc(1); rc = DB_Library_Open(DBD_DEBUG | DBD_ERRMSG); if (DB_ERROR(rc)) { printf("Error: Could not open library (%s)\n", DB_Error_Message_Get()); return 1; } /* Connect to Database */ rc = DB_Connect(&conn, login, pwd, server); if (DB_ERROR(rc)) { printf("Error: Could not connect (%s)\n", DB_Error_Message_Get()); return 1; } /* Init data that are going to be inserted */ say("Setup data and HostVars\n"); setupData(arraySize); if (setupHostVars() == FALSE) return 1; /* Init statement */ rc = DB_Statement_Init(&conn, &st); if (DB_ERROR(rc)) { printf("Error: Could not init statement (err %d: %s)\n", rc, DB_Error_Message_Get()); return 1; } say("Preparing statement for query: '%s'\n", req); rc = DB_Statement_Prepare(&st, req); if (DB_ERROR(rc)) { printf("Error: Could not prepare statement (err %d: %s)\n", rc, DB_Error_Message_Get()); return 1; } say("Bind Vars\n"); rc = DB_Statement_BindVars(&st, arraySize, &hv1, &hv2, &hv3, NULL); if (DB_ERROR(rc)) { printf("Error: Could not bind vars (%s)\n", DB_Error_Message_Get()); return 1; } /* Start transaction */ say("\nStart Transaction\n"); DB_Transaction_Start(&conn); say("Executing Statement (array size: %d; starting from row %d, %d by %d)...\n", arraySize, from, count, count); TOOL_Counter_Start(counter, 0); while (from < arraySize) { if (from + count > arraySize) count = arraySize - from; rc = DB_Statement_Execute(&st, from, count, &res); totalRowsProcessed += res.rowsProcessed; if (DB_ERROR(rc)) break; from += count; } /* End transaction */ TOOL_Counter_Stop(counter, 0); if (DB_ERROR(rc)) { say("Error: %s", DB_Error_Message_Get()); say("%d rows processed (error at iteration %d)\n", totalRowsProcessed, res.errorIteration); } else { say("OK, %d rows processed in %ld ms\n", totalRowsProcessed, TOOL_Counter_Get(counter, 0)); } /*DB_Transaction_Rollback(&conn);*/ say("Committing Transaction\n\n"); DB_Transaction_Commit(&conn); if (req) free(req); TOOL_Counter_Free(counter); DB_Disconnect(&conn); DB_Library_Close(); return 0; } /** * Affichage sur la sortie standard. */ void say(const char *format, ...) { char buffer[512]; va_list v; va_start(v, format); vsnprintf(buffer, sizeof(buffer), format, v); fprintf (stdout, "%s", buffer); va_end(v); } /** * Arguments. */ void parseArgs(int argc, char **argv) { int i = 0; for (i = 1; i < argc; ++i) { if (! strcasecmp(argv[i], "--debuglevel")) { if (++i >= argc) { usage(argv); exit(1); } debugLevel = atoi(argv[i]); DB_Debug_Level_Set(debugLevel); } else if (! strcasecmp(argv[i], "--from")) { if (++i >= argc) { usage(argv); exit(1); } from = atoi(argv[i]); } else if (! strcasecmp(argv[i], "--count")) { if (++i >= argc) { usage(argv); exit(1); } count = atoi(argv[i]); } else if (! strcasecmp(argv[i], "--size")) { if (++i >= argc) { usage(argv); exit(1); } arraySize = atoi(argv[i]); } else if (! strcasecmp(argv[i], "--baseIndex")) { if (++i >= argc) { usage(argv); exit(1); } baseIndex = atoi(argv[i]); } else if (! strcasecmp(argv[i], "--req")) { if (++i >= argc) { usage(argv); exit(1); } req = strdup(argv[i]); } else if (! strcasecmp(argv[i], "--db")) { if (++i >= argc) { usage(argv); exit(1); } splitDBSpec(argv[i], &login, &pwd, &server); } else if (! strcasecmp(argv[i], "--help") || ! strcasecmp(argv[i], "-h") || ! strcasecmp(argv[i], "-?")) { usage(argv); exit(1); } else { usage(argv); exit(1); } } } /** * Usage. */ void usage(char **argv) { char *b = basename(argv[0]); printf(USAGE, b, b, b); } /** * Initializes host variables. * Associates an 'hostvar' with a type, size, array of values and array of indicators. */ int setupHostVars(void) { if (DB_HostVar_Setup(&hv1, DBD_INTEGER, 0, dynInt1, dynIndic1) != DBS_OK || DB_HostVar_Setup(&hv2, DBD_STRING, 20, dynBuf, dynIndic2) != DBS_OK || DB_HostVar_Setup(&hv3, DBD_INTEGER, 0, dynInt3, dynIndic3) != DBS_OK) { say("Error: %s\n", DB_Error_Message_Get()); return FALSE; } return TRUE; } /** * Setup arrays of values and indicators. * (IN) n: Rows count to allocate for each array. */ void setupData(int n) { int i; dynInt1 = malloc(n * sizeof(int)); dynBuf = malloc((unsigned) n * 20); dynInt3 = malloc(n * sizeof(int)); dynIndic1 = malloc(n * sizeof(int)); dynIndic2 = malloc(n * sizeof(int)); dynIndic3 = malloc(n * sizeof(int)); for (i = 0; i < n; i++) { int x = i + baseIndex; dynIndic1[i] = 0; dynIndic2[i] = 0; dynIndic3[i] = 0; dynInt1[i] = x; sprintf(dynBuf+20*i, "A%d", x); dynInt3[i] = 100+x; } } /** * Splits a database specification in the format "login/password@server" into its * basic components 'login', 'password' and 'server'. * Note that the initial 'spec' string may be altered. */ void splitDBSpec(char *spec, const char **login_, const char **pwd_, const char **server_) { char *p = NULL; *login_ = spec; p = strchr(spec, '/'); if (p == NULL) return; *p = 0; *pwd_ = ++p; p = strchr(p, '@'); if (p == NULL) return; *p = 0; *server_ = ++p; }