basic myddas with SQLITE3
This commit is contained in:
parent
071bb36f0e
commit
ada2ad44de
@ -4,7 +4,7 @@
|
||||
|
||||
/* myddas_initialization.c */
|
||||
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);
|
||||
|
||||
#ifdef MYDDAS_STATS
|
||||
@ -19,7 +19,7 @@ void myddas_stats_delete_stats_list(MYDDAS_STATS_STRUCT);
|
||||
void myddas_util_table_write(MYSQL_RES *);
|
||||
#endif
|
||||
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 *);
|
||||
void myddas_util_delete_connection(void *);
|
||||
MYDDAS_UTIL_CONNECTION myddas_util_add_predicate(char * ,Int , char *,void *);
|
||||
|
@ -157,6 +157,7 @@ typedef void *MYDDAS_STATS_TIME;
|
||||
NUMBER = Yap_REGS.MYDDAS_GLOBAL_POINTER->memory_freed;
|
||||
#endif
|
||||
|
||||
#include "myddas_structs.h"
|
||||
#include "MyddasProto.h"
|
||||
|
||||
|
||||
|
@ -7,7 +7,6 @@
|
||||
#endif
|
||||
#include "Yap.h"
|
||||
#include "myddas.h"
|
||||
#include "myddas_structs.h"
|
||||
#ifdef MYDDAS_STATS
|
||||
#include "myddas_statistics.h"
|
||||
#endif
|
||||
@ -19,7 +18,7 @@ myddas_init_initialize_myddas(void){
|
||||
/* We cannot call MYDDAS_MALLOC were because the global
|
||||
register isn't yet initialized */
|
||||
global = (MYDDAS_GLOBAL) malloc (sizeof(struct myddas_global));
|
||||
#ifdef DEBUG
|
||||
#ifdef DEBUGX
|
||||
printf ("MALLOC %p %s %d\n",global,__FILE__,__LINE__);
|
||||
#endif
|
||||
global->myddas_top_connections = NULL;
|
||||
@ -53,6 +52,7 @@ myddas_init_initialize_myddas(void){
|
||||
/* Inserts the new node on the front of the list */
|
||||
MYDDAS_UTIL_CONNECTION
|
||||
myddas_init_initialize_connection(void *conn,void *enviromment,
|
||||
MYDDAS_API api,
|
||||
MYDDAS_UTIL_CONNECTION next){
|
||||
CACHE_REGS
|
||||
MYDDAS_UTIL_CONNECTION new = NULL;
|
||||
@ -62,6 +62,7 @@ myddas_init_initialize_connection(void *conn,void *enviromment,
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
new->api = api;
|
||||
new->predicates=NULL;
|
||||
new->connection=conn;
|
||||
new->odbc_enviromment=enviromment;
|
||||
|
@ -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);
|
||||
new = myddas_util_add_connection(conn,NULL,MYDDAS_API);
|
||||
|
||||
if (new == NULL){
|
||||
#ifdef DEBUG
|
||||
@ -515,7 +515,7 @@ c_db_my_row( USES_REGS1 ) {
|
||||
|
||||
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);
|
||||
head = HeadOfTerm(list);
|
||||
list = TailOfTerm(list);
|
||||
|
@ -348,7 +348,7 @@ c_db_odbc_connect( USES_REGS1 ) {
|
||||
{
|
||||
/* Criar um novo no na lista de ligacoes*/
|
||||
//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){
|
||||
fprintf(stderr,"Error: could not allocate list memory\n");
|
||||
return FALSE;
|
||||
|
@ -22,7 +22,6 @@
|
||||
#include "cut_c.h"
|
||||
#include "myddas.h"
|
||||
#include <stdlib.h>
|
||||
#include "myddas_structs.h"
|
||||
#ifdef MYDDAS_STATS
|
||||
#include "myddas_statistics.h"
|
||||
#endif
|
||||
@ -121,19 +120,26 @@ c_db_initialize_myddas( USES_REGS1 ){
|
||||
static Int
|
||||
c_db_connection_type ( USES_REGS1 ){
|
||||
Term arg_con = Deref(ARG1);
|
||||
Term arg_type = Deref(ARG2);
|
||||
Term arg_type =ARG2;
|
||||
|
||||
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 */
|
||||
Yap_unify(arg_type, MkAtomTerm(Yap_LookupAtom("mysql")));
|
||||
else if (type ==2) /* ODBC Connection */
|
||||
Yap_unify(arg_type, MkAtomTerm(Yap_LookupAtom("odbc")));
|
||||
else /* Not a valid connection*/
|
||||
return FALSE;
|
||||
|
||||
return TRUE;
|
||||
switch (type) {
|
||||
case API_MYSQL:
|
||||
/* MYSQL Connection */
|
||||
return Yap_unify(arg_type, MkAtomTerm(Yap_LookupAtom("mysql")));
|
||||
case API_ODBC:
|
||||
/* ODBC Connection */
|
||||
return Yap_unify(arg_type, MkAtomTerm(Yap_LookupAtom("odbc")));
|
||||
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*/
|
||||
|
@ -85,7 +85,7 @@ static Int c_sqlite3_change_database( USES_REGS1 );
|
||||
void Yap_InitMYDDAS_SQLITE3Preds(void)
|
||||
{
|
||||
/* 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 */
|
||||
Yap_InitCPred("c_sqlite3_number_of_fields",3, c_sqlite3_number_of_fields, 0);
|
||||
@ -144,7 +144,7 @@ c_sqlite3_connect( USES_REGS1 ) {
|
||||
else
|
||||
{
|
||||
/* 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){
|
||||
#ifdef DEBUG
|
||||
@ -313,6 +313,7 @@ c_sqlite3_query( USES_REGS1 ) {
|
||||
/* Send query to server and process it */
|
||||
if (strcmp(mode,"store_result")!=0) {
|
||||
// Leave data for extraction
|
||||
printf(" SQL 0: %s\n", sql);
|
||||
CALL_SQLITE (prepare_v2(db, sql, -1, &stmt, NULL) );
|
||||
rs->stmt = stmt;
|
||||
rs->res_set = NULL;
|
||||
@ -337,7 +338,7 @@ c_sqlite3_query( USES_REGS1 ) {
|
||||
|
||||
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)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
@ -381,9 +382,10 @@ c_sqlite3_number_of_fields( USES_REGS1 ) {
|
||||
|
||||
char sql[256];
|
||||
|
||||
sprintf(sql,"SELECT * FROM TABLE `%s`",relation);
|
||||
sprintf(sql,"SELECT * FROM `%s`",relation);
|
||||
|
||||
/* executar a query SQL */
|
||||
printf(" SQL 1: %s\n", sql);
|
||||
CALL_SQLITE (prepare_v2(db, sql, -1, &stmt, NULL) );
|
||||
|
||||
int fields = sqlite3_column_count( stmt );
|
||||
@ -410,9 +412,10 @@ c_sqlite3_get_attributes_types( USES_REGS1 ) {
|
||||
sqlite3_stmt *stmt;
|
||||
Int rc = TRUE;
|
||||
|
||||
sprintf(sql,"SELECT * FROM TABLE `%s`",relation);
|
||||
sprintf(sql,"SELECT * FROM `%s`",relation);
|
||||
|
||||
/* executar a query SQL */
|
||||
printf(" SQL 3: %s\n", sql);
|
||||
CALL_SQLITE (prepare_v2(db, sql, -1, &stmt, NULL) );
|
||||
|
||||
int fields = sqlite3_column_count( stmt );
|
||||
@ -446,7 +449,8 @@ c_sqlite3_get_attributes_types( USES_REGS1 ) {
|
||||
tm = "blob";
|
||||
break;
|
||||
case SQLITE_NULL:
|
||||
tm = "null";
|
||||
default:
|
||||
tm = "";
|
||||
break;
|
||||
}
|
||||
if (!Yap_unify(head, MkAtomTerm(Yap_LookupAtom(tm))) )
|
||||
@ -505,9 +509,10 @@ c_sqlite3_get_fields_properties( USES_REGS1 ) {
|
||||
|
||||
sqlite3_stmt *stmt;
|
||||
|
||||
sprintf(sql,"SELECT * FROM TABLE `%s`",relation);
|
||||
sprintf(sql,"SELECT * FROM `%s`",relation);
|
||||
|
||||
/* executar a query SQL */
|
||||
printf(" SQL 4: %s\n", sql);
|
||||
CALL_SQLITE (prepare_v2(db, sql, -1, &stmt, NULL) );
|
||||
|
||||
Functor functor = Yap_MkFunctor(Yap_LookupAtom("property"),4);
|
||||
@ -590,9 +595,9 @@ c_sqlite3_change_database( USES_REGS1 ) {
|
||||
static Int
|
||||
c_sqlite3_row_cut( USES_REGS1 ) {
|
||||
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 ) );
|
||||
free(res_set);
|
||||
return TRUE;
|
||||
@ -628,7 +633,7 @@ c_sqlite3_row( USES_REGS1 ) {
|
||||
EXTRA_CBACK_ARG(3,1)= arg_result_set ;
|
||||
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];
|
||||
Int i, arity;
|
||||
@ -637,7 +642,7 @@ c_sqlite3_row( USES_REGS1 ) {
|
||||
sqlite3 *db = res_set->db;
|
||||
if (res_set->stmt == NULL ) {
|
||||
CACHE_REGS
|
||||
Int indx = IntegerOfTerm(EXTRA_CBACK_CUT_ARG(Term,2));
|
||||
Int indx = IntegerOfTerm(EXTRA_CBACK_ARG(3,2));
|
||||
Int rc = true;
|
||||
// data needs to be copied to Prolog
|
||||
// row by row
|
||||
@ -723,9 +728,9 @@ c_sqlite3_row( USES_REGS1 ) {
|
||||
tf = Yap_MkApplTerm(Yap_MkFunctor(Yap_LookupAtom("null"),1),1,null_atom);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!Yap_unify(head, tf))
|
||||
rc = FALSE;
|
||||
}
|
||||
#ifdef MYDDAS_STATS
|
||||
end = myddas_stats_walltime();
|
||||
|
||||
|
@ -36,9 +36,17 @@ struct myddas_list_preds {
|
||||
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 {
|
||||
void *connection;
|
||||
int tag;
|
||||
|
||||
MYDDAS_API api;
|
||||
/*If variable env is NULL, then it's a
|
||||
MySQL connection, if not then it as the pointer
|
||||
to the ODBC enviromment variable */
|
||||
|
@ -49,10 +49,11 @@ myddas_util_connection_type(void *con){
|
||||
if (con_node == NULL)
|
||||
return 0;
|
||||
|
||||
if (con_node->odbc_enviromment != NULL) /* ODBC */
|
||||
return 2;
|
||||
else
|
||||
return 1;
|
||||
return con_node->api;
|
||||
// if (con_node->odbc_enviromment != NULL) /* ODBC */
|
||||
// return 2;
|
||||
//else
|
||||
// return 1;
|
||||
}
|
||||
|
||||
|
||||
@ -161,7 +162,7 @@ myddas_util_search_connection(void *conn){
|
||||
}
|
||||
|
||||
MYDDAS_UTIL_CONNECTION
|
||||
myddas_util_add_connection(void *conn, void *enviromment){
|
||||
myddas_util_add_connection(void *conn, void *enviromment, MYDDAS_API api){
|
||||
CACHE_REGS
|
||||
MYDDAS_UTIL_CONNECTION node=NULL;
|
||||
MYDDAS_UTIL_CONNECTION temp=NULL;
|
||||
@ -171,7 +172,7 @@ myddas_util_add_connection(void *conn, void *enviromment){
|
||||
return node;
|
||||
}
|
||||
//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)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
@ -354,7 +355,7 @@ myddas_util_get_pred_name(void *pointer){
|
||||
}
|
||||
|
||||
char *
|
||||
myddas_util_get_pred_module(void *pointer){
|
||||
vmyddas_util_get_pred_module(void *pointer){
|
||||
MYDDAS_UTIL_PREDICATE temp = (MYDDAS_UTIL_PREDICATE) pointer;
|
||||
return temp->pred_module;
|
||||
}
|
||||
@ -383,7 +384,7 @@ void check_int( void ){
|
||||
if (top->predicates != NULL)
|
||||
{
|
||||
printf ("\t******\n");
|
||||
printf ("\t===== PREDICADOS =====\n");
|
||||
printf ("\t===== PREDICATES =====\n");
|
||||
for (pred = top->predicates ; pred != NULL ; pred = pred->next)
|
||||
{
|
||||
printf ("\t--------------\n");
|
||||
|
@ -228,7 +228,7 @@ If we import a database relation, such as an edge relation representing the edge
|
||||
?- db_import('Edge',edge).
|
||||
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
|
||||
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
@ -891,9 +891,9 @@ db_open(odbc,Connection,ODBCEntry,User,Password) :-
|
||||
set_value(Connection,Con).
|
||||
#endif
|
||||
#ifdef MYDDAS_SQLITE3
|
||||
db_open(sqlite3,Connection,ODBCEntry,User,Password) :-
|
||||
'$error_checks'(db_open(odbc,Connection,ODBCEntry,User,Password)),
|
||||
c_db_sqlite3_connect(ODBCEntry,User,Password,Con),
|
||||
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
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
@ -56,6 +56,11 @@
|
||||
]).
|
||||
|
||||
|
||||
:- use_module(myddas_sqlite3,[
|
||||
sqlite3_result_set/1
|
||||
]).
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_import/3
|
||||
% db_import/2
|
||||
@ -65,17 +70,7 @@ db_import(RelationName,PredName):-
|
||||
db_import(Connection,RelationName,PredName) :-
|
||||
'$error_checks'(db_import(Connection,RelationName,PredName)),
|
||||
get_value(Connection,Con),
|
||||
c_db_connection_type(Con,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_sqllite3_number_of_fields(RelationName,Con,Arity)
|
||||
),
|
||||
table_arity( Con, ConType, RelationName, Arity ),
|
||||
db_module(Module),
|
||||
not c_db_check_if_exists_pred(PredName,Arity,Module),
|
||||
|
||||
@ -84,16 +79,9 @@ db_import(Connection,RelationName,PredName) :-
|
||||
assert(myddas_prolog2sql:R),
|
||||
|
||||
Size is 2*Arity,
|
||||
'$make_a_list'(Size,TypesList),
|
||||
length(TypesList, Size),
|
||||
% get attributes types in TypesList [field0,type0,field1,type1...]
|
||||
( ConType == mysql ->
|
||||
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)
|
||||
),
|
||||
table_attributes( ConType, Con, RelationName, TypesList ),
|
||||
|
||||
% assert attributes facts
|
||||
'$assert_attribute_information'(0,Arity,RelationName,TypesList),
|
||||
@ -104,37 +92,7 @@ db_import(Connection,RelationName,PredName) :-
|
||||
|
||||
M=myddas_assert_predicates,
|
||||
%build PredName clause
|
||||
( ConType == mysql ->
|
||||
|
||||
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)))))))]
|
||||
),
|
||||
|
||||
table_access_predicate( ConType, Con, Arity, P, LA, M, Assert ),
|
||||
assert(Module:Assert),
|
||||
c_db_add_preds(PredName,Arity,Module,Con).
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
@ -166,26 +124,7 @@ db_view(Connection,PredName,DbGoal) :-
|
||||
c_db_connection_type(Con,ConType),
|
||||
|
||||
% build view clause
|
||||
( ConType == mysql ->
|
||||
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
|
||||
),
|
||||
table_view( ConType, Con, CopyView, CopyGoal, Arity, LA, M, Assert ),
|
||||
assert(Module:Assert),
|
||||
c_db_add_preds(ViewName,Arity,Module,Con).
|
||||
|
||||
@ -205,11 +144,7 @@ db_insert(Connection,RelationName,PredName) :-
|
||||
c_db_connection_type(Con,ConType),
|
||||
|
||||
% get relation arity
|
||||
( ConType == mysql ->
|
||||
c_db_my_number_of_fields(RelationName,Con,Arity)
|
||||
;
|
||||
c_db_odbc_number_of_fields(RelationName,Con,Arity)
|
||||
),
|
||||
table_arity( Con, ConType, RelationName, Arity ),
|
||||
db_module(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...]
|
||||
% and build PredName clause
|
||||
( ConType == mysql ->
|
||||
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))))]
|
||||
),
|
||||
table_insert( ConType, Con, RelationName, TypesList, Predicate, LA, Assert ),
|
||||
assert(Module:Assert),
|
||||
c_db_add_preds(PredName,Arity,Module,Con).
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
@ -308,3 +230,124 @@ db_listing(Name):-
|
||||
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) ))).
|
||||
|
Reference in New Issue
Block a user