1 /*
2 # PostgreSQL Database Modeler (pgModeler)
3 #
4 # Copyright 2006-2020 - Raphael Araújo e Silva <raphael@pgmodeler.io>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation version 3.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # The complete text of GPLv3 is at LICENSE file on source code root directory.
16 # Also, you can get the complete GNU General Public License at <http://www.gnu.org/licenses/>
17 */
18 
19 #include "rule.h"
20 
Rule()21 Rule::Rule()
22 {
23 	execution_type=BaseType::Null;
24 	obj_type=ObjectType::Rule;
25 	attributes[Attributes::EventType]="";
26 	attributes[Attributes::Table]="";
27 	attributes[Attributes::Condition]="";
28 	attributes[Attributes::ExecType]="";
29 	attributes[Attributes::Commands]="";
30 }
31 
setCommandsAttribute()32 void Rule::setCommandsAttribute()
33 {
34 	QString str_cmds;
35 	unsigned i, qtd;
36 
37 	qtd=commands.size();
38 	for(i=0; i < qtd; i++)
39 	{
40 		str_cmds+=commands[i];
41 		if(i < (qtd-1)) str_cmds+=QString(";");
42 	}
43 
44 	attributes[Attributes::Commands]=str_cmds;
45 }
46 
setEventType(EventType type)47 void Rule::setEventType(EventType type)
48 {
49 	setCodeInvalidated(event_type != type);
50 	event_type=type;
51 }
52 
setExecutionType(ExecutionType type)53 void Rule::setExecutionType(ExecutionType type)
54 {
55 	setCodeInvalidated(execution_type != type);
56 	execution_type=type;
57 }
58 
setConditionalExpression(const QString & expr)59 void Rule::setConditionalExpression(const QString &expr)
60 {
61 	setCodeInvalidated(conditional_expr != expr);
62 	conditional_expr=expr;
63 }
64 
addCommand(const QString & cmd)65 void Rule::addCommand(const QString &cmd)
66 {
67 	//Raises an error if the command is empty
68 	if(cmd.isEmpty())
69 		throw Exception(ErrorCode::InsEmptyRuleCommand,__PRETTY_FUNCTION__,__FILE__,__LINE__);
70 	else
71 	{
72 		QString cmd_aux=cmd;
73 		cmd_aux.remove(';');
74 		commands.push_back(cmd_aux);
75 		setCodeInvalidated(true);
76 	}
77 }
78 
getEventType()79 EventType Rule::getEventType()
80 {
81 	return event_type;
82 }
83 
getExecutionType()84 ExecutionType Rule::getExecutionType()
85 {
86 	return execution_type;
87 }
88 
getConditionalExpression()89 QString Rule::getConditionalExpression()
90 {
91 	return conditional_expr;
92 }
93 
getCommand(unsigned cmd_idx)94 QString Rule::getCommand(unsigned cmd_idx)
95 {
96 	//Raises an error if the command index is out of bound
97 	if(cmd_idx >= commands.size())
98 		throw Exception(ErrorCode::RefRuleCommandInvalidIndex,__PRETTY_FUNCTION__,__FILE__,__LINE__);
99 
100 	return commands[cmd_idx];
101 }
102 
getCommandCount()103 unsigned Rule::getCommandCount()
104 {
105 	return commands.size();
106 }
107 
removeCommand(unsigned cmd_idx)108 void Rule::removeCommand(unsigned cmd_idx)
109 {
110 	//Raises an error if the command index is out of bound
111 	if(cmd_idx>=commands.size())
112 		throw Exception(ErrorCode::RefRuleCommandInvalidIndex,__PRETTY_FUNCTION__,__FILE__,__LINE__);
113 
114 	commands.erase(commands.begin() + cmd_idx);
115 	setCodeInvalidated(true);
116 }
117 
removeCommands()118 void Rule::removeCommands()
119 {
120 	commands.clear();
121 	setCodeInvalidated(true);
122 }
123 
getCodeDefinition(unsigned def_type)124 QString Rule::getCodeDefinition(unsigned def_type)
125 {
126 	QString code_def=getCachedCode(def_type, false);
127 	if(!code_def.isEmpty()) return code_def;
128 
129 	setCommandsAttribute();
130 	attributes[Attributes::Condition]=conditional_expr;
131 	attributes[Attributes::ExecType]=(~execution_type);
132 	attributes[Attributes::EventType]=(~event_type);
133 
134 	if(getParentTable())
135 		attributes[Attributes::Table]=getParentTable()->getName(true);
136 
137 	return BaseObject::__getCodeDefinition(def_type);
138 }
139 
getSignature(bool format)140 QString Rule::getSignature(bool format)
141 {
142 	if(!getParentTable())
143 		return BaseObject::getSignature(format);
144 
145 	return QString("%1 ON %2").arg(this->getName(format)).arg(getParentTable()->getSignature(true));
146 }
147