//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% void mSQLDBC::setSQL(const char *sql){ if (!isempty(sql)){ char *sql1 = strdup(sql); //Because sql is const :)
static int msql_execute (dbconn_t * c, const char * s) { if (msqlQuery(c->msql.handle, s) != -1) { c->msql.result_set = msqlStoreResult(); if (!c->msql.result_set) { /* Query was an UPDATE or INSERT or DELETE */ return 0; } return msqlNumRows(c->msql.result_set); } return -1; }
void main(void) { int dbh; m_result *tmp; m_row res; time_t clock; if( (dbh=msqlConnect(NULL))<0) die(msqlErrMsg); if( msqlSelectDB(dbh, "web") < 0) die(msqlErrMsg); if( msqlQuery(dbh, "select hostname, who_updated, date_updated from virtuals") < 0) die(msqlErrMsg); tmp=msqlStoreResult(); puts("Content-type: text/html\n"); puts("<html><head><title>Virtual Hosts</title></head>\n"); puts("<body bgcolor=\"ffffff\">\n"); puts("<h2>Virtual Hosts on Heavy</h2>\n<table>\n"); puts("<tr><td>Hostname</td><td>Who updated</td><td>Last Updated</td></tr>"); while( (res=msqlFetchRow(tmp)) !=NULL) { clock=atol(res[2]); printf("<tr><td color=\"eeeeff\"><a \ href=\"/cgi-bin/tools/hosts/gethostinfo.cgi?%s\">%s</a></td> \ <td>%s</td><td>%s</td></tr>\n", res[0], res[0], res[1], ctime(&clock) ); } puts("</table><hr align=\"left\" width=\"50%\">"); puts("<font size=\"-2\">"); puts("Copyright © 1996 <a href=\"http://www.ipa.net/\">Internet Partners of America</a>"); puts("</font></body></html>"); msqlFreeResult(tmp); msqlClose(dbh); }
void main(int argc, char *argv[]) { int dbh, tmpint; m_result *tmp; m_row res; char query[500]; static char *selected="SELECTED"; char *c[2]={"", ""}, *t[2]={"", ""}, *e[2]={"", ""}, *a[2]={"", ""}; struct cgiform *form; char *hostname; puts("Content-type: text/html\n"); puts("<html><head><title>Info</title></head>\n"); puts("<body bgcolor=\"ffffff\">\n"); if( (dbh=msqlConnect(NULL))<0) die(msqlErrMsg); if( msqlSelectDB(dbh, "web") < 0) die(msqlErrMsg); if(argc>1) { hostname=argv[1]; } else { form=cgiinit(); hostname=cgigetdata(form, "hostname"); } sprintf(query, "select * from virtuals where hostname='%s'", hostname); if( msqlQuery(dbh, query) < 0) { printf("Oops, didn't like that: %s<br>\n", msqlErrMsg); puts("</body></html>"); exit(0); } tmp=msqlStoreResult(); puts("<h2>Edit a virtual host</h2>"); puts("<form action=\"/cgi-bin/tools/hosts/edithost.cgi\" \ method=\"POST\">"); res=msqlFetchRow(tmp); if(res==NULL) { printf("Problem getting the data<br>\n"); puts("</body></html>"); exit(0); } tmpint=atoi(res[6]); c[tmpint]=selected; tmpint=atoi(res[4]); t[tmpint]=selected; tmpint=atoi(res[5]); e[tmpint]=selected; tmpint=atoi(res[9]); a[tmpint]=selected; puts("<table>\n"); printf("<tr><td>Hostname:</td><td><input name=\"hostname\" \ value=\"%s\"></td></tr>\n", res[0]); printf("<tr><td>Admin:</td><td><input name=\"admin\" \ value=\"%s\"></td></tr>\n", res[1]); printf("<tr><td>User:</td><td><input name=\"user\" \ value=\"%s\"></td></tr>\n", res[2]); printf("<tr><td>Group:</td><td><input name=\"group\" \ value=\"%s\"></td></tr>\n", res[3]); printf("<tr><td>Keep Transfer log:</td><td><select name=\"trans_log\">\ <option value=\"0\" %s>No\n<option value=\"1\" %s>Yes</select>\n", t[0], t[1]); printf("<tr><td>Keep Error log:</td><td><select name=\"err_log\">\ <option value=\"0\" %s>No\n<option value=\"1\" %s>Yes</select>\n", e[0], e[1]); printf("<tr><td>Create CGI bin:</td><td><select name=\"cgi_bin\">\ <option value=\"0\" %s>No\n<option value=\"1\" %s>Yes</select>\n", c[0], c[1]); printf("<tr><td>Active:</td><td><select name=\"active\">\ <option value=\"0\" %s>No\n<option value=\"1\" %s>Yes</select>\n", a[0], a[1]); puts("</table><input type=\"submit\" value=\"Save it\"></form><br>"); printf("<a href=\"/cgi-bin/tools/hosts/deletehost.cgi?%s\">Delete %s</a>", res[0], res[0]); puts("<hr align=\"left\" width=\"50%\">"); puts("<font size=\"-2\">"); puts("Copyright © 1996 <a href=\"/\">SPY Internetworking</a>"); puts("</font></body></html>"); msqlFreeResult(tmp); msqlClose(dbh); }
/* * SubVar * * This function substitutes variables designated by the leading separator (sep) * with values with matching names. The string buffer will be realloced in 512-byte * chunks if needed. */ char *SubVar(char sep, char *var_name, char *var_value, char *oldbuf, int *bufsize) { char *buf, *s, *t; int l, n; char o; char *local_buf; #if MSQL char *u; int i,j; int numi, numj; char temp[1024]; m_result *result=NULL; m_row record; int new_connection=0; #endif l=strlen(oldbuf); n=strlen(var_value); if(l-(int)strlen(var_name)-1+(int)strlen(var_value) >(int)(*bufsize-1)) { #if DEBUG fprintf(fperr,"Query buffer re-allocated\n"); fflush(fperr); #endif buf=realloc(oldbuf,*bufsize+((n>512)?n:512)); *bufsize+=((n>512)?n:512); } else buf=oldbuf; /* I don't trust strdup() on many systems */ local_buf=malloc(*bufsize*sizeof(char)); strcpy(local_buf,buf); if(!local_buf) { html_error("FI Error","Unable to allocate memory"); exit(-1); } s=local_buf; while(*s!='\0') { if(*s==sep) { if(s>local_buf && *(s-1)=='\\') { /* Allow for escaping the separator */ s++; continue; } n=strcspn(s+1," ,()'\n:;.{}[]-+=~!@#$%^*<>\""); t=s+n+1; o=*t; *t='\0'; if(!strcasecmp(var_name,s+1)) { *s='\0'; #if MSQL if(!strcasecmp(var_name,"sql")) { /* looking for database name */ #if DEBUG fprintf(fperr,"var_name=[%s], var_value=[%s]\n",var_name,var_value); fflush(fperr); #endif u=strchr(var_value,' '); if(u) { *u='\0'; if(strlen(current_db)==0) { if(dbsock==-1) { dbsock=msqlConnect(NULL); new_connection=1; } if(msqlSelectDB(dbsock,var_value)<0) { strcpy(temp,msqlErrMsg); current_db[0]='\0'; } else { strcpy(current_db,var_value); } } if(msqlQuery(dbsock,u+1)<0) strcpy(temp,msqlErrMsg); else { temp[0]='\0'; msqlStoreResult(result); numi=msqlNumRows(result); for(i=0;i<numi;i++) { if(strlen(before_record)) strcat(temp,before_record); msqlDataSeek(result,i); numj=msqlNumFields(result); record=msqlFetchRow(result); for(j=0;j<numj;j++) { if(strlen(before_element)) strcat(temp,before_element); strcat(temp,record[j]); if(strlen(after_element)) strcat(temp,after_element); } if(strlen(after_record)) strcat(temp,after_record); } msqlFreeResult(result); if(new_connection) { msqlClose(dbsock); dbsock=-1; current_db[0]='\0'; } } } } #endif sprintf(buf,"%s%s%c%s",local_buf,var_value,o,t+1); strcpy(local_buf,buf); } else { *t=o; } s=t+1; } else s++; } free(local_buf); return(buf); }
SQLRETURN _Execute( SQLHSTMT hDrvStmt ) { HDRVSTMT hStmt = (HDRVSTMT)hDrvStmt; int nColumn; int nCols; int nRow; m_result *pResults; /* mSQL DATA */ m_row rowResult; /* mSQL ROW */ m_field *pField; /* mSQL COL HDR */ COLUMNHDR *pColumnHeader; /* SANITY CHECKS */ if( NULL == hStmt ) return SQL_INVALID_HANDLE; sprintf( hStmt->szSqlMsg, "hStmt = $%08lX", hStmt ); logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_WARNING, LOG_WARNING, hStmt->szSqlMsg ); if( hStmt->pszQuery == NULL ) { logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_WARNING, LOG_WARNING, "SQL_ERROR No prepared statement" ); return SQL_ERROR; } /************************** * Free any current results **************************/ if ( hStmt->hStmtExtras->aResults ) _FreeResults( hStmt->hStmtExtras ); /************************** * send prepared query to server **************************/ if ( (hStmt->hStmtExtras->nRows = msqlQuery( ((HDRVDBC)hStmt->hDbc)->hDbcExtras->hServer, hStmt->pszQuery )) == -1 ) { sprintf( hStmt->szSqlMsg, "SQL_ERROR Query failed. %s", msqlErrMsg ); logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_WARNING, LOG_WARNING, hStmt->szSqlMsg ); return SQL_ERROR; } /************************** * snapshot our results (assume no results means UPDATE, DELETE or INSERT **************************/ pResults = msqlStoreResult(); if ( !pResults ) return SQL_SUCCESS; /************************** * allocate memory for columns headers and result data (row 0 is column header while col 0 is reserved for bookmarks) **************************/ hStmt->hStmtExtras->nRows = msqlNumRows( pResults ); hStmt->hStmtExtras->nCols = msqlNumFields( pResults ); hStmt->hStmtExtras->aResults = malloc( sizeof(char*) * (hStmt->hStmtExtras->nRows+1) * (hStmt->hStmtExtras->nCols+1) ); if ( hStmt->hStmtExtras->aResults == NULL ) { logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_WARNING, LOG_WARNING, "Not enough memory. (malloc failed)" ); hStmt->hStmtExtras->nRows = 0; hStmt->hStmtExtras->nCols = 0; msqlFreeResult( pResults ); return SQL_ERROR; } memset( hStmt->hStmtExtras->aResults, 0, sizeof(char*) * (hStmt->hStmtExtras->nRows+1) * (hStmt->hStmtExtras->nCols+1) ); /************************** * gather column header information (save col 0 for bookmarks) **************************/ for ( nColumn = 1; nColumn <= hStmt->hStmtExtras->nCols; nColumn++ ) { pField = msqlFetchField( pResults ); (hStmt->hStmtExtras->aResults)[nColumn] = malloc( sizeof(COLUMNHDR) ); memset( (hStmt->hStmtExtras->aResults)[nColumn], 0, sizeof(COLUMNHDR) ); pColumnHeader = (COLUMNHDR*)(hStmt->hStmtExtras->aResults)[nColumn]; _NativeToSQLColumnHeader( pColumnHeader, pField ); } /************************ * gather data (save col 0 for bookmarks) ************************/ nCols = hStmt->hStmtExtras->nCols; nRow = 0; while ( (rowResult = msqlFetchRow( pResults )) != NULL ) { nRow++; msqlFieldSeek( pResults, 0 ); for ( nColumn=1; nColumn <= nCols; nColumn++ ) { if ( rowResult[nColumn-1] ) (hStmt->hStmtExtras->aResults)[nRow*nCols+nColumn] = (char *)strdup( rowResult[nColumn-1] ); } } hStmt->hStmtExtras->nRow = 0; /************************** * free the snapshot **************************/ msqlFreeResult( pResults ); logPushMsg( hStmt->hLog, __FILE__, __FILE__, __LINE__, LOG_INFO, LOG_INFO, "SQL_SUCCESS" ); return SQL_SUCCESS; }