src/PythonHandler.h
branchv_0
changeset 16 c71600851b01
parent 15 60a17d8fc223
child 17 7a91400b84c2
--- 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<writer::RelationalWriter> relationalWriter;
 
 	wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
@@ -56,6 +59,7 @@
 	wregex relationNameRegEx;
 	string_t pythonCode;
 
+	vector<writer::AttributeMetadata> currentWriterMetadata;
 	vector<string_t> currentRecord;
 	integer_t currentAttributeIndex = 0;
 	boolean_t includeCurrentRecord = true;
@@ -86,15 +90,15 @@
 
 	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
 		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
-		vector<writer::AttributeMetadata> 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());