From 1ce97801b62261e5584d35229d1e6f468fe96dcd Mon Sep 17 00:00:00 2001 From: hummypkg Date: Mon, 22 Aug 2011 13:57:15 +0000 Subject: [PATCH] add mysqldump --- main.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/main.c b/main.c index 0ed11d6..24f4b48 100644 --- a/main.c +++ b/main.c @@ -51,6 +51,7 @@ syntax() " Commands:\n" " dump Show a parsed summary of the EPG.\n" " dumpraw Show raw data from the EPG.\n" + " sqldump Produce SQL statements from EPG data.\n" " now Show what is currently on.\n" " parse Parse the EPG, no output.\n" " search Search programme names for text.\n" @@ -99,6 +100,127 @@ dumpraw(struct epg *epg __attribute__((unused)), /* Strings should all be safe now the huffman module is in place... */ #define safeprintf printf +static char * +sql_escape_len(unsigned int len, char *txt) +{ + static char buf[0x1000]; + char *p; + unsigned int i; + + p = buf; + for (i = 0; i < len && *txt; i++) + { + if (*txt == '\'') + *p++ = '\\'; + *p++ = *txt++; + } + *p = '\0'; + + return buf; +} + +void +sqldumpstart() +{ + printf("create table if not exists epg\n"); + printf("(\n"); + printf("service_id bigint unsigned not null default 0,\n"); + printf("event_id bigint unsigned not null default 0,\n"); + printf("start datetime not null,\n"); + printf("duration bigint unsigned not null,\n"); + printf("name varchar(255),\n"); + printf("text varchar(255),\n"); + printf("warning varchar(255),\n"); + printf("content_type bigint unsigned not null,\n"); + printf("content varchar(255),\n"); + printf("event_crid varchar(255),\n"); + printf("series_crid varchar(255),\n"); + printf("rec_crid varchar(255),\n"); + printf("primary key (service_id, event_id),\n"); + printf("index name (name),\n"); + printf("index text (text)\n"); + printf(") engine=innodb;\n"); + printf("truncate epg;\n"); + printf("set autocommit = 0;\n"); +} + +void +sqldumpend() +{ + printf("commit;\n"); +} + +void +sqldump(struct epg *epg __attribute__((unused)), + struct section *s, struct data *d, struct descriptor **ds, + void *var __attribute__((unused))) +{ + time_t tm; + + tm = mjd(d->start_date, d->start_hour, d->start_min, d->start_sec); + + printf("replace into epg set \n"); + printf(" service_id = %d,\n", s->service_id); + printf(" event_id = %d,\n", d->event_id); + printf(" start = %ld,\n", tm); + printf(" duration = %d", + d->dur_hour * 3600 + d->dur_min * 60 + d->dur_sec); + if (ds[PARSER_SHORT_EVENT]) + { + struct descriptor *d = ds[PARSER_SHORT_EVENT]; + + DECOMPRESS(d->content.d77.name, d->content.d77.namelen); + DECOMPRESS(d->content.d77.text, d->content.d77.textlen); + + printf(",\n name = '%s'", + sql_escape_len(d->content.d77.namelen, + d->content.d77.name)); + printf(",\n text = '%s'", + sql_escape_len(d->content.d77.textlen, + d->content.d77.text)); + } + if (ds[PARSER_USER_DEFINED]) + { + struct descriptor *d = ds[PARSER_USER_DEFINED]; + DECOMPRESS(d->content.d137.warning, d->content.d137.warninglen); + + printf(",\n warning = '%s'", + sql_escape_len(d->content.d137.warninglen, + d->content.d137.warning)); + } + if (ds[PARSER_CONTENT]) + { + printf(",\n content_type = %d", + ds[PARSER_CONTENT]->content.d84.level1); + printf(",\n content = '%s'", content_type(ds[PARSER_CONTENT])); + } + + if (ds[PARSER_CRID_EVENT]) + { + struct descriptor *d = ds[PARSER_CRID_EVENT]; + printf(",\n event_crid = '%.*s'", + d->content.d118.crids[0].cridlen, + d->content.d118.crids[0].crid); + } + + if (ds[PARSER_CRID_SERIES]) + { + struct descriptor *d = ds[PARSER_CRID_SERIES]; + printf(",\n series_crid = '%.*s'", + d->content.d118.crids[0].cridlen, + d->content.d118.crids[0].crid); + } + + if (ds[PARSER_CRID_REC]) + { + struct descriptor *d = ds[PARSER_CRID_REC]; + printf(",\n rec_crid = '%.*s'", + d->content.d118.crids[0].cridlen, + d->content.d118.crids[0].crid); + } + printf(";\n"); +} + void dump(struct epg *epg __attribute__((unused)), struct section *s, struct data *d, struct descriptor **ds, @@ -453,6 +575,12 @@ nextopt: parse(epgpath, pass, NULL, filter); else if (!strcmp(argv[0], "dumpraw")) parse(epgpath, dumpraw, NULL, filter); + else if (!strcmp(argv[0], "sqldump")) + { + sqldumpstart(); + parse(epgpath, sqldump, NULL, filter); + sqldumpend(); + } else if (!strcmp(argv[0], "now")) { time_t tm;