summaryrefslogtreecommitdiff
path: root/src/location.c
blob: 9f19c16e17ca61ae730a9d6c11fae9cd6e7f7da9 (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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/***************************************************************************
 *            location.c
 *
 *  Copyright  2005  Ian McIntosh
 *  ian_mcintosh@linuxadvocate.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 Library 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 <stdlib.h>
#include <gtk/gtk.h>
#include "main.h"
#include "map.h"
#include "location.h"
#include "db.h"

gboolean location_lookup_attribute_name(const gchar* pszName, gint* pnReturnID);

struct {
} g_Location;

void location_init()
{
}

// get a new point struct from the allocator
gboolean location_alloc(location_t** ppLocation)
{
	g_return_val_if_fail(ppLocation != NULL, FALSE);
	g_return_val_if_fail(*ppLocation == NULL, FALSE);	// must be a pointer to a NULL pointer

	location_t* pNew = g_slice_new0(location_t);
	if(pNew) {
		*ppLocation = pNew;
		return TRUE;
	}
	return FALSE;
}

// return a location_t struct to the allocator
void location_free(location_t* pLocation)
{
	g_return_if_fail(pLocation != NULL);

	g_free(pLocation->pszName);

	// give back to allocator
	g_slice_free(location_t, pLocation);
}

gboolean location_insert(gint nLocationSetID, mappoint_t* pPoint, gint* pnReturnID)
{
	g_assert(pPoint != NULL);
	g_assert(nLocationSetID > 0);

	g_assert(pnReturnID != NULL);
	g_assert(*pnReturnID == 0);	// must be pointer to an int==0

	// create query SQL
	gchar* pszSQL = g_strdup_printf(
		"INSERT INTO Location SET ID=NULL, LocationSetID=%d, Coordinates=GeometryFromText('POINT(%f %f)');",
		nLocationSetID, pPoint->fLatitude, pPoint->fLongitude);

	db_query(pszSQL, NULL);
	g_free(pszSQL);

	*pnReturnID = db_get_last_insert_id();
	return TRUE;
}

gboolean location_insert_attribute_name(const gchar* pszName, gint* pnReturnID)
{
	g_assert(pszName != NULL);

	gchar* pszSafeName = db_make_escaped_string(pszName);
	gchar* pszSQL = g_strdup_printf(
		"INSERT INTO LocationAttributeName"
		" SET Name='%s';", 
		pszSafeName
		);

	gboolean bResult = db_query(pszSQL, NULL);
	g_free(pszSQL);
	db_free_escaped_string(pszSafeName);

	// inserted?
	if(bResult == TRUE) {
		if(pnReturnID) {
			*pnReturnID = db_get_last_insert_id();
			g_print("returning %d\n", *pnReturnID);
		}
		return TRUE;
	}
	else {
		// couldn't insert?  try to find existing
		if(location_lookup_attribute_name(pszName, pnReturnID)) {
			return TRUE;
		}
		return FALSE;
	}
}

gboolean location_lookup_attribute_name(const gchar* pszName, gint* pnReturnID)
{
	g_assert(pszName != NULL);

	gchar* pszSafeName = db_make_escaped_string(pszName);
	gchar* pszSQL = g_strdup_printf("SELECT ID FROM LocationAttributeName WHERE Name='%s';", pszSafeName);

	db_resultset_t* pResultSet = NULL;

	db_query(pszSQL, &pResultSet);
	g_free(pszSQL);
	db_free_escaped_string(pszSafeName);

	db_row_t aRow = db_fetch_row(pResultSet);
	if(aRow != NULL) {
		if(pnReturnID) {
			*pnReturnID = atoi(aRow[0]);
		}
		return TRUE;
	}
	return FALSE;
}

gboolean location_insert_attribute(gint nLocationID, gint nAttributeID, const gchar* pszValue, gint* pnReturnID)
{
	g_assert(nLocationID != 0);
	g_assert(nAttributeID != 0);
	g_assert(pszValue != NULL);

	gchar* pszSafeValue = db_make_escaped_string(pszValue);
	gchar* pszSQL = g_strdup_printf(
		"INSERT INTO LocationAttributeValue"
		" SET LocationID=%d, AttributeNameID=%d, Value='%s';", 
		nLocationID, nAttributeID, pszSafeValue
		);

	db_query(pszSQL, NULL);
	g_free(pszSQL);
	db_free_escaped_string(pszSafeValue);

	if(pnReturnID) {
		*pnReturnID = db_get_last_insert_id();
	}
	return TRUE;
}

gboolean location_load(gint nLocationID, mappoint_t* pReturnCoordinates, gint* pnReturnLocationSetID)
{
	db_resultset_t* pResultSet = NULL;
	db_row_t aRow;

	gchar* pszSQL = g_strdup_printf(
		"SELECT LocationSetID, AsBinary(Coordinates)"
		" FROM Location"
		" WHERE ID=%d", nLocationID);

	db_query(pszSQL, &pResultSet);
	g_free(pszSQL);

	g_return_val_if_fail(pResultSet, FALSE);
	
	aRow = db_fetch_row(pResultSet);

	if(aRow) {
		if(pnReturnLocationSetID != NULL) {
			*pnReturnLocationSetID = atoi(aRow[0]);
		}
		if(pReturnCoordinates != NULL) {
			db_parse_wkb_point(aRow[1], pReturnCoordinates);
		}
		db_free_result(pResultSet);
		return TRUE;
	}
	else {
		db_free_result(pResultSet);
		return FALSE;
	}
}

void location_load_attributes(gint nLocationID, GPtrArray* pAttributeArray)
{
	db_resultset_t* pResultSet = NULL;
	db_row_t aRow;

	g_assert(pAttributeArray->len == 0);

	gchar* pszSQL = g_strdup_printf(
		"SELECT LocationAttributeValue.ID, LocationAttributeName.Name, LocationAttributeValue.Value"
		" FROM LocationAttributeValue"
		" LEFT JOIN LocationAttributeName ON (LocationAttributeValue.AttributeNameID=LocationAttributeName.ID)"
		" WHERE LocationAttributeValue.LocationID=%d",
		nLocationID
		);

	db_query(pszSQL, &pResultSet);
	g_free(pszSQL);

	if(pResultSet) {
		while((aRow = db_fetch_row(pResultSet))) {
			locationattribute_t* pNew = g_new0(locationattribute_t, 1);

			pNew->nValueID = atoi(aRow[0]);
			pNew->pszName = g_strdup((aRow[1] == NULL) ? "" : aRow[1]);
			pNew->pszValue = g_strdup((aRow[2] == NULL) ? "" : aRow[2]);

			g_ptr_array_add(pAttributeArray, pNew);
		}
		db_free_result(pResultSet);
	}
}

void location_free_attributes(GPtrArray* pAttributeArray)
{
	gint i;
	for(i=(pAttributeArray->len-1) ; i>=0 ; i--) {
		locationattribute_t* pAttribute = g_ptr_array_remove_index_fast(pAttributeArray, i);
		g_free(pAttribute->pszName);
		g_free(pAttribute->pszValue);
		g_free(pAttribute);
	}
	g_assert(pAttributeArray->len == 0);
}