# HG changeset patch # User František Kučera # Date 1544830574 -3600 # Node ID c71600851b011896fdb99afd25603c972d49e3a7 # Parent 60a17d8fc22361d83cb125c84a1e7475f961e1eb local variables named like attributes → simple queries: WHERE = a == "c" or b == "f" or r[0] == "aX" diff -r 60a17d8fc223 -r c71600851b01 src/PythonHandler.h --- a/src/PythonHandler.h Fri Dec 14 02:24:33 2018 +0100 +++ b/src/PythonHandler.h Sat Dec 15 00:36:14 2018 +0100 @@ -48,6 +48,9 @@ class PythonHandler : public RelationalReaderStringHadler { private: + const wregex ATTRIBUTE_NAMES_ALLOWED = wregex(L"[a-zA-Z0-9_]"); + const wregex ATTRIBUTE_NAMES_DISALLOWED = wregex(L"WHERE|and|or|[0-9_].*"); // TODO: more keywords + shared_ptr relationalWriter; wstring_convert> convertor; // TODO: support also other encodings. @@ -56,6 +59,7 @@ wregex relationNameRegEx; string_t pythonCode; + vector currentWriterMetadata; vector currentRecord; integer_t currentAttributeIndex = 0; boolean_t includeCurrentRecord = true; @@ -86,15 +90,15 @@ void startRelation(string_t name, vector attributes) override { // TODO: move to a reusable method (or use same metadata on both reader and writer side?) - vector writerMetadata; + currentWriterMetadata.clear(); for (AttributeMetadata readerMetadata : attributes) { - writerMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())}); + currentWriterMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())}); } currentRecord.resize(attributes.size()); filterCurrentRelation = regex_match(name, relationNameRegEx); - relationalWriter->startRelation(name, writerMetadata, true); + relationalWriter->startRelation(name, currentWriterMetadata, true); } void attribute(const string_t& value) override { @@ -111,12 +115,18 @@ //PyUnicode_FromWideChar() + pyModule = PyImport_AddModule((char*) "__main__"); // FIXME: variable and Py_DecodeLocale ? + pyDict = PyModule_GetDict(pyModule); + for (int i = 0; i < currentRecord.size(); i++) { - PyList_SetItem(pyRecord, i, PyUnicode_FromString(convertor.to_bytes(currentRecord[i]).c_str())); + PyObject* pyValue = PyUnicode_FromString(convertor.to_bytes(currentRecord[i]).c_str()); + PyList_SetItem(pyRecord, i, pyValue); + string_t attributeName = currentWriterMetadata[i].attributeName; + if (regex_match(attributeName, ATTRIBUTE_NAMES_ALLOWED) && !regex_match(attributeName, ATTRIBUTE_NAMES_DISALLOWED)) { + PyDict_SetItemString(pyDict, convertor.to_bytes(attributeName).c_str(), pyValue); + } } - pyModule = PyImport_AddModule((char*) "__main__"); // FIXME: variable and Py_DecodeLocale ? - pyDict = PyModule_GetDict(pyModule); PyDict_SetItemString(pyDict, "r", pyRecord); PyRun_SimpleString(convertor.to_bytes(pythonCode).c_str());