Exemplo n.º 1
0
ONPHP_METHOD(ExtractPart, toDialectString)
{
	zval *dialect, *what, *from, *whatString, *fromString;
	smart_str string = {0};
	
	ONPHP_GET_ARGS("O", &dialect, onphp_ce_Dialect);
	
	what = ONPHP_READ_PROPERTY(getThis(), "what");
	from = ONPHP_READ_PROPERTY(getThis(), "from");
	
	ONPHP_CALL_METHOD_0(what, "tostring", &whatString);
	
	ONPHP_CALL_METHOD_1_NORET(from, "todialectstring", &fromString, dialect);
	
	if (EG(exception)) {
		ZVAL_FREE(whatString);
		return;
	}
	
	smart_str_appendl(&string, "EXTRACT(", 8);
	onphp_append_zval_to_smart_string(&string, whatString);
	smart_str_appendl(&string, " FROM ", 6);
	onphp_append_zval_to_smart_string(&string, fromString);
	smart_str_appendc(&string, ')');
	smart_str_0(&string);
	
	zval_ptr_dtor(&whatString);
	zval_ptr_dtor(&fromString);
	
	RETURN_STRINGL(string.c, string.len, 0);
}
Exemplo n.º 2
0
ONPHP_METHOD(Ternary, decide)
{
	zval
		*true,
		*false,
		*null,
		*trinity = ONPHP_READ_PROPERTY(getThis(), "trinity");
	
	ONPHP_GET_ARGS("zz|z", &true, &false, &null);
	
	if (Z_TYPE_P(trinity) == IS_BOOL) {
		if (zval_is_true(trinity)) {
			RETURN_ZVAL(true, 1, 0);
		} else {
			RETURN_ZVAL(false, 1, 0);
		}
	} else if (Z_TYPE_P(trinity) == IS_NULL) {
		if (ZEND_NUM_ARGS() == 3) {
			RETURN_ZVAL(null, 1, 0);
		} else {
			RETURN_NULL();
		}
	}
	
	ONPHP_THROW(WrongStateException, NULL);
}
Exemplo n.º 3
0
ONPHP_METHOD(Ternary, create)
{
	zval *object, *value;
	
	if (ZEND_NUM_ARGS() == 1) {
		ONPHP_GET_ARGS("z", &value);
	} else {
		ALLOC_INIT_ZVAL(value);
		ZVAL_NULL(value);
	}
	
	ONPHP_MAKE_OBJECT(Ternary, object);
	
	ONPHP_TERNARY_SET_VALUE(object);
	
out:
	if (ZEND_NUM_ARGS() != 1) {
		ZVAL_FREE(value);
	}
	
	if (EG(exception)) {
		zval_ptr_dtor(&object);
		return;
	}
	
	RETURN_ZVAL(object, 1, 1);
}
Exemplo n.º 4
0
ONPHP_METHOD(DBField, setTable)
{
	zval *table;
	
	if (Z_TYPE_P(ONPHP_READ_PROPERTY(getThis(), "table")) != IS_NULL) {
		ONPHP_THROW(
			WrongStateException,
			"you should not override setted table"
		);
	}
	
	ONPHP_GET_ARGS("z", &table);
	
	if (!ONPHP_INSTANCEOF(table, DialectString)) {
		zval *from_table;
		
		ONPHP_MAKE_FOREIGN_OBJECT("FromTable", from_table);
		
		ONPHP_CALL_METHOD_1_NORET(from_table, "__construct", NULL, table);
		
		if (EG(exception)) {
			ZVAL_FREE(from_table);
			return;
		}
		
		ONPHP_UPDATE_PROPERTY(getThis(), "table", from_table);
		
		zval_ptr_dtor(&from_table);
	} else {
		ONPHP_UPDATE_PROPERTY(getThis(), "table", table);
	}
	
	RETURN_THIS;
}
Exemplo n.º 5
0
ONPHP_METHOD(DBValue, __construct)
{
	zval *value;
	
	ONPHP_GET_ARGS("z", &value);
	
	ONPHP_UPDATE_PROPERTY(getThis(), "value", value);
}
Exemplo n.º 6
0
ONPHP_METHOD(Joiner, toDialectString)
{
	zval
		*dialect,
		*from = ONPHP_READ_PROPERTY(getThis(), "from"),
		*table;
	zend_class_entry **cep;
	smart_str string = {0};
	unsigned int i = 0, count = zend_hash_num_elements(Z_ARRVAL_P(from));
	
	if (!count) {
		RETURN_NULL();
	} else {
		smart_str_appendl(&string, " FROM ", 6);
	}
	
	ONPHP_GET_ARGS("O", &dialect, onphp_ce_Dialect);
	
	ONPHP_FIND_FOREIGN_CLASS("SelectQuery", cep);
	
	for (i = 0; i < count; ++i) {
		zval *out;
		
		ONPHP_ARRAY_GET(from, i, table);
		
		if (i == 0) {
			/* nop */
		} else {
			if (ONPHP_INSTANCEOF(from, FromTable)) {
				zval *name;
				
				ONPHP_CALL_METHOD_0(table, "gettable", &name);
				
				if (instanceof_function(Z_OBJCE_P(table), *cep TSRMLS_CC)) {
					smart_str_appendl(&string, ", ", 2);
				} else {
					smart_str_appendc(&string, ' ');
				}
				
				zval_ptr_dtor(&name);
			} else {
				smart_str_appendc(&string, ' ');
			}
		}
		
		ONPHP_CALL_METHOD_1(table, "todialectstring", &out, dialect);
		
		onphp_append_zval_to_smart_string(&string, out);
		
		zval_ptr_dtor(&out);
	}
	
	smart_str_0(&string);
	
	RETURN_STRINGL(string.c, string.len, 0);
}
Exemplo n.º 7
0
ONPHP_METHOD(DBValue, create)
{
	zval *object, *value;
	
	ONPHP_GET_ARGS("z", &value);
	
	ONPHP_MAKE_OBJECT(DBValue, object);
	
	ONPHP_UPDATE_PROPERTY(object, "value", value);
	
	RETURN_ZVAL(object, 1, 1);
}
Exemplo n.º 8
0
ONPHP_METHOD(DBField, __construct)
{
	zval *field, *table;
	
	ONPHP_GET_ARGS("z|z", &field, &table);
	
	ONPHP_UPDATE_PROPERTY(getThis(), "field", field);
	
	if (ZEND_NUM_ARGS() == 2) {
		ONPHP_CALL_METHOD_1(getThis(), "settable", NULL, table);
	}
}
Exemplo n.º 9
0
ONPHP_METHOD(Joiner, from)
{
	zval *from, *fromList;
	
	ONPHP_GET_ARGS("O", &from, onphp_ce_FromTable);
	
	fromList = ONPHP_READ_PROPERTY(getThis(), "from");
	
	ONPHP_ARRAY_ADD(fromList, from);
	
	RETURN_THIS;
}
Exemplo n.º 10
0
ONPHP_METHOD(Joiner, hasJoinedTable)
{
	char *table;
	unsigned int tableLength;
	zval *tables;
	
	ONPHP_GET_ARGS("s", &table, &tableLength);
	
	tables = ONPHP_READ_PROPERTY(getThis(), "tables");
	
	RETURN_BOOL(ONPHP_ASSOC_ISSET(tables, table));
}
Exemplo n.º 11
0
ONPHP_METHOD(DBField, toDialectString)
{
	smart_str string = {0};
	zval *table, *field, *dialect, *cast, *quoted;
	
	ONPHP_GET_ARGS("O", &dialect, onphp_ce_Dialect);
	
	table = ONPHP_READ_PROPERTY(getThis(), "table");
	field = ONPHP_READ_PROPERTY(getThis(), "field");
	cast = ONPHP_READ_PROPERTY(getThis(), "cast");
	
	// either null or instance of DialectString
	if (Z_TYPE_P(table) == IS_OBJECT) {
		zval *tmp;
		
		ONPHP_CALL_METHOD_1(table, "todialectstring", &tmp, dialect);
		
		onphp_append_zval_to_smart_string(&string, tmp);
		zval_ptr_dtor(&tmp);
		smart_str_appendc(&string, '.');
	}
	
	ONPHP_CALL_METHOD_1(dialect, "quotefield", &quoted, field);
	
	onphp_append_zval_to_smart_string(&string, quoted);
	
	zval_ptr_dtor(&quoted);
	
	smart_str_0(&string);
	
	if (Z_STRLEN_P(cast)) {
		zval *tmp, *out;
		
		ALLOC_INIT_ZVAL(tmp);
		
		ZVAL_STRINGL(tmp, string.c, string.len, 1);
		
		ONPHP_CALL_METHOD_2_NORET(dialect, "tocasted", &out, tmp, cast);
		
		ZVAL_FREE(tmp);
		smart_str_free(&string);
		
		if (EG(exception)) {
			return;
		}
		
		RETURN_ZVAL(out, 1, 1);
	}
	
	RETURN_STRINGL(string.c, string.len, 0);
}
Exemplo n.º 12
0
ONPHP_METHOD(Ternary, spawn)
{
	zval *value, *true, *false, *null, *result, *out;
	
	ONPHP_GET_ARGS("zzz|z", &value, &true, &false, &null);
	
	ALLOC_INIT_ZVAL(result);
	
	ONPHP_MAKE_OBJECT(Ternary, out);
	
	if (ZEND_NUM_ARGS() == 3) {
		ALLOC_INIT_ZVAL(null);
		ZVAL_NULL(null);
	}
	
	if (
		is_identical_function(result, value, true TSRMLS_CC)
		&& (zval_is_true(result))
	) {
		ONPHP_UPDATE_PROPERTY_BOOL(out, "trinity", 1);
	} else if (
		is_identical_function(result, value, false TSRMLS_CC)
		&& (zval_is_true(result))
	) {
		ONPHP_UPDATE_PROPERTY_BOOL(out, "trinity", 0);
	} else if (
		(
			is_identical_function(result, value, null TSRMLS_CC)
			&& (zval_is_true(result))
		) || (
			Z_TYPE_P(null) == IS_NULL
		)
	) {
		ONPHP_UPDATE_PROPERTY_NULL(out, "trinity");
	} else {
		ONPHP_THROW_NORET(WrongArgumentException, "failed to spawn Ternary");
	}
	
	ZVAL_FREE(result);
	
	if (ZEND_NUM_ARGS() == 3) {
		ZVAL_FREE(null);
	}
	
	if (EG(exception)) {
		return;
	}
	
	RETURN_ZVAL(out, 1, 1);
}
Exemplo n.º 13
0
ONPHP_METHOD(DBField, create)
{
	zval *field, *table, *object;
	
	ONPHP_GET_ARGS("z|z", &field, &table);
	
	ONPHP_MAKE_OBJECT(DBField, object);
	
	ONPHP_UPDATE_PROPERTY(object, "field", field);
	
	if (ZEND_NUM_ARGS() == 2) {
		ONPHP_CALL_METHOD_1(object, "settable", NULL, table);
	}
	
	RETURN_ZVAL(object, 1, 1);
}
Exemplo n.º 14
0
ONPHP_METHOD(DBBinary, create)
{
	zval *object, *value;
	
	ONPHP_GET_ARGS("z", &value);
	
	ONPHP_MAKE_OBJECT(DBBinary, object);
	
	ONPHP_CALL_PARENT_1_NORET(object, "__construct", NULL, value);
	
	if (EG(exception)) {
		ZVAL_FREE(object);
		return;
	}
	
	RETURN_ZVAL(object, 1, 1);
}
Exemplo n.º 15
0
ONPHP_METHOD(ExtractPart, create)
{
	zval *object, *what, *from;
	
	ONPHP_GET_ARGS("zz", &what, &from);
	
	ONPHP_MAKE_OBJECT(ExtractPart, object);
	
	ONPHP_CALL_METHOD_2_NORET(object, "__construct", NULL, what, from);
	
	if (EG(exception)) {
		ZVAL_FREE(object);
		return;
	}
	
	RETURN_ZVAL(object, 1, 1);
}
Exemplo n.º 16
0
ONPHP_METHOD(QuerySkeleton, where)
{
	zval
		*exp,
		*logic,
		*where = ONPHP_READ_PROPERTY(getThis(), "where");
	
	if (Z_TYPE_P(where) != IS_ARRAY) {
		ONPHP_THROW(WrongStateException, NULL);
	}
	
	zend_bool where_not_empty = (
		(Z_TYPE_P(where) == IS_ARRAY)
		&& (zend_hash_num_elements(Z_ARRVAL_P(where)) > 0)
	);
	
	ONPHP_GET_ARGS("O|z", &exp, onphp_ce_LogicalObject, &logic);
	
	if (
		(ZEND_NUM_ARGS() == 1)
		&& where_not_empty
	) {
		ONPHP_THROW(
			WrongArgumentException,
			"you have to specify expression logic"
		);
	} else {
		zval *whereLogic = ONPHP_READ_PROPERTY(getThis(), "whereLogic");
		
		if (Z_TYPE_P(whereLogic) != IS_ARRAY) {
			ONPHP_THROW(WrongStateException, NULL);
		}
		
		if (!where_not_empty || (ZEND_NUM_ARGS() == 1)) {
			add_next_index_null(whereLogic);
		} else {
			ONPHP_ARRAY_ADD(whereLogic, logic);
		}
		
		ONPHP_ARRAY_ADD(where, exp);
	}
	
	RETURN_THIS;
}
Exemplo n.º 17
0
ONPHP_METHOD(DBValue, toDialectString)
{
	zval *dialect, *cast, *value, *out, *result;
	
	ONPHP_GET_ARGS("O", &dialect, onphp_ce_Dialect);
	
	value = ONPHP_READ_PROPERTY(getThis(), "value");
	
	ONPHP_CALL_METHOD_1(dialect, "quotevalue", &out, value);
	
	cast = ONPHP_READ_PROPERTY(getThis(), "cast");
	
	if (Z_STRLEN_P(cast)) {
		ONPHP_CALL_METHOD_2(dialect, "tocasted", &result, out, cast);
		zval_ptr_dtor(&out);
		RETURN_ZVAL(result, 1, 1);
	} else {
		RETURN_ZVAL(out, 1, 1);
	}
}
Exemplo n.º 18
0
ONPHP_METHOD(DBBinary, toDialectString)
{
	zval *dialect, *value, *quoted;
	smart_str string = {0};
	
	ONPHP_GET_ARGS("O", &dialect, onphp_ce_Dialect);
	
	ONPHP_CALL_METHOD_0(getThis(), "getvalue", &value);
	
	ONPHP_CALL_METHOD_1(dialect, "quotebinary", &quoted, value);
	
	zval_ptr_dtor(&value);
	
	smart_str_appendc(&string, '\'');
	onphp_append_zval_to_smart_string(&string, quoted);
	zval_ptr_dtor(&quoted);
	smart_str_appendc(&string, '\'');
	smart_str_0(&string);
	
	RETURN_STRINGL(string.c, string.len, 0);
}
Exemplo n.º 19
0
ONPHP_METHOD(ExtractPart, toMapped)
{
	zval *dao, *query, *what, *from, *atom;
	
	ONPHP_GET_ARGS("oo", &dao, &query);
		
	what = ONPHP_READ_PROPERTY(getThis(), "what");
	from = ONPHP_READ_PROPERTY(getThis(), "from");
	
	ONPHP_CALL_METHOD_2(dao, "guessatom", &atom, from, query);
	
	ONPHP_CALL_STATIC_2_NORET(ExtractPart, "create", &query, what, atom);
	
	zval_ptr_dtor(&atom);
	
	if (EG(exception)) {
		return;
	}
	
	RETURN_ZVAL(query, 1, 1);
}
Exemplo n.º 20
0
ONPHP_METHOD(ExtractPart, __construct)
{
	zval *what, *from, *fromField;
	zend_class_entry **cep;
	
	ONPHP_GET_ARGS("zz", &what, &from);
	
	if (ONPHP_INSTANCEOF(from, DialectString)) {
		if (
			!(
				ONPHP_INSTANCEOF(from, DBValue)
				|| ONPHP_INSTANCEOF(from, DBField)
			)
		) {
			ONPHP_THROW(WrongArgumentException, NULL);
		}
	}
	
	ONPHP_MAKE_OBJECT(DBField, fromField);
	
	ONPHP_CALL_METHOD_1_NORET(fromField, "__construct", NULL, from);
	
	if (EG(exception)) {
		ZVAL_FREE(fromField);
		return;
	}
	
	ONPHP_UPDATE_PROPERTY(getThis(), "from", fromField);
	
	zval_ptr_dtor(&fromField);
	
	ONPHP_FIND_FOREIGN_CLASS("DatePart", cep);
	
	if (
		!(
			(Z_TYPE_P(what) == IS_OBJECT)
			&& instanceof_function(Z_OBJCE_P(what), *cep TSRMLS_CC)
		)
	) {
		zval *whatPart;
		
		ALLOC_INIT_ZVAL(whatPart);
		object_init_ex(whatPart, *cep);
		Z_TYPE_P(whatPart) = IS_OBJECT;
		
		ONPHP_CALL_METHOD_1_NORET(whatPart, "__construct", NULL, what);
		
		if (EG(exception)) {
			ZVAL_FREE(whatPart);
			return;
		}
		
		ONPHP_UPDATE_PROPERTY(getThis(), "what", whatPart);
		
		zval_ptr_dtor(&whatPart);
	} else {
		ONPHP_UPDATE_PROPERTY(getThis(), "what", what);
		
		zval_ptr_dtor(&what);
	}
}
Exemplo n.º 21
0
ONPHP_METHOD(PrimitiveNumber, import)
{
    zval *scope, *result, *name, *value, *min, *max, *out;

    ONPHP_GET_ARGS("a", &scope);

    zend_call_method_with_1_params(
        &getThis(),
        onphp_ce_BasePrimitive,
        NULL,
        "import",
        &result,
        scope
    );

    if (EG(exception)) {
        zval_ptr_dtor(&result);
        return;
    }

    if (!ONPHP_CHECK_EMPTY(result)) {
        zval_ptr_dtor(&result);
        RETURN_NULL();
    }

    zval_ptr_dtor(&result);

    name = ONPHP_READ_PROPERTY(getThis(), "name");

    ONPHP_ASSOC_GET(scope, Z_STRVAL_P(name), value);

    ONPHP_CALL_METHOD_1_NORET(getThis(), "checknumber", NULL, value);

    if (EG(exception)) {
        zend_clear_exception(TSRMLS_C);
        RETURN_FALSE;
    }

    ONPHP_CALL_METHOD_1(getThis(), "castnumber", &out, value);

    ONPHP_CALL_METHOD_0(getThis(), "selffilter", NULL);

    if (
        (Z_TYPE_P(out) == IS_LONG)
        && (min = ONPHP_READ_PROPERTY(getThis(), "min"))
        && (max = ONPHP_READ_PROPERTY(getThis(), "max"))
        && !(
            (IS_NULL != Z_TYPE_P(min))
            && (Z_LVAL_P(out) < Z_LVAL_P(min))
        ) && !(
            (IS_NULL != Z_TYPE_P(max))
            && (Z_LVAL_P(out) > Z_LVAL_P(max))
        )
    ) {
        ONPHP_UPDATE_PROPERTY_LONG(getThis(), "value", Z_LVAL_P(out));

        RETVAL_TRUE;
    } else {
        RETVAL_FALSE;
    }

    zval_ptr_dtor(&out);
}
Exemplo n.º 22
0
ONPHP_METHOD(QuerySkeleton, toDialectString)
{
	zval *where;
	unsigned int array_count = 0;
	
	where = ONPHP_READ_PROPERTY(getThis(), "where");
	
	if (
		(Z_TYPE_P(where) == IS_ARRAY)
		&& (array_count = zend_hash_num_elements(Z_ARRVAL_P(where)))
		&& (array_count > 0)
	) {
		zval *exp, *out, *logic, *whereLogic, *dialect;
		unsigned int i, retval_len;
		char *retval;
		char output_logic = 0;
		smart_str clause = {0};
		
		ONPHP_GET_ARGS("O", &dialect, onphp_ce_Dialect);
		
		whereLogic = ONPHP_READ_PROPERTY(getThis(), "whereLogic");
		
		smart_str_appendl(&clause, " WHERE", 6);
		
		for (i = 0; i < array_count; ++i) {
			ONPHP_ARRAY_GET(where, i, exp);
			
			ONPHP_CALL_METHOD_1(exp, "todialectstring", &out, dialect);
			
			if (Z_STRLEN_P(out)) {
				
				ONPHP_ARRAY_GET(whereLogic, i, logic);
				
				if (EG(exception)) {
					zval_ptr_dtor(&out);
					return;
				}
				
				// can be null
				if (Z_TYPE_P(logic) == IS_STRING) {
					onphp_append_zval_to_smart_string(&clause, logic);
				}
				
				smart_str_appendc(&clause, ' ');
				
				onphp_append_zval_to_smart_string(&clause, out);
				
				smart_str_appendc(&clause, ' ');
				
				output_logic = 1;
			} else if (
				(output_logic == 0)
				&& ((i + 1) <= array_count)
				&& ONPHP_ARRAY_ISSET(whereLogic, i + 1)
			) {
				add_index_null(whereLogic, i + 1);
			}
			
			zval_ptr_dtor(&out);
		}
		
		retval = (char *) php_trim(clause.c, clause.len, " ", 1, NULL, 2 TSRMLS_CC);
		smart_str_0(&clause);
		smart_str_free(&clause);
		retval_len = strlen(retval);
		
		RETURN_STRINGL(retval, retval_len, 0);
	}
	
	RETURN_NULL();
}