From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 5C43C2BB8C for ; Sun, 14 Apr 2019 11:04:14 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id c5aa7olFiW2L for ; Sun, 14 Apr 2019 11:04:14 -0400 (EDT) Received: from smtpng3.m.smailru.net (smtpng3.m.smailru.net [94.100.177.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 76F2A2BB73 for ; Sun, 14 Apr 2019 11:04:13 -0400 (EDT) From: Nikita Pettik Subject: [tarantool-patches] [PATCH 3/9] sql: use msgpack types instead of custom ones Date: Sun, 14 Apr 2019 18:04:01 +0300 Message-Id: <7d55169718d4bc193a41710da12cfd6a7d4edebc.1555252410.git.korablev@tarantool.org> In-Reply-To: References: In-Reply-To: References: Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-Help: List-Unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-Subscribe: List-Owner: List-post: List-Archive: To: tarantool-patches@freelists.org Cc: v.shpilevoy@tarantool.org, kostja@tarantool.org, Nikita Pettik This patch provides straightforward refactoring replacing enum sql_type with enum mp_type. Note that we use msgpack format instead of field_type since it is more suitable when dealing with NULLs. --- src/box/bind.c | 37 ++++++------------ src/box/execute.c | 12 +++--- src/box/lua/execute.c | 7 +--- src/box/lua/lua_sql.c | 12 +++--- src/box/sql/date.c | 2 +- src/box/sql/func.c | 100 ++++++++++++++++++++++++------------------------ src/box/sql/legacy.c | 2 +- src/box/sql/sqlInt.h | 16 ++------ src/box/sql/vdbeapi.c | 66 ++++++++++---------------------- src/box/sql/whereexpr.c | 2 +- 10 files changed, 102 insertions(+), 154 deletions(-) diff --git a/src/box/bind.c b/src/box/bind.c index 4b57e23c8..5aa79751a 100644 --- a/src/box/bind.c +++ b/src/box/bind.c @@ -35,15 +35,6 @@ #include "sql/sqlLimit.h" #include "sql/vdbe.h" -const char *sql_type_strs[] = { - NULL, - "INTEGER", - "FLOAT", - "TEXT", - "BLOB", - "NULL", -}; - const char * sql_bind_name(const struct sql_bind *bind) { @@ -74,7 +65,8 @@ sql_bind_decode(struct sql_bind *bind, int i, const char **packet) bind->name = NULL; bind->name_len = 0; } - switch (mp_typeof(**packet)) { + enum mp_type type = mp_typeof(**packet); + switch (type) { case MP_UINT: { uint64_t n = mp_decode_uint(packet); if (n > INT64_MAX) { @@ -83,49 +75,40 @@ sql_bind_decode(struct sql_bind *bind, int i, const char **packet) return -1; } bind->i64 = (int64_t) n; - bind->type = SQL_INTEGER; bind->bytes = sizeof(bind->i64); break; } case MP_INT: bind->i64 = mp_decode_int(packet); - bind->type = SQL_INTEGER; bind->bytes = sizeof(bind->i64); break; case MP_STR: bind->s = mp_decode_str(packet, &bind->bytes); - bind->type = SQL_TEXT; break; case MP_DOUBLE: bind->d = mp_decode_double(packet); - bind->type = SQL_FLOAT; bind->bytes = sizeof(bind->d); break; case MP_FLOAT: bind->d = mp_decode_float(packet); - bind->type = SQL_FLOAT; bind->bytes = sizeof(bind->d); break; case MP_NIL: mp_decode_nil(packet); - bind->type = SQL_NULL; bind->bytes = 1; break; case MP_BOOL: /* sql doesn't support boolean. Use int instead. */ bind->i64 = mp_decode_bool(packet) ? 1 : 0; - bind->type = SQL_INTEGER; bind->bytes = sizeof(bind->i64); break; case MP_BIN: bind->s = mp_decode_bin(packet, &bind->bytes); - bind->type = SQL_BLOB; break; case MP_EXT: bind->s = *packet; mp_next(packet); bind->bytes = *packet - bind->s; - bind->type = SQL_BLOB; break; case MP_ARRAY: diag_set(ClientError, ER_SQL_BIND_TYPE, "ARRAY", @@ -138,6 +121,7 @@ sql_bind_decode(struct sql_bind *bind, int i, const char **packet) default: unreachable(); } + bind->type = type; return 0; } @@ -189,13 +173,16 @@ sql_bind_column(struct sql_stmt *stmt, const struct sql_bind *p, } } switch (p->type) { - case SQL_INTEGER: + case MP_INT: + case MP_UINT: + case MP_BOOL: rc = sql_bind_int64(stmt, pos, p->i64); break; - case SQL_FLOAT: + case MP_DOUBLE: + case MP_FLOAT: rc = sql_bind_double(stmt, pos, p->d); break; - case SQL_TEXT: + case MP_STR: /* * Parameters are allocated within message pack, * received from the iproto thread. IProto thread @@ -207,10 +194,10 @@ sql_bind_column(struct sql_stmt *stmt, const struct sql_bind *p, rc = sql_bind_text64(stmt, pos, p->s, p->bytes, SQL_STATIC); break; - case SQL_NULL: + case MP_NIL: rc = sql_bind_null(stmt, pos); break; - case SQL_BLOB: + case MP_BIN: rc = sql_bind_blob64(stmt, pos, (const void *) p->s, p->bytes, SQL_STATIC); break; @@ -227,7 +214,7 @@ sql_bind_column(struct sql_stmt *stmt, const struct sql_bind *p, case SQL_TOOBIG: default: diag_set(ClientError, ER_SQL_BIND_VALUE, sql_bind_name(p), - sql_type_strs[p->type]); + mp_type_strs[p->type]); break; } return -1; diff --git a/src/box/execute.c b/src/box/execute.c index af2182867..2da2bc88b 100644 --- a/src/box/execute.c +++ b/src/box/execute.c @@ -133,9 +133,9 @@ sql_column_to_messagepack(struct sql_stmt *stmt, int i, struct region *region) { size_t size; - int type = sql_column_type(stmt, i); + enum mp_type type = sql_column_type(stmt, i); switch (type) { - case SQL_INTEGER: { + case MP_INT: { int64_t n = sql_column_int64(stmt, i); if (n >= 0) size = mp_sizeof_uint(n); @@ -150,7 +150,7 @@ sql_column_to_messagepack(struct sql_stmt *stmt, int i, mp_encode_int(pos, n); break; } - case SQL_FLOAT: { + case MP_DOUBLE: { double d = sql_column_double(stmt, i); size = mp_sizeof_double(d); char *pos = (char *) region_alloc(region, size); @@ -159,7 +159,7 @@ sql_column_to_messagepack(struct sql_stmt *stmt, int i, mp_encode_double(pos, d); break; } - case SQL_TEXT: { + case MP_STR: { uint32_t len = sql_column_bytes(stmt, i); size = mp_sizeof_str(len); char *pos = (char *) region_alloc(region, size); @@ -170,7 +170,7 @@ sql_column_to_messagepack(struct sql_stmt *stmt, int i, mp_encode_str(pos, s, len); break; } - case SQL_BLOB: { + case MP_BIN: { uint32_t len = sql_column_bytes(stmt, i); const char *s = (const char *)sql_column_blob(stmt, i); @@ -189,7 +189,7 @@ sql_column_to_messagepack(struct sql_stmt *stmt, int i, } break; } - case SQL_NULL: { + case MP_NIL: { size = mp_sizeof_nil(); char *pos = (char *) region_alloc(region, size); if (pos == NULL) diff --git a/src/box/lua/execute.c b/src/box/lua/execute.c index 71d64bc27..45d3fc949 100644 --- a/src/box/lua/execute.c +++ b/src/box/lua/execute.c @@ -146,7 +146,6 @@ lua_sql_bind_decode(struct lua_State *L, struct sql_bind *bind, int idx, int i) FALLTHROUGH; case MP_INT: bind->i64 = field.ival; - bind->type = SQL_INTEGER; bind->bytes = sizeof(bind->i64); break; case MP_STR: @@ -162,28 +161,23 @@ lua_sql_bind_decode(struct lua_State *L, struct sql_bind *bind, int idx, int i) } memcpy(buf, field.sval.data, field.sval.len + 1); bind->s = buf; - bind->type = SQL_TEXT; bind->bytes = field.sval.len; break; case MP_DOUBLE: case MP_FLOAT: bind->d = field.dval; - bind->type = SQL_FLOAT; bind->bytes = sizeof(bind->d); break; case MP_NIL: - bind->type = SQL_NULL; bind->bytes = 1; break; case MP_BOOL: /* SQLite doesn't support boolean. Use int instead. */ bind->i64 = field.bval ? 1 : 0; - bind->type = SQL_INTEGER; bind->bytes = sizeof(bind->i64); break; case MP_BIN: bind->s = mp_decode_bin(&field.sval.data, &bind->bytes); - bind->type = SQL_BLOB; break; case MP_EXT: diag_set(ClientError, ER_SQL_BIND_TYPE, "USERDATA", @@ -200,6 +194,7 @@ lua_sql_bind_decode(struct lua_State *L, struct sql_bind *bind, int idx, int i) default: unreachable(); } + bind->type = field.type; lua_pop(L, lua_gettop(L) - idx); return 0; } diff --git a/src/box/lua/lua_sql.c b/src/box/lua/lua_sql.c index 3d0047e16..239ed1b2f 100644 --- a/src/box/lua/lua_sql.c +++ b/src/box/lua/lua_sql.c @@ -56,21 +56,21 @@ lua_sql_call(sql_context *pCtx, int nVal, sql_value **apVal) { lua_rawgeti(L, LUA_REGISTRYINDEX, func_info->func_ref); for (int i = 0; i < nVal; i++) { sql_value *param = apVal[i]; - switch (sql_value_type(param)) { - case SQL_INTEGER: + switch (sql_value_mp_type(param)) { + case MP_INT: luaL_pushint64(L, sql_value_int64(param)); break; - case SQL_FLOAT: + case MP_DOUBLE: lua_pushnumber(L, sql_value_double(param)); break; - case SQL_TEXT: + case MP_STR: lua_pushstring(L, (const char *) sql_value_text(param)); break; - case SQL_BLOB: + case MP_BIN: lua_pushlstring(L, sql_value_blob(param), (size_t) sql_value_bytes(param)); break; - case SQL_NULL: + case MP_NIL: lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_nil_ref); break; default: diff --git a/src/box/sql/date.c b/src/box/sql/date.c index 5f5272ea3..ead0c01d0 100644 --- a/src/box/sql/date.c +++ b/src/box/sql/date.c @@ -937,7 +937,7 @@ isDate(sql_context * context, int argc, sql_value ** argv, DateTime * p) if (argc == 0) { return setDateTimeToCurrent(context, p); } - if ((eType = sql_value_type(argv[0])) == SQL_FLOAT + if ((eType = sql_value_type(argv[0])) == MP_DOUBLE || eType == SQL_INTEGER) { setRawDateNumber(p, sql_value_double(argv[0])); } else { diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 9adfeec67..3cdb119c8 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -87,10 +87,10 @@ minmaxFunc(sql_context * context, int argc, sql_value ** argv) pColl = sqlGetFuncCollSeq(context); assert(mask == -1 || mask == 0); iBest = 0; - if (sql_value_type(argv[0]) == SQL_NULL) + if (sql_value_mp_type(argv[0]) == MP_NIL) return; for (i = 1; i < argc; i++) { - if (sql_value_type(argv[i]) == SQL_NULL) + if (sql_value_mp_type(argv[i]) == MP_NIL) return; if ((sqlMemCompare(argv[iBest], argv[i], pColl) ^ mask) >= 0) { @@ -109,17 +109,17 @@ typeofFunc(sql_context * context, int NotUsed, sql_value ** argv) { const char *z = 0; UNUSED_PARAMETER(NotUsed); - switch (sql_value_type(argv[0])) { - case SQL_INTEGER: + switch (sql_value_mp_type(argv[0])) { + case MP_INT: z = "integer"; break; - case SQL_TEXT: + case MP_STR: z = "text"; break; - case SQL_FLOAT: + case MP_DOUBLE: z = "real"; break; - case SQL_BLOB: + case MP_BIN: z = "blob"; break; default: @@ -139,15 +139,15 @@ lengthFunc(sql_context * context, int argc, sql_value ** argv) assert(argc == 1); UNUSED_PARAMETER(argc); - switch (sql_value_type(argv[0])) { - case SQL_BLOB: - case SQL_INTEGER: - case SQL_FLOAT:{ + switch (sql_value_mp_type(argv[0])) { + case MP_BIN: + case MP_INT: + case MP_DOUBLE:{ sql_result_int(context, sql_value_bytes(argv[0])); break; } - case SQL_TEXT:{ + case MP_STR:{ const unsigned char *z = sql_value_text(argv[0]); if (z == 0) return; @@ -173,8 +173,8 @@ absFunc(sql_context * context, int argc, sql_value ** argv) { assert(argc == 1); UNUSED_PARAMETER(argc); - switch (sql_value_type(argv[0])) { - case SQL_INTEGER:{ + switch (sql_value_mp_type(argv[0])) { + case MP_INT:{ i64 iVal = sql_value_int64(argv[0]); if (iVal < 0) { if (iVal == SMALLEST_INT64) { @@ -192,7 +192,7 @@ absFunc(sql_context * context, int argc, sql_value ** argv) sql_result_int64(context, iVal); break; } - case SQL_NULL:{ + case MP_NIL:{ /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */ sql_result_null(context); break; @@ -229,19 +229,19 @@ position_func(struct sql_context *context, int argc, struct Mem **argv) UNUSED_PARAMETER(argc); struct Mem *needle = argv[0]; struct Mem *haystack = argv[1]; - int needle_type = sql_value_type(needle); - int haystack_type = sql_value_type(haystack); + enum mp_type needle_type = sql_value_mp_type(needle); + enum mp_type haystack_type = sql_value_mp_type(haystack); - if (haystack_type == SQL_NULL || needle_type == SQL_NULL) + if (haystack_type == MP_NIL || needle_type == MP_NIL) return; /* * Position function can be called only with string * or blob params. */ struct Mem *inconsistent_type_arg = NULL; - if (needle_type != SQL_TEXT && needle_type != SQL_BLOB) + if (needle_type != MP_STR && needle_type != MP_BIN) inconsistent_type_arg = needle; - if (haystack_type != SQL_TEXT && haystack_type != SQL_BLOB) + if (haystack_type != MP_STR && haystack_type != MP_BIN) inconsistent_type_arg = haystack; if (inconsistent_type_arg != NULL) { diag_set(ClientError, ER_INCONSISTENT_TYPES, "TEXT or BLOB", @@ -268,7 +268,7 @@ position_func(struct sql_context *context, int argc, struct Mem **argv) if (n_needle_bytes > 0) { const unsigned char *haystack_str; const unsigned char *needle_str; - if (haystack_type == SQL_BLOB) { + if (haystack_type == MP_BIN) { needle_str = sql_value_blob(needle); haystack_str = sql_value_blob(haystack); assert(needle_str != NULL); @@ -398,14 +398,14 @@ substrFunc(sql_context * context, int argc, sql_value ** argv) int negP2 = 0; assert(argc == 3 || argc == 2); - if (sql_value_type(argv[1]) == SQL_NULL - || (argc == 3 && sql_value_type(argv[2]) == SQL_NULL) + if (sql_value_mp_type(argv[1]) == MP_NIL + || (argc == 3 && sql_value_mp_type(argv[2]) == MP_NIL) ) { return; } - p0type = sql_value_type(argv[0]); + p0type = sql_value_mp_type(argv[0]); p1 = sql_value_int(argv[1]); - if (p0type == SQL_BLOB) { + if (p0type == MP_BIN) { len = sql_value_bytes(argv[0]); z = sql_value_blob(argv[0]); if (z == 0) @@ -460,7 +460,7 @@ substrFunc(sql_context * context, int argc, sql_value ** argv) } } assert(p1 >= 0 && p2 >= 0); - if (p0type != SQL_BLOB) { + if (p0type != MP_BIN) { /* * In the code below 'cnt' and 'n_chars' is * used because '\0' is not supposed to be @@ -507,13 +507,13 @@ roundFunc(sql_context * context, int argc, sql_value ** argv) char *zBuf; assert(argc == 1 || argc == 2); if (argc == 2) { - if (SQL_NULL == sql_value_type(argv[1])) + if (MP_NIL == sql_value_mp_type(argv[1])) return; n = sql_value_int(argv[1]); if (n < 0) n = 0; } - if (sql_value_type(argv[0]) == SQL_NULL) + if (sql_value_mp_type(argv[0]) == MP_NIL) return; r = sql_value_double(argv[0]); /* If Y==0 and X will fit in a 64-bit int, @@ -900,13 +900,13 @@ likeFunc(sql_context *context, int argc, sql_value **argv) int nPat; sql *db = sql_context_db_handle(context); int is_like_ci = SQL_PTR_TO_INT(sql_user_data(context)); - int rhs_type = sql_value_type(argv[0]); - int lhs_type = sql_value_type(argv[1]); + int rhs_type = sql_value_mp_type(argv[0]); + int lhs_type = sql_value_mp_type(argv[1]); - if (lhs_type != SQL_TEXT || rhs_type != SQL_TEXT) { - if (lhs_type == SQL_NULL || rhs_type == SQL_NULL) + if (lhs_type != MP_STR || rhs_type != MP_STR) { + if (lhs_type == MP_NIL || rhs_type == MP_NIL) return; - char *inconsistent_type = rhs_type != SQL_TEXT ? + char *inconsistent_type = rhs_type != MP_STR ? mem_type_to_str(argv[0]) : mem_type_to_str(argv[1]); diag_set(ClientError, ER_INCONSISTENT_TYPES, "TEXT", @@ -1018,8 +1018,8 @@ quoteFunc(sql_context * context, int argc, sql_value ** argv) { assert(argc == 1); UNUSED_PARAMETER(argc); - switch (sql_value_type(argv[0])) { - case SQL_FLOAT:{ + switch (sql_value_mp_type(argv[0])) { + case MP_DOUBLE:{ double r1, r2; char zBuf[50]; r1 = sql_value_double(argv[0]); @@ -1033,11 +1033,11 @@ quoteFunc(sql_context * context, int argc, sql_value ** argv) SQL_TRANSIENT); break; } - case SQL_INTEGER:{ + case MP_INT:{ sql_result_value(context, argv[0]); break; } - case SQL_BLOB:{ + case MP_BIN:{ char *zText = 0; char const *zBlob = sql_value_blob(argv[0]); int nBlob = sql_value_bytes(argv[0]); @@ -1063,7 +1063,7 @@ quoteFunc(sql_context * context, int argc, sql_value ** argv) } break; } - case SQL_TEXT:{ + case MP_STR:{ int i, j; u64 n; const unsigned char *zArg = sql_value_text(argv[0]); @@ -1092,7 +1092,7 @@ quoteFunc(sql_context * context, int argc, sql_value ** argv) break; } default:{ - assert(sql_value_type(argv[0]) == SQL_NULL); + assert(sql_value_mp_type(argv[0]) == MP_NIL); sql_result_text(context, "NULL", 4, SQL_STATIC); break; } @@ -1228,13 +1228,13 @@ replaceFunc(sql_context * context, int argc, sql_value ** argv) assert(zStr == sql_value_text(argv[0])); /* No encoding change */ zPattern = sql_value_text(argv[1]); if (zPattern == 0) { - assert(sql_value_type(argv[1]) == SQL_NULL + assert(sql_value_mp_type(argv[1]) == MP_NIL || sql_context_db_handle(context)->mallocFailed); return; } nPattern = sql_value_bytes(argv[1]); if (nPattern == 0) { - assert(sql_value_type(argv[1]) != SQL_NULL); + assert(sql_value_mp_type(argv[1]) != MP_NIL); sql_result_value(context, argv[0]); return; } @@ -1302,7 +1302,7 @@ trimFunc(sql_context * context, int argc, sql_value ** argv) unsigned char **azChar = 0; /* Individual characters in zCharSet */ int nChar; /* Number of characters in zCharSet */ - if (sql_value_type(argv[0]) == SQL_NULL) { + if (sql_value_mp_type(argv[0]) == MP_NIL) { return; } zIn = sql_value_text(argv[0]); @@ -1499,10 +1499,10 @@ sumStep(sql_context * context, int argc, sql_value ** argv) UNUSED_PARAMETER(argc); p = sql_aggregate_context(context, sizeof(*p)); assert(p != NULL); - int type = sql_value_type(argv[0]); - if (type == SQL_NULL) + enum mp_type type = sql_value_mp_type(argv[0]); + if (type == MP_NIL) return; - if (type != SQL_FLOAT && type != SQL_INTEGER) { + if (type != MP_DOUBLE && type != MP_INT) { if (mem_apply_numeric_type(argv[0]) != 0) { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, sql_value_text(argv[0]), "number"); @@ -1510,10 +1510,10 @@ sumStep(sql_context * context, int argc, sql_value ** argv) context->isError = SQL_TARANTOOL_ERROR; return; } - type = sql_value_type(argv[0]); + type = sql_value_mp_type(argv[0]); } p->cnt++; - if (type == SQL_INTEGER) { + if (type == MP_INT) { i64 v = sql_value_int64(argv[0]); p->rSum += v; if ((p->approx | p->overflow) == 0 && @@ -1578,7 +1578,7 @@ countStep(sql_context * context, int argc, sql_value ** argv) { CountCtx *p; p = sql_aggregate_context(context, sizeof(*p)); - if ((argc == 0 || SQL_NULL != sql_value_type(argv[0])) && p) { + if ((argc == 0 || MP_NIL != sql_value_mp_type(argv[0])) && p) { p->n++; } } @@ -1605,7 +1605,7 @@ minmaxStep(sql_context * context, int NotUsed, sql_value ** argv) if (!pBest) return; - if (sql_value_type(argv[0]) == SQL_NULL) { + if (sql_value_mp_type(argv[0]) == MP_NIL) { if (pBest->flags) sqlSkipAccumulatorLoad(context); } else if (pBest->flags) { @@ -1657,7 +1657,7 @@ groupConcatStep(sql_context * context, int argc, sql_value ** argv) const char *zSep; int nVal, nSep; assert(argc == 1 || argc == 2); - if (sql_value_type(argv[0]) == SQL_NULL) + if (sql_value_mp_type(argv[0]) == MP_NIL) return; pAccum = (StrAccum *) sql_aggregate_context(context, sizeof(*pAccum)); diff --git a/src/box/sql/legacy.c b/src/box/sql/legacy.c index 599fba0be..64b1564ee 100644 --- a/src/box/sql/legacy.c +++ b/src/box/sql/legacy.c @@ -130,7 +130,7 @@ sql_exec(sql * db, /* The database on which the SQL executes */ && sql_column_type(pStmt, i) != - SQL_NULL) { + MP_NIL) { sqlOomFault(db); goto exec_out; } diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index b5c596d2f..9134f767d 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -455,8 +455,8 @@ sql_value_int64(sql_value *); const unsigned char * sql_value_text(sql_value *); -int -sql_value_type(sql_value *); +enum mp_type +sql_value_mp_type(sql_value *); sql * sql_context_db_handle(sql_context *); @@ -580,8 +580,8 @@ const unsigned char * sql_column_text(sql_stmt *, int iCol); -int -sql_column_type(sql_stmt *, int iCol); +enum mp_type +sql_column_type(sql_stmt *stmt, int field_no); sql_value * sql_column_value(sql_stmt *, @@ -632,14 +632,6 @@ sql_exec(sql *, /* An open database */ #define SQL_CONSTRAINT_TRIGGER (SQL_CONSTRAINT | (7<<8)) #define SQL_CONSTRAINT_UNIQUE (SQL_CONSTRAINT | (8<<8)) -enum sql_type { - SQL_INTEGER = 1, - SQL_FLOAT = 2, - SQL_TEXT = 3, - SQL_BLOB = 4, - SQL_NULL = 5, -}; - /** * Subtype of a main type. Allows to do some subtype specific * things: serialization, unpacking etc. diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c index be5c9dff9..6e867ca84 100644 --- a/src/box/sql/vdbeapi.c +++ b/src/box/sql/vdbeapi.c @@ -243,44 +243,18 @@ sql_value_text(sql_value * pVal) * fundamental datatypes: 64-bit signed integer 64-bit IEEE floating * point number string BLOB NULL */ -int -sql_value_type(sql_value * pVal) -{ - static const u8 aType[] = { - SQL_BLOB, /* 0x00 */ - SQL_NULL, /* 0x01 */ - SQL_TEXT, /* 0x02 */ - SQL_NULL, /* 0x03 */ - SQL_INTEGER, /* 0x04 */ - SQL_NULL, /* 0x05 */ - SQL_INTEGER, /* 0x06 */ - SQL_NULL, /* 0x07 */ - SQL_FLOAT, /* 0x08 */ - SQL_NULL, /* 0x09 */ - SQL_FLOAT, /* 0x0a */ - SQL_NULL, /* 0x0b */ - SQL_INTEGER, /* 0x0c */ - SQL_NULL, /* 0x0d */ - SQL_INTEGER, /* 0x0e */ - SQL_NULL, /* 0x0f */ - SQL_BLOB, /* 0x10 */ - SQL_NULL, /* 0x11 */ - SQL_TEXT, /* 0x12 */ - SQL_NULL, /* 0x13 */ - SQL_INTEGER, /* 0x14 */ - SQL_NULL, /* 0x15 */ - SQL_INTEGER, /* 0x16 */ - SQL_NULL, /* 0x17 */ - SQL_FLOAT, /* 0x18 */ - SQL_NULL, /* 0x19 */ - SQL_FLOAT, /* 0x1a */ - SQL_NULL, /* 0x1b */ - SQL_INTEGER, /* 0x1c */ - SQL_NULL, /* 0x1d */ - SQL_INTEGER, /* 0x1e */ - SQL_NULL, /* 0x1f */ - }; - return aType[pVal->flags & MEM_PURE_TYPE_MASK]; +enum mp_type +sql_value_mp_type(sql_value *pVal) +{ + switch (pVal->flags & MEM_PURE_TYPE_MASK) { + case MEM_Int: return MP_INT; + case MEM_Real: return MP_DOUBLE; + case MEM_Str: return MP_STR; + case MEM_Blob: return MP_BIN; + case MEM_Bool: return MP_BOOL; + case MEM_Null: return MP_NIL; + default: unreachable(); + } } /* Make a copy of an sql_value object @@ -1036,12 +1010,12 @@ sql_column_value(sql_stmt * pStmt, int i) return (sql_value *) pOut; } -int +enum mp_type sql_column_type(sql_stmt * pStmt, int i) { - int iType = sql_value_type(columnMem(pStmt, i)); + enum mp_type type = sql_value_mp_type(columnMem(pStmt, i)); columnMallocFailure(pStmt); - return iType; + return type; } enum sql_subtype @@ -1414,16 +1388,16 @@ int sql_bind_value(sql_stmt * pStmt, int i, const sql_value * pValue) { int rc; - switch (sql_value_type((sql_value *) pValue)) { - case SQL_INTEGER:{ + switch (sql_value_mp_type((sql_value *) pValue)) { + case MP_INT:{ rc = sql_bind_int64(pStmt, i, pValue->u.i); break; } - case SQL_FLOAT:{ + case MP_DOUBLE:{ rc = sql_bind_double(pStmt, i, pValue->u.r); break; } - case SQL_BLOB:{ + case MP_BIN:{ if (pValue->flags & MEM_Zero) { rc = sql_bind_zeroblob(pStmt, i, pValue->u.nZero); @@ -1434,7 +1408,7 @@ sql_bind_value(sql_stmt * pStmt, int i, const sql_value * pValue) } break; } - case SQL_TEXT:{ + case MP_STR:{ rc = bindText(pStmt, i, pValue->z, pValue->n, SQL_TRANSIENT); break; diff --git a/src/box/sql/whereexpr.c b/src/box/sql/whereexpr.c index 1fb8fd29f..a80c7a0ab 100644 --- a/src/box/sql/whereexpr.c +++ b/src/box/sql/whereexpr.c @@ -290,7 +290,7 @@ like_optimization_is_valid(Parse *pParse, Expr *pExpr, Expr **ppPrefix, pVal = sqlVdbeGetBoundValue(pReprepare, iCol, FIELD_TYPE_SCALAR); - if (pVal && sql_value_type(pVal) == SQL_TEXT) { + if (pVal && sql_value_mp_type(pVal) == MP_STR) { z = (char *)sql_value_text(pVal); } sqlVdbeSetVarmask(pParse->pVdbe, iCol); -- 2.15.1