src/PythonHandler.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 19 Dec 2018 23:10:20 +0100
branchv_0
changeset 20 c087f1316ef9
parent 18 6a178b86d048
child 23 8ce11a192aad
permissions -rw-r--r--
builds with cmake 3.7.2 (e.g. on Debian GNU/Linux 9.6)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#pragma once
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <memory>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <string>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <vector>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <iostream>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <sstream>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <locale>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <codecvt>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <regex>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
10
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    29
#include <Python.h>
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    30
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/reader/typedefs.h>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include <relpipe/reader/TypeId.h>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include <relpipe/reader/handlers/AttributeMetadata.h>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
#include <relpipe/writer/Factory.h>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    38
#include <relpipe/cli/RelpipeCLIException.h>
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    39
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
namespace relpipe {
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
namespace tr {
7
aebaf590a838 use smart pointer
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    42
namespace grep {
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    44
using namespace std;
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
using namespace relpipe;
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
using namespace relpipe::reader;
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
using namespace relpipe::reader::handlers;
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
15
60a17d8fc223 fix class name
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    49
class PythonHandler : public RelationalReaderStringHadler {
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
private:
18
6a178b86d048 better regex, avoid Python keyword name collisions: WHERE = type == "btrfs" or a_pass == "1"
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    51
	const wregex ATTRIBUTE_NAMES_ALLOWED = wregex(L"[a-zA-Z0-9_]+");
6a178b86d048 better regex, avoid Python keyword name collisions: WHERE = type == "btrfs" or a_pass == "1"
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    52
	const wregex ATTRIBUTE_NAMES_DISALLOWED = wregex(L"WHERE|[0-9_].*|False|None|True|and|as|assert|break|class|continue|def|del|elif|else|except|finally|for|from|global|if|import|in|is|lambda|nonlocal|not|or|pass|raise|return|try|while|with|yield"); // Python keywords from: import keyword; keyword.kwlist
16
c71600851b01 local variables named like attributes → simple queries: WHERE = a == "c" or b == "f" or r[0] == "aX"
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    53
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    54
	shared_ptr<writer::RelationalWriter> relationalWriter;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    55
11
5e0b317f4100 execute arbitrary python code,
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    56
	wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
10
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    57
	wchar_t* pythonProgramName;
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    58
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    59
	wregex relationNameRegEx;
11
5e0b317f4100 execute arbitrary python code,
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    60
	string_t pythonCode;
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    61
16
c71600851b01 local variables named like attributes → simple queries: WHERE = a == "c" or b == "f" or r[0] == "aX"
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    62
	vector<writer::AttributeMetadata> currentWriterMetadata;
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    63
	vector<string_t> currentRecord;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    64
	integer_t currentAttributeIndex = 0;
11
5e0b317f4100 execute arbitrary python code,
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    65
	boolean_t includeCurrentRecord = true;
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    66
	boolean_t filterCurrentRelation = false;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    67
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
public:
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
15
60a17d8fc223 fix class name
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    70
	PythonHandler(ostream& output, const vector<string_t>& arguments) {
7
aebaf590a838 use smart pointer
František Kučera <franta-hg@frantovo.cz>
parents: 3
diff changeset
    71
		relationalWriter.reset(writer::Factory::create(output));
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    72
10
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    73
		pythonProgramName = Py_DecodeLocale("relpipe-tr-python", NULL);
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    74
		Py_SetProgramName(pythonProgramName);
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    75
		Py_Initialize();
12
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    76
11
5e0b317f4100 execute arbitrary python code,
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    77
		if (arguments.size() == 2) {
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    78
			relationNameRegEx = wregex(arguments[0]);
11
5e0b317f4100 execute arbitrary python code,
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    79
			pythonCode = arguments[1];
17
7a91400b84c2 add TODO comments, n.b. this is very experimental code
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    80
			// TODO: allow relation structure changes: if there are more arguments, the describe the output relation
7a91400b84c2 add TODO comments, n.b. this is very experimental code
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    81
			// TODO: allow also other modes:
7a91400b84c2 add TODO comments, n.b. this is very experimental code
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    82
			//	- insert additional records
7a91400b84c2 add TODO comments, n.b. this is very experimental code
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    83
			//	- process whole relation at once (can do e.g. some aggregations)
7a91400b84c2 add TODO comments, n.b. this is very experimental code
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    84
			//	- process whole stream at once (can do e.g. some joins or unions)
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    85
		} else {
10
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    86
			PyMem_RawFree(pythonProgramName);
11
5e0b317f4100 execute arbitrary python code,
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    87
			throw cli::RelpipeCLIException(L"Usage: relpipe-tr-python <relationNameRegExp> <pythonCode>", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    88
		}
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
	}
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
15
60a17d8fc223 fix class name
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    91
	virtual ~PythonHandler() {
10
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    92
		Py_FinalizeEx();
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    93
		PyMem_RawFree(pythonProgramName);
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    94
	}
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    95
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    96
	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
16
c71600851b01 local variables named like attributes → simple queries: WHERE = a == "c" or b == "f" or r[0] == "aX"
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
    98
		currentWriterMetadata.clear();
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
		for (AttributeMetadata readerMetadata : attributes) {
16
c71600851b01 local variables named like attributes → simple queries: WHERE = a == "c" or b == "f" or r[0] == "aX"
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   100
			currentWriterMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())});
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
		}
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   102
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   103
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   104
		currentRecord.resize(attributes.size());
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   105
		filterCurrentRelation = regex_match(name, relationNameRegEx);
16
c71600851b01 local variables named like attributes → simple queries: WHERE = a == "c" or b == "f" or r[0] == "aX"
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   106
		relationalWriter->startRelation(name, currentWriterMetadata, true);
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
	}
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
	void attribute(const string_t& value) override {
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   110
		if (filterCurrentRelation) {
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   111
			currentRecord[currentAttributeIndex] = value;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   112
			currentAttributeIndex++;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   113
11
5e0b317f4100 execute arbitrary python code,
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   114
			if (currentAttributeIndex > 0 && currentAttributeIndex % currentRecord.size() == 0) {
12
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   115
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   116
				PyObject* pyModule;
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   117
				PyObject* pyDict;
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   118
				PyObject* pyWhere;
14
9ed7c3b6ad7a modify records from Python: r[1] = "hello from Python"
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   119
				PyObject* pyRecord = PyList_New(currentRecord.size());
13
df354b680b89 pass record data from C++ to Python and filtering: WHERE = record[0] == "a"
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   120
df354b680b89 pass record data from C++ to Python and filtering: WHERE = record[0] == "a"
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   121
				//PyUnicode_FromWideChar()
df354b680b89 pass record data from C++ to Python and filtering: WHERE = record[0] == "a"
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   122
16
c71600851b01 local variables named like attributes → simple queries: WHERE = a == "c" or b == "f" or r[0] == "aX"
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   123
				pyModule = PyImport_AddModule((char*) "__main__"); // FIXME: variable and Py_DecodeLocale ?
c71600851b01 local variables named like attributes → simple queries: WHERE = a == "c" or b == "f" or r[0] == "aX"
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   124
				pyDict = PyModule_GetDict(pyModule);
c71600851b01 local variables named like attributes → simple queries: WHERE = a == "c" or b == "f" or r[0] == "aX"
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   125
13
df354b680b89 pass record data from C++ to Python and filtering: WHERE = record[0] == "a"
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   126
				for (int i = 0; i < currentRecord.size(); i++) {
17
7a91400b84c2 add TODO comments, n.b. this is very experimental code
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   127
					// TODO: pass particular data-types to Python, not only strings
16
c71600851b01 local variables named like attributes → simple queries: WHERE = a == "c" or b == "f" or r[0] == "aX"
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   128
					PyObject* pyValue = PyUnicode_FromString(convertor.to_bytes(currentRecord[i]).c_str());
c71600851b01 local variables named like attributes → simple queries: WHERE = a == "c" or b == "f" or r[0] == "aX"
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   129
					PyList_SetItem(pyRecord, i, pyValue);
c71600851b01 local variables named like attributes → simple queries: WHERE = a == "c" or b == "f" or r[0] == "aX"
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   130
					string_t attributeName = currentWriterMetadata[i].attributeName;
18
6a178b86d048 better regex, avoid Python keyword name collisions: WHERE = type == "btrfs" or a_pass == "1"
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   131
					if (regex_match(attributeName, ATTRIBUTE_NAMES_ALLOWED)) {
6a178b86d048 better regex, avoid Python keyword name collisions: WHERE = type == "btrfs" or a_pass == "1"
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   132
						if (regex_match(attributeName, ATTRIBUTE_NAMES_DISALLOWED)) attributeName = L"a_" + attributeName;
16
c71600851b01 local variables named like attributes → simple queries: WHERE = a == "c" or b == "f" or r[0] == "aX"
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   133
						PyDict_SetItemString(pyDict, convertor.to_bytes(attributeName).c_str(), pyValue);
c71600851b01 local variables named like attributes → simple queries: WHERE = a == "c" or b == "f" or r[0] == "aX"
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   134
					}
13
df354b680b89 pass record data from C++ to Python and filtering: WHERE = record[0] == "a"
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   135
				}
df354b680b89 pass record data from C++ to Python and filtering: WHERE = record[0] == "a"
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   136
14
9ed7c3b6ad7a modify records from Python: r[1] = "hello from Python"
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   137
				PyDict_SetItemString(pyDict, "r", pyRecord);
12
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   138
11
5e0b317f4100 execute arbitrary python code,
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   139
				PyRun_SimpleString(convertor.to_bytes(pythonCode).c_str());
13
df354b680b89 pass record data from C++ to Python and filtering: WHERE = record[0] == "a"
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   140
12
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   141
				// FIXME: check Python error and throw exception
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   142
				// if (PyErr_Occurred()) throw cli::RelpipeCLIException(L"Python code failed.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: review exit code
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   143
				// if (PyErr_Occurred()) { PyErr_Print(); PyErr_Clear(); }
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   144
13
df354b680b89 pass record data from C++ to Python and filtering: WHERE = record[0] == "a"
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   145
12
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   146
				pyWhere = PyDict_GetItemString(pyDict, "WHERE");
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   147
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   148
				if (pyWhere) includeCurrentRecord = PyLong_AsLong(pyWhere);
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   149
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   150
14
9ed7c3b6ad7a modify records from Python: r[1] = "hello from Python"
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   151
				if (includeCurrentRecord) {
9ed7c3b6ad7a modify records from Python: r[1] = "hello from Python"
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   152
					for (int i = 0; i < currentRecord.size(); i++) {
9ed7c3b6ad7a modify records from Python: r[1] = "hello from Python"
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   153
						Py_ssize_t l;
9ed7c3b6ad7a modify records from Python: r[1] = "hello from Python"
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   154
						currentRecord[i] = wstring(PyUnicode_AsWideCharString(PyList_GetItem(pyRecord, i), &l));
9ed7c3b6ad7a modify records from Python: r[1] = "hello from Python"
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   155
					}
9ed7c3b6ad7a modify records from Python: r[1] = "hello from Python"
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   156
				}
9ed7c3b6ad7a modify records from Python: r[1] = "hello from Python"
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   157
9ed7c3b6ad7a modify records from Python: r[1] = "hello from Python"
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   158
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   159
				if (includeCurrentRecord) for (string_t v : currentRecord) relationalWriter->writeAttribute(v);
11
5e0b317f4100 execute arbitrary python code,
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   160
				includeCurrentRecord = true;
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   161
			}
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   162
11
5e0b317f4100 execute arbitrary python code,
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   163
			currentAttributeIndex = currentAttributeIndex % currentRecord.size();
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   164
		} else {
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   165
			relationalWriter->writeAttribute(value);
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   166
		}
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   167
	}
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   168
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   169
	void endOfPipe() {
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   170
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   171
	}
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   172
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   173
};
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   174
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   175
}
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   176
}
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   177
}