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
);
36 int main(int argc
, char **argv
)
38 Conf::config
.readAllValues(os::getenv("CSQL_CONFIG_FILE"));
39 char username
[IDENTIFIER_LENGTH
];
41 char password
[IDENTIFIER_LENGTH
];
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;
51 while ((c
= getopt(argc
, argv
, "U:P:D:N:ar?")) != EOF
)
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]);
61 isDSNSpecified
= true;
64 case 'N' : { strcpy(tdbname
, argv
[optind
- 1]);
67 isTDBSpecified
= true;
70 case '?' : {showmsg
=true; break; } //print help
71 case 'a' : { shouldadd
=1; break; }
72 case 'r' : { shouldadd
=2; break;}
76 if (opt
== 10 || true==showmsg
) {
81 if( !isUserNameSpecified
|| !isPasswordSpecified
)
83 strcpy(username
, "NULL");
84 strcpy(password
, "NULL");
90 addEntryToCsqlds( dsn
,username
,password
,tdbname
);
97 }else if (shouldadd
== 2)
99 removeEntryFromCsqlds(dsn
);
102 printf("Invalid option passed\n");
108 DbRetVal
addEntryToCsqlds(char *dsn
, char *uname
, char *pwd
, char *tbdname
)
110 FILE *fp
= fopen(Conf::config
.getDsConfigFile(), "a+");
112 printError(ErrSysInit
, "Invalid path/filename in DS_CONFIG_FILE.\n");
115 bool isDsnExist
=false;
116 char usertmp
[IDENTIFIER_LENGTH
];
117 char pwdtmp
[IDENTIFIER_LENGTH
];
118 char dsntmp
[IDENTIFIER_LENGTH
];
119 char tdbname
[IDENTIFIER_LENGTH
];
122 fscanf(fp
, "%s %s %s %s\n",dsntmp
,usertmp
,pwdtmp
,tdbname
);
123 if (strcmp (dsn
, dsntmp
) == 0) {
125 printf("Applied DSN already Exists\n");
129 fprintf(fp
,"%s %s %s %s\n",dsn
, uname
, pwd
, tbdname
);
134 DbRetVal
removeEntryFromCsqlds(char *dsn
)
137 char tmpFileName
[MAX_FILE_PATH_LEN
];
138 sprintf(tmpFileName
, "%s.tmp", Conf::config
.getDsConfigFile());
139 char usertmp
[IDENTIFIER_LENGTH
];
140 char pwdtmp
[IDENTIFIER_LENGTH
];
141 char dsntmp
[IDENTIFIER_LENGTH
];
142 char tdbname
[IDENTIFIER_LENGTH
];
143 tmpfp
= fopen(tmpFileName
,"w");
144 if( tmpfp
== NULL
) {
145 printError(ErrSysInit
, "Invalid path/filename in TABLE_CONFIG_FILE.\n");
148 fp
= fopen(Conf::config
.getDsConfigFile(),"r");
150 printError(ErrSysInit
, "csqltable.conf file does not exist");
156 fscanf(fp
, "%s %s %s %s\n",dsntmp
,usertmp
,pwdtmp
,tdbname
);
157 if (strcmp (dsn
, dsntmp
) == 0) continue;
158 fprintf(tmpfp
, "%s %s %s %s\n",dsntmp
,usertmp
,pwdtmp
,tdbname
);
162 char sysCommand
[MAX_FILE_PATH_LEN
* 2];
163 sprintf(sysCommand
, "mv %s %s", tmpFileName
, Conf::config
.getDsConfigFile());
164 int ret
= system(sysCommand
);
167 printError(ErrSysInit
, "Check csqltable.conf file permission. unable to remove %s from file", dsn
);