Welcome, Guest. Please login or register.
Did you miss your activation email?

Author Topic: variable database connections  (Read 8171 times)

Paul Traut

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
variable database connections
« on: October 26, 2017, 04:11:57 pm »
Hello,

I try to generate templates with variable database connections.
This means I use variables like

DB_HOST
DB_USER
DB_PASSWD

and within the db-connection I say

Host = $V{DB_HOST}
User = $V{DB_USER}

and so on.

The problem is that it doesn’t really work.
It works within the designer, but when I save the template and use it from "outside" it doesn’t work?
 
Is this not possible or do I something wrong?

Paul

Subst

  • Sr. Member
  • ****
  • Posts: 451
    • View Profile
Re: variable database connections
« Reply #1 on: October 26, 2017, 05:02:45 pm »
first try ti check if variables are visible from your application
for this purpose use

report.dataManager()->containsVariable()
where report id instance of LimeReport::ReportEngine

Arin Alex

  • Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1005
    • View Profile
Re: variable database connections
« Reply #2 on: October 26, 2017, 09:35:29 pm »
Hi

Do you init variables after loading a report or before?
If before, you have to init them after loading.   
Second way is to delete the variables description from a report.
« Last Edit: October 26, 2017, 09:39:42 pm by Arin Alex »

Paul Traut

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: variable database connections
« Reply #3 on: October 27, 2017, 08:31:26 am »
Sorry, what do you mean by "delete the variables description from a report"?
How is it done?

Subst

  • Sr. Member
  • ****
  • Posts: 451
    • View Profile
Re: variable database connections
« Reply #4 on: October 27, 2017, 10:13:23 am »
I think Alex means you need to delete variable from report template in designer and create it from your code

Paul Traut

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: variable database connections
« Reply #5 on: October 27, 2017, 11:10:53 am »
ok, I will try it

Paul Traut

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: variable database connections
« Reply #6 on: October 27, 2017, 03:45:42 pm »
sorry  :-[

how can i create variables out from code??

addVariable (const QString &name, const QVariant &value)

does not work

Paul Traut

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: variable database connections
« Reply #7 on: October 27, 2017, 04:26:43 pm »
Hello Alex,

after some tests I realize that it doesen't work.

Here is a sample from my code, where I create a ReportEngine and set the Variables:


 LimeReport::ReportEngine engine;

    debug(QString("Loading report template '%1'").arg(template_path));

    if (!engine.loadFromFile(template_path))
        return_error(QString("Template loading failed: '%1'").arg(engine.lastError()));

    /////////////////////////////////////////////
    // Set database credentials.

    engine.dataManager()->setReportVariable("PMP_DB", getenv("PMP_DB"));
    engine.dataManager()->setReportVariable("PMP_DB_HOST", getenv("PMP_DB_HOST"));
    engine.dataManager()->setReportVariable("PMP_DB_USER", getenv("PMP_DB_USER"));
    engine.dataManager()->setReportVariable("PMP_DB_PASSWD", getenv("PMP_DB_PASSWD"));

    /////////////////////////////////////////////
    // Set report variables.

    MT::PropertiesIterator it (report.properties);

    while (it.hasNext())
    {
        it.next();

        if (!engine.dataManager()->containsVariable(it.key()))
            return_error(QString("The variable '%1' is not defined in the template").arg(it.key()));

        engine.dataManager()->setReportVariable(it.key(), it.value());
    }

It would be great if somehow the datasource connection can be set from "outside" and is not fix in a template

Paul

Subst

  • Sr. Member
  • ****
  • Posts: 451
    • View Profile
Re: variable database connections
« Reply #8 on: October 27, 2017, 05:39:40 pm »
Hi, Paul!
Why you don't use addModel & createCallbackDataSource methods of LimeReport::DataSourceManager ?

And about credentials..
http://limereport.ru/forum/index.php?topic=145.msg927#msg927

link to the theme about this one, may be help you?

Paul Traut

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: variable database connections
« Reply #9 on: November 06, 2017, 11:42:54 am »
Thank you Subst for your help.
I don't know exactly how addModel and createCallbackDataSource methods could help me.

My question is:
Why does my code not work? Do I something wrong?

and why is it not possible to use an already set db connection?
I think there should be a method like

LimeReport::DataSourceManager
useDBConnection( QSqlDatabase db);

so it would be possible to use any given connection from the application.

Paul

Arin Alex

  • Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1005
    • View Profile
Re: variable database connections
« Reply #10 on: November 06, 2017, 12:04:39 pm »
Hi Paul.
You can use default application connection.
If you check "use default application connection" in connection settings,
report engine will get default application connection.
Possible i will add LimeReport::DataSourceManager useDBConnection( QSqlDatabase db) and it will be initialize default report connection. 
What about variables, if you defined variable in designer it will save variable value to the report file and you have to reinit variable value after load report.
Possible i have to change it behaivor but at the moment it works just like this.

Quote
engine.dataManager()->setReportVariable("PMP_DB", getenv("PMP_DB"));
engine.dataManager()->setReportVariable("PMP_DB_HOST", getenv("PMP_DB_HOST"));
engine.dataManager()->setReportVariable("PMP_DB_USER", getenv("PMP_DB_USER"));
engine.dataManager()->setReportVariable("PMP_DB_PASSWD", getenv("PMP_DB_PASSWD"));
I have checked and it works for me. Could you send me a sample report template ?
And one more thing: if you describe some connection in the report template  and connection with the same name already exists limereport won't create new connection it will uses exists
« Last Edit: November 06, 2017, 01:52:06 pm by Arin Alex »

Paul Traut

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: variable database connections
« Reply #11 on: November 06, 2017, 02:11:56 pm »
I have now find a way, how the default connection works for me.
I have created a report template where I use the default connection and within my code I create a new connection with:

    QSqlDatabase defaultConnection = QSqlDatabase::addDatabase("QOCI");
    defaultConnection.setHostName(getenv("PMP_DB_HOST"));
    defaultConnection.setDatabaseName(getenv("PMP_DB"));
    defaultConnection.setUserName(getenv("PMP_DB_USER"));
    defaultConnection.setPassword(getenv("PMP_DB_PASSWD"));
    defaultConnection.open();

and then it works.

the problem is I have to create a connection explicit with the name "defaultConnection" otherwise it does not work and I cannot use my actual connection (for example myConnection) for the report.

I think the method LimeReport::DataSourceManager useDBConnection( QSqlDatabase db) would be very useful at this point.

Paul

Paul Traut

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: variable database connections
« Reply #12 on: November 06, 2017, 04:40:48 pm »
Hello Alex,

you say in this topic

http://limereport.ru/forum/index.php?topic=145.msg927#msg927

"LimeReport can use a connection which was established by an application by name;"

How is this done?

I think this is what I need in my situation.

Paul
« Last Edit: November 07, 2017, 10:08:31 am by Paul Traut »

Arin Alex

  • Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 1005
    • View Profile
Re: variable database connections
« Reply #13 on: November 07, 2017, 11:24:13 am »
Hi Paul.

if you describe some connection in the report template and а connection with the same name already exists limereport won't create new connection, it will use exists.

Paul Traut

  • Jr. Member
  • **
  • Posts: 59
    • View Profile
Re: variable database connections
« Reply #14 on: November 07, 2017, 12:32:17 pm »
Ah

Ok thank you

Paul