EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
RowsetToSQL.h
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Zhur, Bloody.Rabbit
24 */
25 
26 #ifndef __ROWSETTOSQL_H_INCL__
27 #define __ROWSETTOSQL_H_INCL__
28 
29 #include "python/PyRep.h"
30 
32 extern const size_t INSERT_QUERY_ROW_LIMIT;
33 
42 {
43 public:
46  {
51 
54  };
55 
58 
60  ColumnType type() const { return mType; }
62  bool isNull() const { return mIsNull; }
63 
65  const char* typeString() const { return smTypeStrings[ type() ]; }
67  const char* nullString() const { return smNullStrings[ isNull() ]; }
68 
74  void Process( const PyRep::PyType t );
75 
76 protected:
80  bool mIsNull;
81 
83  static const char* const smTypeStrings[];
85  static const char* const smNullStrings[];
86 };
87 
94 template<typename _Reader>
95 void ClassifyColumnTypes( std::vector<ReaderColumnContentDesc>& into, _Reader& reader )
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 }
109 
118 template<typename _Reader>
119 bool ReaderToSQL( const char* table_name, const char* key_field, FILE* out, _Reader& reader )
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 }
211 
212 #endif
213 
214 
215 
static const char *const smTypeStrings[]
Definition: RowsetToSQL.h:83
Helper class for column description.
Definition: RowsetToSQL.h:41
static const char *const smNullStrings[]
Definition: RowsetToSQL.h:85
const char * typeString() const
Definition: RowsetToSQL.h:65
bool ReaderToSQL(const char *table_name, const char *key_field, FILE *out, _Reader &reader)
Dumps rowset to SQL.
Definition: RowsetToSQL.h:119
void Process(const PyRep::PyType t)
Processes PyRep type to determine type of column.
Definition: RowsetToSQL.cpp:57
ColumnType type() const
Definition: RowsetToSQL.h:60
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
const char * nullString() const
Definition: RowsetToSQL.h:67
PyType
Python wire object types.
Definition: PyRep.h:72
void ClassifyColumnTypes(std::vector< ReaderColumnContentDesc > &into, _Reader &reader)
Classifies columns using ReaderColumnContentDesc.
Definition: RowsetToSQL.h:95
const size_t INSERT_QUERY_ROW_LIMIT
Definition: RowsetToSQL.cpp:30
bool isNull() const
Definition: RowsetToSQL.h:62