CompiledExpr FieldExpr::compile() { if (this->protected_data->internal_type == 0) { std::string compiled_value( get_field_name() + " == '" + this->protected_data->value + "'"); return CompiledExpr(get_compiled_expr_type(), compiled_value.c_str()); } else if (this->protected_data->internal_type == 1) { return CompiledExpr(get_compiled_expr_type(), this->protected_data->function->compile(get_field_name().c_str())); } }
std::string QueryCompiler::compile(Query& query_to_compile, std::string result_var_name) { ConnectiveExpr* expression_to_compile = query_to_compile.get_expr(); // The final script. std::string script; // Keeps first lines of the function (mainly, result variables initialization). std::ostringstream function_header_stream; // Keeps condition of tags. std::ostringstream tags_stream; // Keeps condition of other fields. std::ostringstream fields_stream; // Keeps sequence of `$r'. std::ostringstream result; compile_states::States current_state = compile_states::GROUP_AFTER_GROUP; // Compiling expressions to CompiledExprs. std::list<CompiledExpr> compiled_list = expression_to_compile->compile(); // Iterating over CompiledExprs to create a Jx9 script. std::list<CompiledExpr>::iterator iterator = compiled_list.begin(); CompiledExpr* first_element = &*iterator; ++iterator; CompiledExpr* second_element = &*iterator; // Initializing data. CompileStateData data(first_element, second_element, &function_header_stream, &tags_stream, &fields_stream, &result); ++iterator; for (;iterator != compiled_list.end(); ++iterator) { current_state = state_table[current_state](data); data.current_expr = &*data.next_expr; data.next_expr = &*iterator; } // Last two elements in the list. current_state = state_table[current_state](data); // The last element. data.current_expr = &*data.next_expr; CompiledExpr nope_element = CompiledExpr(compiled_expr::NOPE, ""); data.next_expr = &nope_element; current_state = state_table[current_state](data); // Creating filter function. script += "$filter_func = function($record) { "; // Header of the function. if (function_header_stream.tellp() != 0) { script += function_header_stream.str(); } // Appending tags conditions, if there's any. if (tags_stream.tellp() != 0) { script += " " + tags_stream.str() +" }"; } script += " " + fields_stream.str(); script += " return " + result.str() + ";"; script += " }; "; // Applying filter on database's records, and putting the result in the // result variable. script += "$" + result_var_name + " = db_fetch_all('files', $filter_func);"; return script; }