basic myddas with SQLITE3

This commit is contained in:
Vitor Santos Costa 2015-02-14 11:35:07 +00:00
parent 071bb36f0e
commit ada2ad44de
11 changed files with 346 additions and 281 deletions

View File

@ -4,7 +4,7 @@
/* myddas_initialization.c */ /* myddas_initialization.c */
MYDDAS_GLOBAL myddas_init_initialize_myddas(void); MYDDAS_GLOBAL myddas_init_initialize_myddas(void);
MYDDAS_UTIL_CONNECTION myddas_init_initialize_connection(void *,void *,MYDDAS_UTIL_CONNECTION); MYDDAS_UTIL_CONNECTION myddas_init_initialize_connection(void *,void *,MYDDAS_API,MYDDAS_UTIL_CONNECTION);
MYDDAS_UTIL_PREDICATE myddas_init_initialize_predicate(char *, int, char *,MYDDAS_UTIL_PREDICATE); MYDDAS_UTIL_PREDICATE myddas_init_initialize_predicate(char *, int, char *,MYDDAS_UTIL_PREDICATE);
#ifdef MYDDAS_STATS #ifdef MYDDAS_STATS
@ -19,7 +19,7 @@ void myddas_stats_delete_stats_list(MYDDAS_STATS_STRUCT);
void myddas_util_table_write(MYSQL_RES *); void myddas_util_table_write(MYSQL_RES *);
#endif #endif
Short myddas_util_connection_type(void *); Short myddas_util_connection_type(void *);
MYDDAS_UTIL_CONNECTION myddas_util_add_connection(void *,void *); MYDDAS_UTIL_CONNECTION myddas_util_add_connection(void *,void *,MYDDAS_API);
MYDDAS_UTIL_CONNECTION myddas_util_search_connection(void *); MYDDAS_UTIL_CONNECTION myddas_util_search_connection(void *);
void myddas_util_delete_connection(void *); void myddas_util_delete_connection(void *);
MYDDAS_UTIL_CONNECTION myddas_util_add_predicate(char * ,Int , char *,void *); MYDDAS_UTIL_CONNECTION myddas_util_add_predicate(char * ,Int , char *,void *);

View File

@ -157,6 +157,7 @@ typedef void *MYDDAS_STATS_TIME;
NUMBER = Yap_REGS.MYDDAS_GLOBAL_POINTER->memory_freed; NUMBER = Yap_REGS.MYDDAS_GLOBAL_POINTER->memory_freed;
#endif #endif
#include "myddas_structs.h"
#include "MyddasProto.h" #include "MyddasProto.h"

View File

@ -7,7 +7,6 @@
#endif #endif
#include "Yap.h" #include "Yap.h"
#include "myddas.h" #include "myddas.h"
#include "myddas_structs.h"
#ifdef MYDDAS_STATS #ifdef MYDDAS_STATS
#include "myddas_statistics.h" #include "myddas_statistics.h"
#endif #endif
@ -19,7 +18,7 @@ myddas_init_initialize_myddas(void){
/* We cannot call MYDDAS_MALLOC were because the global /* We cannot call MYDDAS_MALLOC were because the global
register isn't yet initialized */ register isn't yet initialized */
global = (MYDDAS_GLOBAL) malloc (sizeof(struct myddas_global)); global = (MYDDAS_GLOBAL) malloc (sizeof(struct myddas_global));
#ifdef DEBUG #ifdef DEBUGX
printf ("MALLOC %p %s %d\n",global,__FILE__,__LINE__); printf ("MALLOC %p %s %d\n",global,__FILE__,__LINE__);
#endif #endif
global->myddas_top_connections = NULL; global->myddas_top_connections = NULL;
@ -53,6 +52,7 @@ myddas_init_initialize_myddas(void){
/* Inserts the new node on the front of the list */ /* Inserts the new node on the front of the list */
MYDDAS_UTIL_CONNECTION MYDDAS_UTIL_CONNECTION
myddas_init_initialize_connection(void *conn,void *enviromment, myddas_init_initialize_connection(void *conn,void *enviromment,
MYDDAS_API api,
MYDDAS_UTIL_CONNECTION next){ MYDDAS_UTIL_CONNECTION next){
CACHE_REGS CACHE_REGS
MYDDAS_UTIL_CONNECTION new = NULL; MYDDAS_UTIL_CONNECTION new = NULL;
@ -62,6 +62,7 @@ myddas_init_initialize_connection(void *conn,void *enviromment,
{ {
return NULL; return NULL;
} }
new->api = api;
new->predicates=NULL; new->predicates=NULL;
new->connection=conn; new->connection=conn;
new->odbc_enviromment=enviromment; new->odbc_enviromment=enviromment;

View File

@ -149,7 +149,7 @@ c_db_my_connect( USES_REGS1 ) {
else else
{ {
/* Criar um novo no na lista de ligacoes*/ /* Criar um novo no na lista de ligacoes*/
new = myddas_util_add_connection(conn,NULL); new = myddas_util_add_connection(conn,NULL,MYDDAS_API);
if (new == NULL){ if (new == NULL){
#ifdef DEBUG #ifdef DEBUG
@ -515,7 +515,7 @@ c_db_my_row( USES_REGS1 ) {
for (i = 0; i < arity; i++) for (i = 0; i < arity; i++)
{ {
/* Aqui ser<65>o feitas as convers<72>es de tipos de dados */ /* Here we perform data type conversion. */
field = mysql_fetch_field(res_set); field = mysql_fetch_field(res_set);
head = HeadOfTerm(list); head = HeadOfTerm(list);
list = TailOfTerm(list); list = TailOfTerm(list);

View File

@ -348,7 +348,7 @@ c_db_odbc_connect( USES_REGS1 ) {
{ {
/* Criar um novo no na lista de ligacoes*/ /* Criar um novo no na lista de ligacoes*/
//new = add_connection(&TOP,hdbc,henv); //new = add_connection(&TOP,hdbc,henv);
new = myddas_util_add_connection(hdbc,henv); new = myddas_util_add_connection(hdbc,henv,MYDDAS_ODBC);
if (new == NULL){ if (new == NULL){
fprintf(stderr,"Error: could not allocate list memory\n"); fprintf(stderr,"Error: could not allocate list memory\n");
return FALSE; return FALSE;

View File

@ -22,7 +22,6 @@
#include "cut_c.h" #include "cut_c.h"
#include "myddas.h" #include "myddas.h"
#include <stdlib.h> #include <stdlib.h>
#include "myddas_structs.h"
#ifdef MYDDAS_STATS #ifdef MYDDAS_STATS
#include "myddas_statistics.h" #include "myddas_statistics.h"
#endif #endif
@ -121,19 +120,26 @@ c_db_initialize_myddas( USES_REGS1 ){
static Int static Int
c_db_connection_type ( USES_REGS1 ){ c_db_connection_type ( USES_REGS1 ){
Term arg_con = Deref(ARG1); Term arg_con = Deref(ARG1);
Term arg_type = Deref(ARG2); Term arg_type =ARG2;
Int *con = (Int *) IntegerOfTerm(arg_con); Int *con = (Int *) IntegerOfTerm(arg_con);
Int type = myddas_util_connection_type(con); MYDDAS_API type = myddas_util_connection_type(con);
if (type == 1) /* MYSQL Connection */ switch (type) {
Yap_unify(arg_type, MkAtomTerm(Yap_LookupAtom("mysql"))); case API_MYSQL:
else if (type ==2) /* ODBC Connection */ /* MYSQL Connection */
Yap_unify(arg_type, MkAtomTerm(Yap_LookupAtom("odbc"))); return Yap_unify(arg_type, MkAtomTerm(Yap_LookupAtom("mysql")));
else /* Not a valid connection*/ case API_ODBC:
return FALSE; /* ODBC Connection */
return Yap_unify(arg_type, MkAtomTerm(Yap_LookupAtom("odbc")));
return TRUE; case API_SQLITE3:
/* SQLITE3 Connection */
return Yap_unify(arg_type, MkAtomTerm(Yap_LookupAtom("sqlite3")));
case API_POSTGRES:
/* SQLITE3 Connection */
return Yap_unify(arg_type, MkAtomTerm(Yap_LookupAtom("postgres")));
}
return Yap_Error(SYSTEM_ERROR, TermNil, "Unverified DBMS");
} }
/* db_add_preds: PredName * Arity * Module * Connection*/ /* db_add_preds: PredName * Arity * Module * Connection*/

View File

@ -85,7 +85,7 @@ static Int c_sqlite3_change_database( USES_REGS1 );
void Yap_InitMYDDAS_SQLITE3Preds(void) void Yap_InitMYDDAS_SQLITE3Preds(void)
{ {
/* db_dbect: Host x User x Passwd x Database x dbection x ERROR_CODE */ /* db_dbect: Host x User x Passwd x Database x dbection x ERROR_CODE */
Yap_InitCPred("c_db_sqlite3_connect", 4, c_sqlite3_connect, 0); Yap_InitCPred("c_sqlite3_connect", 4, c_sqlite3_connect, 0);
/* db_number_of_fields: Relation x connection x NumberOfFields */ /* db_number_of_fields: Relation x connection x NumberOfFields */
Yap_InitCPred("c_sqlite3_number_of_fields",3, c_sqlite3_number_of_fields, 0); Yap_InitCPred("c_sqlite3_number_of_fields",3, c_sqlite3_number_of_fields, 0);
@ -144,7 +144,7 @@ c_sqlite3_connect( USES_REGS1 ) {
else else
{ {
/* Criar um novo no na lista de ligacoes*/ /* Criar um novo no na lista de ligacoes*/
new = myddas_util_add_connection(db,NULL); new = myddas_util_add_connection(db,NULL,API_SQLITE3);
if (new == NULL){ if (new == NULL){
#ifdef DEBUG #ifdef DEBUG
@ -313,6 +313,7 @@ c_sqlite3_query( USES_REGS1 ) {
/* Send query to server and process it */ /* Send query to server and process it */
if (strcmp(mode,"store_result")!=0) { if (strcmp(mode,"store_result")!=0) {
// Leave data for extraction // Leave data for extraction
printf(" SQL 0: %s\n", sql);
CALL_SQLITE (prepare_v2(db, sql, -1, &stmt, NULL) ); CALL_SQLITE (prepare_v2(db, sql, -1, &stmt, NULL) );
rs->stmt = stmt; rs->stmt = stmt;
rs->res_set = NULL; rs->res_set = NULL;
@ -337,7 +338,7 @@ c_sqlite3_query( USES_REGS1 ) {
CALL_SQLITE (get_table(db, sql, &res_set, &nrows, &length, &msg) ); CALL_SQLITE (get_table(db, sql, &res_set, &nrows, &length, &msg) );
end = myddas_stat_end_query( start ); //end = myddas_stat_end_query( start );
if (res_set == NULL) if (res_set == NULL)
{ {
#ifdef DEBUG #ifdef DEBUG
@ -381,9 +382,10 @@ c_sqlite3_number_of_fields( USES_REGS1 ) {
char sql[256]; char sql[256];
sprintf(sql,"SELECT * FROM TABLE `%s`",relation); sprintf(sql,"SELECT * FROM `%s`",relation);
/* executar a query SQL */ /* executar a query SQL */
printf(" SQL 1: %s\n", sql);
CALL_SQLITE (prepare_v2(db, sql, -1, &stmt, NULL) ); CALL_SQLITE (prepare_v2(db, sql, -1, &stmt, NULL) );
int fields = sqlite3_column_count( stmt ); int fields = sqlite3_column_count( stmt );
@ -410,9 +412,10 @@ c_sqlite3_get_attributes_types( USES_REGS1 ) {
sqlite3_stmt *stmt; sqlite3_stmt *stmt;
Int rc = TRUE; Int rc = TRUE;
sprintf(sql,"SELECT * FROM TABLE `%s`",relation); sprintf(sql,"SELECT * FROM `%s`",relation);
/* executar a query SQL */ /* executar a query SQL */
printf(" SQL 3: %s\n", sql);
CALL_SQLITE (prepare_v2(db, sql, -1, &stmt, NULL) ); CALL_SQLITE (prepare_v2(db, sql, -1, &stmt, NULL) );
int fields = sqlite3_column_count( stmt ); int fields = sqlite3_column_count( stmt );
@ -446,7 +449,8 @@ c_sqlite3_get_attributes_types( USES_REGS1 ) {
tm = "blob"; tm = "blob";
break; break;
case SQLITE_NULL: case SQLITE_NULL:
tm = "null"; default:
tm = "";
break; break;
} }
if (!Yap_unify(head, MkAtomTerm(Yap_LookupAtom(tm))) ) if (!Yap_unify(head, MkAtomTerm(Yap_LookupAtom(tm))) )
@ -505,9 +509,10 @@ c_sqlite3_get_fields_properties( USES_REGS1 ) {
sqlite3_stmt *stmt; sqlite3_stmt *stmt;
sprintf(sql,"SELECT * FROM TABLE `%s`",relation); sprintf(sql,"SELECT * FROM `%s`",relation);
/* executar a query SQL */ /* executar a query SQL */
printf(" SQL 4: %s\n", sql);
CALL_SQLITE (prepare_v2(db, sql, -1, &stmt, NULL) ); CALL_SQLITE (prepare_v2(db, sql, -1, &stmt, NULL) );
Functor functor = Yap_MkFunctor(Yap_LookupAtom("property"),4); Functor functor = Yap_MkFunctor(Yap_LookupAtom("property"),4);
@ -590,9 +595,9 @@ c_sqlite3_change_database( USES_REGS1 ) {
static Int static Int
c_sqlite3_row_cut( USES_REGS1 ) { c_sqlite3_row_cut( USES_REGS1 ) {
struct result_set *res_set=NULL; struct result_set *res_set=NULL;
sqlite3 *db = res_set->db;
res_set = AddressOfTerm(EXTRA_CBACK_CUT_ARG(Term,1)); res_set = AddressOfTerm(CBACK_CUT_ARG(1));
sqlite3 *db = res_set->db;
CALL_SQLITE( finalize( res_set->stmt ) ); CALL_SQLITE( finalize( res_set->stmt ) );
free(res_set); free(res_set);
return TRUE; return TRUE;
@ -628,7 +633,7 @@ c_sqlite3_row( USES_REGS1 ) {
EXTRA_CBACK_ARG(3,1)= arg_result_set ; EXTRA_CBACK_ARG(3,1)= arg_result_set ;
EXTRA_CBACK_ARG(3,2)= MkIntegerTerm(0) ; EXTRA_CBACK_ARG(3,2)= MkIntegerTerm(0) ;
} }
struct result_set *res_set = (struct result_set *) IntegerOfTerm(arg_result_set); struct result_set *res_set = AddressOfTerm(arg_result_set);
Term head, list, null_atom[1]; Term head, list, null_atom[1];
Int i, arity; Int i, arity;
@ -637,7 +642,7 @@ c_sqlite3_row( USES_REGS1 ) {
sqlite3 *db = res_set->db; sqlite3 *db = res_set->db;
if (res_set->stmt == NULL ) { if (res_set->stmt == NULL ) {
CACHE_REGS CACHE_REGS
Int indx = IntegerOfTerm(EXTRA_CBACK_CUT_ARG(Term,2)); Int indx = IntegerOfTerm(EXTRA_CBACK_ARG(3,2));
Int rc = true; Int rc = true;
// data needs to be copied to Prolog // data needs to be copied to Prolog
// row by row // row by row
@ -723,9 +728,9 @@ c_sqlite3_row( USES_REGS1 ) {
tf = Yap_MkApplTerm(Yap_MkFunctor(Yap_LookupAtom("null"),1),1,null_atom); tf = Yap_MkApplTerm(Yap_MkFunctor(Yap_LookupAtom("null"),1),1,null_atom);
break; break;
} }
}
if (!Yap_unify(head, tf)) if (!Yap_unify(head, tf))
rc = FALSE; rc = FALSE;
}
#ifdef MYDDAS_STATS #ifdef MYDDAS_STATS
end = myddas_stats_walltime(); end = myddas_stats_walltime();

View File

@ -36,9 +36,17 @@ struct myddas_list_preds {
MYDDAS_UTIL_PREDICATE previous; MYDDAS_UTIL_PREDICATE previous;
}; };
typedef enum myddas_api {
API_MYSQL = 0,
API_ODBC = 1,
API_SQLITE3 = 2,
API_POSTGRES } MYDDAS_API;
struct myddas_list_connection { struct myddas_list_connection {
void *connection; void *connection;
int tag;
MYDDAS_API api;
/*If variable env is NULL, then it's a /*If variable env is NULL, then it's a
MySQL connection, if not then it as the pointer MySQL connection, if not then it as the pointer
to the ODBC enviromment variable */ to the ODBC enviromment variable */

View File

@ -49,10 +49,11 @@ myddas_util_connection_type(void *con){
if (con_node == NULL) if (con_node == NULL)
return 0; return 0;
if (con_node->odbc_enviromment != NULL) /* ODBC */ return con_node->api;
return 2; // if (con_node->odbc_enviromment != NULL) /* ODBC */
else // return 2;
return 1; //else
// return 1;
} }
@ -161,7 +162,7 @@ myddas_util_search_connection(void *conn){
} }
MYDDAS_UTIL_CONNECTION MYDDAS_UTIL_CONNECTION
myddas_util_add_connection(void *conn, void *enviromment){ myddas_util_add_connection(void *conn, void *enviromment, MYDDAS_API api){
CACHE_REGS CACHE_REGS
MYDDAS_UTIL_CONNECTION node=NULL; MYDDAS_UTIL_CONNECTION node=NULL;
MYDDAS_UTIL_CONNECTION temp=NULL; MYDDAS_UTIL_CONNECTION temp=NULL;
@ -171,7 +172,7 @@ myddas_util_add_connection(void *conn, void *enviromment){
return node; return node;
} }
//put the new connection node on the top of the list //put the new connection node on the top of the list
temp = myddas_init_initialize_connection(conn,enviromment,Yap_REGS.MYDDAS_GLOBAL_POINTER->myddas_top_connections); temp = myddas_init_initialize_connection(conn,enviromment,api,Yap_REGS.MYDDAS_GLOBAL_POINTER->myddas_top_connections);
if (temp == NULL) if (temp == NULL)
{ {
#ifdef DEBUG #ifdef DEBUG
@ -354,7 +355,7 @@ myddas_util_get_pred_name(void *pointer){
} }
char * char *
myddas_util_get_pred_module(void *pointer){ vmyddas_util_get_pred_module(void *pointer){
MYDDAS_UTIL_PREDICATE temp = (MYDDAS_UTIL_PREDICATE) pointer; MYDDAS_UTIL_PREDICATE temp = (MYDDAS_UTIL_PREDICATE) pointer;
return temp->pred_module; return temp->pred_module;
} }
@ -383,7 +384,7 @@ void check_int( void ){
if (top->predicates != NULL) if (top->predicates != NULL)
{ {
printf ("\t******\n"); printf ("\t******\n");
printf ("\t===== PREDICADOS =====\n"); printf ("\t===== PREDICATES =====\n");
for (pred = top->predicates ; pred != NULL ; pred = pred->next) for (pred = top->predicates ; pred != NULL ; pred = pred->next)
{ {
printf ("\t--------------\n"); printf ("\t--------------\n");

View File

@ -228,7 +228,7 @@ If we import a database relation, such as an edge relation representing the edge
?- db_import('Edge',edge). ?- db_import('Edge',edge).
yes yes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
and we then write a query to retrieve all the direct cycles in the sqliand we then write a query to retrieve all the direct cycles in the
graph, such as graph, such as
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -891,9 +891,9 @@ db_open(odbc,Connection,ODBCEntry,User,Password) :-
set_value(Connection,Con). set_value(Connection,Con).
#endif #endif
#ifdef MYDDAS_SQLITE3 #ifdef MYDDAS_SQLITE3
db_open(sqlite3,Connection,ODBCEntry,User,Password) :- db_open(sqlite3,Connection,File,User,Password) :-
'$error_checks'(db_open(odbc,Connection,ODBCEntry,User,Password)), '$error_checks'(db_open(odbc,Connection,File,User,Password)),
c_db_sqlite3_connect(ODBCEntry,User,Password,Con), c_sqlite3_connect(File,User,Password,Con),
set_value(Connection,Con). set_value(Connection,Con).
#endif #endif
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

View File

@ -56,6 +56,11 @@
]). ]).
:- use_module(myddas_sqlite3,[
sqlite3_result_set/1
]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% db_import/3 % db_import/3
% db_import/2 % db_import/2
@ -65,17 +70,7 @@ db_import(RelationName,PredName):-
db_import(Connection,RelationName,PredName) :- db_import(Connection,RelationName,PredName) :-
'$error_checks'(db_import(Connection,RelationName,PredName)), '$error_checks'(db_import(Connection,RelationName,PredName)),
get_value(Connection,Con), get_value(Connection,Con),
c_db_connection_type(Con,ConType), table_arity( Con, ConType, RelationName, Arity ),
% get relation arity
( ConType == mysql ->
c_db_my_number_of_fields(RelationName,Con,Arity)
;
ConType == odbc ->
c_db_odbc_number_of_fields(RelationName,Con,Arity)
;
c_sqllite3_number_of_fields(RelationName,Con,Arity)
),
db_module(Module), db_module(Module),
not c_db_check_if_exists_pred(PredName,Arity,Module), not c_db_check_if_exists_pred(PredName,Arity,Module),
@ -84,16 +79,9 @@ db_import(Connection,RelationName,PredName) :-
assert(myddas_prolog2sql:R), assert(myddas_prolog2sql:R),
Size is 2*Arity, Size is 2*Arity,
'$make_a_list'(Size,TypesList), length(TypesList, Size),
% get attributes types in TypesList [field0,type0,field1,type1...] % get attributes types in TypesList [field0,type0,field1,type1...]
( ConType == mysql -> table_attributes( ConType, Con, RelationName, TypesList ),
c_db_my_get_attributes_types(RelationName,Con,TypesList)
;
ConType == odbc ->
c_db_odbc_get_attributes_types(RelationName,Con,TypesList)
;
c_sqlite3_get_attributes_types(RelationName,Con,TypesList)
),
% assert attributes facts % assert attributes facts
'$assert_attribute_information'(0,Arity,RelationName,TypesList), '$assert_attribute_information'(0,Arity,RelationName,TypesList),
@ -104,37 +92,7 @@ db_import(Connection,RelationName,PredName) :-
M=myddas_assert_predicates, M=myddas_assert_predicates,
%build PredName clause %build PredName clause
( ConType == mysql -> table_access_predicate( ConType, Con, Arity, P, LA, M, Assert ),
Assert =..[':-',P,','(M:'$copy_term_nv'(P,[],G,_),
','(M:translate(G,G,Code),
','(M:queries_atom(Code,FinalSQL),
','(M:db_my_result_set(Mode),
','(M:'$write_or_not'(FinalSQL),
','(M:c_db_my_query(FinalSQL,ResultSet,Con,Mode,_),
','(!,M:c_db_my_row(ResultSet,Arity,LA))))))))]
;
ConType == sqlit3 ->
Assert =..[':-',P,','(M:'$copy_term_nv'(P,[],G,_),
','(M:translate(G,G,Code),
','(M:queries_atom(Code,FinalSQL),
','(M:db_my_result_set(Mode),
','(M:'$write_or_not'(FinalSQL),
','(M:c_sqlit3_query(FinalSQL,ResultSet,Con,Mode,_),
','(!,M:c_sqlit3_row(ResultSet,Arity,LA))))))))]
;
'$make_a_list'(Arity,BindList),
Assert =..[':-',P,','(M:'$copy_term_nv'(P,[],G,_),
','(M:translate(G,G,Code),
','(M:queries_atom(Code,FinalSQL),
','(M:c_db_odbc_query(FinalSQL,ResultSet,Arity,BindList,Con),
','(M:'$write_or_not'(FinalSQL),
','(!,M:c_db_odbc_row(ResultSet,BindList,LA)))))))]
),
assert(Module:Assert), assert(Module:Assert),
c_db_add_preds(PredName,Arity,Module,Con). c_db_add_preds(PredName,Arity,Module,Con).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -166,26 +124,7 @@ db_view(Connection,PredName,DbGoal) :-
c_db_connection_type(Con,ConType), c_db_connection_type(Con,ConType),
% build view clause % build view clause
( ConType == mysql -> table_view( ConType, Con, CopyView, CopyGoal, Arity, LA, M, Assert ),
Assert =..[':-',CopyView,
','(M:'$copy_term_nv'(CopyView,[],ProjT,Dic),
','(M:'$copy_term_nv'(CopyGoal,Dic,NG,_),
','(M:translate(ProjT,NG,Code),
','(M:queries_atom(Code,FinalSQL),
','(M:db_my_result_set(Mode),
','(M:'$write_or_not'(FinalSQL),
','(M:c_db_my_query(FinalSQL,ResultSet,Con,Mode,_),
','(!,M:c_db_my_row(ResultSet,Arity,LA)))))))))]
;
% Assert =..[':-',NewName,
% ','(M:translate(CopyView,CopyGoal,Code),
% ','(M:queries_atom(Code,FinalSQL),
% ','(M:'$make_a_list'(Arity,BindList),
% ','(M:'$write_or_not'(FinalSQL),
% ','(M:c_db_odbc_query(FinalSQL,ResultSet,Arity,BindList,Con),
% ','(!,M:c_db_odbc_row(ResultSet,BindList,LA)))))))]
true
),
assert(Module:Assert), assert(Module:Assert),
c_db_add_preds(ViewName,Arity,Module,Con). c_db_add_preds(ViewName,Arity,Module,Con).
@ -205,11 +144,7 @@ db_insert(Connection,RelationName,PredName) :-
c_db_connection_type(Con,ConType), c_db_connection_type(Con,ConType),
% get relation arity % get relation arity
( ConType == mysql -> table_arity( Con, ConType, RelationName, Arity ),
c_db_my_number_of_fields(RelationName,Con,Arity)
;
c_db_odbc_number_of_fields(RelationName,Con,Arity)
),
db_module(Module), db_module(Module),
not c_db_check_if_exists_pred(PredName,Arity,Module), not c_db_check_if_exists_pred(PredName,Arity,Module),
@ -226,20 +161,7 @@ db_insert(Connection,RelationName,PredName) :-
% get attributes types in TypesList [field0,type0,field1,type1...] % get attributes types in TypesList [field0,type0,field1,type1...]
% and build PredName clause % and build PredName clause
( ConType == mysql -> table_insert( ConType, Con, RelationName, TypesList, Predicate, LA, Assert ),
c_db_my_get_attributes_types(RelationName,Con,TypesList),
Assert =..[':-',Predicate,','(myddas_assert_predicates:'$get_values_for_insert'(TypesList,LA,ValuesList),
','(myddas_assert_predicates:'$make_atom'(['INSERT INTO `',RelationName,'` VALUES ('|ValuesList],SQL),
','(myddas_assert_predicates:db_my_result_set(Mode),
','(myddas_assert_predicates:'$write_or_not'(SQL),
myddas_assert_predicates:c_db_my_query(SQL,_,Con,Mode,_)))))]
;
c_db_odbc_get_attributes_types(RelationName,Con,TypesList),
Assert =..[':-',Predicate,','(myddas_assert_predicates:'$get_values_for_insert'(TypesList,LA,ValuesList),
','(myddas_assert_predicates:'$make_atom'(['INSERT INTO `',RelationName,'` VALUES ('|ValuesList],SQL),
','(myddas_assert_predicates:'$write_or_not'(SQL),
myddas_assert_predicates:c_db_odbc_query(SQL,_,_,_,Con))))]
),
assert(Module:Assert), assert(Module:Assert),
c_db_add_preds(PredName,Arity,Module,Con). c_db_add_preds(PredName,Arity,Module,Con).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -308,3 +230,124 @@ db_listing(Name):-
listing(Module:Name/Arity). listing(Module:Name/Arity).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% table_arity :- number of columns in a relation.
%
table_arity( Con, ConType, RelationName, Arity ) :-
c_db_connection_type(Con,ConType),
writeln( ConType ),
% get relation arity
( ConType == mysql ->
c_db_my_number_of_fields(RelationName,Con,Arity)
;
ConType == odbc ->
c_db_odbc_number_of_fields(RelationName,Con,Arity)
;
c_sqlite3_number_of_fields(RelationName,Con,Arity))
.
% major attributes types.
table_attributes( mysql, Con, RelationName, TypesList ) :-
c_db_my_get_attributes_types(RelationName,Con,TypesList).
table_attributes( odbc, Con, RelationName, TypesList ) :-
c_db_odbc_get_attributes_types(RelationName,Con,TypesList).
table_attributes( sqlite3, Con, RelationName, TypesList ) :-
c_sqlite3_get_attributes_types(RelationName,Con,TypesList).
% predicate for DB-> query
table_access_predicate( mysql, Con, Arity, P, LA, M,
( P :- M:('$copy_term_nv'(P,[],G,_),
translate(G,G,Code),
queries_atom(Code,FinalSQL),
db_my_result_set(Mode),
'$write_or_not'(FinalSQL),
c_db_my_query(FinalSQL,ResultSet,Con,Mode,_),
!,
c_db_my_row(ResultSet,Arity,LA) ))).
table_access_predicate( sqlite3, Con, Arity, P, LA, M,
( P :- M:('$copy_term_nv'(P,[],G,_),
translate(G,G,Code),
queries_atom(Code,FinalSQL),
sqlite3_result_set(Mode),
'$write_or_not'(FinalSQL),
c_sqlite3_query(FinalSQL,ResultSet,Con,Mode,_),
!,
c_sqlite3_row(ResultSet,Arity,LA)
) )).
table_access_predicate( odbc, Con, Arity, P, LA, M,
( P :- M:('$copy_term_nv'(P,[],G,_),
translate(G,G,Code),
queries_atom(Code,FinalSQL),
c_db_odbc_result_set(Mode),
'$write_or_not'(FinalSQL),
c_db_odbc_query(FinalSQL,ResultSet,Con,Mode,_),
!,
c_db_odbc_row(ResultSet,Arity,LA)
) )).
table_insert( mysql, Con, RelationName, TypesList, Predicate, LA,
( Predicate :- myddas_assert_predicates:
( '$get_values_for_insert'(TypesList,LA,ValuesList),
'$make_atom'(['INSERT INTO `',RelationName,'` VALUES ('|ValuesList],SQL),
db_my_result_set(Mode),
'$write_or_not'(SQL),
c_db_my_query(SQL,_,Con,Mode,_)))
).
table_insert( sqlite3, Con, RelationName, TypesList, Predicate, LA,
( Predicate :- myddas_assert_predicates:
( '$get_values_for_insert'(TypesList,LA,ValuesList),
'$make_atom'(['INSERT INTO `',RelationName,'` VALUES ('|ValuesList],SQL),
db_my_result_set(Mode),
'$write_or_not'(SQL),
c_sqlite3_query(SQL,_,Con,Mode,_)))
).
table_insert( odbc, Con, RelationName, TypesList, Predicate, LA,
( Predicate :- myddas_assert_predicates:
( '$get_values_for_insert'(TypesList,LA,ValuesList),
'$make_atom'(['INSERT INTO `',RelationName,'` VALUES ('|ValuesList],SQL),
'$write_or_not'(SQL),
c_odbc_my_query(SQL,_,_,_,Con)))
).
table_view( mysql, Con, CopyView, CopyGoal, Arity, LA, M,
( CopyView :-
M:( '$copy_term_nv'(CopyView,[],ProjT,Dic),
'$copy_term_nv'(CopyGoal,Dic,NG,_),
translate(ProjT,NG,Code),
queries_atom(Code,FinalSQL),
db_my_result_set(Mode),
'$write_or_not'(FinalSQL),
c_db_my_query(FinalSQL,ResultSet,Con,Mode,_),
!,
c_db_my_row(ResultSet,Arity,LA) ))).
table_view( odbc, Con, CopyView, CopyGoal, Arity, LA, M,
( CopyView :-
M:( '$copy_term_nv'(CopyView,[],ProjT,Dic),
'$copy_term_nv'(CopyGoal,Dic,NG,_),
translate(ProjT,NG,Code),
length(BindList, Arity ),
queries_atom(Code,FinalSQL),
'$write_or_not'(FinalSQL),
c_db_odbc_query(FinalSQL,ResultSet,Arity,BindList,Con),
!,
c_db_my_row(ResultSet,Arity,LA) ))).
table_view( sqlite3, Con, CopyView, CopyGoal, Arity, LA, M,
( CopyView :-
M:( '$copy_term_nv'(CopyView,[],ProjT,Dic),
'$copy_term_nv'(CopyGoal,Dic,NG,_),
translate(ProjT,NG,Code),
queries_atom(Code,FinalSQL),
'$write_or_not'(FinalSQL),
c_sqlite3_query(FinalSQL,ResultSet,Con,_,_),
!,
c_db_my_row(ResultSet,Arity,LA) ))).