odbcconvert.c

来自「这个是内存数据库的客户端」· C语言 代码 · 共 2,426 行 · 第 1/5 页

C
2,426
字号
			/* Restricted data type attribute violation */			addStmtError(stmt, "07006", NULL, 0);			return SQL_ERROR;		}		if (lenp)			*lenp = sizeof(TIMESTAMP_STRUCT);		break;	case SQL_C_INTERVAL_YEAR:	case SQL_C_INTERVAL_MONTH:	case SQL_C_INTERVAL_YEAR_TO_MONTH:		if (ardrec && row > 0)			ptr = (SQLPOINTER) ((char *) ptr +row * (bind_type == SQL_BIND_BY_COLUMN ? sizeof(SQL_INTERVAL_STRUCT) : bind_type));		switch (sql_type) {		case SQL_CHAR:			if (parsemonthintervalstring(&data, NULL, &ival) == SQL_ERROR) {				/* Invalid character value for cast				   specification */				addStmtError(stmt, "22018", NULL, 0);				return SQL_ERROR;			}			break;		case SQL_DECIMAL:		case SQL_TINYINT:		case SQL_SMALLINT:		case SQL_INTEGER:		case SQL_BIGINT:			parsemonthinterval(&nval, &ival, type);			break;		case SQL_INTERVAL_MONTH:			break;		default:			/* Restricted data type attribute violation */			addStmtError(stmt, "07006", NULL, 0);			return SQL_ERROR;		}#define p ((SQL_INTERVAL_STRUCT *) ptr)	/* abbrev. */		p->interval_sign = ival.interval_sign;		p->intval.year_month.year = 0;		p->intval.year_month.month = 0;		switch (type) {		case SQL_C_INTERVAL_YEAR:			p->interval_type = SQL_IS_YEAR;			if ((p->intval.year_month.year = ival.intval.year_month.year) >= maxdatetimeval) {				/* Interval field overflow */				addStmtError(stmt, "22015", NULL, 0);				return SQL_ERROR;			}			if (ival.intval.year_month.month) {				/* Fractional truncation */				addStmtError(stmt, "01S07", NULL, 0);			}			break;		case SQL_C_INTERVAL_MONTH:			p->interval_type = SQL_IS_MONTH;			if ((p->intval.year_month.month = ival.intval.year_month.month + 12 * ival.intval.year_month.year) >= maxdatetimeval) {				/* Interval field overflow */				addStmtError(stmt, "22015", NULL, 0);				return SQL_ERROR;			}			break;		case SQL_C_INTERVAL_YEAR_TO_MONTH:			p->interval_type = SQL_IS_YEAR_TO_MONTH;			if ((p->intval.year_month.year = ival.intval.year_month.year) >= maxdatetimeval) {				/* Interval field overflow */				addStmtError(stmt, "22015", NULL, 0);				return SQL_ERROR;			}			p->intval.year_month.month = ival.intval.year_month.month;			break;		}#undef p		if (lenp)			*lenp = sizeof(SQL_INTERVAL_STRUCT);		break;	case SQL_C_INTERVAL_DAY:	case SQL_C_INTERVAL_HOUR:	case SQL_C_INTERVAL_MINUTE:	case SQL_C_INTERVAL_SECOND:	case SQL_C_INTERVAL_DAY_TO_HOUR:	case SQL_C_INTERVAL_DAY_TO_MINUTE:	case SQL_C_INTERVAL_DAY_TO_SECOND:	case SQL_C_INTERVAL_HOUR_TO_MINUTE:	case SQL_C_INTERVAL_HOUR_TO_SECOND:	case SQL_C_INTERVAL_MINUTE_TO_SECOND:		if (ardrec && row > 0)			ptr = (SQLPOINTER) ((char *) ptr +row * (bind_type == SQL_BIND_BY_COLUMN ? sizeof(SQL_INTERVAL_STRUCT) : bind_type));		switch (sql_type) {		case SQL_CHAR:			if (parsesecondintervalstring(&data, NULL, &ival, &ivalprec) == SQL_ERROR) {				/* Invalid character value for cast				   specification */				addStmtError(stmt, "22018", NULL, 0);				return SQL_ERROR;			}			break;		case SQL_DECIMAL:		case SQL_TINYINT:		case SQL_SMALLINT:		case SQL_INTEGER:		case SQL_BIGINT:			ivalprec = parsesecondinterval(&nval, &ival, type);			break;		case SQL_INTERVAL_SECOND:			break;		default:			/* Restricted data type attribute violation */			addStmtError(stmt, "07006", NULL, 0);			return SQL_ERROR;		}#define p ((SQL_INTERVAL_STRUCT *) ptr)	/* abbrev. */		p->interval_sign = ival.interval_sign;		p->intval.day_second.day = 0;		p->intval.day_second.hour = 0;		p->intval.day_second.minute = 0;		p->intval.day_second.second = 0;		p->intval.day_second.fraction = 0;		switch (type) {		case SQL_C_INTERVAL_DAY:			p->interval_type = SQL_IS_DAY;			if ((p->intval.day_second.day = ival.intval.day_second.day) >= maxdatetimeval) {				/* Interval field overflow */				addStmtError(stmt, "22015", NULL, 0);				return SQL_ERROR;			}			if (ival.intval.day_second.hour || ival.intval.day_second.minute || ival.intval.day_second.second || ival.intval.day_second.fraction) {				/* Fractional truncation */				addStmtError(stmt, "01S07", NULL, 0);			}			break;		case SQL_C_INTERVAL_HOUR:			p->interval_type = SQL_IS_HOUR;			if ((p->intval.day_second.hour = ival.intval.day_second.hour + 24 * ival.intval.day_second.day) >= maxdatetimeval) {				/* Interval field overflow */				addStmtError(stmt, "22015", NULL, 0);				return SQL_ERROR;			}			if (ival.intval.day_second.minute || ival.intval.day_second.second || ival.intval.day_second.fraction) {				/* Fractional truncation */				addStmtError(stmt, "01S07", NULL, 0);			}			break;		case SQL_C_INTERVAL_MINUTE:			p->interval_type = SQL_IS_MINUTE;			if ((p->intval.day_second.minute = ival.intval.day_second.minute + 60 * (ival.intval.day_second.hour + 24 * ival.intval.day_second.day)) >= maxdatetimeval) {				/* Interval field overflow */				addStmtError(stmt, "22015", NULL, 0);				return SQL_ERROR;			}			if (ival.intval.day_second.second || ival.intval.day_second.fraction) {				/* Fractional truncation */				addStmtError(stmt, "01S07", NULL, 0);			}			break;		case SQL_C_INTERVAL_SECOND:			p->interval_type = SQL_IS_SECOND;			if ((p->intval.day_second.second = ival.intval.day_second.second + 60 * (ival.intval.day_second.minute + 60 * (ival.intval.day_second.hour + 24 * ival.intval.day_second.day))) >= maxdatetimeval) {				/* Interval field overflow */				addStmtError(stmt, "22015", NULL, 0);				return SQL_ERROR;			}			p->intval.day_second.fraction = ival.intval.day_second.fraction;			break;		case SQL_C_INTERVAL_DAY_TO_HOUR:			p->interval_type = SQL_IS_DAY_TO_HOUR;			if ((p->intval.day_second.day = ival.intval.day_second.day) >= maxdatetimeval) {				/* Interval field overflow */				addStmtError(stmt, "22015", NULL, 0);				return SQL_ERROR;			}			p->intval.day_second.hour = ival.intval.day_second.hour;			if (ival.intval.day_second.minute || ival.intval.day_second.second || ival.intval.day_second.fraction) {				/* Fractional truncation */				addStmtError(stmt, "01S07", NULL, 0);			}			break;		case SQL_C_INTERVAL_DAY_TO_MINUTE:			p->interval_type = SQL_IS_DAY_TO_MINUTE;			if ((p->intval.day_second.day = ival.intval.day_second.day) >= maxdatetimeval) {				/* Interval field overflow */				addStmtError(stmt, "22015", NULL, 0);				return SQL_ERROR;			}			p->intval.day_second.hour = ival.intval.day_second.hour;			p->intval.day_second.minute = ival.intval.day_second.minute;			if (ival.intval.day_second.second || ival.intval.day_second.fraction) {				/* Fractional truncation */				addStmtError(stmt, "01S07", NULL, 0);			}			break;		case SQL_C_INTERVAL_DAY_TO_SECOND:			p->interval_type = SQL_IS_DAY_TO_SECOND;			if ((p->intval.day_second.day = ival.intval.day_second.day) >= maxdatetimeval) {				/* Interval field overflow */				addStmtError(stmt, "22015", NULL, 0);				return SQL_ERROR;			}			p->intval.day_second.hour = ival.intval.day_second.hour;			p->intval.day_second.minute = ival.intval.day_second.minute;			p->intval.day_second.second = ival.intval.day_second.second;			p->intval.day_second.fraction = ival.intval.day_second.fraction;			break;		case SQL_C_INTERVAL_HOUR_TO_MINUTE:			p->interval_type = SQL_IS_HOUR_TO_MINUTE;			if ((p->intval.day_second.hour = ival.intval.day_second.hour + 24 * ival.intval.day_second.day) >= maxdatetimeval) {				/* Interval field overflow */				addStmtError(stmt, "22015", NULL, 0);				return SQL_ERROR;			}			p->intval.day_second.minute = ival.intval.day_second.minute;			if (ival.intval.day_second.second || ival.intval.day_second.fraction) {				/* Fractional truncation */				addStmtError(stmt, "01S07", NULL, 0);			}			break;		case SQL_C_INTERVAL_HOUR_TO_SECOND:			p->interval_type = SQL_IS_HOUR_TO_SECOND;			if ((p->intval.day_second.hour = ival.intval.day_second.hour + 24 * ival.intval.day_second.day) >= maxdatetimeval) {				/* Interval field overflow */				addStmtError(stmt, "22015", NULL, 0);				return SQL_ERROR;			}			p->intval.day_second.minute = ival.intval.day_second.minute;			p->intval.day_second.second = ival.intval.day_second.second;			p->intval.day_second.fraction = ival.intval.day_second.fraction;			break;		case SQL_C_INTERVAL_MINUTE_TO_SECOND:			p->interval_type = SQL_IS_MINUTE_TO_SECOND;			if ((p->intval.day_second.minute = ival.intval.day_second.minute + 60 * (ival.intval.day_second.hour + 24 * ival.intval.day_second.day)) >= maxdatetimeval) {				/* Interval field overflow */				addStmtError(stmt, "22015", NULL, 0);				return SQL_ERROR;			}			p->intval.day_second.second = ival.intval.day_second.second;			p->intval.day_second.fraction = ival.intval.day_second.fraction;			break;		}		if (p->intval.day_second.fraction) {			while (ivalprec < precision) {				ivalprec++;				p->intval.day_second.fraction *= 10;			}			while (ivalprec > precision) {				ivalprec--;				if (stmt->Error == NULL &&				    p->intval.day_second.fraction % 10 != 0) {					/* Fractional truncation */					addStmtError(stmt, "01S07", NULL, 0);				}				p->intval.day_second.fraction /= 10;			}		}#undef p		if (lenp)			*lenp = sizeof(SQL_INTERVAL_STRUCT);		break;	default:		/* Invalid application buffer type */		addStmtError(stmt, "HY003", NULL, 0);		return SQL_ERROR;	}	return stmt->Error ? SQL_SUCCESS_WITH_INFO : SQL_SUCCESS;}#define assign(buf,bufpos,buflen,value,stmt)				\		do {							\			if (bufpos >= buflen) {				\				buf = realloc(buf, buflen += 1024);	\				if (buf == NULL) {			\					/* Memory allocation error */	\					addStmtError(stmt, "HY001", NULL, 0); \					return SQL_ERROR;		\				}					\			}						\			buf[bufpos++] = (value);			\		} while (0)#define assigns(buf,bufpos,buflen,value,stmt)				\		do {							\			size_t _len = strlen(value);			\			size_t _i;					\			while (bufpos + _len >= buflen) {		\				buf = realloc(buf, buflen += 1024);	\				if (buf == NULL) {			\					/* Memory allocation error */	\					addStmtError(stmt, "HY001", NULL, 0); \					return SQL_ERROR;		\				}					\			}						\			for (_i = 0; _i < _len; _i++)			\				buf[bufpos++] = (value)[_i];		\		} while (0)SQLRETURNODBCStore(ODBCStmt *stmt, SQLUSMALLINT param, SQLINTEGER offset, int row, char **bufp, size_t *bufposp, size_t *buflenp, char *sep){	ODBCDescRec *ipdrec, *apdrec;	SQLPOINTER ptr;	SQLINTEGER *indicator_ptr;	SQLUINTEGER bind_type;	SQLSMALLINT ctype, sqltype;	char *sval = NULL;	SQLINTEGER slen = 0;	bignum_t nval;	double fval = 0.0;	DATE_STRUCT dval;	TIME_STRUCT tval;	TIMESTAMP_STRUCT tsval;	int ivalprec = 0;	/* interval second precision */	SQL_INTERVAL_STRUCT ival;	char *buf = *bufp;	size_t bufpos = *bufposp;	size_t buflen = *buflenp;	char data[256];	int i;	assert(param <= stmt->ImplParamDescr->sql_desc_count);	assert(param <= stmt->ApplParamDescr->sql_desc_count);	ipdrec = stmt->ImplParamDescr->descRec + param;	apdrec = stmt->ApplParamDescr->descRec + param;	bind_type = stmt->ApplParamDescr->sql_desc_bind_type;	ptr = apdrec->sql_desc_data_ptr;	if (ptr && offset)		ptr = (SQLPOINTER) ((char *) ptr + offset + row * (bind_type == SQL_BIND_BY_COLUMN ? sizeof(SQLPOINTER) : bind_type));	indicator_ptr = apdrec->sql_desc_indicator_ptr;	if (indicator_ptr && offset)		indicator_ptr = (SQLINTEGER *) ((char *) indicator_ptr + offset + row * (bind_type == SQL_BIND_BY_COLUMN ? sizeof(SQLINTEGER) : bind_type));	if (ptr == NULL &&	    (indicator_ptr == NULL || *indicator_ptr != SQL_NULL_DATA)) {		/* COUNT field incorrect */		addStmtError(stmt, "07002", NULL, 0);		return SQL_ERROR;	}	ctype = apdrec->sql_desc_concise_type;	sqltype = ipdrec->sql_desc_concise_type;	if (ctype == SQL_C_DEFAULT)		ctype = ODBCDefaultType(ipdrec);	switch (ctype) {	case SQL_C_TINYINT:		ctype = apdrec->sql_desc_unsigned ? SQL_C_UTINYINT : SQL_C_STINYINT;		break;	case SQL_C_SHORT:		ctype = apdrec->sql_desc_unsigned ? SQL_C_USHORT : SQL_C_SSHORT;		break;	case SQL_C_LONG:		ctype = apdrec->sql_desc_unsigned ? SQL_C_ULONG : SQL_C_SLONG;		break;	default:		break;	}	if (indicator_ptr != NULL && *indicator_ptr == SQL_NULL_DATA) {		assigns(buf, bufpos, buflen, "NULL", stmt);		*bufp = buf;		*bufposp = bufpos;		*buflenp = buflen;		return SQL_SUCCESS;	}	switch (ctype) {	case SQL_C_CHAR:	case SQL_C_BINARY:		slen = apdrec->sql_desc_octet_length_ptr ? *apdrec->sql_desc_octet_length_ptr : SQL_NTS;		sval = (char *) ptr;		fixODBCstring(sval, slen, SQLINTEGER, addStmtError, stmt);		break;#ifdef WITH_WCHAR	case SQL_C_WCHAR:		slen = apdrec->sql_desc_octet_length_ptr ? *apdrec->sql_desc_octet_length_ptr : SQL_NTS;		sval = (char *) ptr;		fixWcharIn((SQLWCHAR *) ptr, slen, char, sval, addStmtError, stmt, return SQL_ERROR);		break;#endif	case SQL_C_BIT:		nval.precision = 1;		nval.scale = 0;		nval.sign = 1;		nval.val = * (SQLCHAR *) ptr != 0;		break;	case SQL_C_STINYINT:		nval.precision = 1;		nval.scale = 0;		if (* (SQLSCHAR *) ptr < 0) {			nval.sign = 0;			nval.val = - * (SQLSCHAR *) ptr;		} else {			nval.sign = 1;			nval.val = * (SQLSCHAR *) ptr;		}		break;	case SQL_C_UTINYINT:		nval.precision = 1;		nval.scale = 0;		nval.sign = 1;		nval.val = * (SQLCHAR *) ptr;		break;	case SQL_C_SSHORT:		nval.precision = 1;		nval.scale = 0;		if (* (SQLSMALLINT *) ptr < 0) {			nval.sign = 0;			nval.val = - * (SQLSMALLINT *) ptr;		} else {			nval.sign = 1;			nval.val = * (SQLSMALLINT *) ptr;		}		break;	case SQL_C_USHORT:		nval.precision = 1;		nval.scale = 0;		nval.sign = 1;		nval.val = * (SQLUSMALLINT *) ptr;		break;	case SQL_C_SLONG:		nval.precision = 1;		nval.scale = 0;		if (* (SQLINTEGER *) ptr < 0) {			nval.sign = 0;			nval.val = - * (SQLINTEGER *) ptr;		} else {			nval.sign = 1;			nval.val = * (SQLINTEGER *) ptr;		}		break;	case SQL_C_ULONG:		nval.precision = 1;		nval.scale = 0;		nval.sign = 1;		nval.val = * (SQLUINTEGER *) ptr;		break;	case SQL_C_SBIGINT:		nval.precision = 1;		nval.scale = 0;		if (* (SQLBIGINT *) ptr < 0) {			nval.sign = 0;			nval.val = - * (SQLBIGINT *) ptr;		} else {			nval.sign = 1;			nval.val = * (SQLBIGINT *) ptr;		}		break;	case SQL_C_UBIGINT:		nval.precision = 1;		nval.scale = 0;		nval.sign = 1;		nval.val = * (SQLUBIGINT *) ptr;		break;	case SQL_C_NUMERIC:		nval.precision = (unsigned char) apdrec->sql_desc_precision;		nval.scale = (signed char) apdrec->sql_desc_scale;		nval.sign = ((SQL_NUMERIC_STRUCT *) ptr)->sign;		nval.val = 0;		for (i = 0; i < SQL_MAX_NUMERIC_LEN; i++)			nval.val |= ((SQL_NUMERIC_STRUCT *) ptr)->val[i] << (i * 8);		break;	case SQL_C_FLOAT:		fval = * (SQLREAL *) ptr;		break;	case SQL_C_DOUBLE:		fval = * (SQLDOUBLE *) ptr;		break;	case SQL_C_TYPE_DATE:		dval = * (SQL_DATE_STRUCT *) ptr;		break;	case SQL_C_TYPE_TIME:		tval = * (SQL_TIME_STRUCT *) ptr;		break;	case SQL_C_TYPE_TIMESTAMP:		tsval = * (SQL_TIMESTAMP_STRUCT *) ptr;		break;	case SQL_C_INTERVAL_YEAR:		ival.interval_type = SQL_IS_YEAR;		ival.interval_sign = ((SQL_INTERVAL_STRUCT *) ptr)->interval_sign;		ival.intval.year_month.year = ((SQL_INTERVAL_STRUCT *) ptr)->intval.year_month.year;		ival.intval.year_month.month = 0;		break;	case SQL_C_INTERVAL_MONTH:		ival.interval_type = SQL_IS_MONTH;		ival.interval_sign = ((SQL_INTERVAL_STRUCT *) ptr)->interval_

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?