|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <gtest/gtest.h> |
| 8 | +#include "connected_dbc.h" |
| 9 | + |
| 10 | +#include <string.h> |
| 11 | +#include <math.h> |
| 12 | + |
| 13 | +/* placeholders; will be undef'd and redef'd */ |
| 14 | +#define SQL_SCALE |
| 15 | +#define SQL_RAW |
| 16 | +#define SQL_VAL |
| 17 | +#define SQL /* attached for troubleshooting purposes */ |
| 18 | + |
| 19 | +namespace test { |
| 20 | + |
| 21 | +class ColAttribute : public ::testing::Test, public ConnectedDBC { |
| 22 | +}; |
| 23 | + |
| 24 | + |
| 25 | +TEST_F(ColAttribute, NumericAttributePtr_SQLSMALLINT) { |
| 26 | + SQLLEN pNumAttr = -1; |
| 27 | + |
| 28 | +#undef SQL_VAL |
| 29 | +#undef SQL |
| 30 | +#define SQL_VAL "0.98765432100123456789" //20 fractional digits |
| 31 | +#define SQL "CAST(" SQL_VAL " AS SCALED_FLOAT)" |
| 32 | + |
| 33 | + const char json_answer[] = "\ |
| 34 | +{\ |
| 35 | + \"columns\": [\ |
| 36 | + {\"name\": \"" SQL "\", \"type\": \"scaled_float\"}\ |
| 37 | + ],\ |
| 38 | + \"rows\": [\ |
| 39 | + [" SQL_VAL "]\ |
| 40 | + ]\ |
| 41 | +}\ |
| 42 | +"; |
| 43 | + prepareStatement(json_answer); |
| 44 | + |
| 45 | + ret = SQLFetch(stmt); |
| 46 | + ASSERT_TRUE(SQL_SUCCEEDED(ret)); |
| 47 | + |
| 48 | + ret = SQLColAttribute(stmt, 1, SQL_DESC_CONCISE_TYPE, NULL, 0, NULL, |
| 49 | + &pNumAttr); |
| 50 | + ASSERT_TRUE(SQL_SUCCEEDED(ret)); |
| 51 | + ASSERT_EQ(pNumAttr, SQL_C_DOUBLE); |
| 52 | +} |
| 53 | + |
| 54 | +TEST_F(ColAttribute, NumericAttributePtr_SQLLEN) { |
| 55 | + SQLLEN pNumAttr = -1; |
| 56 | + |
| 57 | +#undef SQL_VAL |
| 58 | +#undef SQL |
| 59 | +#define SQL_VAL "0.98765432100123456789" //20 fractional digits |
| 60 | +#define SQL "CAST(" SQL_VAL " AS SCALED_FLOAT)" |
| 61 | + |
| 62 | + const char json_answer[] = "\ |
| 63 | +{\ |
| 64 | + \"columns\": [\ |
| 65 | + {\"name\": \"" SQL "\", \"type\": \"scaled_float\"}\ |
| 66 | + ],\ |
| 67 | + \"rows\": [\ |
| 68 | + [" SQL_VAL "]\ |
| 69 | + ]\ |
| 70 | +}\ |
| 71 | +"; |
| 72 | + prepareStatement(json_answer); |
| 73 | + |
| 74 | + ret = SQLFetch(stmt); |
| 75 | + ASSERT_TRUE(SQL_SUCCEEDED(ret)); |
| 76 | + |
| 77 | + ret = SQLColAttribute(stmt, 1, SQL_DESC_DISPLAY_SIZE, NULL, 0, NULL, |
| 78 | + &pNumAttr); |
| 79 | + ASSERT_TRUE(SQL_SUCCEEDED(ret)); |
| 80 | + // https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/display-size |
| 81 | + // "SQL_FLOAT, SQL_DOUBLE: 24 (a sign, 15 digits, a decimal point, the |
| 82 | + // letter E, a sign, and 3 digits)." |
| 83 | + ASSERT_EQ(pNumAttr, 24); |
| 84 | +} |
| 85 | + |
| 86 | +TEST_F(ColAttribute, NumericAttributePtr_SQLULEN) { |
| 87 | + SQLLEN pNumAttr = -1; |
| 88 | + |
| 89 | +#undef SQL_VAL |
| 90 | +#undef SQL |
| 91 | +#define SQL_VAL "0.98765432100123456789" //20 fractional digits |
| 92 | +#define SQL "CAST(" SQL_VAL " AS SCALED_FLOAT)" |
| 93 | + |
| 94 | + const char json_answer[] = "\ |
| 95 | +{\ |
| 96 | + \"columns\": [\ |
| 97 | + {\"name\": \"" SQL "\", \"type\": \"scaled_float\"}\ |
| 98 | + ],\ |
| 99 | + \"rows\": [\ |
| 100 | + [" SQL_VAL "]\ |
| 101 | + ]\ |
| 102 | +}\ |
| 103 | +"; |
| 104 | + prepareStatement(json_answer); |
| 105 | + |
| 106 | + ret = SQLFetch(stmt); |
| 107 | + ASSERT_TRUE(SQL_SUCCEEDED(ret)); |
| 108 | + |
| 109 | + ret = SQLColAttribute(stmt, 1, SQL_DESC_LENGTH, NULL, 0, NULL, &pNumAttr); |
| 110 | + ASSERT_TRUE(SQL_SUCCEEDED(ret)); |
| 111 | + ASSERT_EQ(pNumAttr, 0); |
| 112 | +} |
| 113 | + |
| 114 | +TEST_F(ColAttribute, NumericAttributePtr_SQLINTEGER) { |
| 115 | + SQLLEN pNumAttr = -1; |
| 116 | + |
| 117 | +#undef SQL_VAL |
| 118 | +#undef SQL |
| 119 | +#define SQL_VAL "0.98765432100123456789" //20 fractional digits |
| 120 | +#define SQL "CAST(" SQL_VAL " AS SCALED_FLOAT)" |
| 121 | + |
| 122 | + const char json_answer[] = "\ |
| 123 | +{\ |
| 124 | + \"columns\": [\ |
| 125 | + {\"name\": \"" SQL "\", \"type\": \"scaled_float\"}\ |
| 126 | + ],\ |
| 127 | + \"rows\": [\ |
| 128 | + [" SQL_VAL "]\ |
| 129 | + ]\ |
| 130 | +}\ |
| 131 | +"; |
| 132 | + prepareStatement(json_answer); |
| 133 | + |
| 134 | + ret = SQLFetch(stmt); |
| 135 | + ASSERT_TRUE(SQL_SUCCEEDED(ret)); |
| 136 | + |
| 137 | + ret = SQLColAttribute(stmt, 1, SQL_DESC_NUM_PREC_RADIX, NULL, 0, NULL, |
| 138 | + &pNumAttr); |
| 139 | + ASSERT_TRUE(SQL_SUCCEEDED(ret)); |
| 140 | + // https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlgettypeinfo-function |
| 141 | + // "If the data type is an approximate numeric type, this column contains |
| 142 | + // the value 2 to indicate that COLUMN_SIZE specifies a number of bits." |
| 143 | + ASSERT_EQ(pNumAttr, 2); |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | +} // test namespace |
| 150 | + |
0 commit comments