1 (edited by MattF 2008-01-16 21:23)

Topic: Separate user and forum db's

Been searching the board, and so far haven't found anything other than the solution for using a separate user table in the same db. Would it be feasible to have all the user information on a completely separate db, and if so, what, roughly, would need doing to achieve it? Would it need a separate function adding in the dblayer and altering the code throughout PunBB to point to the new function wherever user/group queries are concerned?


Cheers,

Matt

Re: Separate user and forum db's

str_replace, replace $db->prefix.'users' with 'dbname.'.$db->prefix.'users

3

Re: Separate user and forum db's

Would that need to be done throughout the code itself, or should using that within the dblayer query function work?

i.e:

        function query($sql, $unbuffered = false)
        {
                $sql = str_replace($this->prefix.'users', 'userdb.'.$this->prefix.'users', $sql);
                $sql = str_replace($this->prefix.'groups', 'userdb.'.$this->prefix.'groups', $sql);
                $sql = str_replace($this->prefix.'online', 'userdb.'.$this->prefix.'online', $sql);

I've just tried that method within the query function, and it error logs as:

STATEMENT:  SELECT u.*, g.*, o.logged FROM userdb.users AS u INNER JOIN userdb.groups AS g ON u.group_id=g.g_id LEFT JOIN userdb.online AS o ON o.ident='[I.P address]' WHERE u.id=1
ERROR:  3F000: schema "userdb" does not exist
LOCATION:  LookupExplicitNamespace, namespace.c:1289
STATEMENT:  SELECT user_id FROM userdb.online WHERE idle=0
WARNING:  25P01: there is no transaction in progress
LOCATION:  EndTransactionBlock, xact.c:2931

Apologies if I'm missing the point again. big_smile


Thanks again,

Matt

Re: Separate user and forum db's

What database system are you using? I know MySQL uses the dbname.tablename format, it could be different for other systems

Re: Separate user and forum db's

He's probably using pgSQL.

Re: Separate user and forum db's

for which I've found it's not possible wink
http://www.archonet.com/pgdocs/join-across-db.html

7

Re: Separate user and forum db's

It is indeed PgSQL. smile

With it not being a native function, (but I've found there is an addon to achieve the functionality, in a fashion), is there any plausible way of integrating this into the query:

http://www.postgresql.org/docs/8.3/stat … blink.html
http://www.postgresql.org/docs/8.3/static/dblink.html

By the looks of that documentation, str_replace wouldn't be upto the job for that syntax. big_smile


Thanks again,

Matt

Re: Separate user and forum db's

Mmm, it looks like it would. Just replace $db->prefix.'users with the proper dblink syntax,

9 (edited by MattF 2008-01-17 04:23)

Re: Separate user and forum db's

$sql = str_replace($this->prefix."users", "dblink('dbname=userdb', '".$this->prefix."users')", $sql);

That would be roughly the alternative to the MySQL method above?

However, by the looks of that first dblink doc, the select statement for that table also needs to be within that dblink function, along the lines of:

$sql = str_replace($this->prefix."users", "dblink('dbname=userdb', 'select [userdb required columns here] from ".$this->prefix."users')", $sql);

Unless I've misinterpreted that dblink documentation, that wouldn't be feasible using that str_replace method above, would it? Or, have I completely misinterpreted that documentation, as is my usual forte? big_smile


Thanks again,

Matt

Re: Separate user and forum db's

Just select * should work

11

Re: Separate user and forum db's

How would things be done for the likes of delete/insert/update? Just been thinking about that. (Dangerous thing for me, I know). big_smile But, the statement/action part couldn't be wildcarded or similar as such, could it?

Would it be simpler to just run a second instance of the dblayer, (adjusted accordingly), in parallel with the existing one specifically for the user/group/online tables, rather than trying to work this dblink into the existing dblayer class?

Re: Separate user and forum db's

If dblink works right for DELETE, etc queries, then the example I just discussed should work fine. If not, then it doesn't wink

13 (edited by MattF 2008-01-17 16:27)

Re: Separate user and forum db's

My minds obviously in thick mode again. big_smile The bit which I'm just totally not grasping is this:

dblink('dbname=userdb', 'SELECT * FROM ".$this->prefix."users')

With the SELECT statement being hardcoded in, how would it know what to do when a DELETE or such was part of the query, rather than SELECT?

Re: Separate user and forum db's

Well, that query would look something like
delete from dblink('dbname=userdb', 'SELECT * FROM ".$this->prefix."users')

Which may or may not work fine, depending upon what dblink is actually doing.

Re: Separate user and forum db's

Looks like annoying syntax when doing joins.

16

Re: Separate user and forum db's

elbekko wrote:

Looks like annoying syntax when doing joins.

Only if it worked. big_smile

Just given it a whirl using that select * inside the dblink function. It's not a happy bunny. This is what it logged:

STATEMENT:  SELECT u.*, g.*, o.logged FROM dblink('dbname=userdb', 'select * from users') AS u INNER JOIN dblink('dbname=userdb', 'select * from groups') AS g ON u.group_id=g.g_id LEFT JOIN dblink('dbname=userdb', 'select * from online') AS o ON o.ident='[I.P]' WHERE u.id=1
ERROR:  42601: a column definition list is required for functions returning "record"

Re: Separate user and forum db's

It's not because more is required than that. Check out the code examples given in the PostgreSQL documentation, you need to define each column individually.
I'm just going to say that there is no simple way to interact between databases in PostgreSQL smile

18

Re: Separate user and forum db's

That's definitely the truth about it not being simple. big_smile

So the straight strip replace is definitely not upto the job then. What feasible way is there of matching and extracting inside the query function the parts that need to go into the dblink function? Is it a case of going for full blown preg_replace/preg_match type regexes?

Thanks for all the advice, btw. smile


Matt