Re: [SQL] [JDBC] Calling a table in another database from plpgsql

2005-01-09 Thread Dave Cramer
Kris is correct, this is a jdbc list, however to satisfy your curiosity, 
you can't access data from another database.

Dave
[EMAIL PROTECTED] wrote:
Hi,
I have function called test1() in database 1. I need to input the value from
this function into another table which is in database2.
How can i achieve this task in plpgsql. I went through some documentation and
found out that it is possible through dblink.
I infact downloaded the dblink package in contrib folder. But still when i tried
to use dblink in the following manner:
SELECT * from dblink('host=192.168.0.50 dbname=AK1 user=akteam','Select userid
from user_reg') as t (userid integer);
I am getting an error as "ERROR:  function dblink("unknown", "unknown") does not
exist"
Is dblink the only way of connecting the databases and if so what is the exact
step by step procedure to be follwed to download dblink and use it to connect
databases.
Thanks in advance,
Regards,
deepthi



---(end of broadcast)---
TIP 6: Have you searched our list archives?
  http://archives.postgresql.org
 

--
Dave Cramer
http://www.postgresintl.com
519 939 0336
ICQ#14675561
---(end of broadcast)---
TIP 2: you can get off all lists at once with the unregister command
   (send "unregister YourEmailAddressHere" to [EMAIL PROTECTED])


Re: [SQL] Calling a table residing in another database from Plpgsql

2005-01-09 Thread CoL
hi,
[EMAIL PROTECTED] wrote, On 1/5/2005 06:13:
I infact downloaded the dblink package in contrib folder. But still when i
tried to use dblink in the following manner:
SELECT * from dblink('host=192.168.0.50 dbname=AK1 user=akteam','Select
userid from user_reg') as t (userid integer);
I am getting an error as "ERROR:  function dblink("unknown", "unknown")
does not exist"
try to set up dblink. Read the README.dblink file, Installation: part.
Use dblink_connect to connect, then you can query with dblink()
read the docs befor you try to use it.
PS: or better use schemas, not different databases.
C.
---(end of broadcast)---
TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]


[SQL] Question about insert/update RULEs.

2005-01-09 Thread Dmitri Bichko
Hello,

I am trying to use the RULE system to simulate updatable views, with the
final goal of simulating polymorphism in an ORM-like system
(Class::DBI).

Same old idea - when selecting from foo C:DBI checks the "type" column
and reblesses (I guess casts, in non-perl world) the object to the
appropriate class (which will lazily fetch the additional columns).

Here's what I have so far:

CREATE TABLE "foo" (
  "foo_id"  serial PRIMARY KEY,
  "type"varchar NOT NULL DEFAULT 'base' CONSTRAINT types CHECK
(type IN ('base', 'bar'))
  "a"   varchar NOT NULL,
  "b"   varchar DEFAULT 'some text',
);

CREATE TABLE "foo_bar" (
  "foo_id" integer PRIMARY KEY CONSTRAINT foo REFERENCES foo (foo_id) ON
DELETE CASCADE ON UPDATE CASCADE DEFERABLE,
  "c"   varchar
);

CREATE VIEW "bar" AS
SELECT f.foo_id, f.a, f.b, b.c
FROM foo f JOIN foo_bar b USING(foo_id);

CREATE RULE "bar_insert" AS ON INSERT TO "bar"
DO INSTEAD (
INSERT INTO "foo" (foo_id, type, a, b) VALUES
(NEW.foo_id, 'bar', NEW.a, NEW.b);
INSERT INTO "foo_bar" (foo_id, c) VALUES (NEW.foo_id,
NEW.c);
);

CREATE RULE "bar_update" AS ON UPDATE TO "bar"
DO INSTEAD (
UPDATE "foo" SET a = NEW.a, b = NEW.b WHERE foo_id =
OLD.foo_id;
UPDATE "foo_bar" SET c = NEW.c WHERE foo_id =
OLD.foo_id;
);

CREATE RULE "bar_delete" AS ON DELETE TO "bar"
DO INSTEAD
DELETE FROM "foo" WHERE foo_id = OLD.foo_id;


The problem is that for the sequence to do the right thing, I have to
select nextval first in a separate query and then pass it explicitely to
INSERT INTO "bar" (which C:DBI does anyway, but I'd like to do better).

If I were to do this:
 foo insert: foo_id = COALESCE(NEW.foo_id, nextval('foo_foo_id_seq')),
 foo_bar insert: foo_id = COALESCE(NEW.foo_id,
currval('foo_foo_id_seq')),

Will the currval() be guaranteed to be the same value that the nextval()
got?  I am not quite sure what the "scope" of currval() is.

Also, using COALESCE is the only way I can think of to deal with default
values, is there a way to have the rule respect the defaults of the
underlying tables without having to specify them again in the rule
itself (ie COALESCE(NEW.b, 'some text')?

Secondly, the fact that UPDATE queries work seems a little magical to me
(I got the rule examples from the docs): if a user does not specify a
value for a column in an update, is its NEW value set to OLD?  Also, it
seems that I can specify arbitrary WHERE clauses for the updates and
they work properly, but it seems that the rule only matches on the
primary key - why does this work?

I've been having trouble finding documentation about what other
information (in addition to NEW and OLD) about the query being rewritten
is available to the INSTEAD queries - mainly because I don't know what
these things are called, so I don't know where to look (ie it's not just
a query, it's not a function - what is this block referred to as?).

Sorry, that was a little stream of consiousness.  More generally - is
there anything else I should be aware of when trying this approach?

Thanks for the help, if this works it will be quite an elegant solution
to an annoying problem.

Dmitri

---(end of broadcast)---
TIP 4: Don't 'kill -9' the postmaster


[SQL] Easier way to 'strip' on multiple matches?

2005-01-09 Thread Marc G. Fournier
I need to strip out all spaces, and all \' from a string ... is there an 
easier way then doing:

select lower(replace(replace(name, ' ', ''), '\\\'', '')) from business;
Thanks ...

Marc G. Fournier   Hub.Org Networking Services (http://www.hub.org)
Email: [EMAIL PROTECTED]   Yahoo!: yscrappy  ICQ: 7615664
---(end of broadcast)---
TIP 5: Have you checked our extensive FAQ?
  http://www.postgresql.org/docs/faqs/FAQ.html