1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
/* Copyright (C) 2003,2004 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
#include <winsock2.h>
#endif
#include "mysql.h"
#include "myx_const_string.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
class Row_field;
///////////////////////////////////////////////////////////////////////////////
class Query_reader
{
public:
Query_reader(MYSQL * mysql, const char * query);
~Query_reader();
bool read ();
size_t get_num_rows() {return (size_t)mysql_num_rows(res);}
protected:
friend class Row_field;
const char * query;
MYSQL * mysql;
MYSQL_RES * res;
MYSQL_ROW row;
MYSQL_FIELD * fields;
Row_field * first, * last;
bool start();
};
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
class Row_field
{
public:
Row_field()
{
i_field= (size_t)-1;
}
void bind(Query_reader * reader, const const_string & column_name)
{
this->column_name= column_name;
this->reader= reader;
this->next= 0;
if (reader->last)
{
reader->last->next= this;
}
else
{
reader->first= this;
}
reader->last= this;
}
MYSQL_FIELD * get_field()
{ return i_field==(size_t)-1 ? 0 : reader->fields + i_field; }
char * get_value() { return i_field==(size_t)-1 ? 0 : reader->row[i_field]; }
const const_string & get_name () const { return column_name; }
bool is_valid() const { return reader->row[i_field]!=0 && i_field!=(size_t)-1; }
protected:
friend class Query_reader;
Query_reader * reader;
const_string column_name;
size_t i_field;
Row_field * next;
};
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#define DECLARE_QUERY_FORMAT(query_format_name,columns_and_constructor_begin)\
struct query_format_name : public Query_reader\
{\
query_format_name(MYSQL * mysql, const char * query)\
: Query_reader(mysql,query){bind();}\
columns_and_constructor_begin\
}\
};
///////////////////////////////////////////////////////////////////////////////
#define DECLARE_QUERY_RESULT_COLUMN_NAME(col, name, other_columns)\
Row_field col;\
other_columns\
col.bind(this,name);
///////////////////////////////////////////////////////////////////////////////
#define DECLARE_QUERY_RESULT_COLUMN(col_name, other_columns)\
DECLARE_QUERY_RESULT_COLUMN_NAME(col_name,\
const_string(#col_name,\
sizeof(#col_name)-1),\
other_columns)
///////////////////////////////////////////////////////////////////////////////
#define DECLARE_QUERY_FORMAT_BIND()\
void bind()\
{\
#ifdef __cplusplus
}
#endif /* __cplusplus */
|