static PHP_METHOD(midgard_object_class, connect_default)
{
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	php_midgard_object_class_connect_default(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
static PHP_METHOD(midgard_object_class, has_metadata)
{
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	zval *zvalue;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zvalue) == FAILURE) {
		return;
	}

	const char *php_classname = NULL;

	if (Z_TYPE_P(zvalue) == IS_STRING) {
		php_classname = Z_STRVAL_P(zvalue);
	} else if (Z_TYPE_P(zvalue) == IS_OBJECT) {
		php_classname = Z_OBJCE_P(zvalue)->name;
	} else {
		php_error_docref(NULL TSRMLS_CC, E_WARNING, "argument should be object or string");
		return;
	}

	const gchar *g_classname = php_class_name_to_g_class_name(php_classname);
	MidgardObjectClass *klass = MIDGARD_OBJECT_GET_CLASS_BY_NAME(g_classname);

	if (!klass) {
		php_error(E_WARNING, "MidgardObjectClass not found");
		return;
	}

	RETURN_BOOL(midgard_reflector_object_has_metadata_class(g_classname));
}
static PHP_METHOD(midgard_object_class, factory)
{
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	char *class_name;
	int class_name_length;
	zval *zvalue = NULL;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z", &class_name, &class_name_length, &zvalue) == FAILURE)
		return;

	zend_class_entry **pce = NULL;
	if (zend_lookup_class_ex(class_name, class_name_length, 1, &pce TSRMLS_CC) == FAILURE) {
		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can not find %s class", class_name);
		return;
	}

	zend_class_entry *ce = *pce;

	object_init_ex(return_value, ce); /* Initialize new object for which QB has been created for */

	/* Call class constructor on given instance */
	if (zvalue == NULL) {
		zend_call_method_with_0_params(&return_value, ce, &ce->constructor, "__construct", NULL);
	} else {
		zend_call_method_with_1_params(&return_value, ce, &ce->constructor, "__construct", NULL, zvalue);
	}
}
Exemplo n.º 4
0
static PHP_METHOD(midgard_query_builder, add_constraint_with_property)
{
	RETVAL_FALSE;
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	char *name_a, *name_b, *op;
	int name_a_length, name_b_length, op_length;
	gboolean rv;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss",
				&name_a, &name_a_length,
				&op, &op_length,
				&name_b, &name_b_length) != SUCCESS)
	{
		return;
	}

	_GET_BUILDER_OBJECT;

	rv = midgard_query_builder_add_constraint_with_property(builder,
			(const gchar *)name_a, (const gchar *)op, (const gchar *)name_b);

	RETURN_BOOL(rv);
}
static PHP_METHOD(midgard_object_class, get_object_by_guid)
{
	RETVAL_FALSE;
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	char *guid;
	int guid_length;
	const gchar *type_name;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &guid, &guid_length) == FAILURE)
		return;

	MidgardObject *object = midgard_schema_object_factory_get_object_by_guid(mgd, guid);

	if (!object) {
		php_midgard_error_exception_throw(mgd TSRMLS_CC);
		return;
	}

	type_name = G_OBJECT_TYPE_NAME(G_OBJECT(object));
	zend_class_entry *ce = zend_fetch_class((gchar *)type_name, strlen(type_name), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);

	if (ce == NULL) {
		php_error(E_WARNING, "Can not find %s class", type_name);
		return;
	}

	php_midgard_gobject_new_with_gobject(return_value, ce, G_OBJECT(object), TRUE TSRMLS_CC);
}
Exemplo n.º 6
0
/* Object constructor */
static PHP_FUNCTION(php_midgard_view_constructor)
{
	RETVAL_FALSE;
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	zval *zval_object = getThis();
	GObject *gobject;

	gobject = __php_gobject_ptr(zval_object);

	if (!gobject) {
		if (zend_parse_parameters_none() == FAILURE)
			return;

		MidgardView *view = g_object_new(g_type_from_name(Z_OBJCE_P(zval_object)->name), NULL);

		if (!view) {
			php_midgard_error_exception_throw(mgd TSRMLS_CC);
			return;
		}

		MGD_PHP_SET_GOBJECT(zval_object, view);
	} else {
		// we already have gobject injected
	}
}
Exemplo n.º 7
0
static PHP_METHOD(midgard_query_builder, add_constraint)
{
	RETVAL_FALSE;
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	char *name, *op;
	long name_length, op_length;
	zval *value;
	zval *zval_object = getThis();
	zend_bool rv;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssz", &name, &name_length, &op, &op_length, &value) != SUCCESS) {
		return;
	}

	MidgardQueryBuilder *builder = MIDGARD_QUERY_BUILDER(__php_gobject_ptr(zval_object));

	GValue *gvalue = php_midgard_zval2gvalue(value TSRMLS_CC);

	if (gvalue == NULL)
		RETURN_FALSE;

	rv = midgard_query_builder_add_constraint(builder, name, op, gvalue);

	g_value_unset(gvalue);
	g_free(gvalue);

	RETURN_BOOL(rv);
}
Exemplo n.º 8
0
/* Object constructor */
static PHP_METHOD(midgard_query_builder, __construct)
{
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	char *classname;
	long classname_length;
	zval *zval_object = getThis();
	GObject *gobject;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &classname, &classname_length) == FAILURE) {
		return;
	}

	zend_class_entry *ce = zend_fetch_class(classname, classname_length, ZEND_FETCH_CLASS_AUTO TSRMLS_CC);

	if (ce == NULL) {
		php_error(E_WARNING, "Didn't find %s class", classname);
		php_midgard_error_exception_force_throw(mgd, MGD_ERR_INVALID_OBJECT TSRMLS_CC);
		return;
	}

	zend_class_entry *base_ce = php_midgard_get_baseclass_ptr(ce);
	const gchar *g_base_class_name = php_class_name_to_g_class_name(base_ce->name);

	GType classtype = g_type_from_name(g_base_class_name);

	if (!g_type_is_a(classtype, MIDGARD_TYPE_DBOBJECT)) {
		php_error(E_WARNING, "Expected %s derived class", g_type_name(MIDGARD_TYPE_DBOBJECT));
		php_midgard_error_exception_force_throw(mgd, MGD_ERR_INVALID_OBJECT TSRMLS_CC);
		return;
	}

	gobject = __php_gobject_ptr(zval_object);

	if (!gobject) {
		MidgardQueryBuilder *builder = midgard_query_builder_new(mgd, g_base_class_name);

		if (!builder) {
			php_midgard_error_exception_throw(mgd TSRMLS_CC);
			return;
		}

		MGD_PHP_SET_GOBJECT(zval_object, builder);
	} else {
		// we already have gobject injected
	}

	php_midgard_gobject *php_gobject = __php_objstore_object(zval_object);
	/* Set user defined class. We might need it when execute is invoked */
	php_gobject->user_ce = ce;
	php_gobject->user_class_name = (char *)ce->name;
}
Exemplo n.º 9
0
static PHP_METHOD(midgard_query_builder, count)
{
	RETVAL_FALSE;
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	if (zend_parse_parameters_none() == FAILURE) {
		return;
	}

	_GET_BUILDER_OBJECT;

	RETURN_LONG(midgard_query_builder_count(builder));
}
Exemplo n.º 10
0
static PHP_METHOD(midgard_query_builder, include_deleted)
{
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	RETVAL_FALSE;

	if (zend_parse_parameters_none() == FAILURE)
		return;

	_GET_BUILDER_OBJECT;

	midgard_query_builder_include_deleted(builder);

	RETURN_TRUE;
}
static PHP_METHOD(midgard_object_class, undelete)
{
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	char *guid;
	int guid_length;
	gboolean rv;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &guid, &guid_length)  == FAILURE) {
		return;
	}

	rv = midgard_schema_object_factory_object_undelete(mgd, (const gchar *)guid);
	RETURN_BOOL(rv);
}
Exemplo n.º 12
0
static PHP_METHOD(midgard_query_builder, end_group)
{
	RETVAL_FALSE;
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	gboolean rv;

	if (zend_parse_parameters_none() == FAILURE)
		return;

	_GET_BUILDER_OBJECT;

	rv = midgard_query_builder_end_group(builder);

	RETURN_BOOL(rv);
}
Exemplo n.º 13
0
static PHP_METHOD(midgard_query_builder, execute)
{
	RETVAL_FALSE;
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	if (zend_parse_parameters_none() == FAILURE)
		return;

	_GET_BUILDER_OBJECT;
	zend_class_entry *ce = php_gobject->user_ce;

	if (ce == NULL) {
		php_error(E_WARNING, "Query Builder instance not associated with any class");
		return;
	}

	guint i, n_objects;
	GObject **objects = midgard_query_builder_execute(builder, &n_objects);

	array_init(return_value);

	if (!objects)
		return;

	/* TODO: Should use iterator, instead, and create objects lazily */
	/* Initialize zend objects for the same class which was used to initialize Query Builder */
	for (i = 0; i < n_objects; i++) {
		GObject *gobject = objects[i];
		zval *zobject;

		/* TODO: Simplify code below. If possible. */
		MAKE_STD_ZVAL(zobject);
		object_init_ex(zobject, ce); /* Initialize new object for which QB has been created for */
		MGD_PHP_SET_GOBJECT(zobject, gobject); // inject our gobject
		zend_call_method_with_0_params(&zobject, ce, &ce->constructor, "__construct", NULL); /* Call class constructor on given instance */

		zend_hash_next_index_insert(HASH_OF(return_value), &zobject, sizeof(zval *), NULL);
	}

	if (objects)
		g_free(objects);
}
Exemplo n.º 14
0
static PHP_METHOD(midgard_query_builder, add_order)
{
	RETVAL_FALSE;
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	char *field, *order = "ASC";
	int field_length, order_length;
	gboolean rv;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &field, &field_length, &order, &order_length) != SUCCESS) {
		return;
	}

	_GET_BUILDER_OBJECT;

	rv = midgard_query_builder_add_order(builder, field, order);

	RETURN_BOOL(rv);
}
Exemplo n.º 15
0
static PHP_METHOD(midgard_query_builder, begin_group)
{
	RETVAL_FALSE;
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	char *type = NULL;
	int type_length;
	gboolean rv;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &type, &type_length) != SUCCESS) {
		return;
	}

	_GET_BUILDER_OBJECT;

	rv = midgard_query_builder_begin_group(builder, type);

	RETURN_BOOL(rv);
}
Exemplo n.º 16
0
static PHP_METHOD(midgard_query_builder, execute)
{
	RETVAL_FALSE;
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	if (zend_parse_parameters_none() == FAILURE)
		return;

	_GET_BUILDER_OBJECT;
	zend_class_entry *ce = php_gobject->user_ce;

	if (ce == NULL) {
		php_error(E_WARNING, "Query Builder instance not associated with any class");
		return;
	}

	guint i, n_objects;
	GObject **objects = midgard_query_builder_execute(builder, &n_objects);

	array_init(return_value);

	if (!objects)
		return;

	/* TODO: Should use iterator, instead, and create objects lazily */
	for (i = 0; i < n_objects; i++) {
		GObject *gobject = objects[i];

		zval *zobject;
		MAKE_STD_ZVAL(zobject);

		php_midgard_gobject_new_with_gobject(zobject, ce, gobject, TRUE TSRMLS_CC);

		zend_hash_next_index_insert(HASH_OF(return_value), &zobject, sizeof(zval *), NULL);
	}

	if (objects)
		g_free(objects);
}
Exemplo n.º 17
0
static PHP_METHOD(midgard_query_builder, set_offset)
{
	RETVAL_FALSE;
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	long offset;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &offset) != SUCCESS) {
		return;
	}

	_GET_BUILDER_OBJECT;

	if (offset < 0) {
		php_error(E_WARNING, "Ingoring a negative query offset");
		RETURN_FALSE;
	}

	midgard_query_builder_set_offset(builder, offset);
	RETURN_TRUE;
}
static PHP_METHOD(midgard_object_class, get_schema_value)
{
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	zval *zvalue;
	char *name;
	int name_length;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs", &zvalue, &name, &name_length) == FAILURE) {
		return;
	}

	const gchar *classname = NULL;

	if (Z_TYPE_P(zvalue) == IS_STRING) {
		classname = (const gchar *) Z_STRVAL_P(zvalue);
	} else if (Z_TYPE_P(zvalue) == IS_OBJECT) {
		classname = (const gchar *) Z_OBJCE_P(zvalue)->name;
	} else {
		php_error_docref(NULL TSRMLS_CC, E_WARNING, "first argument should be object or string");
		return;
	}

	MidgardObjectClass *klass = MIDGARD_OBJECT_GET_CLASS_BY_NAME(classname);

	if (!klass) {
		php_error(E_WARNING, "MidgardObjectClass not found");
		return;
	}

	const gchar *schema_value = midgard_reflector_object_get_schema_value(classname, (const gchar *)name);

	if (!schema_value)
		RETURN_NULL();

	RETURN_STRING((char *)schema_value, 1);
}
Exemplo n.º 19
0
static PHP_METHOD(midgard_query_builder, set_limit)
{
	RETVAL_FALSE;
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	long limit;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &limit) != SUCCESS) {
		return;
	}

	_GET_BUILDER_OBJECT;

	/* TODO, check if limit check can be ignored */
	if (limit < 0) {
		php_error(E_WARNING, "Ignoring a negative query limit");
		RETURN_FALSE;
	}

	midgard_query_builder_set_limit(builder, limit);
	RETURN_TRUE;
}
static PHP_METHOD(midgard_object_class, get_property_parent)
{
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	zval *zvalue;
	MidgardObjectClass *klass = NULL;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zvalue) == FAILURE) {
		return;
	}

	const gchar *classname = NULL;

	if (Z_TYPE_P(zvalue) == IS_STRING) {
		classname = (const gchar *)Z_STRVAL_P(zvalue);
	} else if (Z_TYPE_P(zvalue) == IS_OBJECT) {
		classname = (const gchar *)Z_OBJCE_P(zvalue)->name;
	} else {
		php_error_docref(NULL TSRMLS_CC, E_WARNING, "argument should be object or string");
		return;
	}

	klass = MIDGARD_OBJECT_GET_CLASS_BY_NAME(classname);

	if (!klass) {
		php_error(E_WARNING, "MidgardObjectClass not found");
		return;
	}

	const gchar *property_parent = midgard_reflector_object_get_property_parent(classname);

	if (!property_parent)
		RETURN_NULL();

	RETURN_STRING((gchar *)property_parent, 1);
}
Exemplo n.º 21
0
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "php_midgard.h"
#include "php_midgard_gobject.h"

#include "php_midgard__helpers.h"

static zend_class_entry *php_midgard_replicator_class;

static PHP_METHOD(midgard_replicator, export)
{
    RETVAL_FALSE;
    MidgardConnection *mgd = mgd_handle(TSRMLS_C);
    CHECK_MGD(mgd);

    zval *zobject;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &zobject) == FAILURE)
        return;

    gboolean exported = midgard_replicator_export(__midgard_dbobject_get_ptr(zobject));
    RETURN_BOOL(exported);
}

ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_replicator_export, 0, 0, 1)
ZEND_ARG_OBJ_INFO(0, object, midgard_dbobject, 0)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_object_class, factory)
{
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	char *class_name;
	int class_name_length;
	zval *zvalue = NULL;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z", &class_name, &class_name_length, &zvalue) == FAILURE)
		return;

	zend_class_entry **pce = NULL;
#if PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 3
	if (zend_lookup_class_ex(class_name, class_name_length, NULL, 1, &pce TSRMLS_CC) == FAILURE) {
#else
	if (zend_lookup_class_ex(class_name, class_name_length, 1, &pce TSRMLS_CC) == FAILURE) {
#endif
		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can not find %s class", class_name);
		return;
	}

	zend_class_entry *ce = *pce;

	object_init_ex(return_value, ce); /* Initialize new object for which QB has been created for */

	/* Call class constructor on given instance */
	if (zvalue == NULL) {
		zend_call_method_with_0_params(&return_value, ce, &ce->constructor, "__construct", NULL);
	} else {
		zend_call_method_with_1_params(&return_value, ce, &ce->constructor, "__construct", NULL, zvalue);
	}
}

ZEND_BEGIN_ARG_INFO_EX(arginfo_midgard_object_class_factory, 0, 0, 1)
	ZEND_ARG_INFO(0, classname)
	ZEND_ARG_INFO(0, id)
ZEND_END_ARG_INFO()

static PHP_METHOD(midgard_object_class, get_object_by_guid)
{
	RETVAL_FALSE;
	MidgardConnection *mgd = mgd_handle(TSRMLS_C);
	CHECK_MGD(mgd);

	char *guid;
	int guid_length;
	const gchar *type_name;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &guid, &guid_length) == FAILURE)
		return;

	MidgardObject *object = midgard_schema_object_factory_get_object_by_guid(mgd, guid);

	if (!object) {
		php_midgard_error_exception_throw(mgd TSRMLS_CC);
		return;
	}

	type_name = G_OBJECT_TYPE_NAME(G_OBJECT(object));
	zend_class_entry *ce = zend_fetch_class((gchar *)type_name, strlen(type_name), ZEND_FETCH_CLASS_AUTO TSRMLS_CC);

	if (ce == NULL) {
		php_error(E_WARNING, "Can not find %s class", type_name);
		return;
	}

	php_midgard_gobject_new_with_gobject(return_value, ce, G_OBJECT(object), TRUE TSRMLS_CC);
}