adding support for mutex from D flag
[csql.git] / src / tools / csqlds.cxx
blob7094bed42aaaa5ee01bdddb7d0872141e0cdbdfc
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 DbRetVal showAllDsn();
35 Config Conf::config;
36 int shouldadd=0;
37 int main(int argc, char **argv)
39 Conf::config.readAllValues(os::getenv("CSQL_CONFIG_FILE"));
40 char username[IDENTIFIER_LENGTH];
41 username [0] = '\0';
42 char password[IDENTIFIER_LENGTH];
43 password [0] = '\0';
44 int c = 0, opt = 10;
45 char dsn[IDENTIFIER_LENGTH];
46 char tdbname[IDENTIFIER_LENGTH];
47 bool isUserNameSpecified=false;
48 bool isPasswordSpecified=false;
49 bool isTDBSpecified=false;
50 bool isDSNSpecified=false;
51 bool showmsg=false;
52 while ((c = getopt(argc, argv, "U:P:D:N:are?")) != EOF)
54 switch (c)
56 case 'U' : { strcpy(username, argv[optind - 1]);
57 isUserNameSpecified=true; opt=10; break; }
58 case 'P' : { strcpy(password, argv[optind - 1]);
59 isPasswordSpecified=true;opt=10; break; }
60 case 'D' : { strcpy(dsn, argv[optind - 1]);
61 opt = 2;
62 isDSNSpecified = true;
63 break;
65 case 'N' : { strcpy(tdbname, argv[optind - 1]);
66 if(opt == 2) opt=3;
67 else opt=10;
68 isTDBSpecified = true;
69 break;
71 case '?' : {showmsg=true; break; } //print help
72 case 'a' : { shouldadd=1; break; }
73 case 'r' : { shouldadd=2; break;}
74 case 'e' : { opt=4; break;}
75 default: opt=10;
77 }//while options
78 if (opt == 10 || true==showmsg) {
79 printUsage();
80 return 0;
83 if( !isUserNameSpecified || !isPasswordSpecified)
85 strcpy(username, "NULL");
86 strcpy(password, "NULL");
89 if(shouldadd == 1)
91 if(opt==3){
92 addEntryToCsqlds( dsn,username,password,tdbname);
94 else{
95 printUsage();
96 return 0;
99 }else if (shouldadd == 2)
101 removeEntryFromCsqlds(dsn);
102 }else if(opt==4){
104 showAllDsn();
105 }else
107 printf("Invalid option passed\n");
108 printUsage();
110 return 0;
113 DbRetVal addEntryToCsqlds(char *dsn, char *uname, char *pwd, char *tbdname)
115 FILE *fp = fopen(Conf::config.getDsConfigFile(), "a+");
116 if (NULL == fp) {
117 printError(ErrSysInit, "Invalid path/filename in DS_CONFIG_FILE.\n");
118 return ErrOS;
120 bool isDsnExist=false;
121 char usertmp[IDENTIFIER_LENGTH];
122 char pwdtmp[IDENTIFIER_LENGTH];
123 char dsntmp[IDENTIFIER_LENGTH];
124 char tdbname[IDENTIFIER_LENGTH];
125 while(!feof(fp))
127 fscanf(fp, "%s %s %s %s\n",dsntmp,usertmp,pwdtmp,tdbname );
128 if (strcmp (dsn, dsntmp) == 0) {
129 isDsnExist=true;
130 printf("Applied DSN already Exists\n");
131 return ErrAlready;
134 fprintf(fp,"%s %s %s %s\n",dsn, uname, pwd, tbdname);
135 fclose(fp);
136 return OK;
139 DbRetVal removeEntryFromCsqlds(char *dsn)
141 FILE *fp,*tmpfp;
142 char tmpFileName[MAX_FILE_PATH_LEN];
143 sprintf(tmpFileName, "%s.tmp", Conf::config.getDsConfigFile());
144 char usertmp[IDENTIFIER_LENGTH];
145 char pwdtmp[IDENTIFIER_LENGTH];
146 char dsntmp[IDENTIFIER_LENGTH];
147 char tdbname[IDENTIFIER_LENGTH];
148 tmpfp = fopen(tmpFileName,"w");
149 if( tmpfp == NULL ) {
150 printError(ErrSysInit, "Invalid path/filename in TABLE_CONFIG_FILE.\n");
151 return ErrSysInit;
153 fp = fopen(Conf::config.getDsConfigFile(),"r");
154 if( fp == NULL ) {
155 printError(ErrSysInit, "csqltable.conf file does not exist");
156 return ErrSysInit;
159 while(!feof(fp))
161 fscanf(fp, "%s %s %s %s\n",dsntmp,usertmp,pwdtmp,tdbname );
162 if (strcmp (dsn, dsntmp) == 0) continue;
163 fprintf(tmpfp, "%s %s %s %s\n",dsntmp,usertmp,pwdtmp,tdbname);
165 fclose(fp);
166 fclose(tmpfp);
167 char sysCommand[MAX_FILE_PATH_LEN * 2];
168 sprintf(sysCommand, "mv %s %s", tmpFileName, Conf::config.getDsConfigFile());
169 int ret = system(sysCommand);
170 if (ret != 0)
172 printError(ErrSysInit, "Check csqltable.conf file permission. unable to remove %s from file", dsn);
173 return ErrSysInit;
175 return OK;
178 DbRetVal showAllDsn()
180 char dsnId[IDENTIFIER_LENGTH]; dsnId[0]='\0';
181 char user[IDENTIFIER_LENGTH]; user[0] = '\0';
182 char passwd[IDENTIFIER_LENGTH]; passwd[0] = '\0';
183 char tdbname[IDENTIFIER_LENGTH]; tdbname[0]='\0';
184 FILE *fp=NULL;
185 bool isDSNExist=false;
186 fp = fopen(Conf :: config.getDsConfigFile(),"r");
187 if(fp==NULL)
189 printError(ErrSysInit, "csqlds.conf file does not exist");
190 return ErrSysInit;
192 printf("===================================================\n");
193 printf("List of Data Sources present in 'csqlds.conf' file.\n");
194 printf("===================================================\n");
195 while(!feof(fp)){
196 fscanf(fp,"%s %s %s %s\n",dsnId,user,passwd,tdbname);
197 printf("%s\n",dsnId);
199 fclose(fp);
200 return OK;