📄 process.c
字号:
/*** Copyright (c) 1995-2001 Hughes Technologies Pty Ltd. All rights** reserved. **** Terms under which this software may be used or copied are** provided in the specific license associated with this product.**** Hughes Technologies disclaims all warranties with regard to this ** software, including all implied warranties of merchantability and ** fitness, in no event shall Hughes Technologies be liable for any ** special, indirect or consequential damages or any damages whatsoever ** resulting from loss of use, data or profits, whether in an action of ** contract, negligence or other tortious action, arising out of or in ** connection with the use or performance of this software.****** $Id: process.c,v 1.18 2002/11/25 01:07:38 bambi Exp $***//*** Module : main : process** Purpose : ** Exports : ** Depends Upon : *//**************************************************************************** STANDARD INCLUDES**************************************************************************/#include <common/config.h>#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#ifdef HAVE_UNISTD_H# include <unistd.h>#endif#ifdef HAVE_STRING_H# include <string.h>#endif#ifdef HAVE_STRINGS_H# include <strings.h>#endif/**************************************************************************** MODULE SPECIFIC INCLUDES**************************************************************************/#ifdef HAVE_DIRENT_H# include <dirent.h>#else# include <direct.h>#endif#include <fcntl.h>#include <errno.h>#include <sys/stat.h>#include <common/msql_defs.h>#include <common/debug/debug.h>#include <msqld/index/index.h>#include <msqld/includes/msqld.h>#include <msqld/includes/errmsg.h>#include <msqld/main/main.h>#include <msqld/main/yaccer.h>#include <msqld/main/table.h>#include <msqld/main/index.h>#include <msqld/main/net.h>#include <msqld/main/version.h>#include <msqld/main/util.h>#include <msqld/main/cache.h>#include <msqld/main/varchar.h>#include <msqld/main/acl.h>#include <msqld/main/compare.h>#include <msqld/main/select.h>#include <msqld/main/parse.h>#include <msqld/main/process.h>#include <common/types/types.h>#include <msqld/cra/cra.h>#include <msqld/lock/lock.h>#include <libmsql/msql.h>#include <msqld/broker/broker.h>/**************************************************************************** GLOBAL VARIABLES**************************************************************************//* HACK */extern char errMsg[];extern char *packet;/**************************************************************************** PRIVATE ROUTINES**************************************************************************/#define _checkWriteAccess(server) \ if (!aclCheckPerms(WRITE_ACCESS) || server->config.readOnly) \ { \ netError(query->clientSock,"Access Denied\n"); \ debugTrace(TRACE_OUT,"processQuery()"); \ return; \ }static int _populateIndexFile(server, query) msqld *server; mQuery_t *query;{ cache_t *entry; mIndex_t *idx; u_int rowNum; row_t row; int fullFlist[MAX_FIELDS], res; entry = tableLoadDefinition(server, query->indexDef.table, NULL, query->curDB); if (!entry) { return(-1); } idx = entry->indices; while(idx) { if ( *(idx->name) != *(query->indexDef.name)) { idx = idx->next; continue; } if (strcmp(idx->name, query->indexDef.name) != 0) { idx = idx->next; continue; } break; } if (idx == NULL) { snprintf(errMsg,MAX_ERR_MSG,"Can't find index def for '%s'\n", query->indexDef.name); msqlDebug1(MOD_ERR, "Can't find index def for '%s'\n", query->indexDef.name); debugTrace(TRACE_OUT,"_populateIndexFile()"); abort(); } utilSetupFields(entry,fullFlist, entry->def); rowNum = 0; while(rowNum < entry->sblk->numRows) { if (tableReadRow(entry,&row,rowNum) < 0) { return(-1); } if (row.header->active == 0) { rowNum++; continue; } tableExtractValues(entry,&row,entry->def,fullFlist,query); if (indexCheckNullFields(entry,&row,idx,fullFlist) < 0) { processDropIndex(server, query,1); return(-1); } res = indexCheckIndex(entry, entry->def, NULL, idx, NO_POS); if (res < 0) { processDropIndex(server, query,1); debugTrace(TRACE_OUT,"_populateIndexFile()"); return(-1); } if (res == 0) { processDropIndex(server, query,1); snprintf(errMsg,MAX_ERR_MSG,KEY_UNIQ_ERROR); msqlDebug0(MOD_ERR, "Non unique value for unique index\n"); debugTrace(TRACE_OUT,"_populateIndexFile()"); return(-1); } indexInsertIndexValue(entry, entry->def, rowNum, idx); rowNum++; } debugTrace(TRACE_OUT,"_populateIndexFile()"); return(0);}static int _checkNullFields(cacheEntry,row, flist) cache_t *cacheEntry; row_t *row; int *flist;{ mField_t *curField; int *curfl; u_char *data; debugTrace(TRACE_IN,"checkNullFields()"); data = row->data; curfl = flist; curField = cacheEntry->def; while(curField) { if (!*(data + *curfl) && (curField->flags & NOT_NULL_FLAG)) { snprintf(errMsg,MAX_ERR_MSG,BAD_NULL_ERROR, curField->name); msqlDebug1(MOD_ERR,"Field \"%s\" cannot be null\n", curField->name); debugTrace(TRACE_OUT,"checkNullFields()"); return(-1); } curfl++; curField = curField->next; } debugTrace(TRACE_OUT,"checkNullFields()"); return(0);}static void _freeFieldList(head) mField_t *head;{ mField_t *cur, *prev; cur = head; while(cur) { prev = cur; cur = cur->next; if (prev->value) { parseFreeValue(prev->value); prev->value = NULL; } prev->next = NULL; free(prev); }}/**************************************************************************** PUBLIC ROUTINES**************************************************************************/void processListDBs(server, sock) msqld *server; int sock;{ DIR *dirp;#ifdef HAVE_DIRENT_H struct dirent *cur;#else struct direct *cur;#endif debugTrace(TRACE_IN,"processListDBs()"); dirp = opendir(server->config.dbDir); if (!dirp) { snprintf(errMsg,MAX_ERR_MSG,BAD_DIR_ERROR,server->config.dbDir, (char*)strerror(errno)); msqlDebug1(MOD_ERR,"Can't open directory \"%s\"\n", server->config.dbDir); debugTrace(TRACE_OUT,"processListDBs()"); return; } /* ** Grab the names dodging any . files */ cur = readdir(dirp); while(cur) { if (*cur->d_name == '.') { cur = readdir(dirp); continue; } snprintf(packet,PKT_LEN, "%d:%s\n",(int)strlen(cur->d_name), cur->d_name); netWritePacket(sock); cur = readdir(dirp); } netEndOfList(sock); closedir(dirp); debugTrace(TRACE_OUT,"processListDBs()"); return;}void processListTables(server, sock,db) msqld *server; int sock; char *db;{ char path[MSQL_PATH_LEN], *cp; DIR *dirp;#ifdef HAVE_DIRENT_H struct dirent *cur;#else struct direct *cur;#endif debugTrace(TRACE_IN,"msqlListTables()"); (void)snprintf(path,MSQL_PATH_LEN,"%s/%s",server->config.dbDir,db); dirp = opendir(path); if (!dirp) { snprintf(errMsg,MAX_ERR_MSG,BAD_DIR_ERROR,path, (char*)strerror(errno)); msqlDebug1(MOD_ERR,"Can't open directory \"%s\"\n",path); debugTrace(TRACE_OUT,"msqlListTables()"); return; } /* ** Skip over '.' and '..' */ cur = readdir(dirp); while(cur) { if (*cur->d_name != '.') break; cur = readdir(dirp); } /* ** Grab the names */ while(cur) { cp = (char *)rindex(cur->d_name,'.'); if (!cp) { cur = readdir(dirp); continue; } if (strcmp(cp,".def") == 0) { *cp = 0; snprintf(packet,PKT_LEN, "%d:%s\n", (int)strlen(cur->d_name),cur->d_name); netWritePacket(sock); } cur = readdir(dirp); } netEndOfList(sock); closedir(dirp); debugTrace(TRACE_OUT,"msqlListTables()"); return;}void processSequenceInfo(server, sock, table, db) msqld *server; int sock; char *table, *db;{ cache_t *cacheEntry; debugTrace(TRACE_IN,"processSequenceInfo()"); if((cacheEntry = tableLoadDefinition(server, table,NULL,db))) { if (cacheEntry->sblk->sequence.step) { snprintf(packet,PKT_LEN,"1:%d:%d", cacheEntry->sblk->sequence.step, cacheEntry->sblk->sequence.value); netWritePacket(sock); } else { netError(sock, "No sequence defined on '%s'\n", table); } } else { netError(sock, "%s\n",errMsg); } debugTrace(TRACE_OUT,"processSequenceInfo()");}void processListFields(server, sock,table,db) msqld *server; int sock; char *table, *db;{ mField_t *curField; char buf[50], buf2[50]; cache_t *cacheEntry; mIndex_t *curIndex; debugTrace(TRACE_IN,"msqlListFields()"); msqlDebug1(MOD_GENERAL,"Table to list = %s\n",table); *errMsg = 0; if((cacheEntry = tableLoadDefinition(server,table,NULL,db))) { curField = cacheEntry->def; while(curField) { snprintf(buf,sizeof(buf),"%d",curField->length); snprintf(buf2,sizeof(buf2),"%d",curField->type); snprintf(packet,PKT_LEN,"%d:%s%d:%s%d:%s%d:%s1:%s1:%s", (int)strlen(table), table, (int)strlen(curField->name), curField->name, (int)strlen(buf2), buf2, (int)strlen(buf), buf, curField->flags & NOT_NULL_FLAG?"Y":"N", "N"); netWritePacket(sock); curField = curField->next; } curIndex = cacheEntry->indices; while(curIndex) { snprintf(buf2,sizeof(buf2),"%d",IDX_TYPE); snprintf(packet,PKT_LEN,"%d:%s%d:%s%d:%s%d:%s1:%s1:%s", (int)strlen(table), table, (int)strlen(curIndex->name), curIndex->name, (int)strlen(buf2), buf2, 1, "0", "N", curIndex->unique?"Y":"N"); netWritePacket(sock); curIndex = curIndex->next; } netEndOfList(sock); } else { if (*errMsg == 0) netError(sock, "Unknown table '%s'\n", table); else netError(sock, errMsg); } debugTrace(TRACE_OUT,"msqlListFields()");}void processListIndex(server, sock,index,table,db) msqld *server; int sock; char *index, *table, *db;{ mIndex_t *curIndex; cache_t *cacheEntry; char *idxType, idxCount[20], idxKeys[20]; mField_t *curField; int count, offset; debugTrace(TRACE_IN,"msqlListIndex()"); msqlDebug1(MOD_GENERAL,"Index to list = %s\n",index); if((cacheEntry = tableLoadDefinition(server,table,NULL,db))) { curIndex = cacheEntry->indices; while(curIndex) { if (*(curIndex->name) != *index) { curIndex = curIndex->next; continue; } if (strcmp(curIndex->name,index) != 0) { curIndex = curIndex->next; continue; } break; } if (curIndex) { idxType = idxGetIndexType(&curIndex->handle); snprintf(idxCount,sizeof(idxCount), "%d", idxGetNumEntries(&curIndex->handle)); snprintf(idxKeys,sizeof(idxKeys), "%d", idxGetNumKeys(&curIndex->handle)); snprintf(packet,PKT_LEN,"%d:%s\n", (int)strlen(idxType), idxType); netWritePacket(sock); snprintf(packet,PKT_LEN,"%d:%s\n", (int)strlen(idxCount), idxCount); netWritePacket(sock); snprintf(packet,PKT_LEN,"%d:%s\n", (int)strlen(idxKeys), idxKeys); netWritePacket(sock); if (curIndex) { count = 0; while(count < curIndex->fieldCount) { offset = curIndex->fields[count]; curField = cacheEntry->def; while(offset) { curField = curField->next; offset--; } snprintf(packet,PKT_LEN,"%d:%s\n", (int)strlen(curField->name), curField->name); count ++; netWritePacket(sock); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -