Skip to content

Commit d2e3884

Browse files
author
rblazek
committed
added isOrphan, deleteAttributes
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@4941 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 0ea594b commit d2e3884

File tree

2 files changed

+157
-34
lines changed

2 files changed

+157
-34
lines changed

src/providers/grass/qgsgrassprovider.cpp

+134-34
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,7 @@ bool QgsGrassProvider::startEdit ( void )
13611361

13621362
return false;
13631363
}
1364+
Vect_set_category_index_update ( map->map );
13641365

13651366
// Write history
13661367
Vect_hist_command ( map->map );
@@ -1963,7 +1964,62 @@ int QgsGrassProvider::dbLinkField ( int link )
19631964
return ( fi->number );
19641965
}
19651966

1967+
QString *QgsGrassProvider::executeSql ( int field, const QString &sql )
1968+
{
1969+
#ifdef QGISDEBUG
1970+
std::cerr << "QgsGrassProvider::executeSql field = " << field << std::endl;
1971+
#endif
1972+
1973+
QString *error = new QString();
1974+
struct field_info *fi = Vect_get_field( mMap, field); // should work also with field = 0
1975+
1976+
// Read attributes
1977+
if ( fi == NULL ) {
1978+
#ifdef QGISDEBUG
1979+
std::cerr << "No field info -> no attributes" << std::endl;
1980+
#endif
1981+
error->setLatin1( "Cannot get field info" );
1982+
return error;
1983+
}
1984+
1985+
#ifdef QGISDEBUG
1986+
std::cerr << "Field info found -> open database" << std::endl;
1987+
#endif
1988+
1989+
QgsGrass::setMapset ( mGisdbase, mLocation, mMapset );
1990+
dbDriver *driver = db_start_driver_open_database ( fi->driver, fi->database );
1991+
1992+
if ( driver == NULL ) {
1993+
std::cerr << "Cannot open database " << fi->database << " by driver " << fi->driver << std::endl;
1994+
error->setAscii("Cannot open database");
1995+
return error;
1996+
}
19661997

1998+
#ifdef QGISDEBUG
1999+
std::cerr << "Database opened" << std::endl;
2000+
#endif
2001+
2002+
dbString dbstr;
2003+
db_init_string (&dbstr);
2004+
db_set_string (&dbstr, (char *)sql.latin1());
2005+
2006+
#ifdef QGISDEBUG
2007+
std::cerr << "SQL: " << db_get_string(&dbstr) << std::endl;
2008+
#endif
2009+
2010+
int ret = db_execute_immediate (driver, &dbstr);
2011+
2012+
if ( ret != DB_OK) {
2013+
std::cerr << "Error: " << db_get_error_msg() << std::endl;
2014+
error->setLatin1( db_get_error_msg() );
2015+
}
2016+
2017+
db_close_database_shutdown_driver ( driver );
2018+
db_free_string(&dbstr);
2019+
2020+
return error;
2021+
2022+
}
19672023

19682024
QString *QgsGrassProvider::createTable ( int field, const QString &key, const QString &columns )
19692025
{
@@ -2061,62 +2117,99 @@ QString *QgsGrassProvider::addColumn ( int field, const QString &column )
20612117
return error;
20622118
}
20632119

2120+
QString query;
2121+
2122+
query.sprintf("alter table %s add column %s", fi->table, column.latin1() );
2123+
2124+
delete error;
2125+
return executeSql ( field, query );
2126+
}
2127+
2128+
QString *QgsGrassProvider::insertAttributes ( int field, int cat )
2129+
{
20642130
#ifdef QGISDEBUG
2065-
std::cerr << "Field info found -> open database" << std::endl;
2131+
std::cerr << "QgsGrassProvider::insertAttributes() field = " << field << " cat = " << cat << std::endl;
20662132
#endif
2067-
QgsGrass::setMapset ( mGisdbase, mLocation, mMapset );
2068-
dbDriver *driver = db_start_driver_open_database ( fi->driver, fi->database );
20692133

2070-
if ( driver == NULL ) {
2071-
std::cerr << "Cannot open database " << fi->database << " by driver " << fi->driver << std::endl;
2072-
error->setAscii("Cannot open database");
2134+
QString *error = new QString();
2135+
struct field_info *fi = Vect_get_field( mMap, field); // should work also with field = 0
2136+
2137+
// Read attributes
2138+
if ( fi == NULL ) {
2139+
#ifdef QGISDEBUG
2140+
std::cerr << "No field info -> no attributes" << std::endl;
2141+
#endif
2142+
error->setLatin1( "Cannot get field info" );
20732143
return error;
20742144
}
20752145

2076-
#ifdef QGISDEBUG
2077-
std::cerr << "Database opened -> add column" << std::endl;
2078-
#endif
2079-
2080-
dbString dbstr;
2081-
db_init_string (&dbstr);
20822146
QString query;
20832147

2084-
query.sprintf("alter table %s add column %s", fi->table, column.latin1() );
2085-
db_set_string (&dbstr, (char *)query.latin1());
2086-
2148+
query.sprintf("insert into %s ( %s ) values ( %d )", fi->table, fi->key, cat );
2149+
2150+
delete error;
2151+
return executeSql ( field, query );
2152+
}
2153+
2154+
QString *QgsGrassProvider::deleteAttributes ( int field, int cat )
2155+
{
20872156
#ifdef QGISDEBUG
2088-
std::cerr << "SQL: " << db_get_string(&dbstr) << std::endl;
2157+
std::cerr << "QgsGrassProvider::deleteAttributes() field = " << field << " cat = " << cat << std::endl;
20892158
#endif
20902159

2091-
int ret = db_execute_immediate (driver, &dbstr);
2160+
QString *error = new QString();
2161+
struct field_info *fi = Vect_get_field( mMap, field); // should work also with field = 0
20922162

2093-
if ( ret != DB_OK) {
2094-
std::cerr << "Error: " << db_get_error_msg() << std::endl;
2095-
error->setLatin1( db_get_error_msg() );
2163+
// Read attributes
2164+
if ( fi == NULL ) {
2165+
#ifdef QGISDEBUG
2166+
std::cerr << "No field info -> no attributes" << std::endl;
2167+
#endif
2168+
error->setLatin1( "Cannot get field info" );
2169+
return error;
20962170
}
20972171

2098-
db_close_database_shutdown_driver ( driver );
2099-
db_free_string(&dbstr);
2172+
QString query;
2173+
2174+
query.sprintf("delete from %s where %s = %d", fi->table, fi->key, cat );
21002175

2101-
return error;
2176+
delete error;
2177+
return executeSql ( field, query );
21022178
}
21032179

2104-
QString *QgsGrassProvider::insertAttributes ( int field, int cat )
2180+
QString *QgsGrassProvider::isOrphan ( int field, int cat, int *orphan)
21052181
{
21062182
#ifdef QGISDEBUG
2107-
std::cerr << "QgsGrassProvider::insertAttributes() field = " << field << " cat = " << cat << std::endl;
2183+
std::cerr << "QgsGrassProvider::isOrphan() field = " << field << " cat = " << cat << std::endl;
21082184
#endif
21092185

21102186
QString *error = new QString();
2187+
2188+
// Check first if another line with such cat exists
2189+
int fieldIndex = Vect_cidx_get_field_index ( mMap, field );
2190+
if ( fieldIndex >= 0 )
2191+
{
2192+
int t, id;
2193+
int ret = Vect_cidx_find_next ( mMap, fieldIndex, cat,
2194+
GV_POINTS|GV_LINES, 0, &t, &id );
2195+
2196+
if ( ret >= 0 ) {
2197+
// Category exists
2198+
*orphan = false;
2199+
return error;
2200+
}
2201+
}
2202+
2203+
// Check if attribute exists
21112204
struct field_info *fi = Vect_get_field( mMap, field); // should work also with field = 0
21122205

21132206
// Read attributes
21142207
if ( fi == NULL ) {
21152208
#ifdef QGISDEBUG
21162209
std::cerr << "No field info -> no attributes" << std::endl;
21172210
#endif
2118-
error->setLatin1( "Cannot get field info" );
2119-
return error;
2211+
*orphan = false;
2212+
return error;
21202213
}
21212214

21222215
#ifdef QGISDEBUG
@@ -2132,26 +2225,33 @@ QString *QgsGrassProvider::insertAttributes ( int field, int cat )
21322225
}
21332226

21342227
#ifdef QGISDEBUG
2135-
std::cerr << "Database opened -> insert new record" << std::endl;
2228+
std::cerr << "Database opened -> select record" << std::endl;
21362229
#endif
21372230

21382231
dbString dbstr;
21392232
db_init_string (&dbstr);
21402233
QString query;
21412234

2142-
query.sprintf("insert into %s ( %s ) values ( %d )", fi->table, fi->key, cat );
2235+
query.sprintf("select %s from %s where %s = %d", fi->key, fi->table, fi->key, cat );
21432236
db_set_string (&dbstr, (char *)query.latin1());
21442237

21452238
#ifdef QGISDEBUG
21462239
std::cerr << "SQL: " << db_get_string(&dbstr) << std::endl;
21472240
#endif
21482241

2149-
int ret = db_execute_immediate (driver, &dbstr);
2150-
2151-
if ( ret != DB_OK) {
2152-
std::cerr << "Error: " << db_get_error_msg() << std::endl;
2153-
error->setLatin1( db_get_error_msg() );
2242+
dbCursor cursor;
2243+
if ( db_open_select_cursor(driver, &dbstr, &cursor, DB_SCROLL) != DB_OK )
2244+
{
2245+
db_close_database_shutdown_driver ( driver );
2246+
error->setAscii("Cannot query database: " + query );
2247+
return error;
21542248
}
2249+
int nRecords = db_get_num_rows ( &cursor );
2250+
#ifdef QGISDEBUG
2251+
std::cerr << "Number of records: " << nRecords << std::endl;
2252+
#endif
2253+
2254+
if ( nRecords > 0 ) { *orphan = true; }
21552255

21562256
db_close_database_shutdown_driver ( driver );
21572257
db_free_string(&dbstr);

src/providers/grass/qgsgrassprovider.h

+23
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,13 @@ class QgsGrassProvider : public QgsVectorDataProvider
385385
*/
386386
int dbLinkField ( int link );
387387

388+
/** Execute SQL statement
389+
* @param field
390+
* @param sql
391+
* @return empty string or error message
392+
*/
393+
QString *executeSql ( int field, const QString &sql );
394+
388395
/** Update attributes
389396
* @param field
390397
* @param cat
@@ -400,6 +407,22 @@ class QgsGrassProvider : public QgsVectorDataProvider
400407
*/
401408
QString *insertAttributes ( int field, int cat );
402409

410+
/** Delete attributes from the table
411+
* @param field
412+
* @param cat
413+
* @return empty string or error message
414+
*/
415+
QString *deleteAttributes ( int field, int cat );
416+
417+
/** Check if a database row exists and it is orphan (no more lines with
418+
* that category)
419+
* @param field
420+
* @param cat
421+
* @param orphan set to true if a record exits and it is orphan
422+
* @return empty string or error message
423+
*/
424+
QString *isOrphan ( int field, int cat, int *orphan);
425+
403426
/** Create table and link vector to this table
404427
* @param field
405428
* @param columns SQL definition for columns, e.g. cat integer, label varchar(10)

0 commit comments

Comments
 (0)