15 #include <QSqlDatabase>
23 Writer( parent ), m_placemarkId( 0 )
25 QSqlDatabase database = QSqlDatabase::addDatabase(
"QSQLITE" );
26 database.setDatabaseName( filename );
27 if ( !database.open() ) {
28 qCritical() <<
"Failed to connect to database";
32 execQuery(
"DROP TABLE IF EXISTS placemarks;" );
33 execQuery(
"CREATE TABLE placemarks ("
40 execQuery(
"DROP TABLE IF EXISTS names" );
41 execQuery(
"CREATE TABLE names ("
42 " id INTEGER PRIMARY KEY,"
43 " name VARCHAR(50) )" );
44 execQuery(
"DROP TABLE IF EXISTS regions" );
45 execQuery(
"CREATE TABLE regions ("
46 " id INTEGER PRIMARY KEY,"
47 " parent INTEGER NOT NULL,"
48 " lft INTEGER NOT NULL,"
49 " rgt INTEGER NOT NULL,"
53 execQuery(
"DROP VIEW IF EXISTS places" );
54 execQuery(
"CREATE VIEW places AS "
56 " placemarks.regionId AS region,"
57 " names.name AS name,"
58 " placemarks.number AS number,"
59 " placemarks.category AS category,"
60 " placemarks.lon AS lon,"
61 " placemarks.lat AS lat"
63 " INNER JOIN placemarks"
64 " ON names.id=placemarks.nameId" );
65 execQuery(
"BEGIN TRANSACTION" );
70 execQuery(
"END TRANSACTION" );
71 execQuery(
"CREATE INDEX namesIndex ON names(name)" );
72 execQuery(
"CREATE INDEX placemarksIndex ON placemarks(regionId,nameId,category)" );
73 execQuery(
"CREATE INDEX regionsIndex ON regions(name,parent,lft,rgt)" );
79 query.prepare(
"INSERT INTO regions"
80 " (id, parent, lft, rgt, name, lon, lat)"
81 " VALUES (?, ?, ?, ?, ?, ?, ?)" );
82 query.addBindValue( ( qint32 ) region.
identifier() );
84 query.addBindValue( ( qint32 ) region.
left() );
85 query.addBindValue( ( qint32 ) region.
right() );
86 query.addBindValue( region.
name() );
88 query.addBindValue( region.
latitude() );
94 if ( m_lastPlacemark.second != placemark.
name() && !m_placemarks.contains( placemark.
name() ) ) {
95 m_lastPlacemark.first = ++m_placemarkId;
96 m_lastPlacemark.second = placemark.
name();
97 m_placemarks[m_lastPlacemark.second] = m_lastPlacemark.first;
99 QSqlQuery insertQuery;
100 insertQuery.prepare(
"INSERT INTO names"
103 insertQuery.addBindValue( m_lastPlacemark.first );
104 insertQuery.addBindValue( m_lastPlacemark.second );
105 execQuery( insertQuery );
108 Q_ASSERT( m_placemarks.contains( placemark.
name() ) );
111 query.prepare(
"INSERT INTO placemarks"
112 " (regionId, nameId, number, category, lon, lat)"
113 " VALUES (?, ?, ?, ?, ?, ?)" );
114 query.addBindValue( ( qint32 ) placemark.
regionId() );
115 query.addBindValue( m_placemarks[placemark.
name()] );
117 query.addBindValue( ( qint32 ) placemark.
category() );
118 query.addBindValue( placemark.
longitude() );
119 query.addBindValue( placemark.
latitude() );
123 void SqlWriter::execQuery(
const QString &query )
const
125 QSqlQuery sqlQuery( query );
126 if ( sqlQuery.lastError().isValid() ) {
127 qCritical() <<
"Problems occurred when executing the query" << query;
128 qCritical() <<
"SQL error: " << sqlQuery.lastError();
132 void SqlWriter::execQuery( QSqlQuery &query )
const
135 if ( query.lastError().isValid() ) {
136 qCritical() <<
"Problems occurred when executing the query" << query.executedQuery();
137 qCritical() <<
"SQL error: " << query.lastError();
A lightweight data structure to represent administrative regions like villages, cities, states, ...
A lightweight data structure to represent points of interest like addresses with support for serializ...
void addOsmRegion(const OsmRegion ®ion)
int regionId() const
Identifier of the smallest region containing this placemark, 0 if none (~main area).
QString houseNumber() const
Placemark's house number, if any.
qreal latitude() const
Latitude of the placemark's center point, in degree.
QString name() const
Placemark name.
OsmCategory category() const
int parentIdentifier() const
qreal longitude() const
Longitude of the placemark's center point, in degree.
int identifier() const
Unique (per process) region identifier.
qreal longitude() const
Longitude of the region's center point, in degree.
qreal latitude() const
Latitude of the region's center point, in degree.
void addOsmPlacemark(const OsmPlacemark &placemark)
SqlWriter(const QString &filename, QObject *parent=0)