cleanup
[csql.git] / src / tools / csqlds.cxx
blobb2c8eb93153fe561030e2dbc9fbe064e7ab71146
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #include <os.h>
17 #include <CSql.h>
18 #include <TableConfig.h>
20 void printUsage()
22 printf("Usage: csqlds [-U username] [-P passwd] [-D dsnname] [-N tdbname] \n"
23 " [-a] [-r] \n");
24 printf(" username -> Username of TDB .\n");
25 printf(" passwd -> Password of TDB.\n");
26 printf(" dsnname -> DSN to connect to tdb.\n");
27 printf(" tdbname -> Name of Target database\n");
28 printf(" -a -> Add entry to csqlds.conf\n");
29 printf(" -r -> Remove entry from csqlds.conf\n");
30 return;
32 DbRetVal removeEntryFromCsqlds(char *dsn);
33 DbRetVal addEntryToCsqlds(char *dsn, char *uname, char *pwd, char *tbdname);
34 Config Conf::config;
35 bool shouldadd=false;
36 int main(int argc, char **argv)
38 Conf::config.readAllValues(os::getenv("CSQL_CONFIG_FILE"));
39 char username[IDENTIFIER_LENGTH];
40 username [0] = '\0';
41 char password[IDENTIFIER_LENGTH];
42 password [0] = '\0';
43 int c = 0, opt = 10;
44 char dsn[IDENTIFIER_LENGTH];
45 char tdbname[IDENTIFIER_LENGTH];
46 bool isUserNameSpecified=false;
47 bool isPasswordSpecified=false;
48 bool isTDBSpecified=false;
49 bool isDSNSpecified=false;
50 bool showmsg=false;
51 while ((c = getopt(argc, argv, "U:P:D:N:ar?")) != EOF)
53 switch (c)
55 case 'U' : { strcpy(username, argv[optind - 1]);
56 isUserNameSpecified=true; opt=10; break; }
57 case 'P' : { strcpy(password, argv[optind - 1]);
58 isPasswordSpecified=true;opt=10; break; }
59 case 'D' : { strcpy(dsn, argv[optind - 1]);
60 opt = 2;
61 isDSNSpecified = true;
62 break;
64 case 'N' : { strcpy(tdbname, argv[optind - 1]);
65 if(opt == 2) opt=3;
66 else opt=10;
67 isTDBSpecified = true;
68 break;
70 case '?' : {showmsg=true; break; } //print help
71 case 'r' : { shouldadd=false; break;}
72 case 'a' : { shouldadd=true; break; }
73 default: opt=10;
75 }//while options
76 if (opt == 10 || true==showmsg) {
77 printUsage();
78 return 0;
81 if( !isUserNameSpecified || !isPasswordSpecified)
83 strcpy(username, "NULL");
84 strcpy(password, "NULL");
87 if(shouldadd)
89 if(opt==3){
90 addEntryToCsqlds( dsn,username,password,tdbname);
92 else{
93 printUsage();
94 return 0;
97 }else
99 removeEntryFromCsqlds(dsn);
101 return 0;
104 DbRetVal addEntryToCsqlds(char *dsn, char *uname, char *pwd, char *tbdname)
106 FILE *fp = fopen(Conf::config.getDsConfigFile(), "a+");
107 if (NULL == fp) {
108 printError(ErrSysInit, "Invalid path/filename in DS_CONFIG_FILE.\n");
109 return ErrOS;
111 bool isDsnExist=false;
112 char usertmp[IDENTIFIER_LENGTH];
113 char pwdtmp[IDENTIFIER_LENGTH];
114 char dsntmp[IDENTIFIER_LENGTH];
115 char tdbname[IDENTIFIER_LENGTH];
116 while(!feof(fp))
118 fscanf(fp, "%s %s %s %s\n",dsntmp,usertmp,pwdtmp,tdbname );
119 if (strcmp (dsn, dsntmp) == 0) {
120 isDsnExist=true;
121 printf("Applied DSN already Exists\n");
122 return ErrAlready;
125 fprintf(fp,"%s %s %s %s\n",dsn, uname, pwd, tbdname);
126 fclose(fp);
127 return OK;
130 DbRetVal removeEntryFromCsqlds(char *dsn)
132 FILE *fp,*tmpfp;
133 char tmpFileName[MAX_FILE_PATH_LEN];
134 sprintf(tmpFileName, "%s.tmp", Conf::config.getDsConfigFile());
135 char usertmp[IDENTIFIER_LENGTH];
136 char pwdtmp[IDENTIFIER_LENGTH];
137 char dsntmp[IDENTIFIER_LENGTH];
138 char tdbname[IDENTIFIER_LENGTH];
139 tmpfp = fopen(tmpFileName,"w");
140 if( tmpfp == NULL ) {
141 printError(ErrSysInit, "Invalid path/filename in TABLE_CONFIG_FILE.\n");
142 return ErrSysInit;
144 fp = fopen(Conf::config.getDsConfigFile(),"r");
145 if( fp == NULL ) {
146 printError(ErrSysInit, "csqltable.conf file does not exist");
147 return ErrSysInit;
150 while(!feof(fp))
152 fscanf(fp, "%s %s %s %s\n",dsntmp,usertmp,pwdtmp,tdbname );
153 if (strcmp (dsn, dsntmp) == 0) continue;
154 fprintf(tmpfp, "%s %s %s %s\n",dsntmp,usertmp,pwdtmp,tdbname);
156 fclose(fp);
157 fclose(tmpfp);
158 char sysCommand[MAX_FILE_PATH_LEN * 2];
159 sprintf(sysCommand, "mv %s %s", tmpFileName, Conf::config.getDsConfigFile());
160 int ret = system(sysCommand);
161 if (ret != 0)
163 printError(ErrSysInit, "Check csqltable.conf file permission. unable to remove %s from file", dsn);
164 return ErrSysInit;
166 return OK;