File: sqlite.c

package info (click to toggle)
gambas3 3.20.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 76,984 kB
  • sloc: ansic: 197,178; cpp: 124,076; sh: 18,999; javascript: 7,761; sql: 5,399; makefile: 2,354; perl: 1,397; xml: 490; python: 335
file content (213 lines) | stat: -rw-r--r-- 4,563 bytes parent folder | download | duplicates (3)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/***************************************************************************

  sqlite.c

  (c) 2000-2017 BenoƮt Minisini <benoit.minisini@gambas-basic.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, 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., 51 Franklin Street, Fifth Floor, Boston,
  MA 02110-1301, USA.

***************************************************************************/

#define __SQLITE_C

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <libgen.h>

#include "main.h"


/* Internal function to check whether a file is a sqlite database file */

static bool is_sqlite2_database(const char *filename)
{
  FILE* fp;
  bool res;
  char magic_text[48];

  fp = fopen(filename, "r");
  if (!fp)
    return FALSE;

  res = fread(magic_text, 1, 47, fp) == 47;
  fclose(fp);

  if (!res)
    return FALSE;

  magic_text[47] = '\0';

  if (strcmp(magic_text, "** This file contains an SQLite 2.1 database **"))
    return FALSE;

  return TRUE;
}

static bool is_sqlite3_database(const char *filename)
{
	FILE *fp;
	bool res;
	char magic_text[16];

	fp = fopen(filename, "r");
	if (!fp)
		return FALSE;

	res = fread(magic_text, 1, 15, fp) == 15;
	fclose(fp);

	if (!res)
		return FALSE;

	magic_text[15] = '\0';

	if (strcmp(magic_text, "SQLite format 3"))
		return FALSE;

	return TRUE;
}

static bool is_database_file(const char *filename)
{
	return is_sqlite3_database(filename) || is_sqlite2_database(filename);
}

static char *find_database(const char *name, const char *hostName)
{
	char *dbhome = NULL;
	char *fullpath = NULL;
	char *path;

	/* Does Name includes fullpath */
	if (*name == '/')
	{
		if (is_database_file(name))
			return (char *)name;
	}

	/* Hostname contains home area */
	fullpath = GB.NewZeroString(hostName);
	fullpath = GB.AddChar(fullpath, '/');
	fullpath = GB.AddString(fullpath, name, 0);
	
	path = GB.FileName(fullpath, GB.StringLength(fullpath));
	GB.FreeString(&fullpath);
	
	if (is_database_file(path))
		return path;

	/* Check the GAMBAS_SQLITE_DBHOME setting */
	dbhome = getenv("GAMBAS_SQLITE_DBHOME");

	if (dbhome != NULL)
	{
		fullpath = GB.NewZeroString(dbhome);
		fullpath = GB.AddChar(fullpath, '/');
		fullpath = GB.AddString(fullpath, name, 0);

		path = GB.FileName(fullpath, GB.StringLength(fullpath));
		GB.FreeString(&fullpath);

		if (is_database_file(path))
			return path;
	}

	fullpath = GB.NewZeroString(GB.TempDir());
	fullpath = GB.AddString(fullpath, "/sqlite/", 0);
	fullpath = GB.AddString(fullpath, name, 0);
	GB.FreeStringLater(fullpath);

	if (is_database_file(fullpath))
		return fullpath;

	return NULL;
}

//-------------------------------------------------------------------------

static int open_database(DB_DESC *desc, DB_DATABASE * db)
{
	char *host;
	const char *db_fullpath = NULL;
	bool ver2 = FALSE;

	if (!desc->name) // memory database
		goto __SQLITE;

	host = desc->host;
	if (!host)
		host = "";

	db_fullpath = find_database(desc->name, host);
	if (!db_fullpath)
	{
		GB.Error("Unable to locate database `&1` in `&2`", desc->name, host);
		return TRUE;
	}
					 	
	ver2 = is_sqlite2_database(db_fullpath);
	
	if (ver2)
		goto __SQLITE2;
	else
		goto __SQLITE3;
	
__SQLITE:
  
  GB.Component.Load("gb.db.sqlite3");
  GB.Error(NULL);
	
	if (GB.Component.Exist("gb.db.sqlite3"))
		goto __SQLITE3;
	else
		goto __SQLITE2;

__SQLITE2:
		DB_TryAnother("sqlite2");
		return TRUE;

__SQLITE3:
		DB_TryAnother("sqlite3");
		return TRUE;
}


//-------------------------------------------------------------------------

static int database_is_system(DB_DATABASE * db, const char *name)
{
	return FALSE;
}

/*****************************************************************************

  The driver interface

*****************************************************************************/

DB_DRIVER DB_sqlite_pseudo_driver =
{
	.name = "sqlite",
	.Open = open_database,
	.Database.IsSystem = database_is_system
};