00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "rodsGeneralUpdate.h"
00016
00017 #include "rodsClient.h"
00018 #include "icatMidLevelRoutines.h"
00019 #include "icatLowLevel.h"
00020
00021 extern int sGetColumnInfo(int defineVal, char **tableName, char **columnName);
00022 extern icatSessionStruct *chlGetRcs();
00023
00024 extern int icatGeneralQuerySetup();
00025
00026 int updateDebug=0;
00027
00028 int logSQLGenUpdate=0;
00029 char tSQL[MAX_SQL_SIZE];
00030
00031 int
00032 generalInsert(generalUpdateInp_t generalUpdateInp) {
00033 int i, j;
00034 char *tableName, *columnName;
00035 char *firstTableName;
00036 char nextSeqValueIndex=-1;
00037 static char nextStr[MAX_NAME_LEN];
00038 int doBind;
00039 static char myTime[50];
00040
00041 rstrcpy(tSQL, "insert into ", MAX_SQL_SIZE);
00042
00043 for (i=0;i<generalUpdateInp.values.len;i++) {
00044 j = sGetColumnInfo(generalUpdateInp.values.inx[i],
00045 &tableName, &columnName);
00046 if (generalUpdateInp.values.inx[i] < MAX_CORE_TABLE_VALUE) {
00047 return(CAT_TABLE_ACCESS_DENIED);
00048 }
00049 if (updateDebug) printf("j=%d\n",j);
00050 if (j==0) {
00051 if (updateDebug) printf("tableName=%s\n",tableName);
00052 if (updateDebug) printf("columnName=%s\n",columnName);
00053 }
00054 else {
00055 return(j);
00056 }
00057
00058 doBind=1;
00059 if (strncmp(generalUpdateInp.values.value[i],
00060 UPDATE_NEXT_SEQ_VALUE, MAX_NAME_LEN)==0) {
00061
00062 cllNextValueString("R_ExtObjectID", nextStr, MAX_NAME_LEN);
00063 nextSeqValueIndex=i;
00064 doBind=0;
00065 }
00066 if (i==0) {
00067 firstTableName=tableName;
00068 rstrcat(tSQL, tableName, MAX_SQL_SIZE);
00069 rstrcat(tSQL, " (", MAX_SQL_SIZE);
00070 rstrcat(tSQL, columnName, MAX_SQL_SIZE);
00071 if (doBind) {
00072 if (strncmp(generalUpdateInp.values.value[i],
00073 UPDATE_NOW_TIME, MAX_NAME_LEN) == 0) {
00074 getNowStr(myTime);
00075 cllBindVars[cllBindVarCount++]=myTime;
00076 }
00077 else {
00078 cllBindVars[cllBindVarCount++]=generalUpdateInp.values.value[i];
00079 }
00080 }
00081 }
00082 else {
00083 if (strcmp(tableName, firstTableName) !=0) {
00084 return(CAT_INVALID_ARGUMENT);
00085 }
00086 rstrcat(tSQL, ", ", MAX_SQL_SIZE);
00087 rstrcat(tSQL, columnName, MAX_SQL_SIZE);
00088 if (doBind) {
00089 if (strncmp(generalUpdateInp.values.value[i],
00090 UPDATE_NOW_TIME, MAX_NAME_LEN) == 0) {
00091 getNowStr(myTime);
00092 cllBindVars[cllBindVarCount++]=myTime;
00093 }
00094 else {
00095 cllBindVars[cllBindVarCount++]=generalUpdateInp.values.value[i];
00096 }
00097 }
00098 }
00099 }
00100 if (nextSeqValueIndex==0) {
00101 rstrcat(tSQL, ") values (", MAX_SQL_SIZE);
00102 rstrcat(tSQL, nextStr, MAX_SQL_SIZE);
00103 }
00104 else {
00105 rstrcat(tSQL, ") values (?", MAX_SQL_SIZE);
00106 }
00107 for (i=1;i<generalUpdateInp.values.len;i++) {
00108 if (nextSeqValueIndex==i) {
00109 rstrcat(tSQL, ", ", MAX_SQL_SIZE);
00110 rstrcat(tSQL, nextStr, MAX_SQL_SIZE);
00111 }
00112 else {
00113 rstrcat(tSQL, ", ?", MAX_SQL_SIZE);
00114 }
00115 }
00116 rstrcat(tSQL, ")", MAX_SQL_SIZE);
00117 if (updateDebug) printf("tSQL: %s\n", tSQL);
00118
00119 return(0);
00120 }
00121
00122 int
00123 generalDelete(generalUpdateInp_t generalUpdateInp) {
00124 int i, j;
00125 char *tableName, *columnName;
00126 char *firstTableName;
00127
00128 rstrcpy(tSQL, "delete from ", MAX_SQL_SIZE);
00129
00130 for (i=0;i<generalUpdateInp.values.len;i++) {
00131 if (generalUpdateInp.values.inx[i] < MAX_CORE_TABLE_VALUE) {
00132 return(CAT_TABLE_ACCESS_DENIED);
00133 }
00134 j = sGetColumnInfo(generalUpdateInp.values.inx[i],
00135 &tableName, &columnName);
00136 if (updateDebug) printf("j=%d\n",j);
00137 if (j==0) {
00138 if (updateDebug) printf("tableName=%s\n",tableName);
00139 if (updateDebug) printf("columnName=%s\n",columnName);
00140 }
00141 else {
00142 return(j);
00143 }
00144 if (i==0) {
00145 firstTableName=tableName;
00146 rstrcat(tSQL, tableName, MAX_SQL_SIZE);
00147 rstrcat(tSQL, " where ", MAX_SQL_SIZE);
00148 rstrcat(tSQL, columnName, MAX_SQL_SIZE);
00149 rstrcat(tSQL, " = ?", MAX_SQL_SIZE);
00150 cllBindVars[cllBindVarCount++]=generalUpdateInp.values.value[i];
00151 }
00152 else {
00153 if (strcmp(tableName, firstTableName) !=0) {
00154 return(CAT_INVALID_ARGUMENT);
00155 }
00156 rstrcat(tSQL, " and ", MAX_SQL_SIZE);
00157 rstrcat(tSQL, columnName, MAX_SQL_SIZE);
00158 rstrcat(tSQL, " = ?", MAX_SQL_SIZE);
00159 cllBindVars[cllBindVarCount++]=generalUpdateInp.values.value[i];
00160 }
00161 }
00162 if (updateDebug) printf("tSQL: %s\n", tSQL);
00163 return(0);
00164 }
00165
00166
00167
00168 int
00169 chlGeneralUpdate(generalUpdateInp_t generalUpdateInp) {
00170 int status;
00171 static int firstCall=1;
00172 icatSessionStruct *icss;
00173
00174 icss = chlGetRcs();
00175
00176
00177 if (firstCall) {
00178 icatGeneralQuerySetup();
00179 }
00180 if (generalUpdateInp.type == GENERAL_UPDATE_INSERT) {
00181 status = generalInsert(generalUpdateInp);
00182 if (status) return (status);
00183
00184
00185
00186 if (logSQLGenUpdate) rodsLog(LOG_SQL, "chlGeneralUpdate sql 1");
00187 }
00188 else {
00189 if (generalUpdateInp.type == GENERAL_UPDATE_DELETE) {
00190 status = generalDelete(generalUpdateInp);
00191 if (status) return (status);
00192 if (logSQLGenUpdate) rodsLog(LOG_SQL, "chlGeneralUpdate sql 2");
00193 }
00194 else {
00195 return(CAT_INVALID_ARGUMENT);
00196 }
00197 }
00198
00199 status = cmlExecuteNoAnswerSql(tSQL, icss);
00200 if (status != 0) {
00201 rodsLog(LOG_NOTICE,"chlGeneralUpdate cmlExecuteNoAnswerSql insert failure %d",status);
00202 cmlExecuteNoAnswerSql("rollback", icss);
00203 return(status);
00204 }
00205
00206 status = cmlExecuteNoAnswerSql("commit", icss);
00207 if (status != 0) {
00208 rodsLog(LOG_NOTICE,
00209 "chlGeneralUpdate cmlExecuteNoAnswerSql commit failure %d",
00210 status);
00211 return(status);
00212 }
00213
00214 return(0);
00215 }
00216
00217 int
00218 chlDebugGenUpdate(int mode) {
00219 logSQLGenUpdate = mode;
00220 return(0);
00221 }