Skip to content

Commit

Permalink
Moved import library code to SnippetsDB class
Browse files Browse the repository at this point in the history
  • Loading branch information
ffes committed Nov 28, 2015
1 parent 80f9595 commit b8d53b6
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 75 deletions.
80 changes: 7 additions & 73 deletions DlgImportLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,86 +106,20 @@ static bool AddLibraryToCombo(HWND hDlg)
/////////////////////////////////////////////////////////////////////////////
//

static bool ImportSnippets(int orgLibID, int newLibID)
{
// Get all the snippets from the attached database
SqliteStatement stmt(g_db, "SELECT * FROM Import.Snippets WHERE LibraryID = @libid");
stmt.Bind("@libid", orgLibID);

// Go through the records and save them to the database
Snippet snip;
while (stmt.GetNextRecord())
{
snip.Set(&stmt);
snip.SetSnippetID(0);
snip.SetLibraryID(newLibID);
snip.SaveToDB(false);
}
stmt.Finalize();
return true;
}

/////////////////////////////////////////////////////////////////////////////
//

static bool ImportLanguages(int orgLibID, int newLibID)
{
// Get all the languages for this library from the attached database
SqliteStatement stmtSelect(g_db, "SELECT Lang FROM Import.LibraryLang WHERE LibraryID = @libid");
stmtSelect.Bind("@libid", orgLibID);

// Open a select stmt to store the new data in the table
SqliteStatement stmtInsert(g_db, "INSERT INTO LibraryLang(LibraryID, Lang) VALUES (@libid, @lang)");

// Go through the attached records and save them to the database
while (stmtSelect.GetNextRecord())
{
stmtInsert.Bind("@libid", newLibID);
stmtInsert.Bind("@lang", stmtSelect.GetIntColumn(0));

// Put the record in the database
stmtInsert.SaveRecord();
}
stmtSelect.Finalize();
stmtInsert.Finalize();
return true;
}

/////////////////////////////////////////////////////////////////////////////
//

static bool ImportLibrary(Library* lib)
{
WaitCursor wait;

g_db->Open();

// First save the selected library as a new library
long orgLibID = lib->GetLibraryID();
lib->SetLibraryID(0);
lib->SaveToDB(false);

g_db->Attach(s_databaseFile, L"Import");
ImportSnippets(orgLibID, lib->GetLibraryID());
ImportLanguages(orgLibID, lib->GetLibraryID());
g_db->Detach(L"Import");
g_db->Close();

return true;
}

/////////////////////////////////////////////////////////////////////////////
//

static BOOL OnOK(HWND hDlg)
{
if (!Validate(hDlg))
return TRUE;

// Get current LibraryID from the selected item
// Get Library from the selected item
WaitCursor wait;
int item = (int) SendDlgItemMessage(hDlg, IDC_NAME, CB_GETCURSEL, 0, 0L);
Library* lib = (Library*) SendDlgItemMessage(hDlg, IDC_NAME, CB_GETITEMDATA, (WPARAM) item, 0);
ImportLibrary(lib);

// Import the data
g_db->Open();
g_db->ImportLibrary(s_databaseFile, lib->GetLibraryID());
g_db->Close();

// We're done
CleanItems(hDlg);
Expand Down
83 changes: 81 additions & 2 deletions SnippetsDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
/////////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include "NPP/PluginInterface.h"
#include "NppSnippets.h"
#include "SnippetsDB.h"
#include "Library.h"
#include "WaitCursor.h"

SnippetsDB* g_db = NULL;
Expand Down Expand Up @@ -56,13 +59,89 @@ void SnippetsDB::Open()
// Make sure the database has the right version
if (!CheckDBVersion())
{
throw SqliteException("Database has wrong version, please regenerate!");
throw SqliteException("Database has wrong version, please regenerate!");
return;
}

EnableForeignKeys();
}

/////////////////////////////////////////////////////////////////////////////
//

bool SnippetsDB::ImportLibrary(LPCWSTR db, long orgLibID)
{
// Start with attaching the database
Attach(db, L"Import");

// Get requested library from that database
SqliteStatement stmt(this, "SELECT * FROM Import.Library WHERE LibraryID = @id");
stmt.Bind("@id", orgLibID);

if (stmt.GetNextRecord())
{
Library lib(&stmt);

lib.SetLibraryID(0);
lib.SaveToDB(false);

ImportSnippets(orgLibID, lib.GetLibraryID());
ImportLanguages(orgLibID, lib.GetLibraryID());
}
stmt.Finalize();

Detach(L"Import");
return true;
}

/////////////////////////////////////////////////////////////////////////////
//

bool SnippetsDB::ImportSnippets(int orgLibID, int newLibID)
{
// Get all the snippets from the attached database
SqliteStatement stmt(g_db, "SELECT * FROM Import.Snippets WHERE LibraryID = @libid");
stmt.Bind("@libid", orgLibID);

// Go through the records and save them to the database
Snippet snip;
while (stmt.GetNextRecord())
{
snip.Set(&stmt);
snip.SetSnippetID(0);
snip.SetLibraryID(newLibID);
snip.SaveToDB(false);
}
stmt.Finalize();
return true;
}

/////////////////////////////////////////////////////////////////////////////
//

bool SnippetsDB::ImportLanguages(int orgLibID, int newLibID)
{
// Get all the languages for this library from the attached database
SqliteStatement stmtSelect(g_db, "SELECT Lang FROM Import.LibraryLang WHERE LibraryID = @libid");
stmtSelect.Bind("@libid", orgLibID);

// Open a select stmt to store the new data in the table
SqliteStatement stmtInsert(g_db, "INSERT INTO LibraryLang(LibraryID, Lang) VALUES (@libid, @lang)");

// Go through the attached records and save them to the database
while (stmtSelect.GetNextRecord())
{
stmtInsert.Bind("@libid", newLibID);
stmtInsert.Bind("@lang", stmtSelect.GetIntColumn(0));

// Put the record in the database
stmtInsert.SaveRecord();
}
stmtSelect.Finalize();
stmtInsert.Finalize();
return true;
}

/////////////////////////////////////////////////////////////////////////////
// Create the export database

Expand Down Expand Up @@ -169,7 +248,7 @@ bool SnippetsDB::CheckDBVersion()

case 2:
wait.Show();
UpgradeDatabase_2_3();
UpgradeDatabase_2_3();
Vacuum();
// fall through

Expand Down
5 changes: 5 additions & 0 deletions SnippetsDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@ class SnippetsDB : public SqliteDatabase
virtual void Open();
void CreateExportDB();

bool ImportLibrary(LPCWSTR db, long orgLibID);

protected:
void SetValues();
void UpgradeDatabase_1_2();
void UpgradeDatabase_2_3();
bool CheckDBVersion();

bool ImportSnippets(int orgLibID, int newLibID);
bool ImportLanguages(int orgLibID, int newLibID);
};

extern SnippetsDB* g_db;

0 comments on commit b8d53b6

Please sign in to comment.