metadataengine/server/src/mdssqliteconnection.cpp
changeset 20 6dfc5f825351
parent 0 c53acadfccc6
child 25 8e4539ab1889
child 41 2290317b9ec5
--- a/metadataengine/server/src/mdssqliteconnection.cpp	Tue Feb 02 00:24:33 2010 +0200
+++ b/metadataengine/server/src/mdssqliteconnection.cpp	Fri Mar 19 09:38:01 2010 +0200
@@ -96,28 +96,46 @@
     	}
     /**
      * Open database:
-     *   First we try to create new db. If this fails check if db already exists and
-     *   try to open it. Otherwise we cannot open it and we leave
+     *   First we try to open db. If this fails because db not creater yer, then
+     *   try to create it. Otherwise we cannot open it and we leave
      */
-    err = iMdeSqlDb.Create( *iDbFileName, sqlSecurityPolicy, &KMdsSqlDbaConfig );
+    err = iMdeSqlDb.Open( *iDbFileName, &KMdsSqlDbaConfig );
     if ( err != KErrNone )
-    	{
-    	// it could fail because database exists
-    	if ( err == KErrAlreadyExists )
-    		{
-    		err = iMdeSqlDb.Open( *iDbFileName, &KMdsSqlDbaConfig );
-    		if ( err != KErrNone )
-    			{
-    			__LOG1( ELogDb, "Cannot open database %d", err );
-    			}
-    		}
-    	else 
-    		{
-    		__LOG1( ELogDb, "Unknown error while creating %d", err );
-    		}
-
-    	User::LeaveIfError( err );
-    	}
+        {
+        __LOG1( ELogDb, "Cannot open database %d", err );
+            
+        if( err == KErrNotFound )
+            {
+            __LOG1( ELogDb, "Cannot find database %d", err );
+            err = iMdeSqlDb.Create( *iDbFileName, sqlSecurityPolicy, &KMdsSqlDbaConfig );
+            if( err != KErrNone )
+                {
+                __LOG1( ELogDb, "Unknown error while creating %d", err );
+                User::LeaveIfError( err );
+                }
+            }
+        else if( err == KErrCorrupt ||
+                err == KSqlErrCorrupt )
+            {
+            __LOGLB( ELogDb, "Warning: Database is corrupted, will delete and re-create it." );
+            err = DeleteAndReCreateDB( iDbFileName, sqlSecurityPolicy, &KMdsSqlDbaConfig );
+        
+            if ( KErrNone == err  )
+                {
+                err = iMdeSqlDb.Open( *iDbFileName, &KMdsSqlDbaConfig );
+                if ( err != KErrNone )
+                    {
+                    __LOG1( ELogDb, "Cannot open database again after delete and re-create %d", err );
+                    User::LeaveIfError( err );
+                    }
+                } 
+            }
+        else 
+            {
+            __LOG1( ELogDb, "Unknown error while accessing database %d", err );
+            User::LeaveIfError( err );
+            }
+        }
     CleanupStack::PopAndDestroy( &sqlSecurityPolicy );
     }
 
@@ -335,6 +353,11 @@
     	}
     }
 
+TItemId CMdSSqLiteConnection::LastInsertedRowId()
+    {
+    return iMdeSqlDb.LastInsertedRowId();
+    }
+
 void CMdSSqLiteConnection::ColumnsL( const RSqlStatement& aStatement, RRowData& aRow )
     {
     const TInt count( aRow.Size() );
@@ -580,3 +603,20 @@
         }
     }
 
+
+TInt CMdSSqLiteConnection::DeleteAndReCreateDB( const HBufC* aDbFileName,
+                                                const RSqlSecurityPolicy& asqlSecurityPolicy,
+                                                const TDesC8* aKMdsSqlDbaConfig ) 
+    {    
+    TInt err = iMdeSqlDb.Delete( *aDbFileName );
+    if( err!= KErrNone )
+        {
+        __LOG1( ELogDb, "delete database err=%d.", err );
+        return err;
+        }
+
+    err = iMdeSqlDb.Create( *aDbFileName, asqlSecurityPolicy, aKMdsSqlDbaConfig );
+
+    return err;
+    }
+