Project

General

Profile

What is the simple way for writing read-only viewer of database table?

Added by Dmitri Markov over 9 years ago

Hello.

I'm start to learn this framework and I can't found information how I can do the subject in simple way. This program is the program for monitor of data in database. I need only read-only access, I don't need update or insert data in this table.

I found exmple with mapping class(it's not working for me, but it's another question) for ORM model, but if I need only write table in web-browser that will be only show data without any update action I really need use so difficult metod or maybe exist easy way(maybe simple example will be anougth for me, but I can't find such example)?

Thanks


Replies (10)

RE: What is the simple way for writing read-only viewer of database table? - Added by Alex V over 9 years ago

If you want to take full advantage of Wt you need to map the classes to the session.

You will probably need to customise the mapping of the class as it is unlikely your database has a version field.

see section 7 of the Wt::Dbo tutorial

http://www.webtoolkit.eu/wt/doc/tutorial/dbo/tutorial.html#customizing

you could also look into using queries directly but you will need to have a class defined that describes the result

eg (from the dbo tutorial)

session.query("select count(1) from user").where("name = ?").bind("Joe");

RE: What is the simple way for writing read-only viewer of database table? - Added by Dmitri Markov over 9 years ago

Sorry, I was caresless.

I changed my source code. I disable version field and change ID field, but it's not work.

This is source code of my class for table.

#include

#include

#include

#include

class InfoVersion {

public:

Wt::WDateTime DATEON;

Wt::WDateTime EMAIL_DATE;

std::string MO_TITLE;

std::string SERVER_GUID;

std::string SERVER_HOST_NAME;

int ORDER_SERVER_NUMBER;

std::string VERSION_KSAMU;

std::string VERSION_SERVER_KSAMU;

int RECORDS_REESTR;

Wt::WDateTime LASTDATE_CHECK_CARTS;

std::string VERSION_MAINBASE;

std::string VERSION_BASEFILES;

std::string NOTE;

std::string ALIAS_NAME;

std::string SERVER;

template

void persist(Action& a)

{

Wt::Dbo::id(a, SERVER, "SERVER", 16);

Wt::Dbo::field(a, DATEON, "DATEON");

Wt::Dbo::field(a, EMAIL_DATE, "EMAIL_DATE");

Wt::Dbo::field(a, MO_TITLE, "MO_TITLE");

Wt::Dbo::field(a, SERVER_GUID, "SERVER_GUID");

Wt::Dbo::field(a, SERVER_HOST_NAME, "SERVER_HOST_NAME");

Wt::Dbo::field(a, ORDER_SERVER_NUMBER, "ORDER_SERVER_NUMBER");

Wt::Dbo::field(a, VERSION_KSAMU, "VERSION_KSAMU");

Wt::Dbo::field(a, VERSION_SERVER_KSAMU, "VERSION_SERVER_KSAMU");

Wt::Dbo::field(a, RECORDS_REESTR, "RECORDS_REESTR");

Wt::Dbo::field(a, LASTDATE_CHECK_CARTS, "LASTDATE_CHECK_CARTS");

Wt::Dbo::field(a, VERSION_MAINBASE, "VERSION_MAINBASE");

Wt::Dbo::field(a, VERSION_BASEFILES, "VERSION_BASEFILES");

Wt::Dbo::field(a, NOTE, "NOTE");

Wt::Dbo::field(a, ALIAS_NAME, "ALIAS_NAME");

}

};

typedef Wt::Dbo::ptr InfoVersionPtr;

typedef Wt::Dbo::collection InfoVersions;

namespace Wt {

namespace Dbo {

template<>

struct dbo_traits : public dbo_default_traits {

typedef std::string IdType;

static IdType invalidId() { return std::string(); }

static const char *surrogateIdField() { return 0; }

static const char *versionField() { return 0; }

};

}

}

And this is source code of my application.

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include <InfoVersion.C>

#include

class MonitorKSAMUApplication : public Wt::WApplication

{

public:

MonitorKSAMUApplication(const Wt::WEnvironment& env);

~MonitorKSAMUApplication();

private:

Wt::WTable *table;

Wt::Dbo::backend::Firebird *dbConnect;

Wt::Dbo::Session session;

};

MonitorKSAMUApplication::MonitorKSAMUApplication(const Wt::WEnvironment& env):WApplication(env)

{

setTitle(Wt::utf8("Монитор КСАМУ"));

dbConnect = new Wt::Dbo::backend::Firebird();

try

{

dbConnect->connect("localhost", "/media/dmitri/NonCriticalData/DATABASE/Firebird/INFOVERSION.FDB", "SYSDBA", "masterkey", "", "WIN1251", "");

session.setConnection(*dbConnect);

std::string SQLRegLastVersions = "select a.server, A.DATEON, M.title MO_TITLE, S.server_guid, S.server_host_name, ";

SQLRegLastVersions = SQLRegLastVersions + "S.number ORDER_SERVER_NUMBER, A.VERSION_KSAMU, A.VERSION_SERVER_KSAMU, A.RECORDS_REESTR, ";

SQLRegLastVersions = SQLRegLastVersions + "A.LASTDATE_CHECK_CARTS, A.VERSION_MAINBASE, A.VERSION_BASEFILES, S.NOTE, A.EMAIL_DATE, ";

SQLRegLastVersions = SQLRegLastVersions + "S.ALIAS_NAME ";

SQLRegLastVersions = SQLRegLastVersions + "from REG_LAST_INFO_VERSION_KSAMU A ";

SQLRegLastVersions = SQLRegLastVersions + "inner join ref_servers S on s.id=a.server ";

SQLRegLastVersions = SQLRegLastVersions + "inner join ref_mo m on m.id=s.mo ";

SQLRegLastVersions = SQLRegLastVersions + "order by A.DATEON desc ";

Wt::Dbo::Query query = session.find(SQLRegLastVersions);

InfoVersions LastInfoversions = query.resultList();

root()->addWidget(new Wt::WText(Wt::utf8("Монитор КСАМУ")));

table = new Wt::WTable();

table->setHeaderCount(LastInfoversions.size()+1);

table->setWidth("100%");

table~~elementAt(0, 0)>addWidget(new Wt::WText(Wt::utf8("Обновлено")));

table
elementAt(0, 1)>addWidget(new Wt::WText(Wt::utf8("Создано")));

table
elementAt(0, 2)>addWidget(new Wt::WText(Wt::utf8("Сервер")));

table
elementAt(0, 3)~~>addWidget(new Wt::WText(Wt::utf8("Версия КСАМУ")));

int NumRow = 1;

for (InfoVersions::const_iterator i = LastInfoversions.begin(); i != LastInfoversions.end(); ++i)

{

table~~elementAt(NumRow, 0)>addWidget(new Wt::WText(""));

table
elementAt(NumRow, 1)>addWidget(new Wt::WText(""));

table
elementAt(NumRow, 2)>addWidget(new Wt::WText(Wt::utf8((*i)->ALIAS_NAME)));

table
elementAt(NumRow, 3)~~>addWidget(new Wt::WText(Wt::utf8((*i)->VERSION_KSAMU)));

NumRow;

}

root()->addWidget(table);

}

catch(...)

{

root()->addWidget(new Wt::WText(Wt::utf8("Something wrong")));

}

}

MonitorKSAMUApplication::~MonitorKSAMUApplication()

{

delete table;

delete dbConnect;

}

Wt::WApplication *createApplication(const Wt::WEnvironment& env)

{

return new MonitorKSAMUApplication(env);

}

int main(int argc, char **argv)

{

return Wt::WRun(argc, argv, &createApplication);

}

The exception occured in this line Wt::Dbo::Query<InfoVersionPtr> query = session.find<InfoVersion>(SQLRegLastVersions);

What is wrong in my code?

Thanks

RE: What is the simple way for writing read-only viewer of database table? - Added by Wim Dumon over 9 years ago

I believe you need a transaction before you can execute query.find.

If you catch the exception (std::exception) and print the error in the exception, you'll likely see more details than just 'Something wrong':

catch(std::exception &e) {
  root()->addWidget(new Wt::WText(Wt::utf8(e.what())));
}

RE: What is the simple way for writing read-only viewer of database table? - Added by Dmitri Markov over 9 years ago

Ok. I understand my mistakes. This code is worked, but it's not easy, because I must to create View-table for my SQL-query and then mapclass for this. It's really not easy way (

This is my complete source code.

#include

#include

#include

#include

#include

class InfoVersion {

public:

Wt::WDateTime DATEON;

Wt::WDateTime EMAIL_DATE;

Wt::WString MO_TITLE;

Wt::WString SERVER_GUID;

Wt::WString SERVER_HOST_NAME;

int ORDER_SERVER_NUMBER;

Wt::WString VERSION_KSAMU;

Wt::WString VERSION_SERVER_KSAMU;

int RECORDS_REESTR;

Wt::WDateTime LASTDATE_CHECK_CARTS;

Wt::WString VERSION_MAINBASE;

Wt::WString VERSION_BASEFILES;

Wt::WString NOTE;

std::string ALIAS_NAME;

std::string SERVER;

template

void persist(Action& a)

{

Wt::Dbo::id(a, SERVER, "SERVER", 16);

Wt::Dbo::field(a, DATEON, "DATEON");

Wt::Dbo::field(a, MO_TITLE, "MO_TITLE");

Wt::Dbo::field(a, SERVER_GUID, "SERVER_GUID");

Wt::Dbo::field(a, SERVER_HOST_NAME, "SERVER_HOST_NAME");

Wt::Dbo::field(a, ORDER_SERVER_NUMBER, "ORDER_SERVER_NUMBER");

Wt::Dbo::field(a, VERSION_KSAMU, "VERSION_KSAMU");

Wt::Dbo::field(a, VERSION_SERVER_KSAMU, "VERSION_SERVER_KSAMU");

Wt::Dbo::field(a, RECORDS_REESTR, "RECORDS_REESTR");

Wt::Dbo::field(a, LASTDATE_CHECK_CARTS, "LASTDATE_CHECK_CARTS");

Wt::Dbo::field(a, VERSION_MAINBASE, "VERSION_MAINBASE");

Wt::Dbo::field(a, VERSION_BASEFILES, "VERSION_BASEFILES");

Wt::Dbo::field(a, NOTE, "NOTE");

Wt::Dbo::field(a, EMAIL_DATE, "EMAIL_DATE");

Wt::Dbo::field(a, ALIAS_NAME, "ALIAS_NAME");

}

};

typedef Wt::Dbo::ptr InfoVersionPtr;

typedef Wt::Dbo::collection InfoVersions;

namespace Wt {

namespace Dbo {

template<>

struct dbo_traits : public dbo_default_traits {

typedef std::string IdType;

static IdType invalidId() { return std::string(); }

static const char *surrogateIdField() { return 0; }

static const char *versionField() { return 0; }

};

}

}

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include <InfoVersion.C>

#include

class MonitorKSAMUApplication : public Wt::WApplication

{

public:

MonitorKSAMUApplication(const Wt::WEnvironment& env);

~MonitorKSAMUApplication();

private:

Wt::WTable *table;

Wt::Dbo::backend::Firebird *dbConnect;

Wt::Dbo::Session session;

};

MonitorKSAMUApplication::MonitorKSAMUApplication(const Wt::WEnvironment& env):WApplication(env)

{

setTitle(Wt::utf8("Монитор КСАМУ"));

dbConnect = new Wt::Dbo::backend::Firebird();

try

{

dbConnect->connect("localhost", "/media/dmitri/NonCriticalData/DATABASE/Firebird/INFOVERSION.FDB", "SYSDBA", "masterkey", "", "WIN1251", "");

session.setConnection(*dbConnect);

session.mapClass("VREG_LAST_INFO_VERSION_KSAMU");

Wt::Dbo::Transaction transaction(session);

Wt::Dbo::Query query = session.find();

InfoVersions LastInfoversions = query.resultList();

root()->addWidget(new Wt::WText(Wt::utf8("Монитор КСАМУ")));

table = new Wt::WTable();

table->setHeaderCount(LastInfoversions.size()+1);

table->setWidth("100%");

table~~elementAt(0, 0)>addWidget(new Wt::WText(Wt::utf8("Обновлено")));

table
elementAt(0, 1)>addWidget(new Wt::WText(Wt::utf8("Создано")));

table
elementAt(0, 2)>addWidget(new Wt::WText(Wt::utf8("Сервер")));

table
elementAt(0, 3)~~>addWidget(new Wt::WText(Wt::utf8("Версия КСАМУ")));

int NumRow = 1;

for (InfoVersions::const_iterator i = LastInfoversions.begin(); i != LastInfoversions.end(); ++i)

{

table~~elementAt(NumRow, 0)>addWidget(new Wt::WText(""));

table
elementAt(NumRow, 1)>addWidget(new Wt::WText(""));

table
elementAt(NumRow, 2)>addWidget(new Wt::WText(Wt::utf8((*i)->ALIAS_NAME)));

table
elementAt(NumRow, 3)~~>addWidget(new Wt::WText((*i)->VERSION_KSAMU));

NumRow;

}

root()->addWidget(table);

transaction.commit();

}

catch(std::exception &e)

{

root()->addWidget(new Wt::WText(Wt::utf8(e.what())));

}

}

MonitorKSAMUApplication::~MonitorKSAMUApplication()

{

delete table;

delete dbConnect;

}

Wt::WApplication *createApplication(const Wt::WEnvironment& env)

{

return new MonitorKSAMUApplication(env);

}

int main(int argc, char **argv)

{

return Wt::WRun(argc, argv, &createApplication);

}

It's worked, I may see result in my webbrowser, but text with russian leterrs looks like ??. In DB use codepage WIN1251 and russian letters. Use of WIN1251 isn't good, but I can't change it.

Use of Wt::utf8((*i)ALIAS_NAME) or (*i)>ALIAS_NAME.toUTF8() or change std::string to Wt::WString were not helpful, something changed because ? looks a bit differrent, but it's not russian chars.

What I'm doing wrong?

Thanks

RE: What is the simple way for writing read-only viewer of database table? - Added by Wim Dumon over 9 years ago

Hi,

I'm not an exert in charsets or charset conversions for Firebird. Normally, your DB stores strings in a particular charset, and client libraries can request a format of strings retrieved from the DB. If those charsets are not the same, a conversion should occur.

Wt works internally with UTF-8, so it's always easier if your strings are already in UTF-8. It looks to me that you're requesting WIN1251 formatted strings from the database. My first proposal would be to write "UTF8" as charset field in the Firebird connection constructor instead of "WIN1251". A quick google seems to confirm that the DB format will then be converted to UTF-8 when transmitted to the client.

It's not clear from your mail if you also have problems with the Russion text embedded in your cpp code. If that is the case, the easiest solution is to write your strings as L"Russian string". The best solution is not to store non-ascii text in your cpp code, but use a resource bundle instead (using Wt's tr() mechanism).

Best regards,

Wim.

RE: What is the simple way for writing read-only viewer of database table? - Added by Dmitri Markov over 9 years ago

To show russian text that is static in code I use such command Wt::utf8("Обновлено") - it's work great.

To show text from DB I use the same code, but it's not work. My connection string is "dbConnect->connect("localhost", "/media/dmitri/NonCriticalData/DATABASE/Firebird/INFOVERSION.FDB", "SYSDBA", "masterkey", "", "WIN1251", "");"

WIN1251 - it's codepage of my DB. It's all ok if I try to use FlameRobin to show result of my query, but using Wt it's not work :-(

I tried this comand "new Wt::WText(L"Мой текст")" and this really work too. But I can't get the value from DB.

I tried to use for store of value from DB Wt::Wstring and std::string, but the result in browser not looks like russian chars(it's different variations of ). I don't understand what I'm doint wrong.

RE: What is the simple way for writing read-only viewer of database table? - Added by Wim Dumon over 9 years ago

Dmitri,

So did you try to change your connect string into dbConnect->connect("localhost", "/media/dmitri/NonCriticalData/DATABASE/Firebird/INFOVERSION.FDB", "SYSDBA", "masterkey", "", "UTF8", "");

Wim.

RE: What is the simple way for writing read-only viewer of database table? - Added by Dmitri Markov over 9 years ago

Wim, it's crazy, but your advice helped me. I changed charset in connection string from WIN1251 to UTF8.

It's really work. I really don't understand why and how, because it's wrong charset for my DB.

I attached the screenshot of FlameRobin connection and properties of DB.

Thanks

RE: What is the simple way for writing read-only viewer of database table? - Added by Wim Dumon over 9 years ago

Hey Dmitri,

This works because the "UTF8" tells the firebird client what format the string format is on the client, irrespective of how it is stored in the database. The character set for all string data is translated in transit by the server or client software.

Best regards,

Wim.

RE: What is the simple way for writing read-only viewer of database table? - Added by Dmitri Markov over 9 years ago

I did such experiment.

I changed charset WIN1251->UTF8 in FlameRobin connection then open properties of table. Description string is ok(russian chars shows normal) and the result of query also normal.

Then I start my Windows and use IBExpert I changed connection properies. Description string of columns and tables don't shows normall(no russian chars), but the result of query stay normall. So it's IBExpert bug? And so FlameRobin doing some extra actions before show result of query.

I don't no about such conversions beetween client and server. I think that it's charset of DB and I must use of it always in any connection. I will remember this feature for all my life.

I think that my first problems is solved. I will continue my small project.

Thank you very much, Wim.

    (1-10/10)