1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
//////////////////////////////////////////////////////////////////////////
//
// pgScript - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////////////////
#include "pgAdmin3.h"
#include "pgscript/generators/pgsReferenceGen.h"
#include "pgscript/expressions/pgsExecute.h"
#include "pgscript/objects/pgsNumber.h"
#include "pgscript/objects/pgsRecord.h"
#include "pgscript/utilities/pgsThread.h"
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));
}
bool pgsReferenceGen::is_sequence() const
{
return m_sequence;
}
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();
}
pgsReferenceGen::~pgsReferenceGen()
{
}
pgsReferenceGen *pgsReferenceGen::clone()
{
return pnew pgsReferenceGen(*this);
}
|