pgsReferenceGen::pgsReferenceGen(pgsThread * app, const wxString & table, const wxString & column, const bool & sequence, const long & seed) : pgsObjectGen(seed), m_app(app), m_table(table), m_column(column), m_sequence(sequence) { // We need an empty symbol table for calling pgsExecute.eval(...) pgsVarMap vars; // Count the number of lines in the table pgsOperand result = pgsExecute(wxString() << wxT("SELECT count(*) FROM ") << m_table, 0, m_app).eval(vars); wxASSERT(result->is_record()); wxString value = result->value(); if (!value.IsEmpty()) { m_nb_rows = pgsMapm::pgs_str_mapm(result->number().value()); } else { m_nb_rows = 0; } wxLogScriptVerbose(wxT("REFGEN: Number of rows in %s: %s"), m_table.c_str(), pgsMapm::pgs_mapm_str(m_nb_rows).c_str()); // Create an integer generator with that number of lines m_randomizer = pgsRandomizer(pnew pgsIntegerGen(0, m_nb_rows - 1, is_sequence(), m_seed)); }
pgsOperand pgsGenReference::eval(pgsVarMap &vars) const { // Evaluate parameters pgsOperand table(m_table->eval(vars)); pgsOperand column(m_column->eval(vars)); pgsOperand sequence(m_sequence->eval(vars)); pgsOperand seed(m_seed->eval(vars)); // Check parameters and create the generator if (table->is_string() && !table->value().IsEmpty() && column->is_string() && !column->value().IsEmpty() && sequence->is_integer() && seed->is_integer()) { // Check wheter the table and the column do exist pgsOperand result = pgsExecute(wxString() << wxT("SELECT 1 FROM ") << table->value() << wxT(" WHERE ") << column->value() << wxT(" = ") << column->value(), 0, m_app).eval(vars); if (result->pgs_is_true()) { long aux_sequence, aux_seed; sequence->value().ToLong(&aux_sequence); seed->value().ToLong(&aux_seed); return pnew pgsGenerator(pgsVariable::pgsTString, pnew pgsReferenceGen(m_app, table->value(), column->value(), aux_sequence != 0, aux_seed)); } else { throw pgsParameterException(wxString() << value() << wxT(":\ntable/column does not exist")); } } else { // Deal with errors if (!table->is_string() || table->value().IsEmpty()) { throw pgsParameterException(wxString() << value() << wxT(":\ntable should be a non-empty string")); } else if (!column->is_string() || column->value().IsEmpty()) { throw pgsParameterException(wxString() << value() << wxT(":\ncolumn should be a non-empty string")); } else if (!sequence->is_integer()) { throw pgsParameterException(wxString() << value() << wxT(":\nsequence should be an integer")); } else { throw pgsParameterException(wxString() << value() << wxT(":\nseed should be an integer")); } } }
wxString pgsReferenceGen::random() { // We need an empty symbol table for calling pgsExecute.eval(...) pgsVarMap vars; // Choose one line pgsOperand result = pgsExecute(wxString() << wxT("SELECT ") << m_column << wxT(" FROM ") << m_table << wxT(" LIMIT 1 OFFSET ") << m_randomizer->random(), 0, m_app).eval(vars); wxASSERT(result->is_record()); // Return the result as a single value return dynamic_cast<const pgsRecord &>(*result).get(0, 0)->value(); }
pgsExpression *pgsExecute::clone() const { return pnew pgsExecute(*this); }