1 #include "extension.h"
2 #include "pgsqltypes/pgsqltype.h"
3 
Extension()4 Extension::Extension()
5 {
6 	obj_type=ObjectType::Extension;
7 	handles_type=false;
8 	attributes[Attributes::HandlesType]="";
9 	attributes[Attributes::CurVersion]="";
10 	attributes[Attributes::OldVersion]="";
11 }
12 
setName(const QString & name)13 void Extension::setName(const QString &name)
14 {
15 	if(!handles_type)
16 		BaseObject::setName(name);
17 	else
18 	{
19 		QString prev_name, new_name;
20 
21 		prev_name=this->getName(true);
22 		BaseObject::setName(name);
23 		new_name=this->getName(true);
24 
25 		//Renames the PostgreSQL type represented by the extension
26 		PgSqlType::renameUserType(prev_name, this, new_name);
27 	}
28 }
29 
setSchema(BaseObject * schema)30 void Extension::setSchema(BaseObject *schema)
31 {
32 	if(!schema)
33 		this->schema = schema;
34 	else
35 	{
36 		BaseObject::setSchema(schema);
37 
38 		if(handles_type)
39 		{
40 			QString prev_name;
41 			prev_name=this->getName(true);
42 
43 			//Renames the PostgreSQL type represented by the extension
44 			PgSqlType::renameUserType(prev_name, this, this->getName(true));
45 		}
46 	}
47 }
48 
setHandlesType(bool value)49 void Extension::setHandlesType(bool value)
50 {
51 	/* Raises an error if the extension is already registered as a data type and the
52 	try to change the attribute value. This cannot be done to avoid cascade reference breaking
53 	on table columns/functions or any other objects that references PgSQLType */
54 	if(!value && PgSqlType::getUserTypeIndex(this->getName(true), this) != BaseType::Null)
55 		throw Exception(Exception::getErrorMessage(ErrorCode::ExtensionHandlingTypeImmutable)
56 						.arg(this->getName(true)),
57 						ErrorCode::ExtensionHandlingTypeImmutable,__PRETTY_FUNCTION__,__FILE__,__LINE__);
58 
59 	this->handles_type=value;
60 }
61 
setVersion(unsigned ver,const QString & value)62 void Extension::setVersion(unsigned ver, const QString &value)
63 {
64 	if(ver > OldVersion)
65 		throw Exception(ErrorCode::RefAttributeInvalidIndex,__PRETTY_FUNCTION__,__FILE__,__LINE__);
66 
67 	setCodeInvalidated(versions[ver] != value);
68 	versions[ver]=value;
69 }
70 
handlesType()71 bool Extension::handlesType()
72 {
73 	return handles_type;
74 }
75 
getVersion(unsigned ver)76 QString Extension::getVersion(unsigned ver)
77 {
78 	if(ver > OldVersion)
79 		throw Exception(ErrorCode::RefAttributeInvalidIndex,__PRETTY_FUNCTION__,__FILE__,__LINE__);
80 
81 	return versions[ver];
82 }
83 
getCodeDefinition(unsigned def_type)84 QString Extension::getCodeDefinition(unsigned def_type)
85 {
86 	QString code_def=getCachedCode(def_type, false);
87 	if(!code_def.isEmpty()) return code_def;
88 
89 	attributes[Attributes::Name]=this->getName(def_type==SchemaParser::SqlDefinition, false);
90 	attributes[Attributes::HandlesType]=(handles_type ? Attributes::True : "");
91 	attributes[Attributes::CurVersion]=versions[CurVersion];
92 	attributes[Attributes::OldVersion]=versions[OldVersion];
93 
94 	return BaseObject::__getCodeDefinition(def_type);
95 }
96 
getAlterDefinition(BaseObject * object)97 QString Extension::getAlterDefinition(BaseObject *object)
98 {
99 	Extension *ext=dynamic_cast<Extension *>(object);
100 
101 	if(!ext)
102 		throw Exception(ErrorCode::OprNotAllocatedObject,__PRETTY_FUNCTION__,__FILE__,__LINE__);
103 
104 	try
105 	{
106 		attributes[Attributes::AlterCmds]=BaseObject::getAlterDefinition(object);
107 		attributes[Attributes::NewVersion]="";
108 
109 		if(!this->versions[CurVersion].isEmpty() && !ext->versions[CurVersion].isEmpty() &&
110 				this->versions[CurVersion].isEmpty() < ext->versions[CurVersion].isEmpty())
111 			attributes[Attributes::NewVersion]=ext->versions[CurVersion];
112 
113 		return BaseObject::getAlterDefinition(this->getSchemaName(), attributes, false, true);
114 	}
115 	catch(Exception &e)
116 	{
117 		throw Exception(e.getErrorMessage(),e.getErrorCode(),__PRETTY_FUNCTION__,__FILE__,__LINE__,&e);
118 	}
119 }
120 
getDropDefinition(bool cascade)121 QString Extension::getDropDefinition(bool cascade)
122 {
123 	attributes[Attributes::Name] = this->getName(true);
124 	return BaseObject::getDropDefinition(cascade);
125 }
126 
getSignature(bool format)127 QString Extension::getSignature(bool format)
128 {
129 	return this->getName(format, false);
130 }
131 
getName(bool format,bool)132 QString Extension::getName(bool format, bool)
133 {
134 	return BaseObject::getName(format, false);
135 }
136 
operator =(Extension & ext)137 void Extension::operator = (Extension &ext)
138 {
139 	QString prev_name=this->getName(true);
140 
141 	*(dynamic_cast<BaseObject *>(this))=dynamic_cast<BaseObject &>(ext);
142 	this->versions[CurVersion]=ext.versions[CurVersion];
143 	this->versions[OldVersion]=ext.versions[OldVersion];
144 	this->handles_type=ext.handles_type;
145 
146 	if(this->handles_type)
147 		PgSqlType::renameUserType(prev_name, this, this->getName(true));
148 }
149