File: pgsGenerator.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 (154 lines) | stat: -rw-r--r-- 3,054 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//////////////////////////////////////////////////////////////////////////
//
// pgScript - PostgreSQL Tools
//
// Copyright (C) 2002 - 2012, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////////////////


#include "pgAdmin3.h"
#include "pgscript/objects/pgsGenerator.h"

#include "pgscript/objects/pgsNumber.h"
#include "pgscript/objects/pgsRecord.h"
#include "pgscript/objects/pgsString.h"
#include "pgscript/exceptions/pgsCastException.h"

pgsGenerator::pgsGenerator(const pgsTypes &generator_type,
                           pgsObjectGen *randomizer) :
	pgsVariable(generator_type), m_randomizer(randomizer)
{

}

pgsGenerator::~pgsGenerator()
{

}

pgsVariable *pgsGenerator::clone() const
{
	return pnew pgsGenerator(*this);
}

wxString pgsGenerator::value() const
{
	return m_randomizer->random();
}

pgsOperand pgsGenerator::operand() const
{
	switch (type())
	{
		case pgsTInt:
			return pnew pgsNumber(value(), pgsInt);
		case pgsTReal:
			return pnew pgsNumber(value(), pgsReal);
		default:
			return pnew pgsString(value());
	}
}

pgsOperand pgsGenerator::eval(pgsVarMap &vars) const
{
	return this->clone();
}

pgsOperand pgsGenerator::pgs_plus(const pgsVariable &rhs) const
{
	return *operand() + rhs;
}

pgsOperand pgsGenerator::pgs_minus(const pgsVariable &rhs) const
{
	return *operand() - rhs;
}

pgsOperand pgsGenerator::pgs_times(const pgsVariable &rhs) const
{
	return *operand() * rhs;
}

pgsOperand pgsGenerator::pgs_over(const pgsVariable &rhs) const
{
	return *operand() / rhs;
}

pgsOperand pgsGenerator::pgs_modulo(const pgsVariable &rhs) const
{
	return *operand() % rhs;
}

pgsOperand pgsGenerator::pgs_equal(const pgsVariable &rhs) const
{
	return *operand() == rhs;
}

pgsOperand pgsGenerator::pgs_different(const pgsVariable &rhs) const
{
	return *operand() != rhs;
}

pgsOperand pgsGenerator::pgs_greater(const pgsVariable &rhs) const
{
	return *operand() > rhs;
}

pgsOperand pgsGenerator::pgs_lower(const pgsVariable &rhs) const
{
	return *operand() < rhs;
}

pgsOperand pgsGenerator::pgs_lower_equal(const pgsVariable &rhs) const
{
	return *operand() <= rhs;
}

pgsOperand pgsGenerator::pgs_greater_equal(const pgsVariable &rhs) const
{
	return *operand() >= rhs;
}

pgsOperand pgsGenerator::pgs_not() const
{
	return !(*operand());
}

bool pgsGenerator::pgs_is_true() const
{
	return operand()->pgs_is_true();
}

pgsOperand pgsGenerator::pgs_almost_equal(const pgsVariable &rhs) const
{
	return *operand() &= rhs;
}

pgsNumber pgsGenerator::number() const
{
	wxString data = value().Strip(wxString::both);
	pgsTypes type = pgsNumber::num_type(data);
	switch (type)
	{
		case pgsTInt:
			return pgsNumber(data, pgsInt);
		case pgsTReal:
			return pgsNumber(data, pgsReal);
		default:
			throw pgsCastException(data, wxT("number"));
	}
}

pgsRecord pgsGenerator::record() const
{
	pgsRecord rec(1);
	rec.insert(0, 0, operand());
	return rec;
}

pgsString pgsGenerator::string() const
{
	return pgsString(value());
}