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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
//////////////////////////////////////////////////////////////////////////
//
// pgScript - PostgreSQL Tools
//
// Copyright (C) 2002 - 2012, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////////////////
#include "pgAdmin3.h"
#include "pgscript/expressions/pgsGenReference.h"
#include "pgscript/exceptions/pgsParameterException.h"
#include "pgscript/generators/pgsReferenceGen.h"
#include "pgscript/expressions/pgsExecute.h"
#include "pgscript/objects/pgsGenerator.h"
pgsGenReference::pgsGenReference(const pgsExpression *table, const pgsExpression *column,
const pgsExpression *sequence, const pgsExpression *seed, pgsThread *app) :
pgsExpression(), m_table(table), m_column(column), m_sequence(sequence),
m_seed(seed), m_app(app)
{
}
pgsGenReference::~pgsGenReference()
{
pdelete(m_table);
pdelete(m_column);
pdelete(m_sequence);
pdelete(m_seed);
}
pgsExpression *pgsGenReference::clone() const
{
return pnew pgsGenReference(*this);
}
pgsGenReference::pgsGenReference(const pgsGenReference &that) :
pgsExpression(that)
{
m_table = that.m_table->clone();
m_column = that.m_column->clone();
m_sequence = that.m_sequence->clone();
m_seed = that.m_seed->clone();
m_app = that.m_app;
}
pgsGenReference &pgsGenReference::operator =(const pgsGenReference &that)
{
if (this != &that)
{
pgsExpression::operator=(that);
pdelete(m_table);
pdelete(m_column);
pdelete(m_sequence);
pdelete(m_seed);
m_table = that.m_table->clone();
m_column = that.m_column->clone();
m_sequence = that.m_sequence->clone();
m_seed = that.m_seed->clone();
m_app = that.m_app;
}
return (*this);
}
wxString pgsGenReference::value() const
{
return wxString() << wxT("reference[ table = ") << m_table->value()
<< wxT(" column = ") << m_column->value() << wxT(" sequence = ")
<< m_sequence->value() << wxT(" seed = ") << m_seed->value() << wxT(" ]");
}
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"));
}
}
}
|