summaryrefslogtreecommitdiffstats
path: root/database/sqlite
diff options
context:
space:
mode:
authorDavid Sommerseth <dazo@users.sourceforge.net>2008-12-06 02:49:25 +0100
committerDavid Sommerseth <dazo@users.sourceforge.net>2008-12-06 02:49:25 +0100
commit729b9d8911b9645a456bbd828815c279ddaa8f3d (patch)
tree565963eadfc14dd3e11d46a0963304135d564131 /database/sqlite
parent511cbc248423b34c26917aae257576090542dc97 (diff)
downloadeurephia-729b9d8911b9645a456bbd828815c279ddaa8f3d.tar.gz
eurephia-729b9d8911b9645a456bbd828815c279ddaa8f3d.tar.xz
eurephia-729b9d8911b9645a456bbd828815c279ddaa8f3d.zip
Added sorting in db driver in eDBadminGetUserList(...). Improved comments.
Diffstat (limited to 'database/sqlite')
-rw-r--r--database/sqlite/administration.c22
-rw-r--r--database/sqlite/fieldmapping.h34
2 files changed, 50 insertions, 6 deletions
diff --git a/database/sqlite/administration.c b/database/sqlite/administration.c
index 7612c6d..037f52d 100644
--- a/database/sqlite/administration.c
+++ b/database/sqlite/administration.c
@@ -17,7 +17,9 @@
#include <eurephia_values.h>
#include <eurephiadb_session_struct.h>
#include <passwd.h>
+
#include "sqlite.h"
+#include "fieldmapping.h"
#if DRIVERAPIVERSION > 1
/*
@@ -328,28 +330,35 @@ int eDBadminConfigDelete(eurephiaCTX *ctx, const char *key) {
}
eurephiaUSERLIST *eDBadminGetUserList(eurephiaCTX *ctx, const char *sortkeys) {
- eurephiaUSERINFO *list = NULL, *rec = NULL, *last = NULL;
+ eurephiaUSERINFO *rec = NULL, *last = NULL;
eurephiaUSERLIST *ret = NULL;
dbresult *res = NULL;
+ char *dbsort = NULL;
int i = 0;
assert((ctx != NULL) && (ctx->dbc != 0));
+ // Convert the input sort keys to the proper database field names
+ dbsort = eAdminConvertSortKeys(tbl_sqlite_user, sortkeys);
+
+ // Query database for all users
res = sqlite_query(ctx,
"SELECT username, activated, deactivated, last_accessed, uid"
- " FROM openvpn_users"
- "%s", (sortkeys != NULL ? sortkeys : ""));
+ " FROM openvpn_users "
+ "ORDER BY %s", (sortkeys != NULL ? dbsort : "uid"));
if( res == NULL ) {
eurephia_log(ctx, LOG_ERROR, 0, "Error querying the user database");
return NULL;
}
+ // Prepare a list with all users
ret = (eurephiaUSERLIST *) malloc(sizeof(eurephiaUSERLIST)+2);
assert(ret != NULL);
memset(ret, 0, sizeof(eurephiaUSERLIST)+2);
ret->num_users = sqlite_get_numtuples(res);
+ // Register all records
for( i = 0; i < ret->num_users; i++ ) {
rec = eAdminPopulateUSERINFO(
atoi_nullsafe(sqlite_get_value(res, i, 4)), // uid
@@ -359,9 +368,9 @@ eurephiaUSERLIST *eDBadminGetUserList(eurephiaCTX *ctx, const char *sortkeys) {
sqlite_get_value(res, i, 2), // deactivated
sqlite_get_value(res, i, 3) // last accessed
);
- if( list == NULL ) {
- list = rec;
- last = list;
+ if( ret->users == NULL ) {
+ ret->users = rec;
+ last = ret->users;
} else {
last->next = rec;
last = rec;
@@ -369,6 +378,7 @@ eurephiaUSERLIST *eDBadminGetUserList(eurephiaCTX *ctx, const char *sortkeys) {
}
sqlite_free_results(res);
+ // Return a user list
return ret;
}
diff --git a/database/sqlite/fieldmapping.h b/database/sqlite/fieldmapping.h
new file mode 100644
index 0000000..227b50b
--- /dev/null
+++ b/database/sqlite/fieldmapping.h
@@ -0,0 +1,34 @@
+/* fieldmapping.h -- Contains mapping from internal eurephia field ID to
+ * SQLite3 specific field names per table
+ *
+ * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net>
+ *
+ * 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; version 2
+ * of the License.
+ *
+ * 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.
+ *
+ */
+
+#ifndef FIELDMAPPING_H_
+# define FIELDMAPPING_H_
+
+eFieldMap tbl_sqlite_user[] = {
+ {TABLE_USERS, SORTKEY_RECID, "uid"},
+ {TABLE_USERS, SORTKEY_UNAME, "username"},
+ {TABLE_USERS, SORTKEY_ACTIVATED, "activated"},
+ {TABLE_USERS, SORTKEY_DEACTIVATED, "deactivated"},
+ {TABLE_USERS, SORTKEY_LASTACCESS, "last_accessed"},
+ {TABLE_USERS, SORTKEY_NONE, NULL}
+};
+
+#endif /* !FIELDMAPPING_H_ */