Changeset: 31d42333e4f0 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=31d42333e4f0
Added Files:
        monetdb5/extras/pyapi/connection.c
        monetdb5/extras/pyapi/connection.h
Branch: pyapi
Log Message:

Add missing files.


diffs (216 lines):

diff --git a/monetdb5/extras/pyapi/connection.c 
b/monetdb5/extras/pyapi/connection.c
new file mode 100644
--- /dev/null
+++ b/monetdb5/extras/pyapi/connection.c
@@ -0,0 +1,158 @@
+
+#include "connection.h"
+#include "pytypes.h"
+#include "type_conversion.h"
+#include "sql_execute.h"
+#include "sql_storage.h"
+
+// Numpy Library
+#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
+#ifdef __INTEL_COMPILER
+// Intel compiler complains about trailing comma's in numpy source code
+#pragma warning(disable:271)
+#endif
+#include <numpy/arrayobject.h>
+
+static char* _connection_query(Client cntxt, char* query, res_table** result);
+static void _connection_cleanup_result(void* output);
+
+
+static PyObject *
+_connection_execute(Py_ConnectionObject *self, PyObject *args)
+{
+    if (!PyString_CheckExact(args)) {
+            PyErr_Format(PyExc_TypeError,
+                         "expected a query string, but got an object of type 
%s", Py_TYPE(args)->tp_name);
+            return NULL;
+    }
+    {
+        PyObject *result;
+        res_table* output = NULL;
+        char *res = NULL;
+
+        res = _connection_query(self->cntxt, ((PyStringObject*)args)->ob_sval, 
&output);
+        if (res != MAL_SUCCEED) {
+            PyErr_Format(PyExc_Exception, "SQL Query Failed: %s", (res ? res : 
"<no error>"));
+            return NULL;
+        }
+
+        result = PyDict_New();
+        if (output && output->nr_cols > 0) {
+            PyInput input;
+            PyObject *numpy_array;
+            int i;
+            for (i = 0; i < output->nr_cols; i++) {
+                res_col col = output->cols[i];
+                BAT* b = BATdescriptor(col.b);
+
+                input.bat = b;
+                input.count = BATcount(b);
+                input.bat_type = ATOMstorage(getColumnType(b->T->type));
+                input.scalar = false;
+
+                numpy_array = PyMaskedArray_FromBAT(&input, 0, input.count, 
&res);
+                if (!numpy_array) {
+                    _connection_cleanup_result(output);
+                    PyErr_Format(PyExc_Exception, "SQL Query Failed: %s", (res 
? res : "<no error>"));
+                    return NULL;
+                }
+                PyDict_SetItem(result, 
PyString_FromString(output->cols[i].name), numpy_array);
+            }
+            _connection_cleanup_result(output);
+            return result;
+        } else {
+            Py_RETURN_NONE;
+        }
+
+    }
+}
+
+static PyMethodDef _connectionObject_methods[] = {
+    {"execute", (PyCFunction)_connection_execute, METH_O,"execute(query) -> 
executes a SQL query on the database in the current client context"},
+    {NULL,NULL,0,NULL}  /* Sentinel */
+};
+
+PyTypeObject Py_ConnectionType = {
+    PyVarObject_HEAD_INIT(&PyType_Type, 0)
+    "monetdb._connection",
+    sizeof(Py_ConnectionObject),
+    0,
+    0,                                          /* tp_dealloc */
+    0,                                          /* tp_print */
+    0,                                          /* tp_getattr */
+    0,                                          /* tp_setattr */
+    0,                                          /* tp_compare */
+    0,                                          /* tp_repr */
+    0,                                          /* tp_as_number */
+    0,                                          /* tp_as_sequence */
+    0,                                          /* tp_as_mapping */
+    (hashfunc)PyObject_HashNotImplemented,      /* tp_hash */
+    0,                                          /* tp_call */
+    0,                                          /* tp_str */
+    0,                                          /* tp_getattro */
+    0,                                          /* tp_setattro */
+    0,                                          /* tp_as_buffer */
+    Py_TPFLAGS_DEFAULT,                         /* tp_flags */
+    "Connection to MonetDB",                    /* tp_doc */
+    0,                                          /* tp_traverse */
+    0,                                          /* tp_clear */
+    0,                                          /* tp_richcompare */
+    0,                                          /* tp_weaklistoffset */
+    0,                                          /* tp_iter */
+    0,                                          /* tp_iternext */
+    _connectionObject_methods,                  /* tp_methods */
+    0,                                          /* tp_members */
+    0,                                          /* tp_getset */
+    0,                                          /* tp_base */
+    0,                                          /* tp_dict */
+    0,                                          /* tp_descr_get */
+    0,                                          /* tp_descr_set */
+    0,                                          /* tp_dictoffset */
+    0,                                          /* tp_init */
+    PyType_GenericAlloc,                        /* tp_alloc */
+    PyType_GenericNew,                          /* tp_new */
+    PyObject_Del,                               /* tp_free */
+    0,
+    0,
+    0,
+    0,
+    0,
+    0, 
+    0,
+    0
+};
+
+static void _connection_cleanup_result(void* output) 
+{
+    res_table_destroy((res_table*) output);
+}
+
+static char* _connection_query(Client cntxt, char* query, res_table** result) {
+    str res = MAL_SUCCEED;
+    Client c = cntxt;
+    res = SQLstatementIntern(c, &query, "name", 1, 0, result);
+    return res;
+}
+
+
+PyObject *Py_Connection_Create(Client cntxt)
+{
+    register Py_ConnectionObject *op;
+
+    op = (Py_ConnectionObject *)PyObject_MALLOC(sizeof(Py_ConnectionObject));
+    if (op == NULL)
+        return PyErr_NoMemory();
+    PyObject_INIT(op, &Py_ConnectionType);
+
+    op->cntxt = cntxt;
+
+    return (PyObject*) op;
+}
+
+void _connection_init(void)
+{
+    import_array();
+
+    if (PyType_Ready(&Py_ConnectionType) < 0)
+        return;
+}
diff --git a/monetdb5/extras/pyapi/connection.h 
b/monetdb5/extras/pyapi/connection.h
new file mode 100644
--- /dev/null
+++ b/monetdb5/extras/pyapi/connection.h
@@ -0,0 +1,48 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0.  If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Copyright 2008-2015 MonetDB B.V.
+ */
+
+/*
+ * M. Raasveldt
+ * 
+ */
+
+#ifndef _LOOPBACK_QUERY_
+#define _LOOPBACK_QUERY_
+
+#undef _GNU_SOURCE
+#undef _XOPEN_SOURCE
+#undef _POSIX_C_SOURCE
+#include <Python.h>
+
+#include "monetdb_config.h"
+#include "mal.h"
+#include "mal_exception.h"
+#include "mal_interpreter.h"
+#include "mal_stack.h"
+#include "mal_linker.h"
+#include "gdk_utils.h"
+#include "gdk.h"
+#include "sql_catalog.h"
+
+extern PyTypeObject *_connection_type;
+
+typedef struct {
+    PyObject_HEAD
+    Client cntxt;
+} Py_ConnectionObject;
+
+PyAPI_DATA(PyTypeObject) Py_ConnectionType;
+
+#define Py_Connection_Check(op) (Py_TYPE(op) == &Py_ConnectionType)
+#define Py_Connection_CheckExact(op) (Py_TYPE(op) == &Py_ConnectionType)
+
+PyObject *Py_Connection_Create(Client cntxt);
+
+void _connection_init(void);
+
+#endif /* _LOOPBACK_QUERY_ */
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to