[patches] [PATCH 7/7] sql: get rid of sqlite3_column_[origin_]name, columnName

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Wed Feb 28 22:36:54 MSK 2018


Name and alias can be get from sqlite3_column_meta after #2620.

Signed-off-by: Vladislav Shpilevoy <v.shpilevoy at tarantool.org>
---
 src/box/lua/sql.c     |  7 ++++--
 src/box/sql/legacy.c  |  8 +++----
 src/box/sql/sqlite3.h | 64 +--------------------------------------------------
 src/box/sql/vdbe.h    |  9 --------
 src/box/sql/vdbeapi.c | 62 -------------------------------------------------
 5 files changed, 10 insertions(+), 140 deletions(-)

diff --git a/src/box/lua/sql.c b/src/box/lua/sql.c
index 16bf2acdb..a28c1490c 100644
--- a/src/box/lua/sql.c
+++ b/src/box/lua/sql.c
@@ -2,6 +2,7 @@
 #include "box/sql.h"
 
 #include "box/sql/sqlite3.h"
+#include "box/sql/sqliteInt.h"
 #include "box/info.h"
 #include "lua/utils.h"
 #include "info.h"
@@ -132,8 +133,10 @@ lua_push_column_names(struct lua_State *L, struct prep_stmt_list *l)
 	int n = l->column_count;
 	lua_createtable(L, n, 0);
 	for (int i = 0; i < n; i++) {
-		const char *name = sqlite3_column_name(stmt, i);
-		lua_pushstring(L, name == NULL ? "" : name);
+		const struct sql_column_meta *meta =
+			sqlite3_column_meta(stmt, i);
+		assert(meta->alias != NULL);
+		lua_pushstring(L, meta->alias);
 		lua_rawseti(L, -2, i+1);
 	}
 }
diff --git a/src/box/sql/legacy.c b/src/box/sql/legacy.c
index ed3ead5db..80fd3d3ca 100644
--- a/src/box/sql/legacy.c
+++ b/src/box/sql/legacy.c
@@ -108,11 +108,11 @@ sqlite3_exec(sqlite3 * db,	/* The database on which the SQL executes */
 					if (azCols == 0) {
 						goto exec_out;
 					}
+					const struct sql_column_meta *meta;
 					for (i = 0; i < nCol; i++) {
-						azCols[i] =
-						    (char *)
-						    sqlite3_column_name(pStmt,
-									i);
+						meta =
+						  sqlite3_column_meta(pStmt, i);
+						azCols[i] = meta->alias;
 						assert(azCols[i] != 0);
 					}
 					callbackIsInit = 1;
diff --git a/src/box/sql/sqlite3.h b/src/box/sql/sqlite3.h
index 90a77e5df..588935aeb 100644
--- a/src/box/sql/sqlite3.h
+++ b/src/box/sql/sqlite3.h
@@ -377,7 +377,7 @@ typedef int (*sqlite3_callback) (void *, int, char **, char **);
  * sqlite3_exec() callback is a NULL pointer.  ^The 4th argument to the
  * sqlite3_exec() callback is an array of pointers to strings where each
  * entry represents the name of corresponding result column as obtained
- * from [sqlite3_column_name()].
+ * from [sqlite3_column_meta()].
  *
  * ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
  * to an empty string, or a pointer that contains only whitespace and/or
@@ -3866,68 +3866,6 @@ sqlite3_clear_bindings(sqlite3_stmt *);
 SQLITE_API int
 sqlite3_column_count(sqlite3_stmt * pStmt);
 
-/*
- * CAPI3REF: Column Names In A Result Set
- * METHOD: sqlite3_stmt
- *
- * ^These routines return the name assigned to a particular column
- * in the result set of a [SELECT] statement.  ^The sqlite3_column_name()
- * interface returns a pointer to a zero-terminated UTF-8 string
- * and sqlite3_column_name16() returns a pointer to a zero-terminated
- * UTF-16 string.  ^The first parameter is the [prepared statement]
- * that implements the [SELECT] statement. ^The second parameter is the
- * column number.  ^The leftmost column is number 0.
- *
- * ^The returned string pointer is valid until either the [prepared statement]
- * is destroyed by [sqlite3_finalize()] or until the statement is automatically
- * reprepared by the first call to [sqlite3_step()] for a particular run
- * or until the next call to
- * sqlite3_column_name() or sqlite3_column_name16() on the same column.
- *
- * ^If sqlite3_malloc() fails during the processing of either routine
- * (for example during a conversion from UTF-8 to UTF-16) then a
- * NULL pointer is returned.
- *
- * ^The name of a result column is the value of the "AS" clause for
- * that column, if there is an AS clause.  If there is no AS clause
- * then the name of the column is unspecified and may change from
- * one release of SQLite to the next.
-*/
-SQLITE_API const char *
-sqlite3_column_name(sqlite3_stmt *, int N);
-
-/*
- * CAPI3REF: Source Of Data In A Query Result
- * METHOD: sqlite3_stmt
- *
- * ^These routines provide a means to determine the table and
- * table column that is the origin of a particular result column
- * in [SELECT] statement.
- * The _table_ routines return the table name, and the origin_
- * routines return the column name. ^The returned string is valid
- * until the [prepared statement] is destroyed using
- * [sqlite3_finalize()] or until the statement is automatically
- * reprepared by the first call to [sqlite3_step()] for a particular run
- * or until the same information is requested
- * again in a different encoding.
- *
- * ^The names returned are the original un-aliased names of the
- * table and column.
- *
- * ^The first argument to these interfaces is a [prepared statement].
- * ^These functions return information about the Nth result column returned by
- * the statement, where N is the second function argument.
- * ^The left-most column is column 0 for these routines.
- *
- * ^If the Nth column returned by the statement is an expression or
- * subquery and is not a column value, then all of these functions return
- * NULL.  ^These routine might also return NULL if a memory allocation error
- * occurs.  ^Otherwise, they return the name of the attached database, table,
- * or column that query result column was extracted from.
- */
-SQLITE_API const char *
-sqlite3_column_origin_name(sqlite3_stmt *, int);
-
 /**
  * Get column meta information.
  * @param stmt Prepared statement.
diff --git a/src/box/sql/vdbe.h b/src/box/sql/vdbe.h
index 7c49a67bd..ea1ff3a1d 100644
--- a/src/box/sql/vdbe.h
+++ b/src/box/sql/vdbe.h
@@ -154,15 +154,6 @@ typedef struct VdbeOpList VdbeOpList;
 #define P5_ConstraintCheck   3
 #define P5_ConstraintFK      4
 
-/*
- * The Vdbe.aColName array contains 3n Mem structures, where n is the
- * number of columns of data returned by the statement.
- */
-#define COLNAME_NAME     0
-#define COLNAME_TABLE    1
-#define COLNAME_COLUMN   2
-#define COLNAME_N        3	/* Number of COLNAME_xxx symbols */
-
 /*
  * The following macro converts a relative address in the p2 field
  * of a VdbeOp structure into a negative number so that
diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c
index 2d0efbda0..9ce68117a 100644
--- a/src/box/sql/vdbeapi.c
+++ b/src/box/sql/vdbeapi.c
@@ -1133,68 +1133,6 @@ sqlite3_column_type(sqlite3_stmt * pStmt, int i)
 	return iType;
 }
 
-/*
- * Get the N-th element of pStmt->pColName[]. If N is out of
- * range, return NULL.
- *
- * There are up to 5 names for each column.  useType determines which
- * name is returned.  Here are the names:
- *
- *    0      The column name as it should be displayed for output
- *    1      The name of the table that the column derives from
- *    2      The name of the table column that the result column derives from
- *
- * If the result is not a simple column reference (if it is an expression
- * or a constant) then useTypes 3 and 4 return NULL.
- */
-static const char *
-columnName(sqlite3_stmt *pStmt, int N, int useType)
-{
-	const char *ret;
-	Vdbe *p;
-	int n;
-	sqlite3 *db;
-#ifdef SQLITE_ENABLE_API_ARMOR
-	if (pStmt == NULL) {
-		(void)SQLITE_MISUSE_BKPT;
-		return NULL;
-	}
-#endif
-	ret = NULL;
-	p = (Vdbe *) pStmt;
-	db = p->db;
-	assert(db != NULL);
-	n = sqlite3_column_count(pStmt);
-	if (N < n && N >= 0) {
-		if (useType == COLNAME_NAME)
-			ret = p->columns[N].alias;
-		else
-			ret = p->columns[N].name;
-	}
-	return ret;
-}
-
-/*
- * Return the name of the Nth column of the result set returned by SQL
- * statement pStmt.
- */
-const char *
-sqlite3_column_name(sqlite3_stmt * pStmt, int N)
-{
-	return columnName(pStmt, N, COLNAME_NAME);
-}
-
-/*
- * Return the name of the table column from which a result column derives.
- * NULL is returned if the result column is an expression or constant or
- * anything else which is not an unambiguous reference to a database column.
- */
-const char *
-sqlite3_column_origin_name(sqlite3_stmt * pStmt, int N)
-{
-	return columnName(pStmt, N, COLNAME_COLUMN);
-}
-
 const struct sql_column_meta *
 sqlite3_column_meta(sqlite3_stmt *stmt, int fieldno)
 {
-- 
2.14.3 (Apple Git-98)




More information about the Tarantool-patches mailing list