convert.c
来自「postgresql-odbc,跨平台应用」· C语言 代码 · 共 2,560 行 · 第 1/5 页
C
2,560 行
{ BOOL already_copied = FALSE; if (fCType == SQL_C_BINARY) copy_len = (len > cbValueMax) ? cbValueMax : len; else copy_len = (len >= cbValueMax) ? (cbValueMax - 1) : len;#ifdef UNICODE_SUPPORT if (fCType == SQL_C_WCHAR) { copy_len /= WCLEN; copy_len *= WCLEN; }#endif /* UNICODE_SUPPORT */#ifdef HAVE_LOCALE_H switch (field_type) { case PG_TYPE_FLOAT4: case PG_TYPE_FLOAT8: case PG_TYPE_NUMERIC: { struct lconv *lc; char *new_string; int i, j; new_string = malloc(cbValueMax); lc = localeconv(); for (i = 0, j = 0; ptr[i]; i++) if (ptr[i] == '.') { strncpy(&new_string[j], lc->decimal_point, strlen(lc->decimal_point)); j += strlen(lc->decimal_point); } else new_string[j++] = ptr[i]; new_string[j] = '\0'; strncpy_null(rgbValueBindRow, new_string, copy_len + 1); free(new_string); already_copied = TRUE; break; } }#endif /* HAVE_LOCALE_H */ if (!already_copied) { /* Copy the data */ memcpy(rgbValueBindRow, ptr, copy_len); /* Add null terminator */#ifdef UNICODE_SUPPORT if (fCType == SQL_C_WCHAR) { if (copy_len + WCLEN <= cbValueMax) memset(rgbValueBindRow + copy_len, 0, WCLEN); } else#endif /* UNICODE_SUPPORT */ if (copy_len < cbValueMax) rgbValueBindRow[copy_len] = '\0'; } /* Adjust data_left for next time */ if (stmt->current_col >= 0) pgdc->data_left -= copy_len; } /* * Finally, check for truncation so that proper status can * be returned */ if (cbValueMax > 0 && needbuflen > cbValueMax) result = COPY_RESULT_TRUNCATED; else { if (pgdc->ttlbuf != NULL) { free(pgdc->ttlbuf); pgdc->ttlbuf = NULL; } } if (SQL_C_WCHAR == fCType) mylog(" SQL_C_WCHAR, default: len = %d, cbValueMax = %d, rgbValueBindRow = '%s'\n", len, cbValueMax, rgbValueBindRow); else if (SQL_C_BINARY == fCType) mylog(" SQL_C_BINARY, default: len = %d, cbValueMax = %d, rgbValueBindRow = '%.*s'\n", len, cbValueMax, copy_len, rgbValueBindRow); else mylog(" SQL_C_CHAR, default: len = %d, cbValueMax = %d, rgbValueBindRow = '%s'\n", len, cbValueMax, rgbValueBindRow); break; }#ifdef UNICODE_SUPPORT if (SQL_C_WCHAR == fCType && ! wconverted) { char *str = strdup(rgbValueBindRow); SQLLEN ucount = utf8_to_ucs2(str, len, (SQLWCHAR *) rgbValueBindRow, cbValueMax / WCLEN); if (cbValueMax < WCLEN * ucount) result = COPY_RESULT_TRUNCATED; len = ucount * WCLEN; free(str); }#endif /* UNICODE_SUPPORT */ } else { /* * for SQL_C_CHAR, it's probably ok to leave currency symbols in. * But to convert to numeric types, it is necessary to get rid of * those. */ if (field_type == PG_TYPE_MONEY) { if (convert_money(neut_str, midtemp[mtemp_cnt], sizeof(midtemp[0]))) { neut_str = midtemp[mtemp_cnt]; mtemp_cnt++; } else { qlog("couldn't convert money type to %d\n", fCType); return COPY_UNSUPPORTED_TYPE; } } switch (fCType) { case SQL_C_DATE:#if (ODBCVER >= 0x0300) case SQL_C_TYPE_DATE: /* 91 */#endif len = 6; { DATE_STRUCT *ds; if (bind_size > 0) ds = (DATE_STRUCT *) rgbValueBindRow; else ds = (DATE_STRUCT *) rgbValue + bind_row; ds->year = std_time.y; ds->month = std_time.m; ds->day = std_time.d; } break; case SQL_C_TIME:#if (ODBCVER >= 0x0300) case SQL_C_TYPE_TIME: /* 92 */#endif len = 6; { TIME_STRUCT *ts; if (bind_size > 0) ts = (TIME_STRUCT *) rgbValueBindRow; else ts = (TIME_STRUCT *) rgbValue + bind_row; ts->hour = std_time.hh; ts->minute = std_time.mm; ts->second = std_time.ss; } break; case SQL_C_TIMESTAMP:#if (ODBCVER >= 0x0300) case SQL_C_TYPE_TIMESTAMP: /* 93 */#endif len = 16; { TIMESTAMP_STRUCT *ts; if (bind_size > 0) ts = (TIMESTAMP_STRUCT *) rgbValueBindRow; else ts = (TIMESTAMP_STRUCT *) rgbValue + bind_row; ts->year = std_time.y; ts->month = std_time.m; ts->day = std_time.d; ts->hour = std_time.hh; ts->minute = std_time.mm; ts->second = std_time.ss; ts->fraction = std_time.fr; } break; case SQL_C_BIT: len = 1; if (bind_size > 0) *((UCHAR *) rgbValueBindRow) = atoi(neut_str); else *((UCHAR *) rgbValue + bind_row) = atoi(neut_str); /* * mylog("SQL_C_BIT: bind_row = %d val = %d, cb = %d, * rgb=%d\n", bind_row, atoi(neut_str), cbValueMax, * *((UCHAR *)rgbValue)); */ break; case SQL_C_STINYINT: case SQL_C_TINYINT: len = 1; if (bind_size > 0) *((SCHAR *) rgbValueBindRow) = atoi(neut_str); else *((SCHAR *) rgbValue + bind_row) = atoi(neut_str); break; case SQL_C_UTINYINT: len = 1; if (bind_size > 0) *((UCHAR *) rgbValueBindRow) = atoi(neut_str); else *((UCHAR *) rgbValue + bind_row) = atoi(neut_str); break; case SQL_C_FLOAT:#ifdef HAVE_LOCALE_H saved_locale = strdup(setlocale(LC_ALL, NULL)); setlocale(LC_ALL, "C");#endif /* HAVE_LOCALE_H */ len = 4; if (bind_size > 0) *((SFLOAT *) rgbValueBindRow) = (float) get_double_value(neut_str); else *((SFLOAT *) rgbValue + bind_row) = (float) get_double_value(neut_str);#ifdef HAVE_LOCALE_H setlocale(LC_ALL, saved_locale); free(saved_locale);#endif /* HAVE_LOCALE_H */ break; case SQL_C_DOUBLE:#ifdef HAVE_LOCALE_H saved_locale = strdup(setlocale(LC_ALL, NULL)); setlocale(LC_ALL, "C");#endif /* HAVE_LOCALE_H */ len = 8; if (bind_size > 0) *((SDOUBLE *) rgbValueBindRow) = get_double_value(neut_str); else *((SDOUBLE *) rgbValue + bind_row) = get_double_value(neut_str);#ifdef HAVE_LOCALE_H setlocale(LC_ALL, saved_locale); free(saved_locale);#endif /* HAVE_LOCALE_H */ break;#if (ODBCVER >= 0x0300) case SQL_C_NUMERIC:#ifdef HAVE_LOCALE_H /* strcpy(saved_locale, setlocale(LC_ALL, NULL)); setlocale(LC_ALL, "C"); not needed currently */ #endif /* HAVE_LOCALE_H */ { SQL_NUMERIC_STRUCT *ns; int i, nlen, bit, hval, tv, dig, sta, olen; char calv[SQL_MAX_NUMERIC_LEN * 3]; const char *wv; BOOL dot_exist; len = sizeof(SQL_NUMERIC_STRUCT); if (bind_size > 0) ns = (SQL_NUMERIC_STRUCT *) rgbValueBindRow; else ns = (SQL_NUMERIC_STRUCT *) rgbValue + bind_row; for (wv = neut_str; *wv && isspace(*wv); wv++) ; ns->sign = 1; if (*wv == '-') { ns->sign = 0; wv++; } else if (*wv == '+') wv++; while (*wv == '0') wv++; ns->precision = 0; ns->scale = 0; for (nlen = 0, dot_exist = FALSE;; wv++) { if (*wv == '.') { if (dot_exist) break; dot_exist = TRUE; } else if (!isdigit(*wv)) break; else { if (dot_exist) ns->scale++; ns->precision++; calv[nlen++] = *wv; } } memset(ns->val, 0, sizeof(ns->val)); for (hval = 0, bit = 1L, sta = 0, olen = 0; sta < nlen;) { for (dig = 0, i = sta; i < nlen; i++) { tv = dig * 10 + calv[i] - '0'; dig = tv % 2; calv[i] = tv / 2 + '0'; if (i == sta && tv < 2) sta++; } if (dig > 0) hval |= bit; bit <<= 1; if (bit >= (1L << 8)) { ns->val[olen++] = hval; hval = 0; bit = 1L; if (olen >= SQL_MAX_NUMERIC_LEN - 1) { ns->scale = sta - ns->precision; break; } } } if (hval && olen < SQL_MAX_NUMERIC_LEN - 1) ns->val[olen++] = hval; }#ifdef HAVE_LOCALE_H /* setlocale(LC_ALL, saved_locale); */#endif /* HAVE_LOCALE_H */ break;#endif /* ODBCVER */ case SQL_C_SSHORT: case SQL_C_SHORT: len = 2; if (bind_size > 0) *((SQLSMALLINT *) rgbValueBindRow) = atoi(neut_str); else *((SQLSMALLINT *) rgbValue + bind_row) = atoi(neut_str); break; case SQL_C_USHORT: len = 2; if (bind_size > 0) *((SQLUSMALLINT *) rgbValueBindRow) = atoi(neut_str); else *((SQLUSMALLINT *) rgbValue + bind_row) = atoi(neut_str); break; case SQL_C_SLONG: case SQL_C_LONG: len = 4; if (bind_size > 0) *((SQLINTEGER *) rgbValueBindRow) = atol(neut_str); else *((SQLINTEGER *) rgbValue + bind_row) = atol(neut_str); break; case SQL_C_ULONG: len = 4; if (bind_size > 0) *((SQLUINTEGER *) rgbValueBindRow) = ATOI32U(neut_str); else *((SQLUINTEGER *) rgbValue + bind_row) = ATOI32U(neut_str); break;#if (ODBCVER >= 0x0300) && defined(ODBCINT64) case SQL_C_SBIGINT: len = 8; if (bind_size > 0) *((SQLBIGINT *) rgbValueBindRow) = ATOI64(neut_str); else *((SQLBIGINT *) rgbValue + bind_row) = ATOI64(neut_str); break; case SQL_C_UBIGINT: len = 8; if (bind_size > 0) *((SQLUBIGINT *) rgbValueBindRow) = ATOI64U(neut_str); else *((SQLUBIGINT *) rgbValue + bind_row) = ATOI64U(neut_str); break;#endif /* ODBCINT64 */ case SQL_C_BINARY: if (PG_TYPE_UNKNOWN == field_type || PG_TYPE_TEXT == field_type || PG_TYPE_VARCHAR == field_type || PG_TYPE_BPCHAR == field_type || PG_TYPE_TEXTARRAY == field_type || PG_TYPE_VARCHARARRAY == field_type || PG_TYPE_BPCHARARRAY == field_type) { ssize_t len = SQL_NULL_DATA; if (neut_str) len = strlen(neut_str); if (pcbValue) *pcbValueBindRow = len; if (len > 0 && cbValueMax > 0) { memcpy(rgbValueBindRow, neut_str, len < cbValueMax ? len : cbValueMax); if (cbValueMax >= len + 1) rgbValueBindRow[len] = '\0'; } if (cbValueMax >= len) return COPY_OK; else return COPY_RESULT_TRUNCATED; } /* The following is for SQL_C_VARBOOKMARK */ else if (PG_TYPE_INT4 == field_type) { UInt4 ival = ATOI32U(neut_str);inolog("SQL_C_VARBOOKMARK value=%d\n", ival); if (pcbValue) *pcbValueBindRow = sizeof(ival); if (cbValueMax >= sizeof(ival)) { memcpy(rgbValueBindRow, &ival, sizeof(ival)); return COPY_OK; } else return COPY_RESULT_TRUNCATED; } else if (PG_TYPE_BYTEA != field_type) { mylog("couldn't convert the type %d to SQL_C_BINARY\n", field_type); qlog("couldn't convert the type %d to SQL_C_BINARY\n", field_type); return COPY_UNSUPPORTED_TYPE; } /* truncate if necessary */ /* convert octal escapes to bytes */ if (stmt->current_col < 0) { pgdc = &(gdata->fdata); pgdc->data_left = -1; } else pgdc = &gdata->gdata[stmt->current_col]; if (!pgdc->ttlbuf) pgdc->ttlbuflen = 0; if (pgdc->data_left < 0) { if (cbValueMax <= 0) { len = convert_from_pgbinary(neut_str, NULL, 0); result = COPY_RESULT_TRUNCATED; break; } if (len = strlen(neut_str), len >= (int) pgdc->ttlbuflen) { pgdc->ttlbuf = realloc(pgdc->ttlbuf, len + 1); pgdc->ttlbuflen = len + 1; } len = convert_from_pgbinary(neut_str, pgdc->ttlbuf, pgdc->ttlbuflen); pgdc->ttlbufused = len; } else len = pgdc->ttlbufused; ptr = pgdc->ttlbuf; if (stmt->current_col >= 0) { /* * Second (or more) call to SQLGetData so move the * pointer */ if (pgdc->data_left > 0) { ptr += len - pgdc->data_left; len = pgdc->data_left; } /* First call to SQLGetData so initialize data_left */ else pgdc->data_left = len; } if (cbValueMax > 0) { copy_len = (len > cbValueMax) ? cbValueMax : len; /* Copy the data */ memcpy(rgbValueBindRow, ptr, copy_len); /* Adjust data_left for next time */ if (stmt->current_col >= 0) pgdc->data_left -= copy_len; } /* * Finally, check for truncation so that proper status can * be returned */ if (len > cbValueMax) result = COPY_RESULT_TRUNCATED; else if (pgdc->ttlbuf) { free(pgdc->ttlbuf); pgdc->ttlbuf = NULL; } mylog("SQL_C_BINARY: len = %d, copy_len = %d\n", len, copy_len); break; default: qlog("conversion to the type %d isn't supported\n", fCType); return COPY_UNSUPPORTED_TYPE; } } /* store the length of what was copied, if there's a place for it */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?