EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
RowsetToSQL.h File Reference
#include "python/PyRep.h"
Include dependency graph for RowsetToSQL.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  ReaderColumnContentDesc
 Helper class for column description. More...
 

Functions

template<typename _Reader >
void ClassifyColumnTypes (std::vector< ReaderColumnContentDesc > &into, _Reader &reader)
 Classifies columns using ReaderColumnContentDesc. More...
 
template<typename _Reader >
bool ReaderToSQL (const char *table_name, const char *key_field, FILE *out, _Reader &reader)
 Dumps rowset to SQL. More...
 

Variables

const size_t INSERT_QUERY_ROW_LIMIT
 

Function Documentation

template<typename _Reader >
void ClassifyColumnTypes ( std::vector< ReaderColumnContentDesc > &  into,
_Reader &  reader 
)

Classifies columns using ReaderColumnContentDesc.

Parameters
[out]intoArray of ReaderColumnContentDesc objects, which receive column descriptions.
[in]readerRowset reader to use.

Definition at line 95 of file RowsetToSQL.h.

96 {
97  const size_t cc = reader.columnCount();
98 
99  into.clear();
100  into.resize( cc );
101 
102  typename _Reader::iterator cur, end;
103  cur = reader.begin();
104  end = reader.end();
105  for(; cur != end; ++cur)
106  for( size_t col = 0; col < cc; ++col )
107  into[col].Process( cur.GetType( col ) );
108 }
template<typename _Reader >
bool ReaderToSQL ( const char *  table_name,
const char *  key_field,
FILE *  out,
_Reader &  reader 
)

Dumps rowset to SQL.

Parameters
[in]table_nameName of table to be created.
[in]key_fieldKey field of table.
[out]outOutput file.
[in]readerRowset reader to use.

Definition at line 119 of file RowsetToSQL.h.

References INSERT_QUERY_ROW_LIMIT, ReaderColumnContentDesc::nullString(), sLog, and ReaderColumnContentDesc::typeString().

120 {
121  const size_t cc = reader.columnCount();
122 
123  const size_t key_col = reader.FindColumn( key_field );
124  if( cc == key_col )
125  {
126  sLog.Error( "ReaderToSQL", "Unable to find key column '%s'.", key_field );
127  return false;
128  }
129 
130  std::vector<ReaderColumnContentDesc> types;
131  ClassifyColumnTypes<_Reader>( types, reader );
132 
133  fprintf( out,
134  "--- %s schema ---\n"
135  "\n"
136  "CREATE TABLE `%s` (\n",
137  table_name,
138  table_name
139  );
140 
141  std::string field_list;
142  for( size_t col = 0; col < cc; ++col )
143  {
144  const char* colName = reader.columnName( col );
145  const ReaderColumnContentDesc& colDesc = types[col];
146 
147  fprintf( out,
148  "%s `%s` %s %s%s",
149  ( 0 != col ? ",\n" : "" ),
150  colName,
151  colDesc.typeString(),
152  colDesc.nullString(),
153  ( key_col == col ? " PRIMARY KEY" : "" )
154  );
155 
156  if( 0 != col )
157  field_list += ",";
158  field_list += "`";
159  field_list += colName;
160  field_list += "`";
161  }
162 
163  fprintf( out,
164  "%s\n"
165  ");\n"
166  "\n"
167  "--- end %s schema ---\n"
168  "\n"
169  "--- %s data ---\n"
170  "\n",
171  ( NULL == key_field ? ",\n PRIMARY KEY()" : "" ),
172  table_name,
173  table_name
174  );
175 
176  typename _Reader::iterator cur, end;
177  cur = reader.begin();
178  end = reader.end();
179  for( size_t rowIndex = 0; cur != end; ++cur, ++rowIndex )
180  {
181  if( 0 == ( rowIndex % INSERT_QUERY_ROW_LIMIT ) )
182  {
183  if( 0 != rowIndex )
184  fprintf( out, ";\n" );
185 
186  fprintf( out, "INSERT INTO `%s`(%s) VALUES ", table_name, field_list.c_str() );
187  }
188  else
189  fprintf( out, "," );
190 
191  fprintf( out, "(" );
192  for( size_t col = 0; col < cc; ++col )
193  {
194  if( col != 0 )
195  fprintf( out, "," );
196 
197  fprintf( out, "%s", cur.GetAsString( col ).c_str() );
198  }
199  fprintf( out, ")" );
200  }
201 
202  fprintf( out,
203  ";\n"
204  "\n"
205  "--- end %s data ---\n",
206  table_name
207  );
208 
209  return true;
210 }
Helper class for column description.
Definition: RowsetToSQL.h:41
const char * typeString() const
Definition: RowsetToSQL.h:65
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
const char * nullString() const
Definition: RowsetToSQL.h:67
const size_t INSERT_QUERY_ROW_LIMIT
Definition: RowsetToSQL.cpp:30

Here is the call graph for this function:

Variable Documentation

const size_t INSERT_QUERY_ROW_LIMIT

The largest amount of rows to be inserted by single query.

Definition at line 30 of file RowsetToSQL.cpp.

Referenced by ReaderToSQL().