Qore Mapper Module Reference  1.0
 All Classes Namespaces Functions Variables Groups Pages
Mapper.qm.dox.h
1 // -*- mode: c++; indent-tabs-mode: nil -*-
3 
4 /* Mapper.qm Copyright 2014 Qore Technologies, sro
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23 */
24 
25 // this module requires Qore 0.8.9 or better
26 
27 // require type definitions everywhere
28 
29 // enable all warnings
30 
31 
32 /* Version History
33  * 2014-01-03 v1.0: David Nichols <david@qore.org>
34  + the initial version of the Mapper module
35 */
36 
155 namespace Mapper {
158  class Mapper {
159 
160 public:
161  // no public members
162  public :
163 
164 public:
165 
166  private :
169 
171  string enc = "utf-8";
172 
174  *string name;
175 
177  *code trunc;
178 
180  *code input_log;
181 
183  *code output_log;
184 
187 
189  Qore::TimeZone input_timezone = TimeZone::get();
190 
192  bool trunc_all = False;
193 
195  bool allow_dot = False;
196 
199 
201  int count = 0;
202 
204  const OptionKeys = (
205  "enc": True,
206  "name": True,
207  "trunc": True,
208  "input_log": True,
209  "output_log": True,
210  "timezone": True,
211  "input_timezone": True,
212  "trunc_all": True,
213  "crec": True,
214  "date_format": True,
215  );
216 
218  const ValidKeys = (
219  "name": True,
220  "struct": True,
221  "code": True,
222  "maxlen": True,
223  "trunc": True,
224  "mand": True,
225  "number": True,
226  "type": True,
227  "date_format": True,
228  );
229 
231  const ValidTypes = (
232  "number": True,
233  "integer": True,
234  "int": True, // is an alias for "integer"
235  "date": True,
236  );
237 
238 public:
239 
241 
264  constructor(hash map, *hash opts);
265 
266 
268  private constructor();
269 
270 
272  private setup(hash map, *hash opts);
273 
274 
276  private checkMap();
277 
278 
280  private convertToHash(int t, string k, reference fh);
281 
282 
284 
287  private checkMapField(string k, reference fh, *hash th);
288 
289 
291  private bool hasMapping(hash fh);
292 
293 
295  private checkTimezoneOption(hash opts, string rn);
296 
297 
298  string getFieldName(string name);
299 
300 
302 
304  hash validKeys();
305 
306 
308 
310  hash validTypes();
311 
312 
314 
316  hash optionKeys();
317 
318 
320 
329  list mapAll(softlist recs);
330 
331 
333 
343  hash mapData(hash rec);
344 
345 
347 
349  int getCount();
350 
351 
353 
355  resetCount();
356 
357 
359  private mapFieldType(string key, hash m, reference v, hash rec);
360 
361 
363  private error(string fmt);
364 
365 
367  private error2(string ex, string fmt);
368 
369 
371  private any mapSubclass(hash m, any v);
372 
373  };
374 
377 
378 public:
379  public :
380 
381 public:
382 
383  private :
386 
387 public:
388 
390 
394 
395 
397  bool next();
398 
399 
401  bool valid();
402 
403  };
404 
407 
408 public:
409  public :
410 
411 public:
412 
413  private :
416 
417 public:
418 
420 
427 
428 
430 
434 
435 
437  hash getValue();
438 
439 
441 
443  int getCount();
444 
445 
447 
449  resetCount();
450 
451  };
452 };
private mapFieldType(string key, hash m, reference v, hash rec)
performs type handling
Qore::AbstractIterator i
input iterator; AbstractIterator::getValue() must return a hash
Definition: Mapper.qm.dox.h:385
list mapAll(softlist recs)
maps all input records and returns the mapped data as a list of output records
int getCount()
returns the internal record count
private setup(hash map, *hash opts)
sets up the mapper object before checking the mapper hash
hash validTypes()
returns a list of valid field types for this class (can be overridden in subclasses) ...
private any mapSubclass(hash m, any v)
to be overridden as necessary in subclasses
constructor(Qore::AbstractIterator i)
creates the iterator from the arguments passed
bool next()
Moves the current position of the input iterator to the next element; returns False if there are no m...
const True
const OptionKeys
constructor option keys (can be extended by subclassing and reimplementing optionKeys()) ...
Definition: Mapper.qm.dox.h:204
*code output_log
an optional output data logging callback; must accept a hash giving the output data hash ...
Definition: Mapper.qm.dox.h:183
provides a hash iterator based on a mapper object and an iterator input source
Definition: Mapper.qm.dox.h:406
string enc
the output character encoding; if not given then the output encoding is assumed to be UTF-8 ...
Definition: Mapper.qm.dox.h:171
private convertToHash(int t, string k, reference fh)
convert a field definition to a hash if possible
private checkMapField(string k, reference fh, *hash th)
perform per-field pre-processing on the passed map in the constructor
const False
bool allow_dot
do not assume struct when field names have a &quot;.&quot; in them; instead allow input field names to have a &quot;...
Definition: Mapper.qm.dox.h:195
this class is a base class for mapping data; see Mapper Examples for usage examples ...
Definition: Mapper.qm.dox.h:158
list list(...)
hash getValue()
returns the current row transformed with the mapper
int count
count of records mapped
Definition: Mapper.qm.dox.h:201
resetCount()
resets the internal record count
constructor(Qore::AbstractIterator i, hash map, *hash opts)
creates the iterator from the arguments passed
Qore::TimeZone input_timezone
the timezone for input fields in case of parsing text values; if not set defaults to the current Time...
Definition: Mapper.qm.dox.h:189
private error2(string ex, string fmt)
throws the given exception; prepends the map name to the description if known
abstract base class for hash iterator mappping classes based on a mapper object and an iterator input...
Definition: Mapper.qm.dox.h:376
hash validKeys()
returns a list of valid field keys for this class (can be overridden in subclasses) ...
const ValidKeys
default known mapper hash field keys (can be extended by subclassing and reimplementing validKeys()) ...
Definition: Mapper.qm.dox.h:218
private checkMap()
verifies the input map in the constructor
private error(string fmt)
throws a MAP-ERROR exception; prepends the map name to the description if known
*Qore::TimeZone timezone
an optional timezone for output date fields
Definition: Mapper.qm.dox.h:186
bool trunc_all
truncate all option
Definition: Mapper.qm.dox.h:192
const ValidTypes
default known field types (can be extended by subclassing and reimplementing validTypes() and mapFiel...
Definition: Mapper.qm.dox.h:231
hash mapData(hash rec)
processes the input record and returns a hash of the mapped values where the keys in the hash returne...
*hash crec
constant hash value for output mappings
Definition: Mapper.qm.dox.h:198
*code trunc
an optional field truncation callback
Definition: Mapper.qm.dox.h:177
private checkTimezoneOption(hash opts, string rn)
verifies a timezone constructor option
Mapper::Mapper map
data mapper
Definition: Mapper.qm.dox.h:415
private constructor()
private constructor for subclasses
*string name
the optional name for the object (for example a table name); will be prepended to field names in erro...
Definition: Mapper.qm.dox.h:174
int getCount()
returns the internal record count
hash optionKeys()
returns a list of valid constructor options for this class (can be overridden in subclasses) ...
hash map
the hash providing output field names and mappings
Definition: Mapper.qm.dox.h:168
*code input_log
an optional input data logging callback; must accept a hash giving the input data hash ...
Definition: Mapper.qm.dox.h:180
hash hash(object obj)
bool valid()
returns True if the input iterator is currently pointing at a valid element, False if not ...
private bool hasMapping(hash fh)
returns True if the field has a mapping, False if not
resetCount()
resets the internal record count