📄 types.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: types.c,v 1.13 2002/12/05 04:23:24 bambi Exp $***//*** Module : types : types** 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#include <common/portability.h>#include <common/msql_defs.h>#include <msqld/includes/errmsg.h>/**************************************************************************** MODULE SPECIFIC INCLUDES**************************************************************************/#include <common/msql_defs.h>#include <common/debug/debug.h>#include <common/errmsg.h>#include <msqld/index/index.h>#include <msqld/includes/msqld.h>#include <msqld/main/sysvar.h>#include <libmsql/msql.h>#include "types.h"/**************************************************************************** GLOBAL VARIABLES**************************************************************************/#define REG register/**************************************************************************** PRIVATE ROUTINES**************************************************************************/static int _checkIntValueRange(valType, value, minVal, maxVal) int valType; mVal_t *value; int minVal, maxVal;{ if (valType == MONEY_TYPE) { value->type = INT_TYPE; value->val.intVal = value->val.intVal / 100; valType = INT_TYPE; } if ( valType == INT_TYPE || valType == INT8_TYPE || valType == INT16_TYPE || valType == UINT_TYPE || valType == UINT8_TYPE || valType == UINT16_TYPE ) { if (value->val.intVal <= maxVal && value->val.intVal >= minVal) { return(0); } return(-1); } if ( valType == INT64_TYPE || valType == UINT64_TYPE ) { if (value->val.intVal <= maxVal && value->val.intVal >= minVal) { value->type = INT_TYPE; value->val.intVal = (int)value->val.int64Val; return(0); } return(-1); } return(-2);}static int _checkUintValueRange(valType, value, minVal, maxVal) int valType; mVal_t *value; uint minVal, maxVal;{ if (valType == MONEY_TYPE) { value->type = INT_TYPE; value->val.intVal = value->val.intVal / 100; valType = INT_TYPE; } if ( valType == INT_TYPE || valType == INT8_TYPE || valType == INT16_TYPE || valType == UINT_TYPE || valType == UINT8_TYPE || valType == UINT16_TYPE ) { if ((uint)value->val.intVal <= maxVal && (uint)value->val.intVal >= minVal) { return(0); } } if ( valType == INT64_TYPE || valType == UINT64_TYPE ) { if ((uint)value->val.intVal <= maxVal && (uint)value->val.intVal >= minVal) { value->type = INT_TYPE; value->val.intVal = (int)value->val.int64Val; return(0); } } return(-1);}/**************************************************************************** PUBLIC ROUTINES**************************************************************************/extern char errMsg[];/**************************************************************************** Type determination routines*/int typeFieldSize(type) int type;{ /* ** Look for a specific field length for the type */ switch(type) { case CIDR4_TYPE: return(5); } /* ** OK, we'll just use the size of the base type */ switch(typeBaseType(type)) { case INT8_TYPE: case UINT8_TYPE: return(1); case INT16_TYPE: case UINT16_TYPE: return(2); case INT_TYPE: case UINT_TYPE: return(4); case INT64_TYPE: case UINT64_TYPE: return(8); case REAL_TYPE: return(8); } return(0);}int typeBaseType(type) int type;{ switch(type) { case IDENT_TYPE: return(IDENT_TYPE); case CHAR_TYPE: return(CHAR_TYPE); case INT_TYPE: case MONEY_TYPE: return(INT_TYPE); case INT8_TYPE: return(INT8_TYPE); case INT16_TYPE: return(INT16_TYPE); case INT64_TYPE: return(INT64_TYPE); case UINT_TYPE: case DATE_TYPE: case TIME_TYPE: case IPV4_TYPE: return(UINT_TYPE); case UINT8_TYPE: return(UINT8_TYPE); case UINT16_TYPE: return(UINT16_TYPE); case UINT64_TYPE: return(UINT64_TYPE); case REAL_TYPE: return(REAL_TYPE); case TEXT_TYPE: return(TEXT_TYPE); case CIDR4_TYPE: return(BYTE_TYPE); default: fprintf(stderr,"\n\nERROR : Unknown type '%d'\n\n", type); } /* Unknown Type */ exit(0);}int typeRepType(type) int type;{ switch(type) { case CHAR_TYPE: case DATE_TYPE: case TEXT_TYPE: case TIME_TYPE: case IPV4_TYPE: case CIDR4_TYPE: return(CHAR_TYPE); case REAL_TYPE: case MONEY_TYPE: return(REAL_TYPE); case INT_TYPE: case INT8_TYPE: case INT16_TYPE: case INT64_TYPE: return(INT_TYPE); case UINT_TYPE: case UINT8_TYPE: case UINT16_TYPE: case UINT64_TYPE: return(UINT_TYPE); default: fprintf(stderr,"\n\nERROR : Unknown type '%d'\n\n", type); } /* Unknown Type */ exit(0);}int typeValidConditionTarget(fieldType, value) int fieldType; mVal_t *value;{ int valType, res; /* ** Check out the field type and determine if the value's type ** is usable or if it can be cast to something we can use */ valType = value->type; if (valType == SYSVAR_TYPE) { valType = sysvarGetVariableType(value->val.identVal->seg2); } switch(fieldType) { case INT8_TYPE: res = _checkIntValueRange(valType, value, -127, 127); if (res < 0) { if (res == -1) { snprintf(errMsg,MAX_ERR_MSG,INT_RANGE_ERROR); return(-2); } return(-1); } return(0); case INT16_TYPE: res = _checkIntValueRange(valType, value, -32767, 32767); if (res < 0) { if (res == -1) { snprintf(errMsg, MAX_ERR_MSG, INT_RANGE_ERROR); return(-2); } return(-1); } return(0); case INT_TYPE: res = _checkIntValueRange(valType, value, -2147483647, 2147483647); if (res < 0) { if (res == -1) { snprintf(errMsg, MAX_ERR_MSG, INT_RANGE_ERROR); return(-2); } return(-1); } return(0); case UINT8_TYPE: res = _checkUintValueRange(valType, value, 0, 255); if (res < 0) { if(res == -1) { snprintf(errMsg, MAX_ERR_MSG, INT_RANGE_ERROR); return(-2); } return(-1); } return(0); case UINT16_TYPE: res = _checkUintValueRange(valType, value, 0, 65535); if (res < 0) { if (res == -1) { snprintf(errMsg, MAX_ERR_MSG, INT_RANGE_ERROR); return(-2); } return(-1); } return(0); case UINT_TYPE: res = _checkUintValueRange(valType, value, 0, 4294967295u); if ( res < 0 && valType == MONEY_TYPE) { value->type = INT_TYPE; value->val.intVal /= 100; res = 0; } if (res < 0) { if (res == -1) { snprintf(errMsg, MAX_ERR_MSG, INT_RANGE_ERROR); return(-2); } return(-1); } return(0); case INT64_TYPE: case UINT64_TYPE: if ( valType == INT64_TYPE || valType == UINT64_TYPE ) { return(0); } if ( valType == INT_TYPE || valType == INT8_TYPE || valType == INT16_TYPE || valType == UINT_TYPE || valType == UINT8_TYPE || valType == UINT16_TYPE ) { value->type = INT64_TYPE; value->val.int64Val = value->val.intVal; return(0); } if (valType == MONEY_TYPE) { value->type = INT64_TYPE; value->val.int64Val = value->val.intVal / 100; return(0); } return(-1); case CHAR_TYPE: case TEXT_TYPE: if ( value->type == CHAR_TYPE || value->type == TEXT_TYPE ) { return(0); } return(-1); case REAL_TYPE: if ( value->type == REAL_TYPE) { return(0); } if( value->type == INT_TYPE || value->type == INT8_TYPE || value->type == INT16_TYPE || value->type == UINT_TYPE || value->type == UINT8_TYPE || value->type == UINT16_TYPE ) { value->val.realVal = value->val.intVal; value->type = REAL_TYPE; return(0); } if (value->type == MONEY_TYPE) { value->type = REAL_TYPE; value->val.realVal = value->val.intVal / 100; return(0); } return(-1); case MONEY_TYPE: if ( value->type == MONEY_TYPE ) { return(0); } if ( value->type == INT_TYPE || value->type == INT8_TYPE || value->type == INT16_TYPE || value->type == UINT_TYPE || value->type == UINT8_TYPE || value->type == UINT16_TYPE || value->type == REAL_TYPE ) { value->val.intVal=typeScanMoney(value,errMsg, MAX_ERR_MSG); value->type = MONEY_TYPE; return(0); } return(-1); case DATE_TYPE: if (value->type == CHAR_TYPE) { /* ** Scan the literal value for a valid date */ value->val.intVal = typeScanDate(value,errMsg,MAX_ERR_MSG); free(value->val.charVal); value->type = DATE_TYPE; if (value->val.intVal == -1) { return(-2); } } if (value->type == DATE_TYPE) { return(0); } return(-1); case TIME_TYPE: if (value->type == CHAR_TYPE) { /* ** Scan the literal value for a valid time */ value->val.intVal = typeScanTime(value, errMsg, MAX_ERR_MSG); free(value->val.charVal); value->type = TIME_TYPE; if (value->val.intVal == -1) { return(-2); } } if (value->type == TIME_TYPE) { return(0); } return(-1); case IPV4_TYPE: if (value->type == CHAR_TYPE) { /* ** Scan the literal value for an IPv4 addr */ value->val.intVal = typeScanIPv4(value, errMsg, MAX_ERR_MSG); free(value->val.charVal); value->type = IPV4_TYPE; if (value->val.intVal == -1) { return(-2); } } if (value->type == IPV4_TYPE) { return(0); } return(-1); case CIDR4_TYPE: if (value->type == CHAR_TYPE) { /* ** Scan the literal value for a CIDR prefix */ value->val.byteVal = typeScanCIDR4(value, errMsg, MAX_ERR_MSG); free(value->val.charVal); value->type = CIDR4_TYPE; if (value->val.byteVal == NULL) { return(-2); } } if (value->type == CIDR4_TYPE) { return(0); } return(-1); default: /* Hmmm, what is this thing then ? */ msqlDebug1(MOD_ERR, "Unknown type %d", fieldType); return(-1); }}char *typePrintValue(buf, len, type, value) char *buf; int len, type; mVal_t *value;{ char format[10]; switch(type) { case INT_TYPE: case INT8_TYPE: case INT16_TYPE: snprintf(buf,len,"%d", value->val.intVal); return(buf); case UINT_TYPE: case UINT8_TYPE: case UINT16_TYPE: snprintf(buf,len,"%u", (unsigned int)value->val.intVal); return(buf); case DATE_TYPE: typePrintDate(buf,len, value->val.intVal); return(buf); case TIME_TYPE: typePrintTime(buf,len, value->val.intVal); return(buf); case IPV4_TYPE: typePrintIPv4(buf,len, value->val.intVal); return(buf); case MONEY_TYPE: typePrintMoney(buf, len, value->val.intVal); return(buf); case CHAR_TYPE: case TEXT_TYPE: return(value->val.charVal); case CIDR4_TYPE: typePrintCIDR4(buf,len, value->val.byteVal); return(buf); case REAL_TYPE: snprintf(format,sizeof(format), "%%.%df", value->precision); snprintf(buf, len, format, value->val.realVal); return(buf); } return(NULL);}/*char msqlTypeNames[][12] = {"???", "int", "char","real","ident","null","text","date","uint", "money","time","ip","int64","uint64","int8","int16","???"};*/char *typePrintTypeName(type) int type;{ static char unknown[] = "UNKNOWN TYPE"; if (type < 1 || type > 9) return(unknown); return(msqlTypeNames[type]);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -