File: pgsCast.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 (100 lines) | stat: -rw-r--r-- 2,195 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
//////////////////////////////////////////////////////////////////////////
//
// 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/pgsCast.h"

#include "pgscript/exceptions/pgsParameterException.h"
#include "pgscript/objects/pgsNumber.h"

#include "pgscript/parser.tab.hh"
typedef pgscript::pgsParser::token token;

pgsCast::pgsCast(const int &cast_type, const pgsExpression *var) :
	pgsExpression(), m_cast_type(cast_type), m_var(var)
{

}

pgsCast::~pgsCast()
{
	pdelete(m_var);
}

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

pgsCast::pgsCast(const pgsCast &that) :
	pgsExpression(that), m_cast_type(that.m_cast_type)
{
	m_var = that.m_var->clone();
}

pgsCast &pgsCast::operator =(const pgsCast &that)
{
	if (this != &that)
	{
		pgsExpression::operator=(that);
		m_cast_type = that.m_cast_type;
		pdelete(m_var);
		m_var = that.m_var->clone();
	}
	return (*this);
}

wxString pgsCast::value() const
{
	wxString cast_type;

	switch (m_cast_type)
	{
		case token::PGS_INTEGER:
			cast_type = wxT("integer");
			break;
		case token::PGS_REAL:
			cast_type = wxT("real");
			break;
		case token::PGS_RECORD:
			cast_type = wxT("record");
			break;
		case token::PGS_STRING:
			cast_type = wxT("string");
			break;
		default:
			cast_type = wxT("unknown");
			break;
	}
	return wxString() << wxT("CAST (") << m_var->value() << wxT(" AS ")
	       << cast_type.Upper() << wxT(") ");
}

pgsOperand pgsCast::eval(pgsVarMap &vars) const
{
	pgsOperand var = m_var->eval(vars);
	MAPM num;

	switch (m_cast_type)
	{
		case token::PGS_INTEGER:
			num = pgsMapm::pgs_str_mapm(var->number().value());
			num = pgsMapm::pgs_mapm_round(num);
			return pnew pgsNumber(pgsMapm::pgs_mapm_str(num, true), pgsInt);
		case token::PGS_REAL:
			return pnew pgsNumber(var->number().value(), pgsReal);
		case token::PGS_RECORD:
			return var->record().clone();
		case token::PGS_STRING:
			return var->string().clone();
		default:
			return var->clone();
	}
}