summaryrefslogtreecommitdiff
path: root/plugins/Import/src/dbrw/dbrw.cpp
blob: 0e43f23d8b55b0810c27c28499856ba21dd611be (plain)
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
/*

Import plugin for Miranda NG

Copyright (c) 2012-18 Miranda NG team (https://miranda-ng.org)

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#include "../stdafx.h"

static int dbrw_makeDatabase(const wchar_t*)
{
	return 1;
}

static int dbrw_grokHeader(const wchar_t *profile)
{
	HANDLE hFile = CreateFile(profile, GENERIC_READ, 0, NULL, OPEN_ALWAYS, 0, NULL);
	int rc = 1;
	int err = EGROKPRF_CANTREAD;

	if (hFile != INVALID_HANDLE_VALUE) {
		BOOL r;
		char buf[64];
		DWORD dwRead;

		ZeroMemory(buf, sizeof(buf));
		r = ReadFile(hFile, buf, sizeof(buf), &dwRead, NULL);
		CloseHandle(hFile);
		if (r && memcmp(buf, DBRW_HEADER_STR, strlen(DBRW_HEADER_STR)) == 0) {
			sqlite3 *sqlcheck = NULL;
			char *szPath = mir_utf8encodeW(profile);

			rc = sqlite3_open(szPath, &sqlcheck);
			free(szPath);
			if (rc == SQLITE_OK) {
				sqlite3_stmt *stmt;
				err = EGROKPRF_UNKHEADER;

				sqlite3_prepare_v2(sqlcheck, "select * from sqlite_master where type = 'table' and name = 'dbrw_core';", -1, &stmt, NULL);
				if (sqlite3_step(stmt) == SQLITE_ROW) {

					sqlite3_finalize(stmt);
					sqlite3_prepare_v2(sqlcheck, "select val from dbrw_core where setting = 'SchemaVersion';", -1, &stmt, NULL);
					if (sqlite3_step(stmt) == SQLITE_ROW) {
						int sVersion;

						sVersion = sqlite3_column_int(stmt, 0);
						if (sVersion == atoi(DBRW_SCHEMA_VERSION))
							rc = 0;
						else {
							// TODO: Return as valid and upgrade in 
							// dbrw_Load() if schema version is upgradable
						}
					}
				}
				sqlite3_finalize(stmt);
				sqlite3_close(sqlcheck);
			}
		}
		else err = r ? EGROKPRF_UNKHEADER : EGROKPRF_CANTREAD;
	}
	return rc;
}

static MIDatabase* dbrw_Load(const wchar_t *profile, BOOL)
{
	CDbxSQLite *db = new CDbxSQLite();
	db->Open(profile);
	return db;
}

static int dbrw_Unload(MIDatabase *db)
{
	delete (CDbxSQLite*)db;
	return 0;
}

static DATABASELINK dblink =
{
	sizeof(DATABASELINK),
	"dbrw",
	L"dbx SQLite driver",
	dbrw_makeDatabase,
	dbrw_grokHeader,
	dbrw_Load,
	dbrw_Unload
};

void RegisterDbrw()
{
	RegisterDatabasePlugin(&dblink);
}