<commit_before>/* $Id$ */
/* Copyright (c) 2013 <NAME> <<EMAIL>> */
/* This file is part of DeforaOS Database libDatabase */
/* This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. */



#include <Python.h>
#include "Database.h"


/* libDatabase */
/* private */
/* constants */
static char const _libdatabase_database_name[] = "libDatabase::Database";


/* prototypes */
/* Database */
static PyObject * _libdatabase_database_new(PyObject * self, PyObject * args);
static void _libdatabase_database_delete(PyObject * self);

static PyObject * _libdatabase_database_get_last_id(PyObject * self,
		PyObject * args);

static PyObject * _libdatabase_database_query(PyObject * self, PyObject * args);


/* variables */
static PyMethodDef _libdatabase_methods[] =
{
	{ "database_new", _libdatabase_database_new, METH_VARARGS,
		"Instantiates a Database object." },
	{ "database_get_last_id", _libdatabase_database_get_last_id,
		METH_VARARGS, "Obtain the ID of the latest row inserted." },
	{ "database_query", _libdatabase_database_query, METH_VARARGS,
		"Perform a query on the Database object." },
	{ NULL, NULL, 0, NULL }
};


/* public */
/* prototypes */
PyMODINIT_FUNC init_libDatabase(void);


/* functions */
/* init_libDatabase */
PyMODINIT_FUNC init_libDatabase(void)
{
	Py_InitModule("_libDatabase", _libdatabase_methods);
}


/* private */
/* functions */
/* Database */
/* libdatabase_database_new */
static PyObject * _libdatabase_database_new(PyObject * self, PyObject * args)
{
	Database * database;
	char const * engine;
	char const * section;

	if(!PyArg_ParseTuple(args, "ss", &engine, &section))
		return NULL;
	/* FIXME obtain the Config object */
	if((database = database_new(engine, config_new(), section)) == NULL)
		return NULL;
	return PyCapsule_New(database, _libdatabase_database_name,
			_libdatabase_database_delete);
}


/* libdatabase_database_delete */
static void _libdatabase_database_delete(PyObject * self)
{
	Database * database;

	if((database = PyCapsule_GetPointer(self, _libdatabase_database_name))
			== NULL)
		return;
	database_delete(database);
}


/* libdatabase_database_get_last_id */
static PyObject * _libdatabase_database_get_last_id(PyObject * self,
		PyObject * args)
{
	Database * database;
	long long ret;

	if((database = PyCapsule_GetPointer(self, _libdatabase_database_name))
			== NULL)
		return NULL;
	if(!PyArg_ParseTuple(args, ""))
		return NULL;
	ret = database_get_last_id(database);
	return Py_BuildValue("L", ret);
}


/* libdatabase_database_query */
static PyObject * _libdatabase_database_query(PyObject * self, PyObject * args)
{
	Database * database;
	char const * query;
	int ret;

	if((database = PyCapsule_GetPointer(self, _libdatabase_database_name))
			== NULL)
		return NULL;
	if(!PyArg_ParseTuple(args, "s", &query))
		return NULL;
	/* FIXME implement the callbacks one way or another */
	ret = database_query(database, query, NULL, NULL);
	return Py_BuildValue("i", ret);
}<commit_msg>Give the Python binding a better chance to work<commit_after>/* $Id$ */
/* Copyright (c) 2013 <NAME> <<EMAIL>> */
/* This file is part of DeforaOS Database libDatabase */
/* This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. */



#include <Python.h>
#include "Database.h"


/* libDatabase */
/* private */
/* constants */
static char const _libdatabase_database_name[] = "libDatabase::Database";


/* prototypes */
/* Database */
static PyObject * _libdatabase_database_new(PyObject * self, PyObject * args);
static void _libdatabase_database_delete(PyObject * self);

static PyObject * _libdatabase_database_get_last_id(PyObject * self,
		PyObject * args);

static PyObject * _libdatabase_database_query(PyObject * self, PyObject * args);


/* variables */
static PyMethodDef _libdatabase_methods[] =
{
	{ "database_new", _libdatabase_database_new, METH_VARARGS,
		"Instantiates a Database object." },
	{ "database_get_last_id", _libdatabase_database_get_last_id,
		METH_VARARGS, "Obtain the ID of the latest row inserted." },
	{ "database_query", _libdatabase_database_query, METH_VARARGS,
		"Perform a query on the Database object." },
	{ NULL, NULL, 0, NULL }
};


/* public */
/* prototypes */
PyMODINIT_FUNC init_libDatabase(void);


/* functions */
/* init_libDatabase */
PyMODINIT_FUNC init_libDatabase(void)
{
	Py_InitModule("_libDatabase", _libdatabase_methods);
}


/* private */
/* functions */
/* Database */
/* libdatabase_database_new */
static PyObject * _libdatabase_database_new(PyObject * self, PyObject * args)
{
	Database * database;
	char const * engine;
	char const * section;

	if(!PyArg_ParseTuple(args, "ss", &engine, &section))
		return NULL;
	/* FIXME obtain the Config object */
	if((database = database_new(engine, config_new(), section)) == NULL)
		return NULL;
	return PyCapsule_New(database, _libdatabase_database_name,
			_libdatabase_database_delete);
}


/* libdatabase_database_delete */
static void _libdatabase_database_delete(PyObject * self)
{
	Database * database;

	if((database = PyCapsule_GetPointer(self, _libdatabase_database_name))
			== NULL)
		return;
	database_delete(database);
}


/* libdatabase_database_get_last_id */
static PyObject * _libdatabase_database_get_last_id(PyObject * self,
		PyObject * args)
{
	Database * database;
	long long ret;

	if(!PyArg_ParseTuple(args, "O", &self))
		return NULL;
	if((database = PyCapsule_GetPointer(self, _libdatabase_database_name))
			== NULL)
		return NULL;
	ret = database_get_last_id(database);
	return Py_BuildValue("L", ret);
}


/* libdatabase_database_query */
static PyObject * _libdatabase_database_query(PyObject * self, PyObject * args)
{
	Database * database;
	char const * query;
	int ret;

	if(!PyArg_ParseTuple(args, "Os", &self, &query))
		return NULL;
	if((database = PyCapsule_GetPointer(self, _libdatabase_database_name))
			== NULL)
		return NULL;
	/* FIXME implement the callbacks one way or another */
	ret = database_query(database, query, NULL, NULL);
	return Py_BuildValue("i", ret);
}
{'fraction_non_alphanumeric': 0.11020293122886134, 'fraction_numerical': 0.0016910935738444193, 'mean_word_length': 4.938912133891213, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2099347', 'n_tokens_mistral': 2221, 'n_tokens_neox': 2172, 'n_words': 633}