Exemplo n.º 1
0
vector<BaseObject *> Table::getObjects(void)
{
	vector<BaseObject *> list;
	ObjectType types[]={ OBJ_COLUMN, OBJ_CONSTRAINT,
						 OBJ_TRIGGER, OBJ_INDEX, OBJ_RULE };
	unsigned cnt=sizeof(types)/sizeof(ObjectType);

	for(unsigned i=0; i < cnt; i++)
		list.insert(list.end(), getObjectList(types[i])->begin(), getObjectList(types[i])->end()) ;

	return(list);
}
Exemplo n.º 2
0
bool RKWorkplace::openScriptEditor (const KUrl &url, const QString& encoding, bool use_r_highlighting, bool read_only, const QString &force_caption, bool delete_on_close) {
	RK_TRACE (APP);

// is this url already opened?
	if (!url.isEmpty ()) {
	  	RKWorkplaceObjectList script_windows = getObjectList (RKMDIWindow::CommandEditorWindow, RKMDIWindow::AnyWindowState);
		for (RKWorkplaceObjectList::const_iterator it = script_windows.constBegin (); it != script_windows.constEnd (); ++it) {
			  KUrl ourl = static_cast<RKCommandEditorWindow *> (*it)->url ();
			  if (url == ourl) {
				  (*it)->activate ();
				  return true;
			  }
		}
	}

	RKCommandEditorWindow *editor = new RKCommandEditorWindow (view (), use_r_highlighting);

	if (!url.isEmpty ()) {
		if (!editor->openURL (url, encoding, use_r_highlighting, read_only, delete_on_close)) {
			delete editor;
			KMessageBox::messageBox (view (), KMessageBox::Error, i18n ("Unable to open \"%1\"", url.prettyUrl ()), i18n ("Could not open command file"));
			return false;
		}
	}

	if (!force_caption.isEmpty ()) editor->setCaption (force_caption);
	addWindow (editor);
	return true;
}
Exemplo n.º 3
0
void Table::setProtected(bool value)
{
	ObjectType obj_types[]={ OBJ_COLUMN, OBJ_CONSTRAINT,
							 OBJ_INDEX, OBJ_RULE, OBJ_TRIGGER };
	unsigned i;
	vector<TableObject *>::iterator itr, itr_end;
	vector<TableObject *> *list=nullptr;
	TableObject *tab_obj=nullptr;

	//Protected the table child objects
	for(i=0; i < 5; i++)
	{
		list=getObjectList(obj_types[i]);
		itr=list->begin();
		itr_end=list->end();

		while(itr!=itr_end)
		{

			tab_obj=(*itr);

			/* Relationship included object are always protected, so
			the protection state of this objects is not altered */
			if(!tab_obj->isAddedByRelationship())
				tab_obj->setProtected(value);
			itr++;
		}
	}

	//Protectes the table itself
	BaseGraphicObject::setProtected(value);
}
Exemplo n.º 4
0
int View::getObjectIndex(BaseObject *obj)
{
	TableObject *tab_obj=dynamic_cast<TableObject *>(obj);

	if(!obj || (tab_obj && tab_obj->getParentTable()!=this))
		return(-1);
	else
	{
		vector<TableObject *>::iterator itr, itr_end;
		vector<TableObject *> *obj_list=getObjectList(obj->getObjectType());
		bool found=false;

		itr=obj_list->begin();
		itr_end=obj_list->end();

		while(itr!=itr_end && !found)
		{
			found=((*itr)==tab_obj);
			if(!found) itr++;
		}

		if(found)
			return(itr - obj_list->begin());
		else
			return(-1);
	}
}
Exemplo n.º 5
0
unsigned Table::getObjectCount(ObjectType obj_type, bool inc_added_by_rel)
{
	if(TableObject::isTableObject(obj_type) || obj_type==OBJ_TABLE)
	{
		if(obj_type==OBJ_TABLE)
		{
			return(ancestor_tables.size());
		}
		else
		{
			vector<TableObject *> *list=nullptr;
			list=getObjectList(obj_type);

			if(!inc_added_by_rel)
			{
				vector<TableObject *>::iterator itr, itr_end;
				unsigned count=0;

				itr=list->begin();
				itr_end=list->end();
				while(itr!=itr_end)
				{
					if(!(*itr)->isAddedByRelationship()) count++;
					itr++;
				}

				return(count);
			}
			else
				return(list->size());
		}
	}
	else
		throw Exception(ERR_REF_OBJ_INV_INDEX,__PRETTY_FUNCTION__,__FILE__,__LINE__);
}
Exemplo n.º 6
0
int View::getObjectIndex(const QString &name, ObjectType obj_type)
{
	if(name.isEmpty())
		return(-1);
	else
	{
		vector<TableObject *>::iterator itr, itr_end;
		vector<TableObject *> *obj_list=getObjectList(obj_type);
    bool found=false, format=name.contains('"');

		itr=obj_list->begin();
		itr_end=obj_list->end();

		while(itr!=itr_end && !found)
		{
			found=((*itr)->getName(format)==name);
			if(!found) itr++;
		}

		if(found)
			return(itr - obj_list->begin());
		else
			return(-1);
	}
}
Exemplo n.º 7
0
void RKWorkplace::closeAll (int type, int state) {
	RK_TRACE (APP);

	RKWorkplaceObjectList list_to_close = getObjectList (type, state);
	for (RKWorkplaceObjectList::const_iterator it = list_to_close.constBegin (); it != list_to_close.constEnd (); ++it) {
		closeWindow (*it);
	}
}
Exemplo n.º 8
0
TableObject *View::getObject(unsigned obj_idx, ObjectType obj_type)
{
 vector<TableObject *> *obj_list=getObjectList(obj_type);

	//Raises an error if the object index is out of bound
	if(obj_idx >= obj_list->size())
		throw Exception(ERR_REF_OBJ_INV_INDEX,__PRETTY_FUNCTION__,__FILE__,__LINE__);

	return(obj_list->at(obj_idx));
}
Exemplo n.º 9
0
unsigned View::getObjectCount(ObjectType obj_type, bool)
{
	try
	{
		return(getObjectList(obj_type)->size());
	}
	catch(Exception &e)
	{
		throw Exception(e.getErrorMessage(), e.getErrorType(),__PRETTY_FUNCTION__,__FILE__,__LINE__, &e);
	}
}
Exemplo n.º 10
0
void Table::swapObjectsIndexes(ObjectType obj_type, unsigned idx1, unsigned idx2)
{
	vector<TableObject *> *obj_list=nullptr;
	vector<TableObject *>::iterator itr1, itr2;
	TableObject *aux_obj=nullptr, *aux_obj1=nullptr;

	try
	{
		if(idx1!=idx2)
		{
			obj_list=getObjectList(obj_type);

			//Raises an error if both index is out of list bounds
			if(idx1 >= obj_list->size() && idx2 >= obj_list->size())
				throw Exception(ERR_REF_OBJ_INV_INDEX,__PRETTY_FUNCTION__,__FILE__,__LINE__);
			//If the idx1 is out of bound inserts the element idx2 at the list's begin
			else if(idx1 >= obj_list->size())
			{
				aux_obj1=obj_list->front();
				itr2=obj_list->begin() + idx2;
				aux_obj=(*itr2);
				obj_list->erase(itr2);
				obj_list->insert(obj_list->begin(), aux_obj);
			}
			//If the idx2 is out of bound inserts the element idx1 on the list's end
			else if(idx2 >= obj_list->size())
			{
				itr1=obj_list->begin() + idx1;
				aux_obj=(*itr1);
				aux_obj1=obj_list->back();
				obj_list->erase(itr1);
				obj_list->push_back(aux_obj);
			}
			else
			{
				aux_obj=obj_list->at(idx1);
				itr1=obj_list->begin() + idx1;
				itr2=obj_list->begin() + idx2;

				(*itr1)=aux_obj1=(*itr2);
				(*itr2)=aux_obj;
			}

			if(obj_type!=OBJ_COLUMN && obj_type!=OBJ_CONSTRAINT)
				BaseObject::swapObjectsIds(aux_obj, aux_obj1, false);

			setCodeInvalidated(true);
		}
	}
	catch(Exception &e)
	{
		throw Exception(e.getErrorMessage(), e.getErrorType(),__PRETTY_FUNCTION__,__FILE__,__LINE__,&e);
	}
}
Exemplo n.º 11
0
bool Table::isReferRelationshipAddedObject(void)
{
	vector<TableObject *>::iterator itr, itr_end;
	ObjectType types[]={ OBJ_COLUMN, OBJ_CONSTRAINT };
	bool found=false;

	for(unsigned i=0; i < 2 && !found; i++)
	{
		itr=getObjectList(types[i])->begin();
		itr_end=getObjectList(types[i])->end();

		while(itr!=itr_end && !found)
		{
			found=(*itr)->isAddedByRelationship();
			itr++;
		}
	}

	return(found);
}
Exemplo n.º 12
0
void View::removeObject(unsigned obj_idx, ObjectType obj_type)
{
	vector<TableObject *> *obj_list = getObjectList(obj_type);
	vector<TableObject *>::iterator itr;

	//Raises an error if the object index is out of bound
	if(obj_idx >= obj_list->size())
		throw Exception(ERR_REF_OBJ_INV_INDEX,__PRETTY_FUNCTION__,__FILE__,__LINE__);

	itr=obj_list->begin() + obj_idx;
	(*itr)->setParentTable(nullptr);
	obj_list->erase(itr);
}
Exemplo n.º 13
0
void View::addObject(BaseObject *obj, int obj_idx)
{
	if(!obj)
		throw Exception(ERR_ASG_NOT_ALOC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
	else
	{
		try
		{
			vector<TableObject *> *obj_list = getObjectList(obj->getObjectType());
			TableObject *tab_obj=dynamic_cast<TableObject *>(obj);

			//Raises an error if already exists a object with the same name and type
			if(getObjectIndex(obj->getName(), tab_obj->getObjectType()) >= 0)
			{
				throw Exception(QString(Exception::getErrorMessage(ERR_ASG_DUPLIC_OBJECT))
												.arg(obj->getName(true))
												.arg(obj->getTypeName())
												.arg(this->getName(true))
												.arg(this->getTypeName()),
												ERR_ASG_DUPLIC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
			}

			//Validates the object definition
			tab_obj->setParentTable(this);
			tab_obj->getCodeDefinition(SchemaParser::SQL_DEFINITION);

			//Make a additional validation if the object is a trigger
			if(tab_obj->getObjectType()==OBJ_TRIGGER)
				dynamic_cast<Trigger *>(tab_obj)->validateTrigger();

			//Inserts the object at specified position
			if(obj_idx < 0 || obj_idx >= static_cast<int>(obj_list->size()))
				obj_list->push_back(tab_obj);
			else
				obj_list->insert(obj_list->begin() + obj_idx, tab_obj);

			setCodeInvalidated(true);
		}
		catch(Exception &e)
		{
			if(e.getErrorType()==ERR_UNDEF_ATTRIB_VALUE)
				throw Exception(Exception::getErrorMessage(ERR_ASG_OBJ_INV_DEFINITION)
                        .arg(obj->getName())
                        .arg(obj->getTypeName()),
												ERR_ASG_OBJ_INV_DEFINITION,__PRETTY_FUNCTION__,__FILE__,__LINE__, &e);
			else
				throw Exception(e.getErrorMessage(),e.getErrorType(),__PRETTY_FUNCTION__,__FILE__,__LINE__, &e);
		}
	}
}
Exemplo n.º 14
0
View::~View(void)
{
	ObjectType types[]={ OBJ_TRIGGER, OBJ_RULE };
	vector<TableObject *> *list=nullptr;

	for(unsigned i=0; i < 2; i++)
	{
		list=getObjectList(types[i]);
		while(!list->empty())
		{
			delete(list->back());
			list->pop_back();
		}
	}
}
Exemplo n.º 15
0
void RKWorkplace::newObjectViewer (RObject *object) {
	RK_TRACE (APP);
	RK_ASSERT (object);

	RKWorkplaceObjectList object_windows = getObjectList (RKMDIWindow::ObjectWindow, RKMDIWindow::AnyWindowState);
	for (RKWorkplaceObjectList::const_iterator it = object_windows.constBegin (); it != object_windows.constEnd (); ++it) {
		if (static_cast<RObjectViewer *> (*it)->object () == object) {
			(*it)->activate ();
			return;
		}
	}

	RObjectViewer *ov = new RObjectViewer (view (), object);
	addWindow (ov);
}
Exemplo n.º 16
0
void Table::setCodeInvalidated(bool value)
{
	ObjectType types[]={ OBJ_COLUMN, OBJ_CONSTRAINT,
						 OBJ_TRIGGER, OBJ_INDEX, OBJ_RULE };
	unsigned cnt=sizeof(types)/sizeof(ObjectType);
	vector<TableObject *> *list=nullptr;

	for(unsigned i=0; i < cnt; i++)
	{
		list=getObjectList(types[i]);

		for(auto &obj : *list)
			obj->setCodeInvalidated(value);
	}

	BaseObject::setCodeInvalidated(value);
}
Exemplo n.º 17
0
Table::~Table(void)
{
	ObjectType types[]={ OBJ_TRIGGER, OBJ_INDEX, OBJ_RULE,
											 OBJ_CONSTRAINT, OBJ_COLUMN };
	vector<TableObject *> *list=nullptr;

	for(unsigned i=0; i < 5; i++)
	{
		list=getObjectList(types[i]);
		while(!list->empty())
		{
			delete(list->back());
			list->pop_back();
		}
	}

	ancestor_tables.clear();
}
Exemplo n.º 18
0
void RKWorkplace::openHelpWindow (const KURL &url, bool only_once) {
	RK_TRACE (APP);

	RKHelpWindow *hw = new RKHelpWindow (view ());
	if (!url.isEmpty ()) {
		if (only_once) {
			RKWorkplaceObjectList help_windows = getObjectList (RKMDIWindow::HelpWindow, RKMDIWindow::AnyState);
			for (RKWorkplaceObjectList::const_iterator it = help_windows.constBegin (); it != help_windows.constEnd (); ++it) {
				if (static_cast<RKHelpWindow *> (*it)->url ().equals (url, true)) {
					(*it)->activate ();
					return;
				}
			}
		}
		hw->openURL (url);
	}

	addWindow (hw);
}
Exemplo n.º 19
0
void RKWorkplace::openHelpWindow (const KUrl &url, bool only_once) {
	RK_TRACE (APP);

	if (url.isEmpty ()) {
		RK_ASSERT (false);
		return;
	}

	if (only_once) {
		RKWorkplaceObjectList help_windows = getObjectList (RKMDIWindow::HelpWindow, RKMDIWindow::AnyWindowState);
		for (RKWorkplaceObjectList::const_iterator it = help_windows.constBegin (); it != help_windows.constEnd (); ++it) {
			if (static_cast<RKHTMLWindow *> (*it)->url ().equals (url, KUrl::CompareWithoutTrailingSlash | KUrl::CompareWithoutFragment)) {
				(*it)->activate ();
				return;
			}
		}
	}

	RKHTMLWindow *hw = new RKHTMLWindow (view (), RKHTMLWindow::HTMLHelpWindow);
	hw->openURL (url);
	addWindow (hw);
}
Exemplo n.º 20
0
BaseObject *Table::getObject(unsigned obj_idx, ObjectType obj_type)
{
	vector<TableObject *> *obj_list=nullptr;

	if(obj_type==OBJ_TABLE)
	{
		//Raises an error if the object index is out of bound
		if(obj_idx >= ancestor_tables.size())
			throw Exception(ERR_REF_OBJ_INV_INDEX,__PRETTY_FUNCTION__,__FILE__,__LINE__);

		return(ancestor_tables[obj_idx]);
	}
	else
	{
		obj_list=getObjectList(obj_type);
		if(obj_idx < obj_list->size())
			return(obj_list->at(obj_idx));
		else
			//Raises an error if the object index is out of bound
			throw Exception(ERR_REF_OBJ_INV_INDEX,__PRETTY_FUNCTION__,__FILE__,__LINE__);
	}
}
Exemplo n.º 21
0
void View::setProtected(bool value)
{
	ObjectType obj_types[]={ OBJ_RULE, OBJ_TRIGGER };
	unsigned i;
	vector<TableObject *>::iterator itr, itr_end;
	vector<TableObject *> *list=nullptr;

	//Protected the table child objects
	for(i=0; i < sizeof(obj_types)/sizeof(ObjectType); i++)
	{
		list=getObjectList(obj_types[i]);
		itr=list->begin();
		itr_end=list->end();

		while(itr!=itr_end)
		{
			(*itr)->setProtected(value);
			itr++;
		}
	}

	//Protects the view itself
  BaseGraphicObject::setProtected(value);
}
Exemplo n.º 22
0
int EnemyMovementObject::update(int dt) {
	if(NpcMovementObject::update(dt) == -1) {
		return -1;
	}

	if(noAttackTimer > 0) {
		noAttackTimer -= dt;
		if(noAttackTimer < 0) {
			noAttackTimer = 0;
		}
	}



	if(noAttackTimer == 0) {
		Rectangle *thisRect = sprite->getRectWithoutRot();

		if(getObjectList() != NULL) {
			std::list<GameObject*> *spatialList = spatialHashmap->getNearby(this);
			for(std::list<GameObject*>::iterator it = spatialList->begin(); it != spatialList->end(); ++it) {
				GameObject *gameObject = *it;



				if(gameObject->gameObjectInstance->gameObject->type != GameObjectClass::INIMIGO &&
						gameObject->gameObjectInstance->gameObject->type != GameObjectClass::ITEM) {

					Rectangle *objectRect = gameObject->sprite->getRectWithoutRot();

					if(!gameObject->isInvincible() && gameObject->position->z == 0 &&  objectRect->intersectsWith(thisRect)) {
						int dmg = gameObject->addDamage(this, this->gameObjectInstance->gameObject->atacaEncostaPersonagemDano);
						if(dmg != 0) {
							gameObject->setInvincibility(1000);
						}


					}

					delete objectRect;

				}
			}

			delete spatialList;
		}

		delete thisRect;
	}




	if(attackTimer/1000.0f > (float)this->gameObjectInstance->gameObject->tempoAtaque) {
		if(this->gameObjectInstance->gameObject->atiraItemMagiaDirecaoPersonagem) {
			ObjectFunctions *objectFunctions = ObjectFunctions::getInstance();

			GameData *gameData = GameData::getInstance();
			PlayerData *playerData = PlayerData::getInstance();
			GameObjectClass *throwed;
			throwed = gameData->searchGameObjectById(this->gameObjectInstance->gameObject->atiraItemMagiaDirecaoPersonagemIdItem);

			if(playerData->getPlayer() != NULL) {
				objectFunctions->throwObject(this, throwed, 2, Vector2(*playerData->getPlayer()->position),  0);
			}

		}

		if(this->gameObjectInstance->gameObject->atiraItemMaginaDirecaoEstiver) {
			ObjectFunctions *objectFunctions = ObjectFunctions::getInstance();

			GameData *gameData = GameData::getInstance();
			GameObjectClass *throwed;
			throwed = gameData->searchGameObjectById(this->gameObjectInstance->gameObject->atiraItemMaginaDirecaoEstiverIdItem);

			objectFunctions->throwObject(this, throwed, 2, 0);
		}

		attackTimer = 0;
	}





	attackTimer += dt;

}
Exemplo n.º 23
0
int AcceleratedParticle::update(int dt) {

	if(GameObject::update(dt) == -1) {
		return -1;
	}


	/*
	Vector2 nextPosition;
	Vector2 collisionVelocity;



	velocity->addScaledVector(acceleration, dt);
	velocity->limitVector(&maxVelocity);

	nextPosition = *position;
	nextPosition.addScaledVector(velocity, dt);

	collisionVelocity = *velocity;
	collisionVelocity.x *= dt;
	collisionVelocity.y *= dt;

	Vector2 begin;
	begin.x = std::min(position->x, nextPosition.x);
	begin.y = std::min(position->y, nextPosition.y);

	Vector2 end;
	end.x = std::max(position->x, nextPosition.x);
	end.y = std::max(position->y, nextPosition.y);

	float tfirst, tlast;
	int lado = -1;
	tfirst = 1.0f;
	tlast = 1.0f;

	Rectangle *objectRect;
	objectRect = this->sprite->getRectWithoutRot();
	Rectangle newPosition = Rectangle(0, 0, 0, 0);

	bool doubleBreak = false;

	for(float i = begin.x - tilemap->tileWidth*2; i <= end.x + tilemap->tileWidth*2; i += tilemap->tileWidth) {
		for(float j = begin.y - tilemap->tileHeight*2; j <= end.y + tilemap->tileHeight*2; j += tilemap->tileHeight) {
			Tile *tile = tilemap->getTilesetByPosition(i, j);

			if(tile != NULL && tile->collisionType == Tile::COLLISION_BLOCK) {

				Rectangle tileRect = Rectangle(((int)(i/tilemap->tileWidth))*tilemap->tileWidth,
						((int)(j/tilemap->tileHeight))*tilemap->tileHeight,
						tilemap->tileWidth, tilemap->tileHeight);



				if(objectRect->movingIntersectRect(collisionVelocity, tileRect, &tfirst, &tlast, &lado, &newPosition)) {
					std::cout << "Colidiu! " << collisionVelocity.x << " " << collisionVelocity.y << " tfirst: " << tfirst << std::endl;
					doubleBreak = true;
					break;
				} else {
					tfirst = 1.0f;
				}



			}
		}
		if(doubleBreak) {
			break;
		}
	}

	delete objectRect;

	position->addScaledVector(velocity, dt);


	if(doubleBreak) {
		position->x = newPosition.x;
		position->y = newPosition.y;
	}

	if(lado != -1 && doubleBreak) {
		switch(lado) {
		case Rectangle::SIDE_UP:

			break;
		default:
			break;
		}
	}

	*/



	positionAnt.x = position->x;
	positionAnt.y = position->y;


	velocity->addScaledVector(acceleration, dt);
	position->addScaledVector(velocity, dt);






	if(autoCollision && testBoxCollisionTile(dt)) {

		resolveBoxCollisionTile(positionAnt, dt);

	}





	if(glideIntensity == 0 && flightIntensity == 0) {
		acceleration->z = +0.002f;
	} else {
		if(flightIntensity == 0) {
			acceleration->z = 0;
			velocity->z = glideIntensity;
		} else {
			acceleration->z = 0;
			velocity->z = -flightIntensity;
		}

	}



	if(position->z > 0) {
		position->z = 0;
	}





	if(position->z < 0) {
		sprite->setZoom(1 - (position->z)/200.0f);
		setSpriteRotation(1);
	} else {
		setSpriteRotation(0);
	}

	Rectangle *thisRect = sprite->getRectWithoutRot();

	// verifica colisão de zpulo por cima com inimigos
	if(enemyPhysicalAttackCapable) {
		if(getObjectList() != NULL) {
			std::list<GameObject*> *spatialList = spatialHashmap->getNearby(this);
			for(std::list<GameObject*>::iterator it = spatialList->begin(); it != spatialList->end(); ++it) {
				GameObject* gameObject = *it;

				if(gameObject != this) {
					if(gameObject->gameObjectInstance->gameObject->type == GameObjectClass::INIMIGO) {
						if(gameObject->gameObjectInstance->gameObject->derrotaPulandoSobreInimigo) {
							Rectangle *objectRect = gameObject->sprite->getRectWithoutRot();



							if(thisRect->intersectsWith(objectRect) && this->position->z != 0 &&
									gameObject->gameObjectInstance->gameObject->derrotaPulandoSobreInimigo) {
								int nextZ = this->position->z + this->velocity->z*dt;
								if(nextZ > - 5) {
									gameObject->addDamage(this);
									gameObject->setInvincibility(1000);
									if(gameObject->hp <= 0) {
										this->addExp(gameObject->gameObjectInstance->gameObject->experiencePoints);
										PlayerData::getInstance()->addPoints(gameObject->gameObjectInstance->gameObject->defeatPoints);
									}
									this->velocity->z = -0.5f;
								}
							}



							delete objectRect;
						}

						if(gameObject->gameObjectInstance->gameObject->derrotaEncostandoInimigo) {
							Rectangle *objectRect = gameObject->sprite->getRectWithoutRot();

							if(thisRect->intersectsWith(objectRect)) {
								gameObject->addDamage(this);
								if(gameObject->hp <= 0) {
									this->addExp(gameObject->gameObjectInstance->gameObject->experiencePoints);
									PlayerData::getInstance()->addPoints(gameObject->gameObjectInstance->gameObject->defeatPoints);
								}
								this->velocity->x *= -1;
								this->velocity->y *= -1;
							}

							delete objectRect;
						}

					}
				}



			}
		}
	}


	delete thisRect;




	return 0;

}
Exemplo n.º 24
0
BaseObject *Table::getObject(const QString &name, ObjectType obj_type, int &obj_idx)
{
	BaseObject *object=nullptr;
	bool found=false, format=false;

	//Checks if the name contains ", if so, the search will consider formatted names
	format=name.contains('"');

	if(TableObject::isTableObject(obj_type))
	{
		vector<TableObject *>::iterator itr, itr_end;
		vector<TableObject *> *obj_list=nullptr;
		QString aux_name=name;

		obj_list=getObjectList(obj_type);
		itr=obj_list->begin();
		itr_end=obj_list->end();

		while(itr!=itr_end)
		{
			found=((*itr)->getName(format)==aux_name);
			if(!found) itr++;
			else break;
		}

		if(found)
		{
			obj_idx=(itr-obj_list->begin());
			object=(*itr);
		}
		else obj_idx=-1;
	}
	else if(obj_type==OBJ_TABLE)
	{
		vector<Table *>::iterator itr_tab, itr_end_tab;
		QString tab_name, aux_name=name;

		aux_name.remove('"');
		itr_tab=ancestor_tables.begin();
		itr_end_tab=ancestor_tables.end();

		while(itr_tab!=itr_end_tab)
		{
			/* Unlike other object types, tables are always compared with the FORMATTED NAME
			because they must be 'schema-qualified' preventing a table of the same name
			but different schemas are confused */
			tab_name=(*itr_tab)->getName(true).remove('"');
			found=(tab_name==aux_name);
			if(!found) itr_tab++;
			else break;
		}

		if(found)
		{
			obj_idx=(itr_tab-ancestor_tables.begin());
			object=(*itr_tab);
		}
		else obj_idx=-1;
	}
	else
		throw Exception(ERR_OBT_OBJ_INVALID_TYPE,__PRETTY_FUNCTION__,__FILE__,__LINE__);

	return(object);
}
Exemplo n.º 25
0
void Table::removeObject(unsigned obj_idx, ObjectType obj_type)
{
	//Raises an error if the user try to remove a object with invalid type
	if(!TableObject::isTableObject(obj_type) && obj_type!=OBJ_TABLE)
		throw Exception(ERR_REM_OBJ_INVALID_TYPE,__PRETTY_FUNCTION__,__FILE__,__LINE__);

	else if(obj_type==OBJ_TABLE && obj_idx < ancestor_tables.size())
	{
		vector<Table *>::iterator itr;
		Table *tab=nullptr;

		itr=ancestor_tables.begin() + obj_idx;
		ancestor_tables.erase(itr);
		with_oid=false;

		for(auto &obj : ancestor_tables)
		{
			tab=dynamic_cast<Table *>(obj);

			if(!with_oid && tab->isWithOIDs())
			{
				with_oid=true;
				break;
			}
		}
	}
	else if(obj_type!=OBJ_TABLE && obj_type!=BASE_TABLE)
	{
		vector<TableObject *> *obj_list=nullptr;
		vector<TableObject *>::iterator itr;

		obj_list=getObjectList(obj_type);

		//Raises an error if the object index is out of bound
		if(obj_idx >= obj_list->size())
			throw Exception(ERR_REF_OBJ_INV_INDEX,__PRETTY_FUNCTION__,__FILE__,__LINE__);

		if(obj_type!=OBJ_COLUMN)
		{
			itr=obj_list->begin() + obj_idx;
			TableObject *tab_obj=(*itr);
			Constraint *constr=dynamic_cast<Constraint *>(tab_obj);

			tab_obj->setParentTable(nullptr);
			obj_list->erase(itr);

			if(constr && constr->getConstraintType()==ConstraintType::primary_key)
				dynamic_cast<Constraint *>(tab_obj)->setColumnsNotNull(false);
		}
		else
		{
			vector<TableObject *> refs;
			Column *column=nullptr;

			itr=obj_list->begin() + obj_idx;
			column=dynamic_cast<Column *>(*itr);

			//Gets the references to the column before the exclusion
			getColumnReferences(column, refs, true);

			//Case some trigger, constraint, index is referencing the column raises an error
			if(!refs.empty())
			{
				throw Exception(Exception::getErrorMessage(ERR_REM_INDIRECT_REFERENCE)
								.arg(column->getName())
								.arg(column->getTypeName())
								.arg(refs[0]->getName())
						.arg(refs[0]->getTypeName())
						.arg(this->getName(true))
						.arg(this->getTypeName()),
						ERR_REM_INDIRECT_REFERENCE,__PRETTY_FUNCTION__,__FILE__,__LINE__);
			}

			column->setParentTable(nullptr);
			columns.erase(itr);
		}
	}

	setCodeInvalidated(true);
}
Exemplo n.º 26
0
void Table::addObject(BaseObject *obj, int obj_idx)
{
	ObjectType obj_type;

	if(!obj)
		throw Exception(ERR_ASG_NOT_ALOC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
	else
	{
		int idx;
		obj_type=obj->getObjectType();

#ifdef DEMO_VERSION
#warning "DEMO VERSION: table children objects creation limit."
		vector<TableObject *> *obj_list=(obj_type!=OBJ_TABLE ? getObjectList(obj_type) : nullptr);

		if((obj_list && obj_list->size() >= GlobalAttributes::MAX_OBJECT_COUNT) ||
				(obj_type==OBJ_TABLE && ancestor_tables.size() >= GlobalAttributes::MAX_OBJECT_COUNT))
			throw Exception(trUtf8("In demonstration version tables can have only `%1' instances of each child object type or ancestor tables! You've reach this limit for the type: `%2'")
							.arg(GlobalAttributes::MAX_OBJECT_COUNT)
							.arg(BaseObject::getTypeName(obj_type)),
							ERR_CUSTOM,__PRETTY_FUNCTION__,__FILE__,__LINE__);

#endif

		try
		{
			//Raises an error if already exists a object with the same name and type
			if(getObject(obj->getName(),obj_type,idx))
			{
				throw Exception(QString(Exception::getErrorMessage(ERR_ASG_DUPLIC_OBJECT))
								.arg(obj->getName(true))
								.arg(obj->getTypeName())
								.arg(this->getName(true))
								.arg(this->getTypeName()),
								ERR_ASG_DUPLIC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
			}

			//Raises an error if the user try to set the table as ancestor/copy of itself
			else if((obj_type==OBJ_TABLE || obj_type==BASE_TABLE) && obj==this)
				throw Exception(ERR_INV_INH_COPY_RELATIONSHIP,__PRETTY_FUNCTION__,__FILE__,__LINE__);

			switch(obj_type)
			{
				case OBJ_COLUMN:
				case OBJ_CONSTRAINT:
				case OBJ_TRIGGER:
				case OBJ_INDEX:
				case OBJ_RULE:
					TableObject *tab_obj;
					vector<TableObject *> *obj_list;
					Column *col;

					tab_obj=dynamic_cast<TableObject *>(obj);
					col=dynamic_cast<Column *>(tab_obj);

					//Sets the object parent table if there isn't one
					if(!tab_obj->getParentTable())
						tab_obj->setParentTable(this);
					//Raises an error if the parent table of the table object is different from table 'this'
					else if(tab_obj->getParentTable()!=this)
						throw Exception(ERR_ASG_OBJ_BELONGS_OTHER_TABLE,__PRETTY_FUNCTION__,__FILE__,__LINE__);

					//Validates the object SQL code befor insert on table
					obj->getCodeDefinition(SchemaParser::SQL_DEFINITION);

					if(col && col->getType()==this)
					{
						throw Exception(Exception::getErrorMessage(ERR_INV_COLUMN_TABLE_TYPE)
										.arg(col->getName())
										.arg(this->getName()),
										ERR_INV_COLUMN_TABLE_TYPE,__PRETTY_FUNCTION__,__FILE__,__LINE__);
					}
					else if(obj_type==OBJ_CONSTRAINT)
					{
						//Raises a error if the user try to add a second primary key on the table
						if(dynamic_cast<Constraint *>(tab_obj)->getConstraintType()==ConstraintType::primary_key &&
								this->getPrimaryKey())
							throw Exception(ERR_ASG_EXISTING_PK_TABLE,__PRETTY_FUNCTION__,__FILE__,__LINE__);
					}
					else if(obj_type==OBJ_TRIGGER)
						dynamic_cast<Trigger *>(tab_obj)->validateTrigger();

					obj_list=getObjectList(obj_type);

					//Adds the object to the table
					if(obj_idx < 0 || obj_idx >= static_cast<int>(obj_list->size()))
						obj_list->push_back(tab_obj);
					else
					{
						//If there is a object index specified inserts the object at the position
						if(obj_list->size() > 0)
							obj_list->insert((obj_list->begin() + obj_idx), tab_obj);
						else
							obj_list->push_back(tab_obj);
					}

					if(obj_type==OBJ_COLUMN || obj_type==OBJ_CONSTRAINT)
					{
						updateAlterCmdsStatus();

						if(obj_type==OBJ_CONSTRAINT)
							dynamic_cast<Constraint *>(tab_obj)->setColumnsNotNull(true);
					}
				break;

				case OBJ_TABLE:
					Table *tab;
					tab=dynamic_cast<Table *>(obj);
					if(obj_idx < 0 || obj_idx >= static_cast<int>(ancestor_tables.size()))
						ancestor_tables.push_back(tab);
					else
						ancestor_tables.insert((ancestor_tables.begin() + obj_idx), tab);

					/* Updating the storage parameter WITH OIDS depending on the ancestors.
			 According to the docs, the child table will inherit WITH OID status from the parents */
					with_oid=(with_oid || tab->isWithOIDs());
				break;

				default:
					throw Exception(ERR_ASG_OBJECT_INV_TYPE,__PRETTY_FUNCTION__,__FILE__,__LINE__);
				break;
			}

			setCodeInvalidated(true);
		}
		catch(Exception &e)
		{
			if(e.getErrorType()==ERR_UNDEF_ATTRIB_VALUE)
				throw Exception(Exception::getErrorMessage(ERR_ASG_OBJ_INV_DEFINITION)
								.arg(obj->getName())
								.arg(obj->getTypeName()),
								ERR_ASG_OBJ_INV_DEFINITION,__PRETTY_FUNCTION__,__FILE__,__LINE__, &e);
			else
				throw Exception(e.getErrorMessage(),e.getErrorType(),__PRETTY_FUNCTION__,__FILE__,__LINE__, &e);
		}
	}
}
Exemplo n.º 27
0
void Table::addObject(BaseObject *obj, int obj_idx)
{
	ObjectType obj_type;

	if(!obj)
		throw Exception(ERR_ASG_NOT_ALOC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
	else
	{
		int idx;
		obj_type=obj->getObjectType();

		try
		{
			//Raises an error if already exists a object with the same name and type
			if(getObject(obj->getName(),obj_type,idx))
			{
				throw Exception(QString(Exception::getErrorMessage(ERR_ASG_DUPLIC_OBJECT))
												.arg(obj->getName(true))
												.arg(obj->getTypeName())
												.arg(this->getName(true))
												.arg(this->getTypeName()),
												ERR_ASG_DUPLIC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
			}

			//Raises an error if the user try to set the table as ancestor/copy of itself
			else if((obj_type==OBJ_TABLE || obj_type==BASE_TABLE) && obj==this)
				throw Exception(ERR_INV_INH_COPY_RELATIONSHIP,__PRETTY_FUNCTION__,__FILE__,__LINE__);

			switch(obj_type)
			{
				case OBJ_COLUMN:
				case OBJ_CONSTRAINT:
				case OBJ_TRIGGER:
				case OBJ_INDEX:
				case OBJ_RULE:
					TableObject *tab_obj;
					vector<TableObject *> *obj_list;

					tab_obj=dynamic_cast<TableObject *>(obj);

					//Sets the object parent table if there isn't one
					if(!tab_obj->getParentTable())
						tab_obj->setParentTable(this);
					//Raises an error if the parent table of the table object is different from table 'this'
					else if(tab_obj->getParentTable()!=this)
						throw Exception(ERR_ASG_OBJ_BELONGS_OTHER_TABLE,__PRETTY_FUNCTION__,__FILE__,__LINE__);

					//Validates the object SQL code befor insert on table
					obj->getCodeDefinition(SchemaParser::SQL_DEFINITION);

					if(obj_type==OBJ_CONSTRAINT)
					{
						//Raises a error if the user try to add a second primary key on the table
						if(dynamic_cast<Constraint *>(tab_obj)->getConstraintType()==ConstraintType::primary_key &&
							 this->getPrimaryKey())
							throw Exception(ERR_ASG_EXISTING_PK_TABLE,__PRETTY_FUNCTION__,__FILE__,__LINE__);
					}
					else if(obj_type==OBJ_TRIGGER)
						dynamic_cast<Trigger *>(tab_obj)->validateTrigger();

					obj_list=getObjectList(obj_type);

					//Adds the object to the table
					if(obj_idx < 0 || obj_idx >= static_cast<int>(obj_list->size()))
						obj_list->push_back(tab_obj);
					else
					{
						//If there is a object index specified inserts the object at the position
						if(obj_list->size() > 0)
							obj_list->insert((obj_list->begin() + obj_idx), tab_obj);
						else
							obj_list->push_back(tab_obj);
					}
				break;

				case OBJ_TABLE:
					Table *tab;
					tab=dynamic_cast<Table *>(obj);
					if(obj_idx < 0 || obj_idx >= static_cast<int>(ancestor_tables.size()))
						ancestor_tables.push_back(tab);
					else
						ancestor_tables.insert((ancestor_tables.begin() + obj_idx), tab);
				break;

				default:
					throw Exception(ERR_ASG_OBJECT_INV_TYPE,__PRETTY_FUNCTION__,__FILE__,__LINE__);
				break;
			}

		}
		catch(Exception &e)
		{
			if(e.getErrorType()==ERR_UNDEF_ATTRIB_VALUE)
				throw Exception(Exception::getErrorMessage(ERR_ASG_OBJ_INV_DEFINITION)
												.arg(Utf8String::create(obj->getName()))
												.arg(obj->getTypeName()),
												ERR_ASG_OBJ_INV_DEFINITION,__PRETTY_FUNCTION__,__FILE__,__LINE__, &e);
			else
				throw Exception(e.getErrorMessage(),e.getErrorType(),__PRETTY_FUNCTION__,__FILE__,__LINE__, &e);
		}
	}
}
Exemplo n.º 28
0
void Table::removeObject(unsigned obj_idx, ObjectType obj_type)
{
	//Raises an error if the user try to remove a object with invalid type
	if(obj_type!=OBJ_COLUMN && obj_type!=OBJ_CONSTRAINT &&
		 obj_type!=OBJ_TRIGGER && obj_type!=OBJ_INDEX &&
		 obj_type!=OBJ_RULE && obj_type!=OBJ_TABLE)
		throw Exception(ERR_REM_OBJ_INVALID_TYPE,__PRETTY_FUNCTION__,__FILE__,__LINE__);

	else if(obj_type==OBJ_TABLE && obj_idx < ancestor_tables.size())
	{
		vector<Table *>::iterator itr;

		itr=ancestor_tables.begin() + obj_idx;
		ancestor_tables.erase(itr);
	}
	else if(obj_type!=OBJ_TABLE && obj_type!=BASE_TABLE)
	{
		vector<TableObject *> *obj_list=nullptr;
		vector<TableObject *>::iterator itr;

		obj_list=getObjectList(obj_type);

		//Raises an error if the object index is out of bound
		if(obj_idx >= obj_list->size())
			throw Exception(ERR_REF_OBJ_INV_INDEX,__PRETTY_FUNCTION__,__FILE__,__LINE__);

		if(obj_type!=OBJ_COLUMN)
		{
			itr=obj_list->begin() + obj_idx;
			(*itr)->setParentTable(nullptr);
			obj_list->erase(itr);
		}
		else
		{
			vector<TableObject *> refs;
			Column *column=nullptr;

			itr=obj_list->begin() + obj_idx;
			column=dynamic_cast<Column *>(*itr);

			//Gets the references to the column before the exclusion
			getColumnReferences(column, refs, true);

			//Case some trigger, constraint, index is referencing the column raises an error
			if(!refs.empty())
			{
				throw Exception(Exception::getErrorMessage(ERR_REM_INDIRECT_REFERENCE)
												.arg(Utf8String::create(column->getName()))
												.arg(column->getTypeName())
												.arg(Utf8String::create(refs[0]->getName()))
						.arg(refs[0]->getTypeName())
						.arg(Utf8String::create(this->getName(true)))
						.arg(this->getTypeName()),
						ERR_REM_INDIRECT_REFERENCE,__PRETTY_FUNCTION__,__FILE__,__LINE__);
			}

			column->setParentTable(nullptr);
			columns.erase(itr);
		}
	}
}
Exemplo n.º 29
0
void Table::restoreRelObjectsIndexes(ObjectType obj_type)
{
	map<QString, unsigned> *obj_idxs=nullptr;

	if(obj_type==OBJ_COLUMN)
		obj_idxs=&col_indexes;
	else
		obj_idxs=&constr_indexes;

	if(!obj_idxs->empty())
	{
		vector<TableObject *> *list=getObjectList(obj_type);
		vector<TableObject *> new_list;
		vector<TableObject *>::iterator itr;
		QString name;
		TableObject *tab_obj=nullptr;
		unsigned i=0, pos=0, size=0, obj_idx, names_used=0, aux_size=0;

		size=list->size();

		/* Indentify the maximum index on the existing rel objects. This is done
	to correctly resize the new list in order to avoid exceed the list bounds
	and consequently crashing the app */
		for(auto &itr : *obj_idxs)
		{
			if(aux_size < (itr.second + 1))
				aux_size=itr.second + 1;
		}

		/* If the auxiliary size is lesser than the current object list size
	   the new list is resized with same capacity of the "list" vector */
		if(aux_size < size)
			aux_size=size;

		new_list.resize(aux_size);

		for(auto &obj : *list)
		{
			name=obj->getName();

			//Check if the current object is a relationship created one and its name is on the custom index map
			if(obj->isAddedByLinking() && obj_idxs->count(name))
			{
				//Allocate the object on its original position
				obj_idx=obj_idxs->at(name);
				new_list[obj_idx]=obj;
				names_used++;
			}
		}

		/* Allocating the other objects, the ones that aren't created by relationship or
	   the one which were created by relationship but weren't positioned yet */
		pos=i=0;
		while(pos < size && i < size)
		{
			tab_obj=list->at(pos);
			name=tab_obj->getName();

			if(!new_list[i] && obj_idxs->count(name)==0)
			{
				new_list[i]=tab_obj;
				pos++;
				i++;
			}
			else if(obj_idxs->count(name)!=0)
				pos++;
			else if(new_list[i])
				i++;
		}

		//Removing unused items (nullptr ones) from the list using remove_if and lambdas (for predicate)
		new_list.erase(remove_if(new_list.begin(), new_list.end(),
								 [](TableObject *obj){ return(obj==nullptr); }), new_list.end());

		(*list)=new_list;

		/* Checking if the object names used are equal to the map size. If not, indicates that
	   one o more objects on the map doesn't exists anymore on the table thus there is
	   the need to updated the object index map */
		if(names_used!=obj_idxs->size())
			saveRelObjectsIndexes(obj_type);
	}
}