Start implementation of SQLite

This commit is contained in:
William Brawner 2018-02-13 19:32:08 -06:00
parent 151aaad629
commit 2f478441ce
4 changed files with 59 additions and 3 deletions

View file

@ -20,5 +20,6 @@ find_package(libcurl)
target_link_libraries(
Feader
curl
sqlite3
xml2
)

View file

@ -7,6 +7,7 @@
#include "libxml/xmlstring.h"
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
int main(int argc, char **argv) {
if (argc < 2) {
@ -16,8 +17,13 @@ int main(int argc, char **argv) {
printf("Attempting to retrieve XML for URL: %s\n", argv[1]);
}
sqlite3 *db;
setup_database(db);
fead_xml(argv[1]);
close_database(db);
return 0;
}
@ -35,7 +41,7 @@ void fead_xml(char* url) {
parse_xml_items(x);
save_xml_elements(x);
print_xml_elements(x);
cleanup_xml(x);
}
@ -90,7 +96,7 @@ void parse_xml_items(xml *x) {
xmlFree(xChar);
}
void save_xml_elements(xml* x) {
void print_xml_elements(xml* x) {
if (x->xdp == NULL || x->xdp->children == NULL) {
printf("Unable to parse XML\n");
}
@ -139,3 +145,20 @@ void cleanup_xml(xml* x) {
xmlFreeDoc(x->xdp);
free(x);
}
void setup_database(sqlite3* handle) {
char* init_sql =
#include "schema.sql"
;
char* err;
sqlite3_open(DB_FILE, &handle);
sqlite3_exec(handle, init_sql, NULL, NULL, &err);
if (err != NULL) {
printf("Error setting up databases: %s\n", err);
}
}
void close_database(sqlite3* handle) {
sqlite3_close(handle);
}

View file

@ -1,4 +1,6 @@
#include "libxml/tree.h"
#include "sqlite3.h"
#define DB_FILE "feader.db"
typedef struct {
char* data;
@ -17,8 +19,14 @@ void parse_xml_items(xml *x);
void get_xml_ptr(xml* x, char* url);
void print_xml_elements(xml* x);
void save_xml_elements(xml* x);
void cleanup_xml(xml* x);
void fead_xml(char* url);
void setup_database(sqlite3* handle);
void close_database(sqlite3* handle);

24
src/schema.sql Normal file
View file

@ -0,0 +1,24 @@
"CREATE TABLE IF NOT EXISTS articles ( \
id int PRIMARY KEY, \
title text, \
url text, \
feedId int, \
author text, \
isFavorite int, \
featuredImage text, \
content text, \
excerpt text, \
isRead int, \
pubDate date \
); \
CREATE TABLE IF NOT EXISTS feeds ( \
id int PRIMARY KEY, \
title text, \
url text, \
feedUrl text, \
icon text, \
isFavorite int, \
lastPolled date, \
whitelist text, \
blacklist text \
);"