CC = gcc
LD = gcc
CFLAGS = -Wall -D_GNU_SOURCE \
- -Dwith_postgres \
- -Dwith_mysql \
$(OPTIME)
LIBMAIL = libmail.a libmar.a libpos.a
PAR = -j`/usr/bin/getconf _NPROCESSORS_ONLN`
#include "unipos.h"
#include "devsql.h"
+typedef void SQLRES; //Result from database
typedef enum {
- db_postgres, //Postgres database
- db_maria, //Mariadb SQL
- db_unknown
- }SQLDBTYP;
+ db_postgres, //Postgres database
+ db_maria, //Mariadb SQL
+ db_unknown
+ }SQLDBTYP;
typedef struct {
SQLDBTYP sqldb; //database type
MARPTR *msql; //Mariadb|MySQL Reference
}db;
}SQLTYP;
+
+//SQL database request
+#define SELFROM "SELECT * FROM"
+#define EMAILS "emails" //emails tables
+/*
+\f
+*/
+/********************************************************/
+/* */
+/* Procedure to drop result issued by a successfull*/
+/* command. */
+/* */
+/********************************************************/
+static SQLRES *dropresult(SQLRES *rs)
+
+{
+return rs;
+}
+/*
+\f
+*/
+/********************************************************/
+/* */
+/* Procedure to the number of tupple within a */
+/* result issued by a successfull command. */
+/* */
+/********************************************************/
+static int nbrtupple(SQLTYP *sql,SQLRES *rs)
+
+{
+#define OPEP "devsql.c:nbrtupple,"
+
+int numrow;
+
+numrow=0;
+switch(sql->sqldb) {
+ case db_postgres :
+ numrow=pos_nbrtupple((POSRES *)rs);
+ break;
+ case db_maria :
+ numrow=mar_nbrtupple((MARRES *)rs);
+ break;
+ default :
+ (void) rou_alert(0,"%s Unexpected type='%d' (BUG!?)",
+ OPEP,(int)sql->sqldb);
+ break;
+ }
+return numrow;
+
+#undef OPEP
+}
+/*
+\f
+*/
+/********************************************************/
+/* */
+/* Procedure to create a data request and submit it*/
+/* to the proper database daemon. */
+/* */
+/********************************************************/
+static SQLRES *gettupple(SQLTYP *sql,const char *fmt,...)
+
+{
+#define OPEP "devsql.c:gettupple,"
+
+SQLRES *rs;
+va_list args;
+char *cmd;
+
+rs=(SQLRES *)0;
+va_start(args,fmt);
+if ((rou_vasprintf(&cmd,fmt,args))>0) {
+ switch(sql->sqldb) {
+ case db_postgres :
+ rs=(SQLRES *)pos_gettupple(sql->db.psql,cmd);
+ break;
+ case db_maria :
+ rs=(SQLRES *)mar_gettupple(sql->db.msql,cmd);
+ break;
+ default :
+ (void) rou_alert(0,"%s Unexpected type='%d' (BUG!?)",
+ OPEP,(int)sql->sqldb);
+ break;
+ }
+ (void) free(cmd);
+ }
+va_end(args);
+return rs;
+
+#undef OPEP
+}
/*
\f
*/
{
#define OPEP "devsql.c:sql_getuser,"
+#define SELUSR "%s %s WHERE username=%s"
USRTYP *usr;
SQLTYP *sql;
+SQLRES *rs;
int phase;
_Bool proceed;
phase=999;
}
break;
- case 3 : //opening specific database
- switch (sql->sqldb) {
- case db_postgres :
- break;
- case db_maria :
- break;
- default :
- (void) rou_alert(0,"%s Unexpected type='%d' (bug?)",OPEP,sql->sqldb);
- break;
+ case 3 : //getting user information
+ if ((rs=gettupple(sql,SELUSR,SELFROM,EMAILS,email))!=(SQLRES *)0) {
+ (void) rou_alert(0,"%s Unable to get data for user <%s> (Database?)",
+ OPEP,email);
+ phase=999; //Trouble trouble
+ }
+ break;
+ case 4 : //getting user information
+ if (nbrtupple(sql,rs)>0) {
}
+ rs=dropresult(rs);
break;
default :
proceed=false;
phase++;
}
return usr;
+
+#undef SELUSR
#undef OPEP
}
(void) strcat(cleanstr,"'");
return cleanstr;
}
+/*
+\f
+*/
+/********************************************************/
+/* */
+/* Procedure to extract data from the database. */
+/* Return POSRES status pointer (can be NULL is not*/
+/* successfull. */
+/* */
+/********************************************************/
+PUBLIC MARRES *mar_gettupple(MARPTR *marptr,char *command)
+
+{
+#define OPEP "unimar.c:mar_gettupple"
+
+register MARRES *marres;
+
+marres=(MARRES *)0;
+#ifdef DB_MYSQL
+ {
+ MYSQL_RES *mysstatut;
+ register MYSQL *ms;
+
+ mysstatut=(MYSQL_RES *)0;
+ ms=(MYSQL *)marptr;
+ if (mysql_query(ms,command)==0) {
+ if ((mysstatut=mysql_store_result(ms))==(MYSQL_RES *)0)
+ (void) rou_alert(3,"%s result empty for cmd=<%s>",OPEP,command);
+ }
+ else {
+ (void) rou_alert(0,"%s pid='%05d' Unable to carry cmd=<%s>, error=<%s>",
+ OPEP,getpid(),command,mysql_error((MYSQL *)ms));
+ }
+ marres=(MARRES *)mysstatut;
+ }
+#endif
+return marres;
+
+#undef OPEP
+}
+/*
+\f
+*/
+/********************************************************/
+/* */
+/* Procedure to return the number of tupple related*/
+/* related to a previous search. */
+/* */
+/********************************************************/
+PUBLIC int mar_nbrtupple(MARRES *marres)
+
+{
+#define OPEP "unimar.c:mar_nbrtupple,"
+
+register int numrow;
+
+numrow=0;
+#ifdef DB_MYSQL
+ numrow=mysql_num_rows((MYSQL_RES *)marres);
+#endif
+return numrow;
+
+#undef OPEP
+}
//reference to a SQL pointer reference
typedef void MARPTR;
+//reference to a MariaDB result
+typedef void MARRES;
+
//Opening a postscript database
extern MARPTR *mar_opensql(const char *host,const char *sqlport,const char *dbname);
//Procedure to detect and 'clean' any single quote within a string
extern char *mar_cleanquote(char *sequence);
+//procedure to extract data from database
+extern MARRES *mar_gettupple(MARPTR *marptr,char *command);
+
+//procedure to return the number of entry within a result
+extern int mar_nbrtupple(MARRES *marres);
+
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
#include "subrou.h"
#include "unipos.h"
#if DATABASE==USE_POSTGRESQL
#define DB_POSTGRESQL
#endif
+/*
+\f
+*/
+/********************************************************/
+/* */
+/* Procedure to execut a database request by daemon*/
+/* */
+/********************************************************/
+#ifdef DB_POSTGRESQL
+static PGresult *request(PGconn *pf,char *directive)
+
+{
+#define OPEP "unipos.c:request,"
+#define RELAX 1000000
+
+PGresult *statut;
+int try;
+statut=(PGresult *)0;
+try=0;
+while (statut==(PGresult *)0) {
+ try++;
+ switch(PQstatus(pf)) {
+ case CONNECTION_OK :
+ if ((statut=PQexec(pf,directive))!=(PGresult *)0)
+ break;
+ else
+ (void) rou_alert(0,"%s Command <%s> failed, (error=<%s>), retrying",
+ OPEP,directive,PQerrorMessage(pf));
+ // fall through
+ // NO Break, want DB reset
+ default :
+ (void) rou_alert(0,"%s reseting postgres connection (try='%d')",
+ OPEP,try);
+ (void) usleep(RELAX/2);
+ (void) PQreset(pf);
+ (void) usleep(RELAX/2);
+ break;
+ }
+ if (try>10) {
+ (void) rou_alert(0,"%s Unable to carry command <%s> Postgres error!",
+ OPEP,directive);
+ break;
+ }
+ }
+return statut;
+
+#undef RELAX
+#undef OPEP
+}
+#endif
/*
\f
*/
(void) strcat(cleanstr,"'");
return cleanstr;
}
+/*
+\f
+*/
+/********************************************************/
+/* */
+/* Procedure to extract data from the database. */
+/* Return POSRES status pointer (can be NULL is not*/
+/* successfull. */
+/* */
+/********************************************************/
+PUBLIC POSRES *pos_gettupple(POSPTR *posptr,char *command)
+
+{
+#define OPEP "basmys.c:mys_tupple,"
+
+register POSRES *posres;
+
+posres=(POSRES *)0;
+#ifdef DB_POSTGRESQL
+ {
+ PGresult *pgstatut;
+ register PGconn *pf;
+
+ pf=(PGconn *)posptr;
+ if ((pgstatut=request(pf,command))!=(PGresult *)0) {
+ if (PQresultStatus(pgstatut)!=PGRES_TUPLES_OK) {
+ (void) rou_alert(0,"%s SQL command <%s> failed",OPEP,command);
+ (void) PQclear(pgstatut);
+ pgstatut=(PGresult *)0;
+ }
+ }
+ posres=(POSRES *)pgstatut;
+ }
+#endif
+return posres;
+
+#undef OPEP
+}
+/*
+\f
+*/
+/********************************************************/
+/* */
+/* Procedure to retunr the number of tupple related*/
+/* related to a previous search. */
+/* */
+/********************************************************/
+PUBLIC int pos_nbrtupple(POSRES *posres)
+
+{
+register int numrow;
+
+numrow=0;
+#ifdef DB_POSTGRESQL
+numrow=PQntuples((PGresult *)posres);
+#endif
+return numrow;
+}
//reference to a SQL pointer reference
typedef void POSPTR;
+//reference to a POSTGRESQL result
+typedef void POSRES;
+
//Opening a postscript database
extern POSPTR *pos_opensql(const char *host,const char *sqlport,const char *dbname);
//Procedure to detect and 'clean' any single quote within a string
extern char *pos_cleanquote(char *sequence);
+//procedure to extract data from database
+extern POSRES *pos_gettupple(POSPTR *posptr,char *command);
+
+//procedure to return the number of entry within a result
+extern int pos_nbrtupple(POSRES *posres);
+
#endif