/* Collection of code snippets by Arne Vajhøj */
/* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "sqlite3.h"

#include "asset.h"

int main()
{
    sqlite3 *con;
    sqlite3_stmt *trucksel;
    sqlite3_stmt *trailersel;
    sqlite3_stmt *toolsel;
    char *trucksqlstr = "SELECT asset.id,asset.description,asset.assettype,truckasset.model,truckasset.license,truckasset.towcapacity FROM asset JOIN truckasset ON asset.id = truckasset.id";
    char *trailersqlstr = "SELECT asset.id,asset.description,asset.assettype,trailerasset.ownweight,trailerasset.maxcargo,trailerasset.roof FROM asset JOIN trailerasset ON asset.id = trailerasset.id";
    char *toolsqlstr = "SELECT asset.id,asset.description,asset.assettype,toolasset.tooltype,toolasset.name,toolasset.weight FROM asset JOIN toolasset ON asset.id = toolasset.id";
    int stat;
    int count;
    struct asset res[1000];
    int i;
    stat = sqlite3_open("asset.db", &con);
    count = 0;
    stat = sqlite3_prepare(con, trucksqlstr, strlen(trucksqlstr), &trucksel, NULL);
    while(sqlite3_step(trucksel) == SQLITE_ROW)
    {
        count++;
        res[count-1].id = sqlite3_column_int(trucksel, 0);
        strcpy(res[count-1].description, (char *)sqlite3_column_text(trucksel, 1));
        res[count-1].assettype = sqlite3_column_int(trucksel, 2);
        strcpy(res[count-1].u.truck.model, (char *)sqlite3_column_text(trucksel, 3));
        strcpy(res[count-1].u.truck.license, (char *)sqlite3_column_text(trucksel, 4));
        res[count-1].u.truck.towcapacity = sqlite3_column_int(trucksel, 5);
    }
    sqlite3_finalize(trucksel);
    stat = sqlite3_prepare(con, trailersqlstr, strlen(trailersqlstr), &trailersel, NULL);
    while(sqlite3_step(trailersel) == SQLITE_ROW)
    {
        count++;
        res[count-1].id = sqlite3_column_int(trailersel, 0);
        strcpy(res[count-1].description, (char *)sqlite3_column_text(trailersel, 1));
        res[count-1].assettype = sqlite3_column_int(trailersel, 2);
        res[count-1].u.trailer.ownweight = sqlite3_column_int(trailersel, 3);
        res[count-1].u.trailer.maxcargo = sqlite3_column_int(trailersel, 4);
        res[count-1].u.trailer.roof = sqlite3_column_int(trailersel, 5);
    }
    sqlite3_finalize(trailersel);
    stat = sqlite3_prepare(con, toolsqlstr, strlen(toolsqlstr), &toolsel, NULL);
    while(sqlite3_step(toolsel) == SQLITE_ROW)
    {
        count++;
        res[count-1].id = sqlite3_column_int(toolsel, 0);
        strcpy(res[count-1].description, (char *)sqlite3_column_text(toolsel, 1));
        res[count-1].assettype = sqlite3_column_int(toolsel, 2);
        strcpy(res[count-1].u.tool.tooltype, (char *)sqlite3_column_text(toolsel, 3));
        strcpy(res[count-1].u.tool.name, (char *)sqlite3_column_text(toolsel, 4));
        res[count-1].u.tool.weight = sqlite3_column_int(toolsel, 5);
    }
    sqlite3_finalize(toolsel);
    sqlite3_close(con);
    for(i = 0; i < count; i++)
    {
        switch(res[i].assettype)
        {
            case TRUCK:
                printf("%d %s %s %s %d\n", res[i].id, res[i].description, res[i].u.truck.model,res[i].u.truck.license, res[i].u.truck.towcapacity);
                break;
            case TRAILER:
                printf("%d %s %d %d %d\n", res[i].id, res[i].description, res[i].u.trailer.ownweight,res[i].u.trailer.maxcargo, res[i].u.trailer.roof );
                break;
            case TOOL:
                printf("%d %s %s %s %d\n", res[i].id, res[i].description, res[i].u.tool.tooltype,res[i].u.tool.name, res[i].u.tool.weight );
                break;
        }
    }
    return 0;
}
