/* 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 <stdlib.h>
#include <string.h>

#define SOCKET int
#include <mysql.h>

#include "customer.h"
#include "order.h"

int main()
{
    MYSQL *con;
    MYSQL_STMT *stmt;
    MYSQL_BIND out[8];
    char *sqlstr;
    unsigned long int namelen, phonelen, sourcelen, address1len, address2len, contactlen, statuslen, sumvallen;
    int stat;
    int last_customer_id;
    char sumval[11];
    con = mysql_init(NULL);
    con = mysql_real_connect(con, "localhost", "root", "hemmeligt", "Test", 3306, NULL, 0);
    stmt = mysql_stmt_init(con);
    sqlstr = "SELECT customer.id AS cid, name, phone, source "
             "FROM customer JOIN customer_potential ON customer.id = customer_potential.id "
             "ORDER BY cid";
    stat = mysql_stmt_prepare(stmt, sqlstr, strlen(sqlstr));
    stat = mysql_stmt_execute(stmt);
    memset(out, 0, sizeof(out));
    out[0].buffer_type = MYSQL_TYPE_LONG;
    out[0].buffer = &customer_id;
    out[0].buffer_length = sizeof(customer_id);
    out[1].buffer_type = MYSQL_TYPE_STRING;
    out[1].buffer = &customer_name;
    out[1].buffer_length = sizeof(customer_name);
    out[1].length = &namelen;
    out[2].buffer_type = MYSQL_TYPE_STRING;
    out[2].buffer = customer_phone;
    out[2].buffer_length = sizeof(customer_phone);
    out[2].length = &phonelen;
    out[3].buffer_type = MYSQL_TYPE_STRING;
    out[3].buffer = customer_potential_source;
    out[3].buffer_length = sizeof(customer_potential_source);
    out[3].length = &sourcelen;
    stat = mysql_stmt_bind_result(stmt, out);
    stat = mysql_stmt_store_result(stmt);
    while(!mysql_stmt_fetch(stmt))
    {
        customer_name[namelen] = 0;
        customer_phone[phonelen] = 0;
        customer_potential_source[sourcelen] = 0;
        printf("**future customer** %s, %s, %s\n", customer_name, customer_phone, customer_potential_source);
    }
    mysql_stmt_free_result(stmt);
    stmt = mysql_stmt_init(con);
    sqlstr = "SELECT customer.id AS cid,name,address1,address2,phone,order_main.id AS oid,status,SUM(quantity*price) AS sumval "
             "FROM customer JOIN customer_actual ON customer.id = customer_actual.id "
             "              JOIN order_main ON customer.id = order_main.customer "
             "              JOIN order_line ON order_main.id = order_line.order_main "
             "GROUP BY customer.id,name,address1,address2,phone,order_main.id,status "
             "ORDER BY cid, oid";
    stat = mysql_stmt_prepare(stmt, sqlstr, strlen(sqlstr));
    stat = mysql_stmt_execute(stmt);
    memset(out, 0, sizeof(out));
    out[0].buffer_type = MYSQL_TYPE_LONG;
    out[0].buffer = &customer_id;
    out[0].buffer_length = sizeof(customer_id);
    out[1].buffer_type = MYSQL_TYPE_STRING;
    out[1].buffer = customer_name;
    out[1].buffer_length = sizeof(customer_name);
    out[1].length = &namelen;
    out[2].buffer_type = MYSQL_TYPE_STRING;
    out[2].buffer = customer_actual_address1;
    out[2].buffer_length = sizeof(customer_actual_address1);
    out[2].length = &address1len;
    out[3].buffer_type = MYSQL_TYPE_STRING;
    out[3].buffer = customer_actual_address2;
    out[3].buffer_length = sizeof(customer_actual_address2);
    out[3].length = &address2len;
    out[4].buffer_type = MYSQL_TYPE_STRING;
    out[4].buffer = customer_phone;
    out[4].buffer_length = sizeof(customer_phone);
    out[4].length = &phonelen;
    out[5].buffer_type = MYSQL_TYPE_LONG;
    out[5].buffer = &order_id;
    out[5].buffer_length = sizeof(order_id);
    out[6].buffer_type = MYSQL_TYPE_STRING;
    out[6].buffer = order_status;
    out[6].buffer_length = sizeof(order_status);
    out[6].length = &statuslen;
    out[7].buffer_type = MYSQL_TYPE_DECIMAL;
    out[7].buffer = &sumval;
    out[7].buffer_length = sizeof(sumval);
    out[7].length = &sumvallen;
    stat = mysql_stmt_bind_result(stmt, out);
    stat = mysql_stmt_store_result(stmt);
    last_customer_id = 0;
    while(!mysql_stmt_fetch(stmt))
    {
        customer_name[namelen] = 0;
        customer_actual_address1[address1len] = 0;
        customer_actual_address2[address2len] = 0;
        customer_phone[phonelen] = 0;
        order_status[statuslen] = 0;
        sumval[sumvallen] = 0;
        if(last_customer_id != customer_id)
        {
            printf("%s, %s, %s, %s\n", customer_name, customer_actual_address1, customer_actual_address2, customer_phone);
            last_customer_id = customer_id;
        }
        printf("  %d, %s, %s\n", order_id, order_status, sumval);
    }
    mysql_stmt_free_result(stmt);
    mysql_close(con);
    return 0;
}
