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); }
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", "ed, field); onphp_append_zval_to_smart_string(&string, quoted); zval_ptr_dtor("ed); 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); }
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); }
ONPHP_METHOD(PlainForm, clean) { zval **prm, *primitives = ONPHP_READ_PROPERTY(getThis(), "primitives"); ONPHP_FOREACH(primitives, prm) { ONPHP_CALL_METHOD_0(*prm, "clean", NULL); }
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; }
ONPHP_METHOD(Ternary, isFalse) { zval *trinity = ONPHP_READ_PROPERTY(getThis(), "trinity"); RETURN_BOOL( (Z_TYPE_P(trinity) == IS_BOOL) && !zval_is_true(trinity) ); }
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; }
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); }
ONPHP_METHOD(Form, getErrors) { zval *out, *errors = ONPHP_READ_PROPERTY(getThis(), "errors"), *violated = ONPHP_READ_PROPERTY(getThis(), "violated"); zend_call_method_with_2_params( NULL, NULL, NULL, "array_merge", &out, errors, violated ); RETURN_ZVAL(out, 1, 1); }
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); } }
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)); }
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; }
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); }
ONPHP_METHOD(Form, getInnerErrors) { zval *result, *primitives = ONPHP_READ_PROPERTY(getThis(), "primitives"), **prm; ONPHP_CALL_METHOD_0(getThis(), "geterrors", &result); ONPHP_MAKE_ARRAY(result); ONPHP_FOREACH(primitives, prm) { zend_class_entry **pfl, **pf; zval *value; ONPHP_CALL_METHOD_0_NORET(*prm, "getvalue", &value); if (EG(exception)) { ZVAL_FREE(primitives); return; } ONPHP_FIND_FOREIGN_CLASS("PrimitiveFormsList", pfl); ONPHP_FIND_FOREIGN_CLASS("PrimitiveForm", pf); if (ONPHP_CHECK_EMPTY(value)) { if ( instanceof_function(Z_OBJCE_PP(prm), *pfl TSRMLS_CC) || instanceof_function(Z_OBJCE_PP(prm), *pf TSRMLS_CC) ) { zval *name, *errors; ONPHP_CALL_METHOD_0_NORET(*prm, "getinnererrors", &errors); if (EG(exception)) { ZVAL_FREE(primitives); return; } ONPHP_CALL_METHOD_0(*prm, "getname", &name); if (zend_hash_num_elements(Z_ARRVAL_P(errors)) > 0) { ONPHP_ASSOC_SET(result, Z_STRVAL_P(name), errors); } else { ONPHP_ASSOC_UNSET(result, Z_STRVAL_P(name)); } } } }
ONPHP_METHOD(Ternary, toString) { zval *trinity = ONPHP_READ_PROPERTY(getThis(), "trinity"); if (Z_TYPE_P(trinity) == IS_BOOL) { if (zval_is_true(trinity)) { RETURN_STRINGL("true", 4, 1); } else { RETURN_STRINGL("false", 5, 1); } } else if (Z_TYPE_P(trinity) == IS_NULL) { RETURN_STRINGL("null", 4, 1); } ONPHP_THROW(WrongStateException, NULL); }
ONPHP_METHOD(Joiner, getLastTable) { zval *from = ONPHP_READ_PROPERTY(getThis(), "from"); unsigned int count = zend_hash_num_elements(Z_ARRVAL_P(from)); if (count > 0) { zval *name, *table; ONPHP_ARRAY_GET(from, --count, table); ONPHP_CALL_METHOD_1(table, "gettable", &name, table); RETURN_ZVAL(name, 1, 1); } RETURN_NULL(); }
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); }
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(); }
ONPHP_METHOD(Joiner, getTablesCount) { zval *tables = ONPHP_READ_PROPERTY(getThis(), "tables"); RETURN_LONG(zend_hash_num_elements(Z_ARRVAL_P(tables))); }
ONPHP_METHOD(Ternary, isNull) { RETURN_BOOL(Z_TYPE_P(ONPHP_READ_PROPERTY(getThis(), "trinity")) == IS_NULL); }