1/*************************************************************************
2*									 *
3*	 YAP Prolog 							 *
4*									 *
5*	Yap Prolog was developed at NCCUP - Universidade do Porto	 *
6*									 *
7* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997	 *
8*									 *
9**************************************************************************
10*									 *
11* File:		myddas_errors.yap					 *
12* Last rev:								 *
13* mods:									 *
14* comments:	MYDDAS errors checks and errors Messages                 *
15*									 *
16*************************************************************************/
17
18:- module(myddas_errors,[
19			 '$error_checks'/1
20			 ]).
21
22:- use_module(myddas_util_predicates,[
23				      '$make_a_list'/2,
24				      '$check_fields'/2,
25				      '$check_list_on_list'/2
26				      ]).
27
28'$error_checks'(db_abolish(ModulePredName,Arity)):-!,
29	(ModulePredName = Module:PredName ->
30	    atom(Module),
31	    atom(PredName)
32	;
33	    atom(ModulePredName)
34	),
35	number(Arity).
36'$error_checks'(db_show_databases(Connection)):- !,
37	atom(Connection).
38'$error_checks'(db_show_database(Connection,_)):- !,
39	atom(Connection).
40'$error_checks'(db_my_sql_mode(Connection,_)):- !,
41	atom(Connection).
42'$error_checks'(db_change_database(Connection,Database)):- !,
43	atom(Connection),
44	atom(Database).
45'$error_checks'(db_prolog_select_multi(Connection,DbGoalsList,_)):- !,
46	atom(Connection),
47	is_list(DbGoalsList).
48'$error_checks'(db_multi_queries_number(Connection,Number)):-!,
49        atom(Connection),
50	( var(Number) ->
51	    true
52	;
53	    number(Number),
54	    Number > 0
55	).
56'$error_checks'(db_command(Connection,SQL)):-!,
57        atom(Connection),
58        nonvar(SQL).
59#ifdef MYDDAS_STATS
60'$error_checks'(db_stats(_,List)):-!,
61	var(List).
62'$error_checks'(db_stats_time(Reference,Time)):-!,
63	number(Reference),
64	var(Time).
65#endif
66'$error_checks'(db_export_view(Connection,TableViewName,SQLorDbGoal,FieldsInf)):-!,
67        atom(Connection),
68	( atom(TableViewName) -> atom(SQLorDbGoal) ; true ),
69        ( atom(SQLorDbGoal) -> atom(TableViewName) ; true ),
70	is_list(FieldsInf).
71'$error_checks'(db_create_table(Connection,TableName,FieldsInf)):-!,
72        atom(Connection),
73	atom(TableName),
74	FieldsInf = [_|_].
75'$error_checks'(db_insert3(Connection,RelationName,PredName)):-!,
76        atom(Connection),
77	atom(RelationName),
78	atom(PredName).
79'$error_checks'(db_insert2(Connection,_,[query(Att,[rel(Relation,_)],_)])) :- !,
80        atom(Connection),
81	get_value(Connection,Con),
82	% Number of fields of the Relation, must be
83        % equal to the number of attributes
84	c_db_connection_type(Con,ConType),
85	( ConType == mysql ->
86	    c_db_my_number_of_fields(Relation,Con,Arity)
87	;
88	    c_db_odbc_number_of_fields(Relation,Con,Arity)
89	),
90	length(Att,Arity),
91	% All fields must be Instanciated ( FALTA POR O NULL )
92	'$make_a_list'(Arity,FieldsProperties),
93	( ConType == mysql ->
94	    c_db_my_get_fields_properties(Relation,Con,FieldsProperties)
95	;
96	    c_db_odbc_get_fields_properties(Relation,Con,FieldsProperties)
97	),
98	'$check_fields'(Att,FieldsProperties).
99'$error_checks'(db_open(mysql,Connection,Host/Db/Port/_,User,Password)) :- !,
100	nonvar(Host), % ==  \+var(Host)
101	nonvar(User),
102	nonvar(Password),
103	nonvar(Db),
104	integer(Port),
105	atom(Connection),
106	get_value(Connection,[]).  % Nao pode ter nenhum valor atribuido
107'$error_checks'(db_open(odbc,Connection,ODBCEntry,User,Password)) :- !,
108	nonvar(ODBCEntry), % ==  \+var(ODBCEntry)
109	nonvar(User),
110	nonvar(Password),
111	atom(Connection),
112	get_value(Connection,[]).  % Nao pode ter nenhum valor atribuido
113'$error_checks'(db_view(Connection,Pred,DbGoal)) :- !,
114        atom(Connection),
115        nonvar(DbGoal),
116	nonvar(Pred),
117	nonvar(DbGoal),
118	Pred =.. [_|_],
119	DbGoal =.. [_|_].
120	%'$check_list_on_list'(PredArgs,DbGoalArgs).
121'$error_checks'(db_import(Connection,RelationName,PredName)) :- !,
122        atom(Connection),
123        atom(RelationName),
124	atom(PredName).
125'$error_checks'(db_get_attributes_types(Connection,RelationName,_)) :- !,
126        atom(Connection),
127	nonvar(RelationName).
128'$error_checks'(db_call_procedure(_,Procedure,Args,LA)) :- !,
129	nonvar(Procedure),
130	ground(Args),
131	not ground(LA).
132'$error_checks'(db_sql(Connection,SQL,LA)):- !,
133        atom(Connection),
134        nonvar(SQL),
135	not ground(LA).
136'$error_checks'(db_number_of_fields(Connection,RelationName,_)) :- !,
137        atom(Connection),
138	nonvar(RelationName).
139'$error_checks'(db_close(Connection)) :- !,
140        atom(Connection).
141% must have only one relation
142'$error_checks'(db_datalog_describe(Relation,_)) :- !,
143	nonvar(Relation).
144'$error_checks'(db_describe(Connection,Relation,_)) :- !,
145	atom(Connection),
146	nonvar(Relation).
147'$error_checks'(db_my_show_tables(_)):- !.
148'$error_checks'(db_is_database_predicate(PredName,Arity,Module)):-!,
149        nonvar(PredName),
150	nonvar(Arity),
151	nonvar(Module).
152% Prevent the error of given an atom that has no value
153'$error_checks'(get_value(Connection,Con)) :- !,
154	% This also prevents the case of giving the number of the connection
155	% as an argument
156	atom(Connection),
157	var(Con),
158	get_value(Connection,Value),
159	Value \== [].
160
161% Prevent the error of given an atom that has no value
162'$error_checks'(get_value(Conn,Connection)) :- !,
163	% This also prevents the case of giving the number of the connection
164	% as an argument
165	atom(Conn),
166	var(Connection),
167	get_value(Conn,Value),
168	Value \== [].
169