Implemented `dispense <item>`
[tpg/opendispense2.git] / src / cokebank_sqlite / main.c
index 26282dc..6165107 100644 (file)
@@ -7,6 +7,7 @@
  * This file is licenced under the 3-clause BSD Licence. See the file
  * COPYING for full details.
  */
+#include <inttypes.h>
 #include <stdlib.h>
 #include <limits.h>
 #include <stdio.h>
@@ -14,6 +15,8 @@
 #include "../cokebank.h"
 #include <sqlite3.h>
 
+#define DEBUG  1
+
 const char * const csBank_DatabaseSetup = 
 "CREATE TABLE IF NOT EXISTS accounts ("
 "      acct_id INTEGER PRIMARY KEY NOT NULL,"
@@ -34,6 +37,14 @@ const char * const csBank_DatabaseSetup =
 "      FOREIGN KEY (acct_id) REFERENCES accounts (acct_id) ON DELETE CASCADE"
 //                      Deletion of the account frees the card  ^ ^ ^
 ");"
+"CREATE TABLE IF NOT EXISTS items ("
+"      item_id INTEGER PRIMARY KEY NOT NULL,"
+"      item_handler STRING NOT NULL,"
+"      item_index INTEGER NOT NULL,"
+"      item_name STRING NOT NULL,"
+"      item_price INTEGER NOT NULL,"
+"      item_is_enabled BOOLEAN NOT NULL DEFAULT true"
+");"
 "INSERT INTO accounts (acct_name,acct_is_admin,acct_uid) VALUES ('root',1,0);"
 "INSERT INTO accounts (acct_name,acct_is_internal,acct_uid) VALUES ('"COKEBANK_SALES_ACCT"',1,-1);"
 "INSERT INTO accounts (acct_name,acct_is_internal,acct_uid) VALUES ('"COKEBANK_DEBT_ACCT"',1,-2);"
@@ -90,6 +101,8 @@ int Bank_Initialise(const char *Argument)
                        sqlite3_free(errmsg);
                        return 1;
                }
+               
+               Log_Info("SQLite database rebuilt");
        }
        else
        {
@@ -159,7 +172,7 @@ int Bank_GetFlags(int UserID)
 
        // Build Query
        query = mkstr(
-               "SELECT acct_is_disabled,acct_is_coke,acct_is_wheel,acct_is_door,acct_is_internal"
+               "SELECT acct_is_disabled,acct_is_coke,acct_is_admin,acct_is_door,acct_is_internal"
                " FROM accounts WHERE acct_id=%i LIMIT 1",
                UserID
                );
@@ -207,6 +220,10 @@ int Bank_SetFlags(int UserID, int Mask, int Value)
                );
        #undef MAP_FLAG
 
+       #if DEBUG
+       printf("Bank_SetFlags: query=\"%s\"\n", query);
+       #endif
+
        // Execute Query
        rv = sqlite3_exec(gBank_Database, query, NULL, NULL, &errmsg);
        if( rv != SQLITE_OK )
@@ -283,9 +300,9 @@ int Bank_GetAcctByName(const char *Name)
        if( !statement )        return -1;
        
        ret = sqlite3_column_int(statement, 0);
-       if( ret == 0 )  return -1;
-       
        sqlite3_finalize(statement);
+       
+       if( ret == 0 )  return -1;
        return ret;
 }
 
@@ -335,6 +352,7 @@ tAcctIterator *Bank_Iterator(int FlagMask, int FlagValues, int Flags, int MinMax
        const char      *revSort;
        sqlite3_stmt    *ret;
        
+       // Balance condtion
        if( Flags & BANK_ITFLAG_MINBALANCE )
                balanceClause = " AND acct_balance>=";
        else if( Flags & BANK_ITFLAG_MAXBALANCE )
@@ -344,6 +362,7 @@ tAcctIterator *Bank_Iterator(int FlagMask, int FlagValues, int Flags, int MinMax
                MinMaxBalance = 0;
        }
        
+       // Last seen condition
        if( Flags & BANK_ITFLAG_SEENAFTER )
                lastSeenClause = " AND acct_last_seen>=";
        else if( Flags & BANK_ITFLAG_SEENBEFORE )
@@ -352,6 +371,7 @@ tAcctIterator *Bank_Iterator(int FlagMask, int FlagValues, int Flags, int MinMax
                lastSeenClause = " AND datetime(-1,'unixepoch')!=";
        }
        
+       // Sorting clause
        switch( Flags & BANK_ITFLAG_SORTMASK )
        {
        case BANK_ITFLAG_SORT_NONE:
@@ -381,7 +401,7 @@ tAcctIterator *Bank_Iterator(int FlagMask, int FlagValues, int Flags, int MinMax
        query = mkstr("SELECT acct_id FROM accounts WHERE 1=1"
                "%s%s%s%s%s"    // Flags
                "%s%i"  // Balance
-               "%sdatetime(%lli,'unixepoch')"  // Last seen
+               "%sdatetime(%"PRIu64",'unixepoch')"     // Last seen
                "%s%s"  // Sort and direction
                ,
                MAP_FLAG("acct_is_coke", USER_FLAG_COKE),
@@ -390,7 +410,7 @@ tAcctIterator *Bank_Iterator(int FlagMask, int FlagValues, int Flags, int MinMax
                MAP_FLAG("acct_is_internal", USER_FLAG_INTERNAL),
                MAP_FLAG("acct_is_disabled", USER_FLAG_DISABLED),
                balanceClause, MinMaxBalance,
-               lastSeenClause, LastSeen,
+               lastSeenClause, (uint64_t)LastSeen,
                orderClause, revSort
                );
        //printf("query = \"%s\"\n", query);
@@ -436,7 +456,7 @@ int Bank_GetUserAuth(const char *Salt, const char *Username, const char *Passwor
 {
        Salt = Password = Username;     // Shut up GCC
        // DEBUG HACKS!
-       #if 1
+       #if 0
        return Bank_GetAcctByName(Username);
        #else
        return -1;
@@ -532,14 +552,28 @@ sqlite3_stmt *Bank_int_QuerySingle(sqlite3 *Database, const char *Query)
        sqlite3_stmt    *ret;
         int    rv;
        
+       #if DEBUG
+       printf("Bank_int_QuerySingle: (Query='%s')\n", Query);
+       #endif
+       
        // Prepare query
        ret = Bank_int_MakeStatemnt(Database, Query);
-       if( !ret )      return NULL;
+       if( !ret ) {
+               #if DEBUG >= 2
+               printf("Bank_int_QuerySingle: RETURN NULL ret=NULL\n");
+               #endif
+               return NULL;
+       }
        
        // Get row
        rv = sqlite3_step(ret);
        // - Empty result set
-       if( rv == SQLITE_DONE ) return NULL;
+       if( rv == SQLITE_DONE ) {
+               #if DEBUG >= 2
+               printf("Bank_int_QuerySingle: RETURN NULL (rv == SQLITE_DONE)\n");
+               #endif
+               return NULL;
+       }
        // - Other error
        if( rv != SQLITE_ROW ) {
                fprintf(stderr, "SQLite Error: %s\n", sqlite3_errmsg(gBank_Database));
@@ -547,6 +581,9 @@ sqlite3_stmt *Bank_int_QuerySingle(sqlite3 *Database, const char *Query)
                return NULL;
        }
        
+       #if DEBUG >= 2
+       printf("Bank_int_QuerySingle: RETURN %p\n", ret);
+       #endif
        return ret;
 }
 
@@ -555,6 +592,7 @@ sqlite3_stmt *Bank_int_QuerySingle(sqlite3 *Database, const char *Query)
  */
 int Bank_int_IsValidName(const char *Name)
 {
+       if( !Name )     return 0;
        while(*Name)
        {
                if( *Name == '\'' )     return 0;

UCC git Repository :: git.ucc.asn.au