sql rewrite....

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello all...
I have buit a kind of sql rewrite that is:
you define in your postgres databas a function aliarewrite('xxxxx')
where xxxx is the string number or alias to be rewriten by Toolkit.cxx
the function must return the string to be rewritten 

For example:

create or replace function aliasrewrite(text) returns text as '

	begin
            return $1;
        end;

'  language 'plpgsql';

this function returns the same string as entered... but you
can modify it your wish, for example consulting sql tables
or makeing some complex string manipulation...

it is very fast... about 1ms... 

than when you start gk, you can specify a --pgstr parameter specifiying
the connection to the postgres database where the function is

for example

/usr/local/bin/gnugk -c /usr/local/etc/gnugk.ini \
	-r \
	--pid /var/run/gnugk.pid \
	--pgsql "dbname=admdb host=xxxxxx user=gnugkadm password=aaaaaaaaaa" 


I know that is could be done using the same logic as sqlaccount... but I am not
as  good enough to modify that module...

For those who want to experiment....
go to the openh323gk directory and do a patch < patchfile  (see below)
than just do a make opt install and you are done...


Sergio


===============================diff file============================================



--- Toolkit.cxx Sun Jan 23 13:15:07 2005
+++ ../Toolkit.cxx      Mon Jan 24 18:52:44 2005
@@ -26,6 +26,7 @@
 #include "h323util.h"
 #include "Toolkit.h"

+extern char *sqlrewrite(char *p);

 extern const char *ProxySection;

@@ -251,7 +252,18 @@
 bool Toolkit::RewriteTool::RewritePString(PString & s) const
 {
        bool changed = false;
+       char *p;

+       p=sqlrewrite(s.GetPointer());
+       PTRACE(1, "REWRITE  [" << s << "]-> " << p);
+       if (strcmp(p,s)) {
+            PTRACE(1, " SQL REWRITE  [" << s << "]-> " << p);
+            s=p;
+            free(p);
+            changed=true;
+            return changed;
+       }
+       free(p);
        // remove trailing character
        if (s.GetLength() > 1 && s[s.GetLength() - 1] == m_TrailingChar) {
                s = s.Left(s.GetLength() - 1);
@@ -942,3 +954,4 @@
        buf.MakeMinimumSize();
        return buf;
 }
+
--- gk.cxx      Thu Dec 23 00:54:25 2004
+++ ../gk.cxx   Mon Jan 24 19:20:38 2005
@@ -38,6 +38,7 @@
 #include "gktimer.h"
 #include "gk.h"

+void sqlrewrite_init(char *);

 /*
  * many things here should be members of Gatkeeper.
@@ -306,6 +307,7 @@
                 "c-config:"
                 "s-section:"
                 "-pid:"
+                "-pgstr:"
                 "h-help:"
                 );
 }
@@ -317,6 +319,7 @@
        SetConsoleCtrlHandler(WinCtrlHandlerProc, TRUE);
 #else
        struct sigaction sigact;
+       char *pgstr;

        memset(&sigact, 0, sizeof(sigact));
        sigact.sa_handler = UnixShutdownHandler;
@@ -348,6 +351,10 @@
        sigaction(SIGHUP, &sigact, NULL);
        sigaction(SIGUSR1, &sigact, NULL);

+       if (args.HasOption("pgstr")) {
+               pgstr=args.GetOptionString("pgstr").GetPointer();
+               sqlrewrite_init(strdup(pgstr));
+       }
        if (args.HasOption("pid"))
                pidfile = args.GetOptionString("pid");
        PTextFile pid(pidfile, PFile::WriteOnly);
@@ -430,6 +437,7 @@
                "  -c  --config file  : Specify which config file to use\n"
                "  -s  --section sec  : Specify which main section to use in the config file\n"
                "      --pid file     : Specify the pid file\n"
+               "      --pgstr string : Specify the postgres rewrite conn\n"
                "  -h  --help         : Show this message\n" << endl;
 }

--- sqlrewrite.cxx      Mon Jan 24 16:48:01 2005
+++ ../sqlrewrite.cxx   Mon Jan 24 19:20:22 2005
@@ -0,0 +1,48 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pthread.h>
+
+#include "libpq-fe.h"
+
+static pthread_mutex_t mu;
+static char *conn_string = NULL;
+
+static PGconn *c = NULL;
+
+
+char *sqlrewrite(char *s) {
+    char area[1024],*p;
+    PGresult *r;
+
+    if (!conn_string)
+       return strdup(s);
+
+    pthread_mutex_lock(&mu);
+    if (! c) {
+        c=PQconnectdb(conn_string);
+       if (PQstatus(c) != CONNECTION_OK) {
+           c=NULL;
+           pthread_mutex_unlock(&mu);
+           return strdup(s);
+       }
+    }
+    sprintf(area,"select aliasrewrite('%s')",s);
+    r=PQexec(c,area);
+    if (PQresultStatus(r) != PGRES_TUPLES_OK) {
+       PQclear(r);
+       PQfinish(c);
+       c=NULL;
+       return strdup(s);
+    }
+    p=strdup(PQgetvalue(r,0,0));
+    PQclear(r);
+    pthread_mutex_unlock(&mu);
+    return p;
+}
+
+
+void sqlrewrite_init(char *conn) {
+    conn_string=strdup(conn);
+    pthread_mutex_init(&mu,NULL);
+}
--- Makefile    Mon Jan 24 16:51:56 2005
+++ ../Makefile Mon Jan 24 01:17:45 2005
@@ -25,6 +25,7 @@

 PROG    = gnugk
 SOURCES         = main.cxx singleton.cxx job.cxx yasocket.cxx h323util.cxx \
+          sqlrewrite.cxx \
            Toolkit.cxx SoftPBX.cxx GkStatus.cxx RasTbl.cxx          \
            Routing.cxx Neighbor.cxx GkClient.cxx gkauth.cxx         \
            RasSrv.cxx ProxyChannel.cxx gk.cxx version.cxx gkacct.cxx \


-------------------------------------------------------
This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting
Tool for open source databases. Create drag-&-drop reports. Save time
by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc.
Download a FREE copy at http://www.intelliview.com/go/osdn_nl

_______________________________________________________

List: Openh323gk-users@xxxxxxxxxxxxxxxxxxxxx
Archive: http://sourceforge.net/mailarchive/forum.php?forum_id=8549
Homepage: http://www.gnugk.org/

[Index of Archives]     [SIP]     [Open H.323]     [Gnu Gatekeeper]     [Asterisk PBX]     [ISDN Cause Codes]     [Yosemite News]

  Powered by Linux