+/*!@file
+ \brief Database storage for OCK's promo handler
+ \details
+ \author Ken
+ \date Sept 2016
+ ----------------------------------------------------------------------------*/
+/* Standard */
+#include <stdio.h> //print
+#include <string.h> //mem
+/* Third Party */
+#include <mysql/mysql.h>
+/* Internal */
+#include <ock/ock.h>
+
+int db_init(void);
+int db_insert(char*,char*,char*,char*);
+
+MYSQL *mysql;
+
+int
+db_init
+()
+#define HOST "localhost"
+#define USER "ock"
+#define PASS "#0CK0r34!"
+#define DATABASE "ock_db"
+#define PORT 3306
+#define SOCKET NULL
+#define FLAGS 0
+#define DATTABLE "promo_submissions"
+#define DTABFMT \
+ "id not null auto_increment," \
+ "firstname varchar (32) not null," \
+ "lastname varchar (32) not null," \
+ "email varchar (64) not null," \
+ "phone varchar (16) not null," \
+ "primary key (id)"
+{ mysql = mysql_init(NULL);
+ if ((mysql = mysql_real_connect(mysql, HOST, USER, PASS, DATABASE, PORT, SOCKET, FLAGS)) == NULL)
+ { fprintf(stderr,"Failed to establish connection to db.\n");
+ return -1;
+ }
+ if (mysql_query(mysql, "SHOW TABLES LIKE '" DATTABLE "';"))
+ { fprintf(stderr,"Initial query failed\n");
+ return -1;
+ }
+ if (mysql_num_rows(mysql_use_result(mysql)) < 1)
+ if (mysql_query(mysql, "create table " DATTABLE " (" DTABFMT ");"))
+ { fprintf(stderr,"Failed to create table.\n");
+ return -1;
+ }
+ return 0;
+}
+
+int
+db_insert
+( char *fname,
+ char *lname,
+ char *email,
+ char *phone
+)
+#define INSERT_FMT \
+ "INSERT INTO " DATTABLE \
+ " VALUES ('%s','%s','%s','%s')" \
+ , fname, lname, email, phone
+#define INSERT_ERR "SQL Insertion Failed for %s %s - %s %s\n" \
+ , fname, lname, email, phone
+{ char sqlinsert[256];
+ sprintf(sqlinsert, INSERT_FMT);
+ if (mysql_query(mysql, sqlinsert))
+ { fprintf(stderr, INSERT_ERR);
+ return -1;
+ }
+ return 0;
+}