src/PythonHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 06 Jun 2020 01:50:45 +0200
branchv_0
changeset 32 543c495b43e0
parent 29 5105357e9b47
permissions -rw-r--r--
Added tag v0.16 for changeset 9c2cf0bf3eeb
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
29
5105357e9b47 fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 25
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * 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
    10
 * 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
    11
 * 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
    12
 * GNU General Public License for more details.
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * 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
    15
 * 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
    16
 */
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <iostream>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <sstream>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <locale>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <codecvt>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <regex>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
10
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    28
#include <Python.h>
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    29
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/typedefs.h>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/reader/TypeId.h>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include <relpipe/reader/handlers/AttributeMetadata.h>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
#include <relpipe/writer/Factory.h>
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    37
#include <relpipe/cli/RelpipeCLIException.h>
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    38
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
namespace relpipe {
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
namespace tr {
23
8ce11a192aad fix namespace
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
    41
namespace python {
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    43
using namespace std;
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
using namespace relpipe;
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
using namespace relpipe::reader;
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
using namespace relpipe::reader::handlers;
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
25
940bd8320e82 fix typo: Hadler → Handler
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
    48
class PythonHandler : public RelationalReaderStringHandler {
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
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
    50
	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
    51
	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
    52
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    53
	shared_ptr<writer::RelationalWriter> relationalWriter;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    54
11
5e0b317f4100 execute arbitrary python code,
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    55
	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
    56
	wchar_t* pythonProgramName;
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    57
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    58
	wregex relationNameRegEx;
11
5e0b317f4100 execute arbitrary python code,
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    59
	string_t pythonCode;
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    60
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
    61
	vector<writer::AttributeMetadata> currentWriterMetadata;
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    62
	vector<string_t> currentRecord;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    63
	integer_t currentAttributeIndex = 0;
11
5e0b317f4100 execute arbitrary python code,
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    64
	boolean_t includeCurrentRecord = true;
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    65
	boolean_t filterCurrentRelation = false;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    66
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
public:
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
15
60a17d8fc223 fix class name
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    69
	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
    70
		relationalWriter.reset(writer::Factory::create(output));
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    71
10
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    72
		pythonProgramName = Py_DecodeLocale("relpipe-tr-python", NULL);
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    73
		Py_SetProgramName(pythonProgramName);
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    74
		Py_Initialize();
12
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
    75
11
5e0b317f4100 execute arbitrary python code,
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    76
		if (arguments.size() == 2) {
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    77
			relationNameRegEx = wregex(arguments[0]);
11
5e0b317f4100 execute arbitrary python code,
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    78
			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
    79
			// 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
    80
			// 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
    81
			//	- 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
    82
			//	- 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
    83
			//	- 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
    84
		} else {
10
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    85
			PyMem_RawFree(pythonProgramName);
11
5e0b317f4100 execute arbitrary python code,
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    86
			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
    87
		}
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
	}
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
15
60a17d8fc223 fix class name
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    90
	virtual ~PythonHandler() {
10
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    91
		Py_FinalizeEx();
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    92
		PyMem_RawFree(pythonProgramName);
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    93
	}
2c8f5e05c7b7 Python included
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
    94
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    95
	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
    96
		// 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
    97
		currentWriterMetadata.clear();
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
		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
    99
			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
   100
		}
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   101
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
		currentRecord.resize(attributes.size());
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   104
		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
   105
		relationalWriter->startRelation(name, currentWriterMetadata, true);
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
	}
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
	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
   109
		if (filterCurrentRelation) {
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   110
			currentRecord[currentAttributeIndex] = value;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   111
			currentAttributeIndex++;
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   112
11
5e0b317f4100 execute arbitrary python code,
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   113
			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
   114
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   115
				PyObject* pyModule;
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   116
				PyObject* pyDict;
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   117
				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
   118
				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
   119
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
				//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
   121
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
   122
				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
   123
				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
   124
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
   125
				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
   126
					// 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
   127
					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
   128
					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
   129
					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
   130
					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
   131
						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
   132
						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
   133
					}
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
   134
				}
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
14
9ed7c3b6ad7a modify records from Python: r[1] = "hello from Python"
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   136
				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
   137
11
5e0b317f4100 execute arbitrary python code,
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   138
				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
   139
12
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   140
				// 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
   141
				// 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
   142
				// 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
   143
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
   144
12
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   145
				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
   146
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   147
				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
   148
ee69be2212fa relational restriction possible through Python: WHERE = False
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   149
14
9ed7c3b6ad7a modify records from Python: r[1] = "hello from Python"
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   150
				if (includeCurrentRecord) {
9ed7c3b6ad7a modify records from Python: r[1] = "hello from Python"
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   151
					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
   152
						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
   153
						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
   154
					}
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
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   158
				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
   159
				includeCurrentRecord = true;
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   160
			}
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   161
11
5e0b317f4100 execute arbitrary python code,
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   162
			currentAttributeIndex = currentAttributeIndex % currentRecord.size();
8
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   163
		} else {
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   164
			relationalWriter->writeAttribute(value);
f66c759d1111 first working grep version
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   165
		}
3
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   166
	}
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
	void endOfPipe() {
8731263d44f1 PassthroughHandler (string-based version)
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   169
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
}