U++ framework
Do not panic. Ask here before giving up.

Home » U++ Library support » U++ SQL » How to display fields from different tables in one SQLArray
template + convert problem [message #23153 is a reply to message #22348] Sat, 19 September 2009 14:22 Go to previous messageGo to previous message
sergeynikitin is currently offline  sergeynikitin
Messages: 748
Registered: January 2008
Location: Moscow, Russia
Contributor

I make a table in which multiple fields of reference from other tables. The mapping of the field from another table I made with the converter.

Now it looks as follows:

converts.h

struct ConvCompany : Convert
{
	virtual Value Format(const Value& q) const;
};

struct ConvNomencl : Convert
{
	virtual Value Format(const Value& q) const;
};

struct ConvManager : Convert
{
	virtual Value Format(const Value& q) const;
};


converts.cpp:
Value ConvCompany::Format(const Value &q) const
{
	if(q.IsNull()) return Null;
	static VectorMap<int, String> comp;
	static Time lastcleartime;
	if(GetSysTime()-lastcleartime > 600){
		comp.Clear();
		lastcleartime=GetSysTime();
	}
	int f = comp.Find(int(q));
	if(f >= 0){
		return comp[f];
	} else {
		Sql sql;
		sql * Select(COM_NAME).From(COMPANY).Where(COM_ID == q);
		String company;
		if(sql.Fetch()) {
			int sid = int(q);
			company = sql[COM_NAME];
			comp.Add(sid, company);
		} else {
			company = "";
		}
		return company;
	}
}
Value ConvNomencl::Format(const Value& q) const
{
	if(q.IsNull()) return Null;
	static VectorMap<int, String> nom;
	static Time lastcleartime;
	if(GetSysTime()-lastcleartime > 600){
		nom.Clear();
		lastcleartime=GetSysTime();
	}
	int f = nom.Find(int(q));
	if(f >= 0){
		return nom[f];
	} else {
		Sql sql;
		sql * SqlSelect(NOM_ID,NOM_NAME).From(NOMENCL).Where(NOM_ID == q);
		String nomencl;
		if(sql.Fetch()) {
			int sid = sql[NOM_ID];
			nomencl = sql[NOM_NAME];
			nom.Add(sid, nomencl);
		} else {
			nomencl = "";
		}
		return nomencl;
	}
}

Value ConvManager::Format(const Value &q) const
{
	if(q.IsNull()) return Null;
	static VectorMap<int, String> man;
	static Time lastcleartime;
	if(GetSysTime()-lastcleartime > 600){
		man.Clear();
		lastcleartime=GetSysTime();
	}
	int f = man.Find(int(q));
	if(f >= 0){
		return man[f];
	} else {
		Sql sql;
		sql * SqlSelect(USR_REALNAME).From(USER).Where(USR_ID == q);
		String manager;
		if(sql.Fetch()) {
			int sid = int(q);
			manager = sql[USR_REALNAME];
			man.Add(sid, manager);
		} else {
			manager = "";
		}
		return manager;
	}
}


These converters differ only in the name of the table and field names. I want this to be in the form of a template, something like:
template <SqlId TBL ,SqlId ID, SqlId NAME,int CLEARTIME>
struct ConvDict : Convert
//ConvDict<COMPANY,COM_ID,COM_NAME,600>()
{
	virtual Value Format(const Value& q) const {
		if(q.IsNull()) return Null;
		static VectorMap<int, String> dict;
		static Time lastcleartime;
		if(GetSysTime()-lastcleartime > CLEARTIME){
			dict.Clear();
			lastcleartime=GetSysTime();
		}
		int f = dict.Find(int(q));
		if(f >= 0){
			return dict[f];
		} else {
			Sql sql;
			sql * Select(NAME).From(TBL).Where(ID == q);
			String s;
			if(sql.Fetch()) {
				int sid = int(q);
				s = sql[NAME];
				dict.Add(sid, s);
			} else {
				s = "";
			}
			return s;
		}
	}
};


But this code is not compiled, wrote that at this point can not be applied SqlId.

Can someone experienced with this?

How do make a template for the Converter, or Display?


SergeyNikitin<U++>( linux, wine )
{
    under( Ubuntu || Debian || Raspbian );
}
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: DBF crash error while writing record
Next Topic: PATCH/BUGFIX Oracle asTable Failure - Solution
Goto Forum:
  


Current Time: Sat Apr 25 15:12:53 GMT+2 2026

Total time taken to generate the page: 0.00839 seconds