237 lines
7.1 KiB
C
237 lines
7.1 KiB
C
/*---------------------------------------------------------------------------------*/
|
|
/* $RCSfile: testUpdate.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 <stdio.h>
|
|
#include <libgen.h>
|
|
|
|
#include <tool.h>
|
|
#include "../lib/database.h"
|
|
|
|
|
|
/* Globals */
|
|
const char *USAGE = "Usage: %s --req <delete SQL request> --db <Database spec>\n\
|
|
Executes an UPDATE, DELETE or any request that takes no variable argument nor produces\n\
|
|
any output value (other than the number of rows processed).\n\
|
|
Ex: _ \"CREATE TABLE tableX (col1 NUMBER(5) PRIMARY KEY, col2 VARCHAR2(10))\";\n\
|
|
_ \"INSERT INTO tableX VALUES (1, 'A')\";\n\
|
|
_ \"UPDATE tableX SET col1=col1+1, col2='ABC' WHERE col1 < 10\";\n\
|
|
_ \"DELETE FROM tableX WHERE col2=1\";\n\
|
|
_ \"DROP TABLE tableX\".\n\
|
|
\n\
|
|
Command line arguments:\n\
|
|
--req The SQL request.\n\
|
|
--db Database on which to operate. Format: \"login/pwd@server\"\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, ...);
|
|
void splitDBSpec(char *spec, const char **login, const char **pwd, const char **server);
|
|
|
|
int debugLevel = 0;
|
|
char *req = NULL;
|
|
const char *login = "";
|
|
const char *pwd = "";
|
|
const char *server = "";
|
|
DBT_Connection conn;
|
|
DBT_Statement st;
|
|
DBT_Result res;
|
|
|
|
|
|
/**
|
|
* Main.
|
|
*/
|
|
int main(int argc, char **argv) {
|
|
DBT_Status rc;
|
|
TOOLT_Counter *counter = NULL;
|
|
|
|
|
|
/* Parse arguments */
|
|
parseArgs(argc, argv);
|
|
if (req == NULL) {
|
|
usage(argv);
|
|
return 1;
|
|
}
|
|
|
|
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 */
|
|
say("Connecting to %s/%s@%s...\n", login, pwd, server);
|
|
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 statement */
|
|
rc = DB_Statement_Init(&conn, &st);
|
|
if (DB_ERROR(rc)) {
|
|
printf("Error: Could not init statement (err %d)\n", rc);
|
|
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)\n", rc);
|
|
return 1;
|
|
}
|
|
|
|
/* Start transaction */
|
|
say("Start Transaction\n");
|
|
DB_Transaction_Start(&conn);
|
|
|
|
say("\nExecuting Statement...\n");
|
|
TOOL_Counter_Start(counter, 0);
|
|
rc = DB_Statement_Execute(&st, 0, 1, &res);
|
|
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", res.rowsProcessed, res.errorIteration);
|
|
}
|
|
else {
|
|
say("OK, %d rows processed in %ld ms\n", res.rowsProcessed, TOOL_Counter_Get(counter, 0));
|
|
}
|
|
|
|
|
|
/*DB_Transaction_Rollback(&conn);*/
|
|
|
|
say("\nCommitting Transaction\n");
|
|
DB_Transaction_Commit(&conn);
|
|
|
|
if (req)
|
|
free(req);
|
|
TOOL_Counter_Free(counter);
|
|
|
|
DB_Disconnect(&conn);
|
|
DB_Library_Close();
|
|
say("End.\n");
|
|
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], "--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) {
|
|
printf(USAGE, basename(argv[0]));
|
|
}
|
|
|
|
|
|
/**
|
|
* 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;
|
|
}
|