This commit is contained in:
Vítor Santos Costa 2015-03-04 09:59:42 +00:00
parent 5b19e9546a
commit 65c97e7072
4 changed files with 560 additions and 401 deletions

View File

@ -149,7 +149,7 @@ c_db_my_connect( USES_REGS1 ) {
else
{
/* Criar um novo no na lista de ligacoes*/
new = myddas_util_add_connection(conn,NULL,MYDDAS_API);
new = myddas_util_add_connection(conn,NULL,MYDDAS_MYSQL);
if (new == NULL){
#ifdef DEBUG

View File

@ -133,17 +133,17 @@ with the following options:
+ --enable-myddas
This option will detect which development libraries are installed on the computer system, MySQL, ODBC or both, and will compile the Yap system with the support for which libraries it detects;
This option will detect which development libraries are installed on the computer system, MySQL, ODBC or both, and will compile the Yap system with the support for which libraries it detects;
+ --enable-myddas-stats
This option is only available in MySQL. It includes code to get
This option is only available in MySQL. It includes code to get
statistics from the MYDDAS system;
+ --enable-top-level
This option is only available in MySQL. It enables the option to interact with the MySQL server in
This option is only available in MySQL. It enables the option to interact with the MySQL server in
two different ways. As if we were on the MySQL Client Shell, and as if
we were using Datalog.
@ -215,11 +215,11 @@ WHERE A.Name = 'John Doe';
*/
/**
@pred db view(+,+,+).
@pred db view(+,+,+).
@pred db view(+,+).
@pred db view(+,+).
If we import a database relation, such as an edge relation representing the edges of a directed graph, through
@ -258,7 +258,7 @@ db_view/3 and `db_view/2`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All arguments are standard Prolog terms. _Arg1_ through _Argn_
define the attributes to be retrieved from the database, while
_DbGoal_ defines the selection restrictions and join
_DbGoal_ defines the selection restrictions and join
conditions. _Conn_ is the connection identifier, which again can be
dropped. Calling predicate `PredName/n` will retrieve database
tuples using a single SQL query generated for the _DbGoal_. We next show
@ -320,7 +320,7 @@ SQL Compiler Manual.
@pred db_sql(+,?).
@pred db_sql(+,?).
@ -352,7 +352,7 @@ LA = ['D','John Doe',123456789] ?
*/
/** @pred db_assert(+,+).
@pred db_assert(+).
@pred db_assert(+).
@ -398,7 +398,7 @@ Would insert the row: `A,null value,31` into the relation
*/
/** @pred db insert(+,+,+).
@pred db insert(+,+).
@pred db insert(+,+).
@ -435,7 +435,7 @@ yes
@pred db_get_attributes_types(+,?).
@pred db_get_attributes_types(+,?).
@ -471,7 +471,7 @@ connection identifier.
*/
/** @pred db_number_of_fields(+,?).
@pred db_number_of_fields(+,+,?).
@pred db_number_of_fields(+,+,?).
@ -479,8 +479,8 @@ The prototype for this
predicate is the following:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
?- db_number_of_fields(Conn,RelationName,Arity).
?- db_number_of_fields(RelationName,Arity).
?- db_number_of_fields(Conn,RelationName,Arity).
?- db_number_of_fields(RelationName,Arity).
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can use the predicate db_number_of_fields/2 or
`db_number_of_fields/3` to know what is the arity of a given
@ -507,7 +507,7 @@ relation and `myddas` is the connection identifier.
*/
/** @pred db_datalog_describe(+,+).
@pred db_datalog_describe(+).
@pred db_datalog_describe(+).
@ -533,7 +533,7 @@ yes
@pred db_describe(+)
@pred db_describe(+)
The `db_describe/3` predicate does the same action as
db_datalog_describe/2 predicate but with one major
@ -541,7 +541,7 @@ difference. The results are returned by backtracking. For example, the
last query:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
?- db_describe(myddas,'Hello World',Term).
?- db_describe(myddas,'Hello World',Term).
Term = tableInfo('Number',int(11),'YES','',null(0),'') ? ;
Term = tableInfo('Name',char(10),'YES','',null(1),'' ? ;
Term = tableInfo('Letter',char(1),'YES','',null(2),'') ? ;
@ -560,7 +560,7 @@ no
*/
/** @pred db_datalog_show_tables(+).
@pred db_datalog_show_tables
@pred db_datalog_show_tables
If we need to know what relations exists in a given MySQL Schema, we can use
@ -584,7 +584,7 @@ yes
@pred db_show_tables(?)
@pred db_show_tables(?)
@ -611,8 +611,8 @@ no
*/
/**
@pred db_top_level(+,+,+,+,+).
@pred db_top_level(+,+,+,+).
@pred db_top_level(+,+,+,+,+).
@pred db_top_level(+,+,+,+).
@ -635,7 +635,7 @@ discussed above. If the login is successful, automatically the prompt of
the mysql client will be used. For example:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
?- db_top_level(mysql,con1,localhost/guest_db,guest,'').
?- db_top_level(mysql,con1,localhost/guest_db,guest,'').
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
opens a
connection identified by the `con1` atom, to an instance of a MySQL server
@ -644,7 +644,7 @@ empty password. After this is possible to use MYDDAS as the mysql
client.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
?- db_top_level(mysql,con1,localhost/guest_db,guest,'').
?- db_top_level(mysql,con1,localhost/guest_db,guest,'').
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
@ -690,7 +690,7 @@ disable this feature, we must call the `db_verbose/1` predicate with the value 0
*/
/**
@pred db_top_level(+,+,+,+).
@pred db_top_level(+,+,+,+).
*/
@ -728,7 +728,7 @@ We can also see in what module the predicates are being asserted by doing:
?- db_module(X).
X=user
yes
?-
?-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
@ -751,7 +751,7 @@ set created directly from the server. If we want to use this mode, he
simply do
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
?- db_my_result_set(use_result).
?- db_my_result_set(use_result).
yes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
After this command, all
@ -764,7 +764,7 @@ this by doing again `db_my_result_set(store_result)`.
@pred db_my_sql_mode(?SQL_Mode).
@pred db_my_sql_mode(?SQL_Mode).
@ -775,7 +775,7 @@ not to ignore the INSERT statement warnings and instead of taking
action, report an error, we could use the following SQL mode.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
?-db_my_sql_mode(traditional). yes
?-db_my_sql_mode(traditional). yes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can see the available SQL Modes at the MySQL homepage at
<http://www.mysql.org>.
@ -861,54 +861,201 @@ You can see the available SQL Modes at the MySQL homepage at
append/3
]).
:- use_module(library(parameters)).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% db_open/5
% db_open/4
%
#if MYDDAS_DECLARATIONS
:- db_open(Protocol) extra_arguments
db=Db,
port=Port,
socket=Socket,
user=User,
password=Password,
% next arguments all refer to the data access point,
% so they are all are mutually exclusive
data:file=File,
data:host=Host/Db,
data:host=Host/Db/Port,
data:host=Host/Db/Socket,
data:odbc=ODBC_DSN
/* specify conditions */
such_that
/** implicit, types are conditioned on start
atom(Db),
positive_or_zero_integer(Port),
atom(Socket),
atom(User),
atom(Password),
file(File),
atom(ODBC_DSN),
internet_host(Host), */
/* general theory, type hierarchy
atom(X) ==> nonvar(X),
\+ atom(X) * nonvar(X) ==> error(atom, X),
var(X) ==> error(var, X),
internet_host(X) <=> atom(X),
integer(X) ==> nonvar(X),
\+ integer(X) * nonvar(X) ==> error(integer, X),
var(X) ==> error(var, X)
positive_or_zero_integer(X) ==> integer(X),
positive_or_zero_integer(X) ==> positive_or_zero(X)
positive_or_zero(X) ==> number(X),
integer(X) ==> number(X),
float(X) ==> number(X),
rational(X) => number(X),
number(X) ==> nonvar(X),
\+ number(X) * nonvar(X) ==> error(number, X),
var(X) ==> error(var, X),
*/
/************
this unfolds to
P == ( (Protocol = mysql) + Protocol = powstgres + Protocol = odbc + Protocol = sqlite3 ),
Protocol = mysql ==> \+ Protocol = postgres,
Protocol = mysql ==> \+ Protocol = odbc,
Protocol = mysql ==> \+ Protocol = sqlite3,
Protocol = postgres ==> \+ Protocol = mysql,
% P(X,D)
P ==> atom(Protocol),
atom(X) * not P ==> error(domain, X, D),
*/
/* problem specific axioms */
Connection?=myddas,
Host ?= localhost,
User ?= '',
Password ?= '',
t_atom(X) =:= atom(X) \/ t_var(X) \/ type_err(atom(X)),
t_var(X) =:= err(var(X)),
t_integer(X) =:= integer(X) \/ t_var(X) \/ err(integer(X)),
i_atom(X) =:= atom(X) \/ i_var(X) \/ t(atom(X)),
i_var(X) =:= i(var(X)),
i_integer(X) =:= integer(X) \/ i_var(X) \/ err(integer(X)),
( list( X ) =:= ( nil(X) + ( X = cons( A, Y ) * L ) ) ),
list( Protocol ) .
/*
((Protocol = odbc) :-
c^c_db_odbc_connect(ODBCEntry,User,Password,Handle) ),
((Protocol = mysql) :-
c^c_db_my_connect(Host,User,Password,Db,Port,Socket,Handle)),
((Protocol = postgres) :- c^c_postgres_connect(Host/Db/Port/Socket,User,Password, Handle)),
((Protocol = sqlite3) :-
c^c_sqlite3_connect(File,User,Password,Handle),
t_atom(File), i_atom(User)),
Protocol in [ mysql, postgres, odbc, sqlite3 ],
(- (c^c_sqlite3_connect(File,User,Password,Handle)) :- (c ^fail) ).
/*
( c^c_db_my_connect(Host,User,Password,Db,Port,Socket,Handle) ==
-(Protocol = odbc)*
(Protocol = mysql) *
-(Protocol = sqlite3)*
-(Protocol = postgres) ),
% t_internet_host(Host),
% i_atom(User) *
% i_atom(Password) *
% i_positive_or_zero_integer(Port) *
% i_atom(Db) *
% i_atom(Socket)
( c^c_db_odbc_connect(ODBCEntry,User,Password,Handle) ==
(Protocol = odbc)*
-(Protocol = mysql) *
-(Protocol = sqlite3)*
-(Protocol = postgres) ),
( c^c_postgres_connect(Host/Db/Port/Socket,User,Password, Handle) ==
-(Protocol = odbc)*
-(Protocol = mysql) *
-(Protocol = sqlite3)*
(Protocol = posgrtgres) ),
% t_internet_host(Host)*
% i_atom(User) *
% i_atom(Password) *
% i_positive_or_zero_integer(Port) *
% i_atom(Db) *
% i_atom(Socket) *
% 0 is default port and Var to be NULL, the default socket
(c^c_sqlite3_connect(File,User,Password,Handle)
==
-(Protocol = odbc)*
-(Protocol = mysql) *
(Protocol = sqlite3)*
-(Protocol = postgres)).
%(Protocol = sqlite3),
% t_file( File ) *
%
db_open(Interface,HostDb,User,Password):-
db_open(Interface,myddas,HostDb,User,Password).
% i_atom(User) *
% i_atom(Password).
/*
integer(Handle) ==
c^c_db_my_connect(Host,User,Password,Db,Port,Socket,Handle) +
c^c_db_odbc_connect(ODBCEntry,User,Password,Handle) +
c^c_postgres_connect(Host/Db/Port/Socket,User,Password, Handle) +
c^c_sqlite3_connect(File,User,Password,Handle).
*/
#ifdef MYDDAS_MYSQL
db_open(mysql,Connection,Host/Db/Port/Socket,User,Password) :- !,
'$error_checks'(db_open(mysql,Connection,Host/Db/Port/Socket,User,Password)),
c_db_my_connect(Host,User,Password,Db,Port,Socket,Con),
set_value(Connection,Con).
db_open(mysql,Connection,Host/Db/Port,User,Password) :-
integer(Port),!,
db_open(mysql,Connection,Host/Db/Port/_,User,Password). % Var to be NULL, the default socket
db_open(mysql,Connection,Host/Db/Socket,User,Password) :- !,
db_open(mysql,Connection,Host/Db/0/Socket,User,Password). % 0 is default port
db_open(mysql,Connection,Host/Db,User,Password) :-
db_open(mysql,Connection,Host/Db/0/_,User,Password). % 0 is default port and Var to be NULL, the default socket
#endif
#ifdef MYDDAS_POSTGRES
db_open(postgres,Connection,Host/Db/Port/Socket,User,Password) :- !,
'$error_checks'(db_open(postgres,Connection,Host/Db/Port/Socket,User,Password)),
c_db_my_connect(Host,User,Password,Db,Port,Socket,Con),
set_value(Connection,Con).
db_open(postgres,Connection,Host/Db/Port,User,Password) :-
integer(Port),!,
db_open(postgres,Connection,Host/Db/Port/_,User,Password). % Var to be NULL, the default socket
db_open(postgres,Connection,Host/Db/Socket,User,Password) :- !,
db_open(postgres,Connection,Host/Db/0/Socket,User,Password). % 0 is default port
db_open(postgres,Connection,Host/Db,User,Password) :-
db_open(postgres,Connection,Host/Db/0/_,User,Password). % 0 is default port and Var to be NULL, the default socket
#endif
db_open(Protocol) :- true.
#else
db_close:-
db_close(myddas).
db_close(Connection):-
'$error_checks'(db_close(Connection)),
get_value(Connection,Con),
'$abolish_all'(Con).
db_close(Connection) :-
'$error_checks'(db_close(Connection)),
get_value(Connection,Con),
c_db_connection_type(Con,ConType),
( ConType == mysql ->
c_db_my_disconnect(Con)
;ConType == postgres ->
c_postgres_disconnect(Con)
;ConType == sqlite3 ->
c_sqlite3_disconnect(Con)
;
c_db_odbc_disconnect(Con)
),
set_value(Connection,[]). % "deletes" atom
#ifdef MYDDAS_ODBC
db_open(odbc,Connection,ODBCEntry,User,Password) :-
'$error_checks'(db_open(odbc,Connection,ODBCEntry,User,Password)),
c_db_odbc_connect(ODBCEntry,User,Password,Con),
set_value(Connection,Con).
#endif
#ifdef MYDDAS_SQLITE3
db_open(sqlite3,Connection,File,User,Password) :-
'$error_checks'(db_open(odbc,Connection,File,User,Password)),
c_sqlite3_connect(File,User,Password,Con),
set_value(Connection,Con).
#endif
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -919,25 +1066,25 @@ db_open(sqlite3,Connection,File,User,Password) :-
% db_close/0
%
db_close:-
db_close(myddas).
db_close(myddas).
db_close(Connection):-
'$error_checks'(db_close(Connection)),
get_value(Connection,Con),
'$error_checks'(db_close(Connection)),
get_value(Connection,Con),
'$abolish_all'(Con).
db_close(Connection) :-
'$error_checks'(db_close(Connection)),
get_value(Connection,Con),
c_db_connection_type(Con,ConType),
( ConType == mysql ->
'$error_checks'(db_close(Connection)),
get_value(Connection,Con),
c_db_connection_type(Con,ConType),
( ConType == mysql ->
c_db_my_disconnect(Con)
;ConType == postgres ->
;ConType == postgres ->
c_postgres_disconnect(Con)
;ConType == sqlite3 ->
;ConType == sqlite3 ->
c_sqlite3_disconnect(Con)
;
;
c_db_odbc_disconnect(Con)
),
set_value(Connection,[]). % "deletes" atom
),
set_value(Connection,[]). % "deletes" atom
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -947,10 +1094,10 @@ db_close(Connection) :-
%
%
db_verbose(X):-
var(X),!,
get_value(db_verbose,X).
var(X),!,
get_value(db_verbose,X).
db_verbose(N):-!,
set_value(db_verbose,N).
set_value(db_verbose,N).
%default value
:- set_value(db_verbose,0).
:- set_value(db_verbose_filename,myddas_queries).
@ -962,10 +1109,10 @@ db_verbose(N):-!,
%
%
db_module(X):-
var(X),!,
get_value(db_module,X).
var(X),!,
get_value(db_module,X).
db_module(ModuleName):-
set_value(db_module,ModuleName).
set_value(db_module,ModuleName).
% default value
:- db_module(user).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -976,8 +1123,8 @@ db_module(ModuleName):-
%
%
db_is_database_predicate(Module,PredName,Arity):-
'$error_checks'(db_is_database_predicate(PredName,Arity,Module)),
c_db_check_if_exists_pred(PredName,Arity,Module).
'$error_checks'(db_is_database_predicate(PredName,Arity,Module)),
c_db_check_if_exists_pred(PredName,Arity,Module).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#ifdef MYDDAS_STATS
@ -986,19 +1133,19 @@ db_is_database_predicate(Module,PredName,Arity):-
%
%
db_stats(List):-
db_stats(myddas,List).
db_stats(myddas,List).
db_stats(Connection,List):-
'$error_checks'(db_stats(Connection,List)),
NumberOfStats = 10,
'$make_a_list'(NumberOfStats,ListX1),
( var(Connection) ->
'$error_checks'(db_stats(Connection,List)),
NumberOfStats = 10,
'$make_a_list'(NumberOfStats,ListX1),
( var(Connection) ->
c_db_stats(0,ListX1)
;
;
get_value(Connection,Conn),
c_db_stats(Conn,ListX1)
),
'$make_stats_list'(ListX1,List).
),
'$make_stats_list'(ListX1,List).
#ifdef DEBUG
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -1006,8 +1153,8 @@ db_stats(Connection,List):-
% Reference is C pointer (memory reference)
%
db_stats_time(Reference,Time):-
'$error_checks'(db_stats_time(Reference,Time)),
c_db_stats_time(Reference,Time).
'$error_checks'(db_stats_time(Reference,Time)),
c_db_stats_time(Reference,Time).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#endif /* DEBUG */
@ -1022,35 +1169,35 @@ db_stats_time(Reference,Time):-
%compatibility
db_sql_select(Connection,SQL,LA):-
db_sql(Connection,SQL,LA).
db_sql(Connection,SQL,LA).
db_sql(SQL,LA):-
db_sql(myddas,SQL,LA).
db_sql(myddas,SQL,LA).
db_sql(Connection,SQL,LA):-
'$error_checks'(db_sql(Connection,SQL,LA)),
get_value(Connection,Con),
c_db_connection_type(Con,ConType),
'$write_or_not'(SQL),
( ConType == mysql ->
'$error_checks'(db_sql(Connection,SQL,LA)),
get_value(Connection,Con),
c_db_connection_type(Con,ConType),
'$write_or_not'(SQL),
( ConType == mysql ->
db_my_result_set(Mode),
c_db_my_query(SQL,ResultSet,Con,Mode,Arity)
;ConType == postgres ->
;ConType == postgres ->
postgres_result_set(Mode),
c_postgres_query(SQL,ResultSet,Con,Mode,Arity)
;ConType == sqlite3 ->
;ConType == sqlite3 ->
sqlite3_result_set(Mode),
c_sqlite3_query(SQL,ResultSet,Con,Mode,Arity)
;
;
c_db_odbc_number_of_fields_in_query(SQL,Con,Arity)
),
'$make_a_list'(Arity,LA),
( ConType == mysql ->
),
'$make_a_list'(Arity,LA),
( ConType == mysql ->
!,c_db_my_row(ResultSet,Arity,LA)
;
;
'$make_a_list'(Arity,BindList),
c_db_odbc_query(SQL,ResultSet,Arity,BindList,Con),!,
c_db_odbc_row(ResultSet,BindList,LA)
).
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -1059,27 +1206,27 @@ db_sql(Connection,SQL,LA):-
%
%
db_prolog_select(LA,DbGoal):-
db_prolog_select(myddas,LA,DbGoal).
db_prolog_select(myddas,LA,DbGoal).
db_prolog_select(Connection,LA,DbGoal):-
'$lenght'(LA,Arity),
Name=viewname,
functor(ViewName,Name,Arity),
% build arg list for viewname/Arity
ViewName=..[Name|LA],
'$lenght'(LA,Arity),
Name=viewname,
functor(ViewName,Name,Arity),
% build arg list for viewname/Arity
ViewName=..[Name|LA],
'$prolog2sql'(ViewName,DbGoal,SQL),
'$prolog2sql'(ViewName,DbGoal,SQL),
get_value(Connection,Con),
c_db_connection_type(Con,ConType),
'$write_or_not'(SQL),
( ConType == mysql ->
get_value(Connection,Con),
c_db_connection_type(Con,ConType),
'$write_or_not'(SQL),
( ConType == mysql ->
db_my_result_set(Mode),
c_db_my_query(SQL,ResultSet,Con,Mode,_),
!,c_db_my_row(ResultSet,Arity,LA)
;
;
true
).
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -1089,19 +1236,19 @@ db_prolog_select(Connection,LA,DbGoal):-
% db_prolog_select_multi(guest,[(ramos(A,C),A=C),(ramos(D,B),B=10)],[[A],[D,B]]).
%
db_prolog_select_multi(Connection,DbGoalsList,ListOfResults) :-
'$error_checks'(db_prolog_select_multi(Connection,DbGoalsList,ListOfResults)),
'$create_multi_query'(ListOfResults,DbGoalsList,SQL),
'$error_checks'(db_prolog_select_multi(Connection,DbGoalsList,ListOfResults)),
'$create_multi_query'(ListOfResults,DbGoalsList,SQL),
get_value(Connection,Con),
c_db_connection_type(Con,ConType),
'$write_or_not'(SQL),
( ConType == mysql ->
get_value(Connection,Con),
c_db_connection_type(Con,ConType),
'$write_or_not'(SQL),
( ConType == mysql ->
db_my_result_set(Mode),
c_db_my_query(SQL,ResultSet,Con,Mode,_)
;
;
true
),
'$get_multi_results'(Con,ConType,ResultSet,ListOfResults).
),
'$get_multi_results'(Con,ConType,ResultSet,ListOfResults).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -1111,16 +1258,16 @@ db_prolog_select_multi(Connection,DbGoalsList,ListOfResults) :-
%
%
db_command(Connection,SQL):-
'$error_checks'(db_command(Connection,SQL)),
get_value(Connection,Con),
'$write_or_not'(SQL),
c_db_connection_type(Con,ConType),
( ConType == mysql ->
'$error_checks'(db_command(Connection,SQL)),
get_value(Connection,Con),
'$write_or_not'(SQL),
c_db_connection_type(Con,ConType),
( ConType == mysql ->
db_my_result_set(Mode),
c_db_my_query(SQL,_,Con,Mode,_)
;
;
true
).
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -1129,29 +1276,29 @@ db_command(Connection,SQL):-
% db_assert/1
%
db_assert(PredName):-
db_assert(myddas,PredName).
db_assert(myddas,PredName).
db_assert(Connection,PredName):-
translate(PredName,PredName,Code),
'$error_checks'(db_insert2(Connection,PredName,Code)),
'$get_values_for_insert'(Code,ValuesList,RelName),
'$make_atom'(['INSERT INTO `',RelName,'` VALUES '|ValuesList],SQL),
translate(PredName,PredName,Code),
'$error_checks'(db_insert2(Connection,PredName,Code)),
'$get_values_for_insert'(Code,ValuesList,RelName),
'$make_atom'(['INSERT INTO `',RelName,'` VALUES '|ValuesList],SQL),
get_value(Connection,Con),
c_db_connection_type(Con,ConType),
'$write_or_not'(SQL),
( ConType == mysql ->
get_value(Connection,Con),
c_db_connection_type(Con,ConType),
'$write_or_not'(SQL),
( ConType == mysql ->
db_my_result_set(Mode),
c_db_my_query(SQL,_,Con,Mode,_)
;ConType == postgres ->
;ConType == postgres ->
postgres_result_set(Mode),
c_postgres_query(SQL,_,Con,Mode,_)
;ConType == sqlite3 ->
;ConType == sqlite3 ->
sqlite3_result_set(Mode),
c_sqlite3_query(SQL,_,Con,Mode,_)
;
;
c_db_odbc_query(SQL,_,_,_,Con)
).
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -1161,26 +1308,26 @@ db_assert(Connection,PredName):-
% Example [field(campo1,'char(12)',y,y,a),field(campo2,int,y,y,0)]
% TODO Test with ODBC & Type Checks
db_create_table(Connection,TableName,FieldsInf):-
'$error_checks'(db_create_table(Connection,TableName,FieldsInf)),
get_value(Connection,Con),
'$error_checks'(db_create_table(Connection,TableName,FieldsInf)),
get_value(Connection,Con),
'$process_fields'(FieldsInf,FieldString,KeysSQL),
'$make_atom'(['CREATE TABLE `',TableName,'` ( ',FieldString,KeysSQL,' )'],FinalSQL),
'$process_fields'(FieldsInf,FieldString,KeysSQL),
'$make_atom'(['CREATE TABLE `',TableName,'` ( ',FieldString,KeysSQL,' )'],FinalSQL),
c_db_connection_type(Con,ConType),
'$write_or_not'(FinalSQL),
( ConType == mysql ->
c_db_connection_type(Con,ConType),
'$write_or_not'(FinalSQL),
( ConType == mysql ->
db_my_result_set(Mode),
c_db_my_query(FinalSQL,_,Con,Mode,_)
;ConType == posgres ->
;ConType == posgres ->
postgres_result_set(Mode),
c_postsgres_query(FinalSQL,_,Con,Mode,_)
;ConType == sqlite3 ->
;ConType == sqlite3 ->
sqlite3_result_set(Mode),
c_sqlite3_query(FinalSQL,_,Con,Mode,_)
;
;
c_db_odbc_query(FinalSQL,_,_,_,Con)
).
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -1189,28 +1336,28 @@ db_create_table(Connection,TableName,FieldsInf):-
% TODO Test with ODBC
%
db_export_view(Connection,TableViewName,SQLorDbGoal,FieldsInf):-
'$error_checks'(db_export_view(Connection,TableViewName,SQLorDbGoal,FieldsInf)),
get_value(Connection,Con),
'$process_sql_goal'(TableViewName,SQLorDbGoal,TableName,SQL),
'$error_checks'(db_export_view(Connection,TableViewName,SQLorDbGoal,FieldsInf)),
get_value(Connection,Con),
'$process_sql_goal'(TableViewName,SQLorDbGoal,TableName,SQL),
% Case there's some information about the
% attribute fields of the relation given
% by the user
( FieldsInf == [] ->
% Case there's some information about the
% attribute fields of the relation given
% by the user
( FieldsInf == [] ->
'$make_atom'(['CREATE TABLE ',TableName,' AS ',SQL],FinalSQL)
;
;
'$process_fields'(FieldsInf,FieldString,KeysSQL),
'$make_atom'(['CREATE TABLE ',TableName,' (',FieldString,KeysSQL,') AS ',SQL],FinalSQL)
),
),
c_db_connection_type(Con,ConType),
'$write_or_not'(FinalSQL),
( ConType == mysql ->
c_db_connection_type(Con,ConType),
'$write_or_not'(FinalSQL),
( ConType == mysql ->
db_my_result_set(Mode),
c_db_my_query(FinalSQL,_,Con,Mode,_)
;
;
c_db_odbc_query(FinalSQL,_,_,_,Con)
).
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -1220,30 +1367,30 @@ db_export_view(Connection,TableViewName,SQLorDbGoal,FieldsInf):-
% db_update(my1,edge(1,3)-edge(99,99)).
% The case where the set condition is "set" to change all the fields
db_update(Connection,WherePred-SetPred):-
%TODO: error_checks
get_value(Connection,Conn),
%TODO: error_checks
get_value(Connection,Conn),
% Match and Values must be "unifiable"
functor(WherePred,PredName,Arity),
functor(SetPred,PredName,Arity),
% Match and Values must be "unifiable"
functor(WherePred,PredName,Arity),
functor(SetPred,PredName,Arity),
functor(NewRelation,PredName,Arity),
functor(NewRelation,PredName,Arity),
'$extract_args'(WherePred,1,Arity,WhereArgs),
'$extract_args'(SetPred,1,Arity,SetArgs),
'$extract_args'(WherePred,1,Arity,WhereArgs),
'$extract_args'(SetPred,1,Arity,SetArgs),
copy_term(WhereArgs,WhereArgsTemp),
NewRelation=..[PredName|WhereArgsTemp],
translate(NewRelation,NewRelation,Code),
copy_term(WhereArgs,WhereArgsTemp),
NewRelation=..[PredName|WhereArgsTemp],
translate(NewRelation,NewRelation,Code),
'$get_values_for_update'(Code,SetArgs,SetCondition,WhereCondition),
'$get_values_for_update'(Code,SetArgs,SetCondition,WhereCondition),
'$get_table_name'(Code,TableName),
append(SetCondition,WhereCondition,Conditions),
'$make_atom'(['UPDATE `',TableName,'` '|Conditions],SQL),
'$write_or_not'(SQL),
db_my_result_set(Mode),
c_db_my_query(SQL,_,Conn,Mode,_).
'$get_table_name'(Code,TableName),
append(SetCondition,WhereCondition,Conditions),
'$make_atom'(['UPDATE `',TableName,'` '|Conditions],SQL),
'$write_or_not'(SQL),
db_my_result_set(Mode),
c_db_my_query(SQL,_,Conn,Mode,_).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -1251,24 +1398,24 @@ db_update(Connection,WherePred-SetPred):-
% db_get_attributes_types/2
%
db_get_attributes_types(RelationName,TypesList) :-
db_get_attributes_types(myddas,RelationName,TypesList).
db_get_attributes_types(myddas,RelationName,TypesList).
db_get_attributes_types(Connection,RelationName,TypesList) :-
'$error_checks'(db_get_attributes_types(Connection,RelationName,TypesList)),
get_value(Connection,Con),
c_db_connection_type(Con,ConType),
( ConType == mysql ->
'$error_checks'(db_get_attributes_types(Connection,RelationName,TypesList)),
get_value(Connection,Con),
c_db_connection_type(Con,ConType),
( ConType == mysql ->
c_db_my_number_of_fields(RelationName,Con,Arity)
;
;
c_db_odbc_number_of_fields(RelationName,Con,Arity)
),
Size is 2*Arity,
'$make_a_list'(Size,TypesList),
c_db_connection_type(Con,ConType),
( ConType == mysql ->
),
Size is 2*Arity,
'$make_a_list'(Size,TypesList),
c_db_connection_type(Con,ConType),
( ConType == mysql ->
c_db_my_get_attributes_types(RelationName,Con,TypesList)
;
;
c_db_odbc_get_attributes_types(RelationName,Con,TypesList)
).
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -1277,20 +1424,20 @@ db_get_attributes_types(Connection,RelationName,TypesList) :-
%
%
db_number_of_fields(RelationName,Arity) :-
db_number_of_fields(myddas,RelationName,Arity).
db_number_of_fields(myddas,RelationName,Arity).
db_number_of_fields(Connection,RelationName,Arity) :-
'$error_checks'(db_number_of_fields(Connection,RelationName,Arity)),
get_value(Connection,Con),
c_db_connection_type(Con,ConType),
( ConType == mysql ->
'$error_checks'(db_number_of_fields(Connection,RelationName,Arity)),
get_value(Connection,Con),
c_db_connection_type(Con,ConType),
( ConType == mysql ->
c_db_my_number_of_fields(RelationName,Con,Arity)
;ConType == postgres ->
;ConType == postgres ->
c_postgres_number_of_fields(RelationName,Con,Arity)
;ConType == sqlite3 ->
;ConType == sqlite3 ->
c_sqlite3_number_of_fields(RelationName,Con,Arity)
;
;
c_db_odbc_number_of_fields(RelationName,Con,Arity)
).
).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -1299,9 +1446,9 @@ db_number_of_fields(Connection,RelationName,Arity) :-
% TODO: EVERITHING
%
db_multi_queries_number(Connection,Number) :-
'$error_checks'(db_multi_queries_number(Connection,Number)),
get_value(Connection,Con),
c_db_multi_queries_number(Con,Number).
'$error_checks'(db_multi_queries_number(Connection,Number)),
get_value(Connection,Con),
c_db_multi_queries_number(Con,Number).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

View File

@ -114,7 +114,19 @@
nonvar(Password),
atom(Connection),
get_value(Connection,[]). % Nao pode ter nenhum valor atribuido
'$error_checks'(db_open(sqlit3,Connection,File,_User,_Password)) :- !,
'$error_checks'(db_open(postgres,Connection,_Host/_Db/_Port/_,_User,_Password)) :- !,
atom(Connection),
get_value(Connection,[]). % Nao pode ter nenhum valor atribuido
'$error_checks'(db_open(postgres,Connection,_Host/_Db/_Port/_,_User)) :- !,
atom(Connection),
get_value(Connection,[]). % Nao pode ter nenhum valor atribuido
'$error_checks'(db_open(postgres,Connection,_Host/_Db/_Port/_)) :- !,
atom(Connection),
get_value(Connection,[]). % Nao pode ter nenhum valor atribuido
'$error_checks'(db_open(postgres,Connection)) :- !,
atom(Connection),
get_value(Connection,[]). % Nao pode ter nenhum valor atribuido
'$error_checks'(db_open(sqlite3,Connection,File,_User,_Password)) :- !,
nonvar(File), % == \+var(ODBCEntry)
atom(Connection),
get_value(Connection,[]). % Nao pode ter nenhum valor atribuido