Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ SQL » sql session in a global variable?
sql session in a global variable? [message #34831] Tue, 13 December 2011 22:55 Go to next message
Wolfgang is currently offline  Wolfgang
Messages: 146
Registered: November 2011
Location: Germany
Experienced Member
Hi,

I have a method in which my sqlite session is being connected:

.h
class sqltry
{
	public:
		typedef sqltry CLASSNAME;
		sqltry();
	private:
		Sqlite3Session sqlite3;
		Sql sqlPointer;


.cpp
bool sqltry::connect(String path)
{
	if (!sqlite3.Open(path))
	{
		return false;
	}
	Sql sql(sqlite3);
	sqlPointer = sql;
}


But if i try to use the sqlPointer in another method of the same class my app stops working and crashes...

.cpp
bool sqltry::atest(S)
{
	if (sqlPointer.Execute("create table t2 (t1key INTEGER PRIMARY KEY,data TEXT,num double,timeEnter DATE);"))
		PromptOK("DONE!");
	return true;
}



Whats wrong with it?
Re: sql session in a global variable? [message #34839 is a reply to message #34831] Wed, 14 December 2011 10:29 Go to previous messageGo to next message
mr_ped is currently offline  mr_ped
Messages: 805
Registered: November 2005
Location: Czech Republic - Praha
Experienced Contributor

Didn't try the sources, so just by quick reading it...

The "sql" in connect procedure is local variable, so it gets destroyed once it gets out of scope. I'm afraid this is making it's content invalid. The assignment to "sqlPointer", unless the Sql class has very clever copy constructor, will not help to preserve it. I'm too lazy to check actual Sql source, sorry. Wink

Anyway, I don't like the "sqlPointer" variable name, because it is not pointer, but an object, so the variable name is misleading.

I would suggest to completely remove sqlPointer class member variable, and create new instance of Sql every time you need it.

See http://www.ultimatepp.org/srcdoc$Sql$tutorial$en-us.html , especially step 2 may help you. After you set global "SQL", you can then get new instance by simple "Sql sql;" in your code.
Re: sql session in a global variable? [message #34840 is a reply to message #34831] Wed, 14 December 2011 10:33 Go to previous messageGo to next message
Wolfgang is currently offline  Wolfgang
Messages: 146
Registered: November 2011
Location: Germany
Experienced Member
you're right, the "pointer" in var name is misleading... was just my first thought that is is one.

I know about the global SQL but this is it, i want to avoid - I would prefer a variable defined by myself because maybe i need more than one database connection.. and so i tried to handle it.
Re: sql session in a global variable? [message #34842 is a reply to message #34831] Wed, 14 December 2011 11:03 Go to previous messageGo to next message
mr_ped is currently offline  mr_ped
Messages: 805
Registered: November 2005
Location: Czech Republic - Praha
Experienced Contributor

well, after reading that tutorial myself I think your first version may work if you change connect:
bool sqltry::connect(String path)
{
	if (!sqlite3.Open(path))
	{
		return false;
	}
	sqlPointer = sqlite3;
}

And of course rename that sqlPointer to something more verbose, like "someDatabaseForThisAndThatSql", so when you read it two years later, you will get idea what is it good for.
(to get pointers you would need to start to use "*", "&" and new/delete where appropriate. I don't believe you want to do that in this case. Smile

edit - addition:
you can of course use fresh instances as needed even with multiple databases. I would maybe put the most used one into SQL, so then Sql instances for that one would be "Sql sql;", Sql instances for other databases would be created like "Sql sql(other_db_session);"

[Updated on: Wed, 14 December 2011 11:08]

Report message to a moderator

Re: sql session in a global variable? [message #34854 is a reply to message #34842] Thu, 15 December 2011 08:29 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12581
Registered: November 2005
Ultimate Member
Ehm, I have created a reply here, but it seems I forgot to hit "Send".

Well, the problem here is that you cannot assign Sql nor SqlSession. It was a minor bug that copy constructor and operator= were not made private.

So what you can now is something like:

class sqltry
{
	public:
		typedef sqltry CLASSNAME;
		sqltry();
	private:
		Sqlite3Session sqlite3;
                One<Sql> sql;
		Sql& SQL() {
                    if(!sql)
                          sql = new Sql(sqlite3);
                    return *sql;
                }


...and then use SQL()...

Re: sql session in a global variable? [message #34858 is a reply to message #34854] Thu, 15 December 2011 09:51 Go to previous messageGo to next message
Wolfgang is currently offline  Wolfgang
Messages: 146
Registered: November 2011
Location: Germany
Experienced Member
I see what u mean but upp tells me
Quote:

e:\ultimate++\myapps\urlaub\SQL.h(25) : error C2091: function returns function
e:\ultimate++\myapps\urlaub\SQL.h(27) : error C2440: 'return' : cannot convert from 'Upp::Sql' to 'Upp::Sql &(__cdecl *)(void)'


Sqlite3Session sqlSession;
One<Sql> sql;
Sql& SQL()
{
  if(!sql) sql = new Sql(sqlSession);
  return *sql;
}


Line 27 is return *sql;

[Updated on: Thu, 15 December 2011 09:54]

Report message to a moderator

Re: sql session in a global variable? [message #34860 is a reply to message #34858] Thu, 15 December 2011 11:23 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12581
Registered: November 2005
Ultimate Member
Sorry, name clash with SQL define, rename to e.g. ISQL.
Re: sql session in a global variable? [message #34861 is a reply to message #34831] Thu, 15 December 2011 11:26 Go to previous messageGo to next message
Wolfgang is currently offline  Wolfgang
Messages: 146
Registered: November 2011
Location: Germany
Experienced Member
ok... but after all the thinking about a good design for such a sqlite class i decided not to write one. The use of sqlite is such simple with the existing sqlite class in upp that there is no need for an additional class. Very Happy

Sorry for the trouble i caused!

[Updated on: Thu, 15 December 2011 11:26]

Report message to a moderator

Re: sql session in a global variable? [message #34863 is a reply to message #34861] Thu, 15 December 2011 11:56 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12581
Registered: November 2005
Ultimate Member
Anyway, based on this conversation, I have found to be a good (and simple to implement) idea to have Sql::SetSession method.

So your example would be now basically possible to write as:

struct SqlTry {
		Sqlite3Session sqlite3;
                Sql sql;

bool connect(String path)
{
	if (!sqlite3.Open(path))
	{
		return false;
	}
        sql.SetSession(sqlite3);
}

Re: sql session in a global variable? [message #34864 is a reply to message #34831] Thu, 15 December 2011 11:58 Go to previous messageGo to next message
Wolfgang is currently offline  Wolfgang
Messages: 146
Registered: November 2011
Location: Germany
Experienced Member
and then, is Sql sql usable in the whole class after this?
Re: sql session in a global variable? [message #34866 is a reply to message #34864] Thu, 15 December 2011 12:49 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12581
Registered: November 2005
Ultimate Member
"whole class" is somewhat strange definition, but "yes".

Further development: Perhaps rather use Sql0 instead of Sql... (both should work, but Sql might create some DB connection to global SQL session that is then deleted in SetSession).
Re: sql session in a global variable? [message #40754 is a reply to message #34863] Thu, 12 September 2013 22:14 Go to previous messageGo to next message
Alboni is currently offline  Alboni
Messages: 196
Registered: January 2012
Location: Deventer, Netherlands
Experienced Member
mirek wrote on Thu, 15 December 2011 11:56

Anyway, based on this conversation, I have found to be a good (and simple to implement) idea to have Sql::SetSession method.

So your example would be now basically possible to write as:

[code]
.....<snip>
sql.SetSession(sqlite3);





But............. Sql::SetSession is a private member.....
Re: sql session in a global variable? [message #54224 is a reply to message #40754] Fri, 12 June 2020 22:41 Go to previous messageGo to next message
coolman is currently offline  coolman
Messages: 64
Registered: April 2006
Location: Czech Republic
Member
Hi,

I know this is old thread but why the SetSession() is private?

Thanks, Radek

Re: sql session in a global variable? [message #54244 is a reply to message #54224] Sun, 14 June 2020 10:18 Go to previous message
mirek is currently offline  mirek
Messages: 12581
Registered: November 2005
Ultimate Member
coolman wrote on Fri, 12 June 2020 22:41
Hi,

I know this is old thread but why the SetSession() is private?

Thanks, Radek



Because it might be difficult / impossible to do such change for Sql when some operations are in progress. Or it seemed that way at the time.

Mirek
Previous Topic: Application with two different databases schema
Next Topic: SqlArray with an Option
Goto Forum:
  


Current Time: Wed Jul 15 02:52:19 CEST 2020

Total time taken to generate the page: 0.02323 seconds