% Funktionen zum einlesen und einbinden von Datenbeständen aus einer Datenbank

include("db.prova").

on_db_exception() :-
	exception(Ex),
	println([Ex]),
	fail().

set_timelimit(DT) :-
	retractall(timelimit(_)),	
	assert(timelimit(DT)).

timepoint(DT,ST):-
	on_exception(java.sql.SQLException,on_db_exception()),	
	dbopen("rbsla",DB),	
	table(TABLE),
	timelimit(T),
	datetime_epoch(T,EP),
	concat(["epoch > ",EP],WHERE),
	sql_select(DB,TABLE,[epoch, E],[status,ST], [where, WHERE]),
	datetime_epoch(DT,E).

happens(ST,DT):-
	on_exception(java.sql.SQLException,on_db_exception()),	
	dbopen("rbsla",DB),	
	table(TABLE),
	timelimit(T),
	datetime_epoch(T,EP),
	concat(["epoch > ",EP],WHERE),
	sql_select(DB,TABLE,[epoch, E],[status,ST], [where, WHERE]),
	datetime_epoch(DT,E).

save_timepoint(TP):-	
	on_exception(java.sql.SQLException,on_db_exception()),
	dbopen("rbsla",DB),			
	timepoint_construct(TP,DT,ST),
	datetime_epoch(DT,E),
	table(TABLE),
	sql_insert(DB,TABLE,[epoch,status],[E,ST]).


tp_list([]).
timepoint_list_construct(TPL) :- 	
	on_exception(java.sql.SQLException,on_db_exception()),	
	dbopen("rbsla",DB),
	table(TABLE),
	sql_select(DB,TABLE,[year,Y],[month,M],[day,D],[hour,H],[min,MIN],[sec,S],[status,ST]),
	timepoint_construct(TP, datetime(Y,M,D,H,MIN,S), ST),
	tp_list(TPL),
	append(TPL,[TP],NEW),
	retractall(tp_list(_)),
	assert(tp_list(NEW)).