src/PythonHandler.h
branchv_0
changeset 14 9ed7c3b6ad7a
parent 13 df354b680b89
child 15 60a17d8fc223
equal deleted inserted replaced
13:df354b680b89 14:9ed7c3b6ad7a
   105 			if (currentAttributeIndex > 0 && currentAttributeIndex % currentRecord.size() == 0) {
   105 			if (currentAttributeIndex > 0 && currentAttributeIndex % currentRecord.size() == 0) {
   106 
   106 
   107 				PyObject* pyModule;
   107 				PyObject* pyModule;
   108 				PyObject* pyDict;
   108 				PyObject* pyDict;
   109 				PyObject* pyWhere;
   109 				PyObject* pyWhere;
   110 				PyObject* pyRecord = PyTuple_New(currentRecord.size());
   110 				PyObject* pyRecord = PyList_New(currentRecord.size());
   111 
   111 
   112 				//PyUnicode_FromWideChar()
   112 				//PyUnicode_FromWideChar()
   113 
   113 
   114 				for (int i = 0; i < currentRecord.size(); i++) {
   114 				for (int i = 0; i < currentRecord.size(); i++) {
   115 					PyTuple_SetItem(pyRecord, i, PyUnicode_FromString(convertor.to_bytes(currentRecord[i]).c_str()));
   115 					PyList_SetItem(pyRecord, i, PyUnicode_FromString(convertor.to_bytes(currentRecord[i]).c_str()));
   116 				}
   116 				}
   117 
   117 
   118 				pyModule = PyImport_AddModule((char*) "__main__"); // FIXME: variable and Py_DecodeLocale ?
   118 				pyModule = PyImport_AddModule((char*) "__main__"); // FIXME: variable and Py_DecodeLocale ?
   119 				pyDict = PyModule_GetDict(pyModule);
   119 				pyDict = PyModule_GetDict(pyModule);
   120 				PyDict_SetItemString(pyDict, "record", pyRecord);
   120 				PyDict_SetItemString(pyDict, "r", pyRecord);
   121 
   121 
   122 				PyRun_SimpleString(convertor.to_bytes(pythonCode).c_str());
   122 				PyRun_SimpleString(convertor.to_bytes(pythonCode).c_str());
   123 
   123 
   124 				// FIXME: check Python error and throw exception
   124 				// FIXME: check Python error and throw exception
   125 				// if (PyErr_Occurred()) throw cli::RelpipeCLIException(L"Python code failed.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: review exit code
   125 				// if (PyErr_Occurred()) throw cli::RelpipeCLIException(L"Python code failed.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: review exit code
   127 
   127 
   128 
   128 
   129 				pyWhere = PyDict_GetItemString(pyDict, "WHERE");
   129 				pyWhere = PyDict_GetItemString(pyDict, "WHERE");
   130 
   130 
   131 				if (pyWhere) includeCurrentRecord = PyLong_AsLong(pyWhere);
   131 				if (pyWhere) includeCurrentRecord = PyLong_AsLong(pyWhere);
       
   132 
       
   133 
       
   134 				if (includeCurrentRecord) {
       
   135 					for (int i = 0; i < currentRecord.size(); i++) {
       
   136 						Py_ssize_t l;
       
   137 						currentRecord[i] = wstring(PyUnicode_AsWideCharString(PyList_GetItem(pyRecord, i), &l));
       
   138 					}
       
   139 				}
   132 
   140 
   133 
   141 
   134 				if (includeCurrentRecord) for (string_t v : currentRecord) relationalWriter->writeAttribute(v);
   142 				if (includeCurrentRecord) for (string_t v : currentRecord) relationalWriter->writeAttribute(v);
   135 				includeCurrentRecord = true;
   143 				includeCurrentRecord = true;
   136 			}
   144 			}