File: pgsAssignToRecord.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 (138 lines) | stat: -rw-r--r-- 3,690 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
//////////////////////////////////////////////////////////////////////////
//
// 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/pgsAssignToRecord.h"

#include "pgscript/exceptions/pgsParameterException.h"
#include "pgscript/expressions/pgsIdentRecord.h"
#include "pgscript/objects/pgsRecord.h"

pgsAssignToRecord::pgsAssignToRecord(const wxString &name, const pgsExpression *line,
                                     const pgsExpression *column, const pgsExpression *var) :
	pgsAssign(name, var), m_line(line), m_column(column)
{

}

pgsAssignToRecord::~pgsAssignToRecord()
{
	pdelete(m_line);
	pdelete(m_column);
}

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

pgsAssignToRecord::pgsAssignToRecord(const pgsAssignToRecord &that) :
	pgsAssign(that)
{
	m_line = that.m_line->clone();
	m_column = that.m_column->clone();
}

pgsAssignToRecord &pgsAssignToRecord::operator =(const pgsAssignToRecord &that)
{
	if (this != &that)
	{
		pgsAssign::operator=(that);
		pdelete(m_line);
		pdelete(m_column);
		m_line = that.m_line->clone();
		m_column = that.m_column->clone();
	}
	return (*this);
}

wxString pgsAssignToRecord::value() const
{
	return wxString() << wxT("SET ")  << m_name << wxT("[") << m_line->value()
	       << wxT("]") << wxT("[") << m_column->value() << wxT("]")
	       << wxT(" = ") << m_var->value();
}

pgsOperand pgsAssignToRecord::eval(pgsVarMap &vars) const
{
	if (vars.find(m_name) != vars.end() && vars[m_name]->is_record())
	{
		// Get the operand as a record
		pgsRecord &rec = dynamic_cast<pgsRecord &>(*vars[m_name]);

		// Get the value to assign
		pgsOperand var(m_var->eval(vars));

		if (!var->is_record())
		{
			// Evaluate parameters
			pgsOperand line(m_line->eval(vars));
			pgsOperand column(m_column->eval(vars));
			if (line->is_integer())
			{
				long aux_line;
				line->value().ToLong(&aux_line);

				if (column->is_integer() || column->is_string())
				{
					bool success = false;

					if (column->is_integer())
					{
						long aux_column;
						column->value().ToLong(&aux_column);
						if (aux_column < rec.count_columns())
						{
							success = rec.insert(aux_line, aux_column, var);
						}
					}
					else if (column->is_string())
					{
						USHORT aux_column = rec.get_column(column->value());
						if (aux_column < rec.count_columns())
						{
							success = rec.insert(aux_line, aux_column, var);
						}
					}

					if (success == false)
					{
						throw pgsParameterException(wxString() << wxT("An error ")
						                            << wxT("occurred in record affectation: ") << value()
						                            << wxT("\n") << wxT("One possible reason is a ")
						                            << wxT("column index out of range"));
					}
				}

				else
				{
					throw pgsParameterException(wxString() << column->value()
					                            << wxT(" is not a valid column number/name"));
				}
			}
			else
			{
				throw pgsParameterException(wxString() << line->value()
				                            << wxT(" is not a valid line number"));
			}
		}
		else
		{
			throw pgsParameterException(wxString() << wxT("Cannot assign a record")
			                            << wxT(" into a record: right member is a record"));
		}
	}
	else
	{
		throw pgsParameterException(wxString() << m_name << wxT(" is not a record"));
	}

	return pgsIdentRecord(m_name, m_line->clone(), m_column->clone()).eval(vars);
}