MXS-1929: Simplify result set mechanism

The result set mechanism was ill-suited for iteration over
lists. Converting it into a class and inverting it by pushing rows into
the result set instead the result set asking for rows makes it very easy
to use with lists. It also solves some of the consistency problems that
existed with the previous implementation.
This commit is contained in:
Markus Mäkelä
2018-07-31 09:56:21 +03:00
parent 28e55b260f
commit bd48db28ec
3 changed files with 134 additions and 467 deletions

View File

@ -1,82 +0,0 @@
#pragma once
/*
* Copyright (c) 2016 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2022-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
/**
* @file resultset.h The MaxScale generic result set mechanism
*/
#include <maxscale/cdefs.h>
#include <maxscale/dcb.h>
MXS_BEGIN_DECLS
/**
* Column types
*/
typedef enum
{
COL_TYPE_VARCHAR = 0x0f,
COL_TYPE_VARSTRING = 0xfd
} RESULT_COL_TYPE;
/**
* The result set column definition. Each result set has an order linked
* list of column definitions.
*/
typedef struct resultcolumn
{
char *name; /*< Column name */
int len; /*< Column length */
RESULT_COL_TYPE type; /*< Column type */
struct resultcolumn *next; /*< next column */
} RESULT_COLUMN;
/**
* A representation of a row within a result set.
*/
typedef struct resultrow
{
int n_cols; /*< No. of columns in row */
char **cols; /*< The columns themselves */
} RESULT_ROW;
struct resultset;
/**
* Type of callback function used to supply each row
*/
typedef RESULT_ROW * (*RESULT_ROW_CB)(struct resultset *, void *);
/**
* The representation of the result set itself.
*/
typedef struct resultset
{
int n_cols; /*< No. of columns */
RESULT_COLUMN *column; /*< Linked list of column definitions */
RESULT_ROW_CB fetchrow; /*< Fetch a row for the result set */
void *userdata; /*< User data for the fetch row call */
} RESULTSET;
extern RESULTSET *resultset_create(RESULT_ROW_CB, void *);
extern void resultset_free(RESULTSET *);
extern int resultset_add_column(RESULTSET *, const char *, int, RESULT_COL_TYPE);
extern void resultset_column_free(RESULT_COLUMN *);
extern RESULT_ROW *resultset_make_row(RESULTSET *);
extern void resultset_free_row(RESULT_ROW *);
extern int resultset_row_set(RESULT_ROW *, int, const char *);
extern void resultset_stream_mysql(RESULTSET *, DCB *);
extern void resultset_stream_json(RESULTSET *, DCB *);
MXS_END_DECLS

View File

@ -0,0 +1,60 @@
#pragma once
/*
* Copyright (c) 2016 MariaDB Corporation Ab
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
*
* Change Date: 2022-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2 or later of the General
* Public License.
*/
#include <maxscale/cppdefs.hh>
#include <initializer_list>
#include <memory>
#include <string>
#include <vector>
#include <maxscale/dcb.h>
#include <maxscale/mysql_binlog.h>
/**
* A result set consisting of VARCHAR(255) columns
*/
class ResultSet
{
public:
/**
* Create a new result set
*
* @param names List of column names
*
* @return The new result set
*/
static std::unique_ptr<ResultSet> create(std::initializer_list<std::string> names);
/**
* Add a row to the result set
*
* @param values List of values for the row
*/
void add_row(std::initializer_list<std::string> values);
/**
* Write the result set to a DCB
*
* @param dcb DCB where the result set is written
*/
void write(DCB* dcb);
private:
std::vector<std::string> m_columns;
std::vector<std::vector<std::string>> m_rows;
ResultSet(std::initializer_list<std::string> names);
};