File: pgsGenString.cpp

package info (click to toggle)
pgadmin3 1.14.2-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 69,368 kB
  • sloc: cpp: 163,662; sh: 4,339; ansic: 1,636; pascal: 1,120; yacc: 927; makefile: 733; lex: 421; perl: 40
file content (115 lines) | stat: -rw-r--r-- 3,186 bytes parent folder | download
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
//////////////////////////////////////////////////////////////////////////
//
// 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/pgsGenString.h"

#include "pgscript/exceptions/pgsParameterException.h"
#include "pgscript/generators/pgsStringGen.h"
#include "pgscript/objects/pgsGenerator.h"

pgsGenString::pgsGenString(const pgsExpression *min, const pgsExpression *max,
                           const pgsExpression *nb_words, const pgsExpression *seed) :
	pgsExpression(), m_min(min), m_max(max), m_nb_words(nb_words), m_seed(seed)
{

}

pgsGenString::~pgsGenString()
{
	pdelete(m_min);
	pdelete(m_max);
	pdelete(m_nb_words);
	pdelete(m_seed);
}

pgsExpression *pgsGenString::clone() const
{
	return pnew pgsGenString(*this);
}

pgsGenString::pgsGenString(const pgsGenString &that) :
	pgsExpression(that)
{
	m_min = that.m_min->clone();
	m_max = that.m_max->clone();
	m_nb_words = that.m_nb_words->clone();
	m_seed = that.m_seed->clone();
}

pgsGenString &pgsGenString::operator =(const pgsGenString &that)
{
	if (this != &that)
	{
		pgsExpression::operator=(that);
		pdelete(m_min);
		pdelete(m_max);
		pdelete(m_nb_words);
		pdelete(m_seed);
		m_min = that.m_min->clone();
		m_max = that.m_max->clone();
		m_nb_words = that.m_nb_words->clone();
		m_seed = that.m_seed->clone();
	}
	return (*this);
}

wxString pgsGenString::value() const
{
	return wxString() << wxT("string[ min = ") << m_min->value() << wxT(" max = ")
	       << m_max->value() << wxT(" nb_words = ") << m_nb_words->value()
	       << wxT(" seed = ") << m_seed->value() << wxT(" ]");
}

pgsOperand pgsGenString::eval(pgsVarMap &vars) const
{
	// Evaluate parameters
	pgsOperand min(m_min->eval(vars));
	pgsOperand max(m_max->eval(vars));
	pgsOperand nb_words(m_nb_words->eval(vars));
	pgsOperand seed(m_seed->eval(vars));

	// Check parameters and create the generator
	if (min->is_integer() && max->is_integer() && nb_words->is_integer()
	        && seed->is_integer())
	{
		long aux_min, aux_max, aux_nb_words, aux_seed;
		min->value().ToLong(&aux_min);
		max->value().ToLong(&aux_max);
		nb_words->value().ToLong(&aux_nb_words);
		seed->value().ToLong(&aux_seed);
		return pnew pgsGenerator(pgsVariable::pgsTString,
		                         pnew pgsStringGen(aux_min, aux_max, aux_nb_words, aux_seed));
	}
	else
	{
		// Deal with errors
		if (!min->is_integer())
		{
			throw pgsParameterException(wxString() << value()
			                            << wxT(":\nmin should be an integer"));
		}
		else if (!max->is_integer())
		{
			throw pgsParameterException(wxString() << value()
			                            << wxT(":\nmax should be an integer"));
		}
		else if (!nb_words->is_integer())
		{
			throw pgsParameterException(wxString() << value()
			                            << wxT(":\nnb_words should be an integer"));
		}
		else
		{
			throw pgsParameterException(wxString() << value()
			                            << wxT(":\nseed should be an integer"));
		}
	}
}