LimeReport Forum
General Category | Основное => Discussion | Обсуждение => Topic started by: Paul Traut 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
-
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
-
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.
-
Sorry, what do you mean by "delete the variables description from a report"?
How is it done?
-
I think Alex means you need to delete variable from report template in designer and create it from your code
-
ok, I will try it
-
sorry :-[
how can i create variables out from code??
addVariable (const QString &name, const QVariant &value)
does not work
-
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
-
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?
-
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
-
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.
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
-
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
-
Hello Alex,
you say in this topic
http://limereport.ru/forum/index.php?topic=145.msg927#msg927 (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
-
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.
-
Ah
Ok thank you
Paul
-
But than again, my connection for the template would be "fix". At least the name :(
Still think this
LimeReport::DataSourceManager useDBConnection( QSqlDatabase db);
would be the best solution ;)
Paul
-
No problem, i'll add it :)