-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathLibrary.cpp
197 lines (166 loc) · 5.79 KB
/
Library.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/////////////////////////////////////////////////////////////////////////////
// //
// NppSnippets - Code Snippets plugin for Notepad++ //
// Copyright (C) 2010-2011 Frank Fesevur //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program; if not, write to the Free Software //
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //
// //
/////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "NPP/PluginInterface.h"
#include "NppSnippets.h"
#include "Library.h"
/////////////////////////////////////////////////////////////////////////////
//
Library::Library()
{
_LibraryID = 0;
_Name = NULL;
_CreatedBy = NULL;
_Comments = NULL;
_SortAlphabetic = true;
}
Library::Library(SqliteStatement* stmt)
{
_Name = NULL;
_CreatedBy = NULL;
_Comments = NULL;
Set(stmt);
}
Library::~Library()
{
if (_Name != NULL)
free(_Name);
if (_CreatedBy != NULL)
free(_CreatedBy);
if (_Comments != NULL)
free(_Comments);
}
/////////////////////////////////////////////////////////////////////////////
//
void Library::WSetCreatedBy(LPCWCH txt)
{
if (_CreatedBy != NULL)
free(_CreatedBy);
_CreatedBy = (txt == NULL ? NULL : wcsdup(txt));
}
void Library::WSetComments(LPCWCH txt)
{
if (_Comments != NULL)
free(_Comments);
_Comments = (txt == NULL ? NULL : wcsdup(txt));
}
/////////////////////////////////////////////////////////////////////////////
//
void Library::Set(SqliteStatement* stmt)
{
_LibraryID = stmt->GetIntColumn("LibraryID");
WSetName(stmt->GetWTextColumn("Name").c_str());
WSetCreatedBy(stmt->GetWTextColumn("CreatedBy").c_str());
WSetComments(stmt->GetWTextColumn("Comments").c_str());
SetSortAlphabetic(stmt->GetIntColumn("SortBy"));
}
/////////////////////////////////////////////////////////////////////////////
//
void Library::SaveToDB(bool autoOpen)
{
// Try to open the database
g_db->Open();
// Saving a new record or updating an existing?
SqliteStatement stmt(g_db);
if (_LibraryID == 0)
{
// Determine the last used LibraryID
SqliteStatement stmt2(g_db, "SELECT MAX(LibraryID) AS MaxID FROM Library");
stmt2.GetNextRecord();
_LibraryID = stmt2.GetIntColumn("MaxID") + 1;
stmt2.Finalize();
// Prepare the statement
stmt.Prepare("INSERT INTO Library(LibraryID, Name, CreatedBy, Comments, SortBy) VALUES (@id, @name, @createdby, @comments, @sortby)");
}
else
{
// Prepare the statement
stmt.Prepare("UPDATE Library SET Name = @name, CreatedBy = @createdby, Comments = @comments, SortBy = @sortby WHERE LibraryID = @id");
}
// Bind the values to the parameters
stmt.Bind("@id", _LibraryID);
stmt.Bind("@name", _Name);
stmt.Bind("@createdby", _CreatedBy);
stmt.Bind("@comments", _Comments);
stmt.Bind("@sortby", GetSortBy());
// Save the record
stmt.SaveRecord();
stmt.Finalize();
if (autoOpen)
g_db->Close();
}
/////////////////////////////////////////////////////////////////////////////
//
void Library::DeleteFromDB()
{
g_db->Open();
SqliteStatement stmt(g_db, "DELETE FROM Library WHERE LibraryID = @id");
stmt.Bind("@id", _LibraryID);
stmt.SaveRecord();
stmt.Finalize();
g_db->Close();
}
/////////////////////////////////////////////////////////////////////////////
//
void Library::AddLanguageToDB(int lang)
{
LanguageDBHelper("INSERT INTO LibraryLang(LibraryID, Lang) VALUES (@id, @lang)", lang);
}
/////////////////////////////////////////////////////////////////////////////
//
void Library::DeleteLanguageFromDB(int lang)
{
LanguageDBHelper("DELETE FROM LibraryLang WHERE LibraryID = @id AND Lang = @lang)", lang);
}
/////////////////////////////////////////////////////////////////////////////
//
void Library::ExportTo(LPCWCH filename)
{
// Open the db and create/attach the new db
g_db->Open();
g_db->Attach(filename, L"export");
g_db->CreateExportDB();
g_db->Detach(L"export");
g_db->Close();
// Now use that export database as temporary "base" database
// and import the current lib from the temp database
// When all done, restore the original database filename
std::wstring orgFile = g_db->GetFilename();
g_db->SetFilename(filename);
g_db->Open();
g_db->ImportLibrary(orgFile.c_str(), _LibraryID);
g_db->Close();
g_db->SetFilename(orgFile.c_str());
}
/////////////////////////////////////////////////////////////////////////////
//
void Library::LanguageDBHelper(LPCSTR sql, int lang)
{
g_db->Open();
SqliteStatement stmt(g_db, sql);
stmt.Bind("@id", _LibraryID);
stmt.Bind("@lang", lang);
stmt.SaveRecord();
stmt.Finalize();
g_db->Close();
}