bool SqlCeHelper::CreateDatabase(const CString& dbPath)
{
	HRESULT hr =		-1;
	DBPROP				dbprop[1];					// property used in property set to initialize provider
	DBPROPSET			dbpropset[1];				// Property Set used to initialize provider

	// Initialize environment
	//
	hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
	if(FAILED(hr))
	{
         goto Exit;
	}

	VariantInit(&dbprop[0].vValue);	

	HANDLE				hFind;							// File handle
	WIN32_FIND_DATA		FindFileData;					// The file structure description 
	hFind = FindFirstFile(dbPath, &FindFileData);
	if (INVALID_HANDLE_VALUE != hFind)
	{
		FindClose(hFind);
		DeleteDatabase(dbPath);
	}

	// Initialize a property with name of database
	//
    dbprop[0].dwPropertyID	= DBPROP_INIT_DATASOURCE;
	dbprop[0].dwOptions		= DBPROPOPTIONS_REQUIRED;
    dbprop[0].vValue.vt		= VT_BSTR;
	dbprop[0].vValue.bstrVal= SysAllocString(dbPath);
	if(NULL == dbprop[0].vValue.bstrVal)
	{
		goto Exit;
	}

	// Initialize the property set
	//
	dbpropset[0].guidPropertySet = DBPROPSET_DBINIT;
	dbpropset[0].rgProperties	 = dbprop;
	dbpropset[0].cProperties	 = sizeof(dbprop)/sizeof(dbprop[0]);

	hr = dataSource.Create(CLSID_SQLSERVERCE, 1, dbpropset, &session);
	if(FAILED(hr))
	{
		goto Exit;
	}

Exit:
	VariantClear(&dbprop[0].vValue);
	return (hr >= 0);
}
// ================================================================================
// FUNCTION		: CopyAddrBookToBackupDb
// DESCRIPTION	: Copy all contacts in addrressbook to backup database.
// ================================================================================
bool CDbHandler::CopyAddrBookToBackupDb()
{
	IDatabase *pBkpDatabase;
	IAddrBook * pIAddrBook=NULL;
	IAddrRec* rec=NULL;
	CContactInfo *pContact;

	//delete previous backup database.
	//this should change to: create a tempory backup database. after successfully creating this.
	//rename current bakup to old backup and temp backup to backup and delete old backup
	if ( !DeleteDatabase( SM_BACKUPDB )) return false;

	pContact = new CContactInfo();
	if ( NULL==pContact ) return false;

	if ( !OpenBackupDb(&pBkpDatabase))
	{
		delete pContact;
		return false;
	}

	//enumerate addressbook
	if ( SUCCESS != ISHELL_CreateInstance(m_pIShell, AEECLSID_ADDRBOOK, (void **)&pIAddrBook))
	{
		delete pContact;
		IDATABASE_Release(pBkpDatabase);
		return false;
	}

	if( AEE_SUCCESS == IADDRBOOK_EnumRecInit(pIAddrBook,AEE_ADDR_CAT_NONE, AEE_ADDRFIELD_NONE,NULL,0) )
	{
		while( NULL != (rec=IADDRBOOK_EnumNextRec(pIAddrBook)) )
		{

			pContact->SetContactInfo(rec);
			//Add contact to backup database
			AddToBackupDb( pBkpDatabase, pContact);

			IADDRREC_Release(rec);
		
		}
	}

	delete pContact;
	//release address book
	IADDRBOOK_Release(pIAddrBook);
	IDATABASE_Release(pBkpDatabase);

	return true;
}
PLDHashOperator
DeleteDataStoresAppEnumerator(
                             const uint32_t& aAppId,
                             nsAutoPtr<DataStoreInfo>& aInfo,
                             void* aUserData)
{
  AssertIsInMainProcess();
  MOZ_ASSERT(NS_IsMainThread());

  auto* appId = static_cast<uint32_t*>(aUserData);
  if (*appId != aAppId) {
    return PL_DHASH_NEXT;
  }

  DeleteDatabase(aInfo->mName, aInfo->mManifestURL);
  return PL_DHASH_REMOVE;
}
Exemple #4
0
void 
CreateDatabase( struct Connection* conn ) {
    for ( int i=0; i<MAX_ROWS; i++ ) {
        struct Address addr = { .id = i, .set = 0 };
        conn->db->rows[ i ] = addr;
    }
}

void
SetDatabase( struct Connection* conn, int id, const char* name, const char* email ) {
    struct Address* addr = &conn->db->rows[ id ];
    if ( addr->set ) {
        Die( "Already set, delete it first" );
    }

    addr->set = 1;

    char* res = strncpy( addr->name, name, MAX_DATA );

    if ( !res ) {
        Die( "Name copy failed." );
    }

    res = strncpy( addr->email, email, MAX_DATA );
    if ( !res ) {
        Die( "Email copy failed." );
    }
}

void 
GetDatabase( struct Connection* conn, int id ) {
    struct Address* addr = &conn->db->rows[ id ];

    if ( addr->set ) {
        PrintAddress( addr );
    } else {
        Die( "ID is not set." );
    }
}

void
DeleteDatabase( struct Connection* conn, int id ) {
    struct Address addr = { .id = id, .set = 0 };
    conn->db->rows[ id ] = addr;
}

void
ListDatabase( struct Connection* conn ) {
    struct Database* db = conn->db;

    for ( int i=0; i<MAX_ROWS; i++ ) {
        struct Address* cur = &db->rows[ i ];

        if ( cur->set ) {
            PrintAddress( cur );
        }
    }
}

int
main( int argc, char* argv[] ) {
    if ( argc < 3 ) {
        Die( "USAGE: ex17 <dbfile> <action> [action params]" );
    }

    char* filename = argv[ 1 ];
    char action = argv[ 2 ][ 0 ];
    struct Connection* conn = OpenDatabase( filename, action );
    int id = 0;

    if ( argc > 3 ) {
        id = atoi( argv[ 3 ] );
    }
    if ( id>= MAX_ROWS ) {
        Die( "There is not that many records." );
    }

    if ( action == 'c' ) {
        CreateDatabase( conn );
        WriteDatabase( conn );
    } else if ( action == 'g' ) {
        if ( argc != 4 ) {
            Die( "Need an id to get" );
        } else {
            GetDatabase( conn, id );
        }
    } else if ( action == 's' ) {
        if ( argc != 6 ) {
            Die( "Need id, name, email to set" );
        } else {
            SetDatabase( conn, id, argv[ 4 ], argv[ 5 ] );
            WriteDatabase( conn );
        }
    } else if ( action == 'd' ) {
        if ( argc != 4 ) {
            Die( "Need id to delete" );
        } else {
            DeleteDatabase( conn, id );
            WriteDatabase( conn );
        }
    } else if ( action == 'l' ) {
        ListDatabase( conn );
    } else {
        Die( "Invalid action, only: c=create, g=get, d=delete, l=list" );
    }
    CloseDatabase( conn );
    return 0;
}
// ================================================================================
// FUNCTION		: DeleteChangeDb
// DESCRIPTION	: Delete changes database.
// ================================================================================
bool CDbHandler::DeleteChangeDb()
{
	return DeleteDatabase(SM_CHANGEDB);
}