cgi frontend
[henge/webcc.git] / src / ock / db_store.c
1 /*!@file
2 \brief Database storage for OCK's promo handler
3 \details
4 \author Ken
5 \date Sept 2016
6 ----------------------------------------------------------------------------*/
7 /* Standard */
8 #include <stdio.h> //print
9 #include <string.h> //mem
10 /* Third Party */
11 #include <mysql/mysql.h>
12 /* Internal */
13 #include <ock/ock.h>
14
15 int db_init(void);
16 int db_insert(char*,char*,char*,char*);
17
18 MYSQL *mysql;
19
20 int
21 db_init
22 ()
23 #define HOST "localhost"
24 #define USER "ock"
25 #define PASS "#0CK0r34!"
26 #define DATABASE "ock_db"
27 #define PORT 3306
28 #define SOCKET NULL
29 #define FLAGS 0
30 #define DATTABLE "promo_submissions"
31 #define DTABFMT \
32 "id not null auto_increment," \
33 "firstname varchar (32) not null," \
34 "lastname varchar (32) not null," \
35 "email varchar (64) not null," \
36 "phone varchar (16) not null," \
37 "primary key (id)"
38 { mysql = mysql_init(NULL);
39 if ((mysql = mysql_real_connect(mysql, HOST, USER, PASS, DATABASE, PORT, SOCKET, FLAGS)) == NULL)
40 { fprintf(stderr,"Failed to establish connection to db.\n");
41 return -1;
42 }
43 if (mysql_query(mysql, "SHOW TABLES LIKE '" DATTABLE "';"))
44 { fprintf(stderr,"Initial query failed\n");
45 return -1;
46 }
47 if (mysql_num_rows(mysql_use_result(mysql)) < 1)
48 if (mysql_query(mysql, "create table " DATTABLE " (" DTABFMT ");"))
49 { fprintf(stderr,"Failed to create table.\n");
50 return -1;
51 }
52 return 0;
53 }
54
55 int
56 db_insert
57 ( char *fname,
58 char *lname,
59 char *email,
60 char *phone
61 )
62 #define INSERT_FMT \
63 "INSERT INTO " DATTABLE \
64 " VALUES ('%s','%s','%s','%s')" \
65 , fname, lname, email, phone
66 #define INSERT_ERR "SQL Insertion Failed for %s %s - %s %s\n" \
67 , fname, lname, email, phone
68 { char sqlinsert[256];
69 sprintf(sqlinsert, INSERT_FMT);
70 if (mysql_query(mysql, sqlinsert))
71 { fprintf(stderr, INSERT_ERR);
72 return -1;
73 }
74 return 0;
75 }