1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
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. *
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. *
15 ***************************************************************************/
18 #include <TableConfig.h>
22 printf("Usage: csqlds [-U username] [-P passwd] [-D dsnname] [-N tdbname] \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");
32 DbRetVal
removeEntryFromCsqlds(char *dsn
);
33 DbRetVal
addEntryToCsqlds(char *dsn
, char *uname
, char *pwd
, char *tbdname
);
34 DbRetVal
showAllDsn();
37 int main(int argc
, char **argv
)
39 Conf::config
.readAllValues(os::getenv("CSQL_CONFIG_FILE"));
40 char username
[IDENTIFIER_LENGTH
];
42 char password
[IDENTIFIER_LENGTH
];
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;
52 while ((c
= getopt(argc
, argv
, "U:P:D:N:are?")) != EOF
)
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]);
62 isDSNSpecified
= true;
65 case 'N' : { strcpy(tdbname
, argv
[optind
- 1]);
68 isTDBSpecified
= true;
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;}
78 if (opt
== 10 || true==showmsg
) {
83 if( !isUserNameSpecified
|| !isPasswordSpecified
)
85 strcpy(username
, "NULL");
86 strcpy(password
, "NULL");
92 addEntryToCsqlds( dsn
,username
,password
,tdbname
);
99 }else if (shouldadd
== 2)
101 removeEntryFromCsqlds(dsn
);
107 printf("Invalid option passed\n");
113 DbRetVal
addEntryToCsqlds(char *dsn
, char *uname
, char *pwd
, char *tbdname
)
115 FILE *fp
= fopen(Conf::config
.getDsConfigFile(), "a+");
117 printError(ErrSysInit
, "Invalid path/filename in DS_CONFIG_FILE.\n");
120 bool isDsnExist
=false;
121 char usertmp
[IDENTIFIER_LENGTH
];
122 char pwdtmp
[IDENTIFIER_LENGTH
];
123 char dsntmp
[IDENTIFIER_LENGTH
];
124 char tdbname
[IDENTIFIER_LENGTH
];
127 fscanf(fp
, "%s %s %s %s\n",dsntmp
,usertmp
,pwdtmp
,tdbname
);
128 if (strcmp (dsn
, dsntmp
) == 0) {
130 printf("Applied DSN already Exists\n");
134 fprintf(fp
,"%s %s %s %s\n",dsn
, uname
, pwd
, tbdname
);
139 DbRetVal
removeEntryFromCsqlds(char *dsn
)
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");
153 fp
= fopen(Conf::config
.getDsConfigFile(),"r");
155 printError(ErrSysInit
, "csqltable.conf file does not exist");
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
);
167 char sysCommand
[MAX_FILE_PATH_LEN
* 2];
168 sprintf(sysCommand
, "mv %s %s", tmpFileName
, Conf::config
.getDsConfigFile());
169 int ret
= system(sysCommand
);
172 printError(ErrSysInit
, "Check csqltable.conf file permission. unable to remove %s from file", dsn
);
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';
185 bool isDSNExist
=false;
186 fp
= fopen(Conf :: config
.getDsConfigFile(),"r");
189 printError(ErrSysInit
, "csqlds.conf file does not exist");
192 printf("===================================================\n");
193 printf("List of Data Sources present in 'csqlds.conf' file.\n");
194 printf("===================================================\n");
196 fscanf(fp
,"%s %s %s %s\n",dsnId
,user
,passwd
,tdbname
);
197 printf("%s\n",dsnId
);