update dev300-m58
[ooovba.git] / applied_patches / 0346-connectivity-workben-postgresql.diff
blob4ac687774c4bab0d6409031c626484472dbeb91a
1 --- /dev/null 2007-08-27 20:56:43.916000000 +0200
2 +++ connectivity/workben/postgresql/ddl.py 2007-01-07 14:50:38.000000000 +0100
3 @@ -0,0 +1,185 @@
4 +#*************************************************************************
5 +#
6 +# $RCSfile: ddl.py,v $
7 +#
8 +# $Revision: 1.1.2.5 $
9 +#
10 +# last change: $Author: jbu $ $Date: 2007/01/07 13:50:38 $
12 +# The Contents of this file are made available subject to the terms of
13 +# either of the following licenses
15 +# - GNU Lesser General Public License Version 2.1
16 +# - Sun Industry Standards Source License Version 1.1
18 +# Sun Microsystems Inc., October, 2000
20 +# GNU Lesser General Public License Version 2.1
21 +# =============================================
22 +# Copyright 2000 by Sun Microsystems, Inc.
23 +# 901 San Antonio Road, Palo Alto, CA 94303, USA
25 +# This library is free software; you can redistribute it and/or
26 +# modify it under the terms of the GNU Lesser General Public
27 +# License version 2.1, as published by the Free Software Foundation.
29 +# This library is distributed in the hope that it will be useful,
30 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
31 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 +# Lesser General Public License for more details.
34 +# You should have received a copy of the GNU Lesser General Public
35 +# License along with this library; if not, write to the Free Software
36 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
37 +# MA 02111-1307 USA
40 +# Sun Industry Standards Source License Version 1.1
41 +# =================================================
42 +# The contents of this file are subject to the Sun Industry Standards
43 +# Source License Version 1.1 (the "License"); You may not use this file
44 +# except in compliance with the License. You may obtain a copy of the
45 +# License at http://www.openoffice.org/license.html.
47 +# Software provided under this License is provided on an "AS IS" basis,
48 +# WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
49 +# WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
50 +# MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
51 +# See the License for the specific provisions governing your rights and
52 +# obligations concerning the Software.
54 +# The Initial Developer of the Original Code is: Ralph Thomas
56 +# Copyright: 2000 by Sun Microsystems, Inc.
58 +# All Rights Reserved.
60 +# Contributor(s): Ralph Thomas, Joerg Budischewski
62 +#*************************************************************************
63 +from com.sun.star.sdbc import SQLException
64 +import sys
66 +def dumpResultSet( rs ):
67 + meta = rs.getMetaData()
68 + for i in range(1, meta.getColumnCount()+1):
69 + sys.stdout.write(meta.getColumnName( i ) + "\t")
70 + sys.stdout.write( "\n" )
71 + while rs.next():
72 + for i in range( 1, meta.getColumnCount()+1):
73 + sys.stdout.write( rs.getString( i ) + "\t" )
74 + sys.stdout.write( "\n" )
75 + rs.beforeFirst()
79 +def executeIgnoringException( stmt, sql ):
80 + try:
81 + stmt.executeUpdate(sql)
82 + except SQLException:
83 + pass
85 +def cleanGroupsAndUsers( stmt ):
86 + rs = stmt.executeQuery("SELECT groname FROM pg_group WHERE groname LIKE 'pqsdbc_%'" )
87 + stmt2 = stmt.getConnection().createStatement()
88 + while rs.next():
89 + stmt2.executeUpdate("DROP GROUP " + rs.getString(1) )
91 + rs.close()
92 + rs = stmt.executeQuery( "SELECT usename FROM pg_user WHERE usename LIKE 'pqsdbc_%'" )
93 + while rs.next():
94 + stmt2.executeUpdate( "DROP USER " + rs.getString(1) )
99 +def executeDDLs( connection ):
101 + stmt = connection.createStatement()
104 + executeIgnoringException( stmt, "DROP VIEW customer2" )
105 + executeIgnoringException( stmt, "DROP TABLE orderpos" )
106 + executeIgnoringException( stmt, "DROP TABLE ordertab" )
107 + executeIgnoringException( stmt, "DROP TABLE product" )
108 + executeIgnoringException( stmt, "DROP TABLE customer" )
109 + executeIgnoringException( stmt, "DROP TABLE blub" )
110 + executeIgnoringException( stmt, "DROP TABLE foo" )
111 + executeIgnoringException( stmt, "DROP TABLE nooid" )
112 + executeIgnoringException( stmt, "DROP TABLE nooid2" )
113 + cleanGroupsAndUsers( stmt )
114 + executeIgnoringException( stmt, "DROP DOMAIN pqsdbc_short" )
115 + executeIgnoringException( stmt, "DROP DOMAIN pqsdbc_amount" )
116 + executeIgnoringException( stmt, "DROP SCHEMA pqsdbc_test" )
118 + ddls = (
119 + "BEGIN",
120 + "CREATE DOMAIN pqsdbc_short AS int2",
121 + "CREATE DOMAIN pqsdbc_amount AS integer",
122 + "CREATE USER pqsdbc_joe",
123 + "CREATE USER pqsdbc_susy",
124 + "CREATE USER pqsdbc_boss",
125 + "CREATE USER pqsdbc_customer", # technical user (e.g. a webfrontend)
126 + "CREATE GROUP pqsdbc_employees WITH USER pqsdbc_joe,pqsdbc_susy",
127 + "CREATE GROUP pqsdbc_admin WITH USER pqsdbc_susy,pqsdbc_boss",
128 + "CREATE SCHEMA pqsdbc_test",
129 + "CREATE TABLE customer ( "+
130 + "id char(8) UNIQUE PRIMARY KEY, "+
131 + "name text, " +
132 + "dummySerial serial UNIQUE) WITH OIDS",
133 + "COMMENT ON TABLE customer IS 'contains customer attributes'",
134 + "COMMENT ON COLUMN customer.id IS 'unique id'",
135 + "CREATE TABLE product ("+
136 + "id char(8) UNIQUE PRIMARY KEY,"+
137 + "name text,"+
138 + "price numeric(10,2),"+
139 + "image bytea) WITH OIDS",
141 + "CREATE TABLE ordertab ( "+
142 + "id char(8) UNIQUE PRIMARY KEY,"+
143 + "customerid char(8) CONSTRAINT cust REFERENCES customer(id) ON DELETE CASCADE ON UPDATE RESTRICT,"+
144 + "orderdate char(8),"+
145 + "delivered boolean ) WITH OIDS",
146 + "CREATE TABLE orderpos ( "+
147 + "orderid char(8) REFERENCES ordertab(id),"+
148 + "id char(3),"+
149 + "productid char(8) REFERENCES product(id),"+
150 + "amount pqsdbc_amount,"+
151 + "shortamount pqsdbc_short,"+
152 + "PRIMARY KEY (orderid,id)) WITH OIDS",
153 + "CREATE TABLE nooid ("+
154 + "id char(8) UNIQUE PRIMARY KEY,"+
155 + "name text) "+
156 + "WITHOUT OIDS",
157 + "CREATE TABLE nooid2 ("+
158 + "id serial UNIQUE PRIMARY KEY,"+
159 + "name text) "+
160 + "WITHOUT OIDS",
161 + "CREATE VIEW customer2 AS SELECT id,name FROM customer",
162 + "GRANT SELECT ON TABLE customer,product,orderpos,ordertab TO pqsdbc_customer",
163 + "GRANT SELECT ON TABLE product TO GROUP pqsdbc_employees",
164 + "GRANT SELECT,UPDATE, INSERT ON TABLE customer TO GROUP pqsdbc_employees",
165 + "GRANT ALL ON TABLE orderpos,ordertab TO GROUP pqsdbc_employees, GROUP pqsdbc_admin",
166 + "GRANT ALL ON TABLE customer TO GROUP pqsdbc_admin", # the admin is allowed to delete customers
167 + "GRANT ALL ON TABLE product TO pqsdbc_boss", # only the boss may change the product table
168 + "INSERT INTO public.customer VALUES ('C1','John Doe')",
169 + "INSERT INTO \"public\" . \"customer\" VALUES ('C2','Bruce Springsteen')",
171 + "INSERT INTO \"public\".product VALUES ('PZZ2','Pizza Mista',6.95,'\\003foo\\005')",
172 + "INSERT INTO product VALUES ('PZZ5','Pizza Funghi',5.95,'\\001foo\\005')",
173 + "INSERT INTO product VALUES ('PAS1','Lasagne',5.49,NULL)",
175 + "INSERT INTO ordertab VALUES ( '1', 'C2', '20030403','true')",
176 + "INSERT INTO ordertab VALUES ( '2', 'C1', '20030402','false')",
178 + "INSERT INTO orderpos VALUES ( '1','001', 'PZZ2',2,0)",
179 + "INSERT INTO orderpos VALUES ( '1','002', 'PZZ5',3,-1)",
180 + "INSERT INTO orderpos VALUES ( '2','001', 'PAS1',5,1)",
181 + "INSERT INTO orderpos VALUES ( '2','002', 'PZZ2',3,2)",
182 + "COMMIT" )
183 + for i in ddls:
184 + stmt.executeUpdate(i)
186 + connection.getTables() # force refresh of metadata
188 + stmt.close()
189 --- /dev/null 2007-08-27 20:56:43.916000000 +0200
190 +++ connectivity/workben/postgresql/main.py 2004-05-09 22:04:59.000000000 +0200
191 @@ -0,0 +1,90 @@
192 +#*************************************************************************
194 +# $RCSfile: main.py,v $
196 +# $Revision: 1.1.2.2 $
198 +# last change: $Author: jbu $ $Date: 2004/05/09 20:04:59 $
200 +# The Contents of this file are made available subject to the terms of
201 +# either of the following licenses
203 +# - GNU Lesser General Public License Version 2.1
204 +# - Sun Industry Standards Source License Version 1.1
206 +# Sun Microsystems Inc., October, 2000
208 +# GNU Lesser General Public License Version 2.1
209 +# =============================================
210 +# Copyright 2000 by Sun Microsystems, Inc.
211 +# 901 San Antonio Road, Palo Alto, CA 94303, USA
213 +# This library is free software; you can redistribute it and/or
214 +# modify it under the terms of the GNU Lesser General Public
215 +# License version 2.1, as published by the Free Software Foundation.
217 +# This library is distributed in the hope that it will be useful,
218 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
219 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
220 +# Lesser General Public License for more details.
222 +# You should have received a copy of the GNU Lesser General Public
223 +# License along with this library; if not, write to the Free Software
224 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
225 +# MA 02111-1307 USA
228 +# Sun Industry Standards Source License Version 1.1
229 +# =================================================
230 +# The contents of this file are subject to the Sun Industry Standards
231 +# Source License Version 1.1 (the "License"); You may not use this file
232 +# except in compliance with the License. You may obtain a copy of the
233 +# License at http://www.openoffice.org/license.html.
235 +# Software provided under this License is provided on an "AS IS" basis,
236 +# WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
237 +# WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
238 +# MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
239 +# See the License for the specific provisions governing your rights and
240 +# obligations concerning the Software.
242 +# The Initial Developer of the Original Code is: Joerg Budischewski
244 +# Copyright: 2000 by Sun Microsystems, Inc.
246 +# All Rights Reserved.
248 +# Contributor(s): Joerg Budischewski
252 +#*************************************************************************
253 +import uno
254 +import unohelper
255 +import unittest
256 +import statement
257 +import preparedstatement
258 +import metadata
259 +import sdbcx
260 +import sys
261 +import os
263 +ctx = uno.getComponentContext()
265 +# needed for the tests
266 +unohelper.addComponentsToContext(
267 + ctx,ctx,
268 + ("postgresql-sdbc.uno","postgresql-sdbc-impl.uno","typeconverter.uno"),
269 + "com.sun.star.loader.SharedLibrary")
271 +runner = unittest.TextTestRunner(sys.stderr,1,2)
272 +dburl = sys.argv[1] # os.environ['USER'] + "_pqtest"
273 +print "dburl=" + dburl
275 +suite = unittest.TestSuite()
276 +suite.addTest(statement.suite(ctx,dburl))
277 +suite.addTest(preparedstatement.suite(ctx,dburl))
278 +suite.addTest(metadata.suite(ctx,dburl))
279 +suite.addTest(sdbcx.suite(ctx,dburl))
281 +runner.run(suite)
282 --- /dev/null 2007-08-27 20:56:43.916000000 +0200
283 +++ connectivity/workben/postgresql/makefile.mk 2004-05-09 22:04:59.000000000 +0200
284 @@ -0,0 +1,104 @@
285 +#*************************************************************************
287 +# $RCSfile: makefile.mk,v $
289 +# $Revision: 1.1.2.2 $
291 +# last change: $Author: jbu $ $Date: 2004/05/09 20:04:59 $
293 +# The Contents of this file are made available subject to the terms of
294 +# either of the following licenses
296 +# - GNU Lesser General Public License Version 2.1
297 +# - Sun Industry Standards Source License Version 1.1
299 +# Sun Microsystems Inc., October, 2000
301 +# GNU Lesser General Public License Version 2.1
302 +# =============================================
303 +# Copyright 2000 by Sun Microsystems, Inc.
304 +# 901 San Antonio Road, Palo Alto, CA 94303, USA
306 +# This library is free software; you can redistribute it and/or
307 +# modify it under the terms of the GNU Lesser General Public
308 +# License version 2.1, as published by the Free Software Foundation.
310 +# This library is distributed in the hope that it will be useful,
311 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
312 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
313 +# Lesser General Public License for more details.
315 +# You should have received a copy of the GNU Lesser General Public
316 +# License along with this library; if not, write to the Free Software
317 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
318 +# MA 02111-1307 USA
321 +# Sun Industry Standards Source License Version 1.1
322 +# =================================================
323 +# The contents of this file are subject to the Sun Industry Standards
324 +# Source License Version 1.1 (the "License"); You may not use this file
325 +# except in compliance with the License. You may obtain a copy of the
326 +# License at http://www.openoffice.org/license.html.
328 +# Software provided under this License is provided on an "AS IS" basis,
329 +# WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
330 +# WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
331 +# MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
332 +# See the License for the specific provisions governing your rights and
333 +# obligations concerning the Software.
335 +# The Initial Developer of the Original Code is: Sun Microsystems, Inc.
337 +# Copyright: 2000 by Sun Microsystems, Inc.
339 +# All Rights Reserved.
341 +# Contributor(s): _______________________________________
345 +#*************************************************************************
347 +PRJ=..$/..
349 +PRJNAME=connectivity
350 +TARGET=postgresql-test
351 +LIBTARGET=NO
352 +TARGETTYPE=CUI
353 +ENABLE_EXCEPTIONS=TRUE
355 +# --- Settings -----------------------------------------------------
357 +.INCLUDE : settings.mk
358 +.INCLUDE : sv.mk
359 +# --- Files --------------------------------------------------------
362 +PYFILES = \
363 + $(DLLDEST)$/statement.py \
364 + $(DLLDEST)$/preparedstatement.py \
365 + $(DLLDEST)$/main.py \
366 + $(DLLDEST)$/ddl.py \
367 + $(DLLDEST)$/sdbcx.py \
368 + $(DLLDEST)$/metadata.py
370 +ALL : \
371 + $(PYFILES) \
372 + doc
374 +.INCLUDE : target.mk
376 +$(DLLDEST)$/%.py: %.py
377 + +cp $? $@
380 +.PHONY doc:
381 + @echo "start test with dmake runtest dburl=your-url"
382 + @echo " e.g. dmake runtest dburl=sdbc:postgresql:dbname=pqtest"
383 + @echo " MUST: Create a separate datbases before (here pqtest),"
384 + @echo " (SOME TABLES GET DROPPED)"
386 +runtest : ALL
387 + +cd $(DLLDEST) && python main.py "$(dburl)"
389 --- /dev/null 2007-08-27 20:56:43.916000000 +0200
390 +++ connectivity/workben/postgresql/metadata.py 2006-05-27 13:33:11.000000000 +0200
391 @@ -0,0 +1,151 @@
392 +#*************************************************************************
394 +# $RCSfile: metadata.py,v $
396 +# $Revision: 1.1.2.4 $
398 +# last change: $Author: jbu $ $Date: 2006/05/27 11:33:11 $
400 +# The Contents of this file are made available subject to the terms of
401 +# either of the following licenses
403 +# - GNU Lesser General Public License Version 2.1
404 +# - Sun Industry Standards Source License Version 1.1
406 +# Sun Microsystems Inc., October, 2000
408 +# GNU Lesser General Public License Version 2.1
409 +# =============================================
410 +# Copyright 2000 by Sun Microsystems, Inc.
411 +# 901 San Antonio Road, Palo Alto, CA 94303, USA
413 +# This library is free software; you can redistribute it and/or
414 +# modify it under the terms of the GNU Lesser General Public
415 +# License version 2.1, as published by the Free Software Foundation.
417 +# This library is distributed in the hope that it will be useful,
418 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
419 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
420 +# Lesser General Public License for more details.
422 +# You should have received a copy of the GNU Lesser General Public
423 +# License along with this library; if not, write to the Free Software
424 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
425 +# MA 02111-1307 USA
428 +# Sun Industry Standards Source License Version 1.1
429 +# =================================================
430 +# The contents of this file are subject to the Sun Industry Standards
431 +# Source License Version 1.1 (the "License"); You may not use this file
432 +# except in compliance with the License. You may obtain a copy of the
433 +# License at http://www.openoffice.org/license.html.
435 +# Software provided under this License is provided on an "AS IS" basis,
436 +# WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
437 +# WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
438 +# MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
439 +# See the License for the specific provisions governing your rights and
440 +# obligations concerning the Software.
442 +# The Initial Developer of the Original Code is: Ralph Thomas
444 +# Copyright: 2000 by Sun Microsystems, Inc.
446 +# All Rights Reserved.
448 +# Contributor(s): Ralph Thomas, Joerg Budischewski
450 +#*************************************************************************
451 +import unittest
452 +import sys
453 +import ddl
455 +from com.sun.star.sdbc.DataType import SMALLINT, INTEGER, BIGINT , DATE, TIME, TIMESTAMP, NUMERIC
457 +def dumpResultSet( rs , count ):
458 +# for i in range(1, count):
459 +# sys.stdout.write(meta.getColumnName( i ) + "\t")
460 + sys.stdout.write( "\n" )
461 + while rs.next():
462 + for i in range( 1, count+1):
463 + sys.stdout.write( rs.getString( i ) + "\t" )
464 + sys.stdout.write( "\n" )
465 + rs.beforeFirst()
470 +def suite(ctx,dburl):
471 + suite = unittest.TestSuite()
472 + suite.addTest(TestCase("testDatabaseMetaData",ctx,dburl))
473 + suite.addTest(TestCase("testTypeGuess",ctx,dburl))
474 + return suite
476 +class TestCase(unittest.TestCase):
477 + def __init__(self,method,ctx,dburl):
478 + unittest.TestCase.__init__(self,method)
479 + self.ctx = ctx
480 + self.dburl = dburl
483 + def setUp( self ):
484 + self.driver = self.ctx.ServiceManager.createInstanceWithContext(
485 + 'org.openoffice.comp.connectivity.pq.Driver' , self.ctx )
486 + self.connection = self.driver.connect( self.dburl, () )
487 + ddl.executeDDLs( self.connection )
489 + def tearDown( self ):
490 + self.connection.close()
492 + def testDatabaseMetaData( self ):
493 + meta = self.connection.getMetaData()
495 + rs = meta.getTables( None, "public", "%", () )
496 +# dumpResultSet( rs, 5)
498 + rs = meta.getColumns( None, "%", "customer", "%" )
499 +# dumpResultSet( rs, 18 )
501 + rs = meta.getPrimaryKeys( None, "public" , "%" )
502 +# dumpResultSet( rs , 6 )
503 + rs = meta.getTablePrivileges( None, "public" , "%" )
504 +# dumpResultSet( rs , 7 )
505 + rs = meta.getColumns( None, "public" , "customer", "%" )
506 +# dumpResultSet( rs , 18 )
507 + rs = meta.getTypeInfo()
508 +# dumpResultSet(rs, 18)
509 + while rs.next():
510 + if rs.getString(1) == "pqsdbc_short":
511 + self.failUnless( rs.getInt(2) == SMALLINT )
512 + break
513 + self.failUnless( not rs.isAfterLast() ) # domain type cannot be found
516 + rs = meta.getIndexInfo( None, "public" , "customer", False, False )
517 +# dumpResultSet( rs, 13 )
519 + def testTypeGuess( self ):
520 + stmt = self.connection.createStatement()
521 + rs = stmt.executeQuery( "SELECT sum(amount) FROM orderpos" )
522 + meta = rs.getMetaData()
523 + self.failUnless( meta.getColumnType(1) == BIGINT )
525 + stmt = self.connection.createStatement()
526 + rs = stmt.executeQuery( "SELECT sum(price) FROM product" )
527 + meta = rs.getMetaData()
528 + self.failUnless( meta.getColumnType(1) == NUMERIC )
530 + rs = stmt.executeQuery( "SELECT max(ttime) FROM firsttable" )
531 + meta = rs.getMetaData()
532 + self.failUnless( meta.getColumnType(1) == TIME )
534 + rs = stmt.executeQuery( "SELECT max(tdate) FROM firsttable" )
535 + meta = rs.getMetaData()
536 + self.failUnless( meta.getColumnType(1) == DATE )
538 + rs = stmt.executeQuery( "SELECT max(ttimestamp) FROM firsttable" )
539 + meta = rs.getMetaData()
540 + self.failUnless( meta.getColumnType(1) == TIMESTAMP )
541 +# rs.next()
542 +# print rs.getString( 1 )
543 --- /dev/null 2008-11-25 09:24:02.506388553 +0100
544 +++ connectivity/workben/postgresql/preparedstatement.py 2008-07-07 23:37:11.000000000 +0200
545 @@ -0,0 +1,230 @@
546 +#*************************************************************************
548 +# $RCSfile: preparedstatement.py,v $
550 +# $Revision: 1.1.2.9 $
552 +# last change: $Author: jbu $ $Date: 2008/07/07 21:37:11 $
554 +# The Contents of this file are made available subject to the terms of
555 +# either of the following licenses
557 +# - GNU Lesser General Public License Version 2.1
558 +# - Sun Industry Standards Source License Version 1.1
560 +# Sun Microsystems Inc., October, 2000
562 +# GNU Lesser General Public License Version 2.1
563 +# =============================================
564 +# Copyright 2000 by Sun Microsystems, Inc.
565 +# 901 San Antonio Road, Palo Alto, CA 94303, USA
567 +# This library is free software; you can redistribute it and/or
568 +# modify it under the terms of the GNU Lesser General Public
569 +# License version 2.1, as published by the Free Software Foundation.
571 +# This library is distributed in the hope that it will be useful,
572 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
573 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
574 +# Lesser General Public License for more details.
576 +# You should have received a copy of the GNU Lesser General Public
577 +# License along with this library; if not, write to the Free Software
578 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
579 +# MA 02111-1307 USA
582 +# Sun Industry Standards Source License Version 1.1
583 +# =================================================
584 +# The contents of this file are subject to the Sun Industry Standards
585 +# Source License Version 1.1 (the "License"); You may not use this file
586 +# except in compliance with the License. You may obtain a copy of the
587 +# License at http://www.openoffice.org/license.html.
589 +# Software provided under this License is provided on an "AS IS" basis,
590 +# WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
591 +# WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
592 +# MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
593 +# See the License for the specific provisions governing your rights and
594 +# obligations concerning the Software.
596 +# The Initial Developer of the Original Code is: Ralph Thomas
598 +# Copyright: 2000 by Sun Microsystems, Inc.
600 +# All Rights Reserved.
602 +# Contributor(s): Ralph Thomas, Joerg Budischewski
604 +#*************************************************************************
605 +import unittest
606 +import sys
607 +import ddl
608 +from uno import ByteSequence
609 +from com.sun.star.sdbc import SQLException
610 +from com.sun.star.sdbc.ResultSetConcurrency import UPDATABLE
611 +from com.sun.star.sdbc.DataType import NUMERIC,VARCHAR
613 +def suite(ctx,dburl):
614 + suite = unittest.TestSuite()
615 + suite.addTest(TestCase("testQuery",ctx,dburl))
616 + suite.addTest(TestCase("testGeneratedResultSet",ctx,dburl))
617 + suite.addTest(TestCase("testUpdateableResultSet",ctx,dburl))
618 + suite.addTest(TestCase("testQuoteQuote",ctx,dburl))
619 + return suite
621 +def realEquals( a,b,eps ):
622 + val = a - b
623 + if val < 0:
624 + val = -1. * val
625 + return val < eps
627 +class TestCase(unittest.TestCase):
628 + def __init__(self,method,ctx,dburl):
629 + unittest.TestCase.__init__(self,method)
630 + self.ctx = ctx
631 + self.dburl = dburl
633 + def setUp(self):
634 + self.driver = self.ctx.ServiceManager.createInstanceWithContext(
635 + 'org.openoffice.comp.connectivity.pq.Driver', self.ctx )
636 + self.connection = self.driver.connect( self.dburl, () )
637 + ddl.executeDDLs( self.connection )
639 + def testDown( self ):
640 + self.connection.close()
642 + def testQuery( self ):
644 + stmts = "SELECT product.id FROM product WHERE product.price > :lowprice AND product.price < :upprice", \
645 + "SELECT product.id FROM product WHERE product.price > ? AND product.price < ?" , \
646 + "SELECT \"product\".\"id\" FROM product WHERE \"product\".\"price\" > :lowprice AND \"product\".\"price\" < :upprice"
649 + for stmt in stmts:
650 + prepstmt = self.connection.prepareStatement( stmt )
651 + prepstmt.setDouble( 1, 5.80 )
652 + prepstmt.setObjectWithInfo( 2, 7. , NUMERIC, 2)
653 + prepstmt.setObjectWithInfo( 2, "7.0000", NUMERIC, 2 )
654 + rs = prepstmt.executeQuery( )
655 + self.failUnless( rs.getMetaData().getColumnCount() == 1 )
656 + self.failUnless( rs.getMetaData().getColumnName(1) == "id")
657 + self.failUnless( prepstmt.getMetaData().getColumnCount() == 1 )
658 + self.failUnless( prepstmt.getMetaData().getColumnName(1) == "id" )
659 + self.failUnless( rs.next() )
660 + self.failUnless( rs.getString( 1 ).strip() == "PZZ2" )
661 + self.failUnless( rs.next() )
662 + self.failUnless( rs.getString( 1 ).strip() == "PZZ5" )
663 + self.failUnless( rs.isLast() )
665 + prepstmt = self.connection.prepareStatement(
666 + "SELECT name FROM product WHERE id = ?" )
667 + prepstmt.setString( 1, 'PZZ2' )
668 + rs = prepstmt.executeQuery()
669 + self.failUnless( rs.next() )
670 + self.failUnless( rs.getString( 1 ) == "Pizza Mista" )
671 + self.failUnless( rs.isLast() )
673 + prepstmt = self.connection.prepareStatement(
674 + "SELECT name FROM product WHERE image = ?" )
675 + prepstmt.setBytes( 1, ByteSequence( "\001foo\005" ) )
676 + rs = prepstmt.executeQuery()
677 + self.failUnless( rs.next() )
678 + self.failUnless( rs.getString( 1 ) == "Pizza Funghi" )
679 + self.failUnless( rs.isLast() )
681 + prepstmt = self.connection.prepareStatement(
682 + "SELECT * FROM ordertab WHERE delivered = ?" )
683 + prepstmt.setBoolean( 1 , False )
684 + rs = prepstmt.executeQuery()
685 + self.failUnless( rs.next() )
686 + self.failUnless( rs.getString( 1 ).strip() == "2" )
687 + self.failUnless( rs.isLast() )
689 + stmt = self.connection.createStatement()
690 + rs = stmt.executeQuery( "SELECT * FROM \"public\".\"customer\"" )
692 + stmt.executeUpdate( "DELETE FROM product where id='PAS5'" )
693 + prepstmt =self.connection.prepareStatement(
694 + "INSERT INTO product VALUES(?,'Ravioli',?,NULL)" );
695 + prepstmt.setObjectWithInfo( 1, "PAS5" ,VARCHAR,0)
696 + prepstmt.setObjectWithInfo( 2, "9.223" ,NUMERIC,2)
697 + prepstmt.executeUpdate()
698 + rs= stmt.executeQuery( "SELECT price FROM product WHERE id = 'PAS5'" )
699 + self.failUnless( rs.next() )
700 + self.failUnless( rs.getString( 1 ).strip() == "9.22" )
702 + stmt.executeUpdate( "DELETE FROM product where id='PAS5'" )
703 + prepstmt =self.connection.prepareStatement(
704 + "INSERT INTO product VALUES('PAS5','Ravioli',?,NULL)" );
705 + prepstmt.setObjectWithInfo( 1, 9.223,NUMERIC,2 )
706 + prepstmt.executeUpdate()
707 + rs= stmt.executeQuery( "SELECT price FROM product WHERE id = 'PAS5'" )
708 + self.failUnless( rs.next() )
709 + self.failUnless( rs.getString( 1 ).strip() == "9.22" )
711 + def testGeneratedResultSet( self ):
712 + prepstmt = self.connection.prepareStatement(
713 + "INSERT INTO customer VALUES( ?, ? )" )
714 + prepstmt.setString( 1, "C3" )
715 + prepstmt.setString( 2, "Norah Jones" )
716 + prepstmt.executeUpdate()
717 + rs = prepstmt.getGeneratedValues()
718 + self.failUnless( rs.next() )
719 + self.failUnless( rs.getInt( 3 ) == 3 )
721 + prepstmt = self.connection.prepareStatement(
722 + "INSERT INTO public.nooid (id,name) VALUES( ?, ? )" )
723 + prepstmt.setString( 1, "C3" )
724 + prepstmt.setString( 2, "Norah Jones" )
725 + prepstmt.executeUpdate()
726 + rs = prepstmt.getGeneratedValues()
727 + self.failUnless( rs.next() )
728 + self.failUnless( rs.getString(1).rstrip() == "C3" )
730 + prepstmt = self.connection.prepareStatement(
731 + "INSERT INTO public.nooid2 (name) VALUES( ? )" )
732 + prepstmt.setString( 1, "Norah Jones" )
733 + prepstmt.executeUpdate()
734 + rs = prepstmt.getGeneratedValues()
735 + self.failUnless( rs )
736 + self.failUnless( rs.next() )
737 + self.failUnless( rs.getString(2) == "Norah Jones" )
738 + self.failUnless( rs.getString(1) == "1" )
740 + def testUpdateableResultSet( self ):
741 + stmt = self.connection.createStatement()
742 + stmt.ResultSetConcurrency = UPDATABLE
743 + rs = stmt.executeQuery( "SELECT * FROM orderpos" )
744 +# ddl.dumpResultSet( rs )
745 + rs.next()
746 + rs.deleteRow()
747 + rs.next()
748 + rs.updateInt( 4 , 32 )
749 + rs.updateRow()
751 + rs.moveToInsertRow()
752 + rs.updateString( 1 , '2' )
753 + rs.updateString( 2, '003' )
754 + rs.updateString( 3, 'PZZ5' )
755 + rs.updateInt( 4, 22 )
756 + rs.insertRow()
758 + rs = stmt.executeQuery( "SELECT * FROM orderpos" )
759 + rs = stmt.executeQuery( "SELECT * FROM \"public\".\"orderpos\"" )
760 +# ddl.dumpResultSet( rs )
762 + def testQuoteQuote( self ):
763 + stmt = self.connection.prepareStatement( "select 'foo''l'" )
764 + rs = stmt.executeQuery()
765 + self.failUnless( rs )
766 + self.failUnless( rs.next() )
767 + self.failUnless( rs.getString(1) == "foo'l" )
769 + stmt = self.connection.prepareStatement( "select 'foo''''l'" )
770 + rs = stmt.executeQuery()
771 + self.failUnless( rs )
772 + self.failUnless( rs.next() )
773 + self.failUnless( rs.getString(1) == "foo''l" )
776 --- /dev/null 2007-08-27 20:56:43.916000000 +0200
777 +++ connectivity/workben/postgresql/sdbcx.py 2007-01-07 14:50:38.000000000 +0100
778 @@ -0,0 +1,306 @@
779 +#*************************************************************************
781 +# $RCSfile: sdbcx.py,v $
783 +# $Revision: 1.1.2.6 $
785 +# last change: $Author: jbu $ $Date: 2007/01/07 13:50:38 $
787 +# The Contents of this file are made available subject to the terms of
788 +# either of the following licenses
790 +# - GNU Lesser General Public License Version 2.1
791 +# - Sun Industry Standards Source License Version 1.1
793 +# Sun Microsystems Inc., October, 2000
795 +# GNU Lesser General Public License Version 2.1
796 +# =============================================
797 +# Copyright 2000 by Sun Microsystems, Inc.
798 +# 901 San Antonio Road, Palo Alto, CA 94303, USA
800 +# This library is free software; you can redistribute it and/or
801 +# modify it under the terms of the GNU Lesser General Public
802 +# License version 2.1, as published by the Free Software Foundation.
804 +# This library is distributed in the hope that it will be useful,
805 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
806 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
807 +# Lesser General Public License for more details.
809 +# You should have received a copy of the GNU Lesser General Public
810 +# License along with this library; if not, write to the Free Software
811 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
812 +# MA 02111-1307 USA
815 +# Sun Industry Standards Source License Version 1.1
816 +# =================================================
817 +# The contents of this file are subject to the Sun Industry Standards
818 +# Source License Version 1.1 (the "License"); You may not use this file
819 +# except in compliance with the License. You may obtain a copy of the
820 +# License at http://www.openoffice.org/license.html.
822 +# Software provided under this License is provided on an "AS IS" basis,
823 +# WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
824 +# WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
825 +# MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
826 +# See the License for the specific provisions governing your rights and
827 +# obligations concerning the Software.
829 +# The Initial Developer of the Original Code is: Joerg Budischewski
831 +# Copyright: 2000 by Sun Microsystems, Inc.
833 +# All Rights Reserved.
835 +# Contributor(s): Joerg Budischewski
839 +#*************************************************************************
840 +import unittest
841 +import ddl
842 +import unohelper
843 +import sys
844 +from com.sun.star.sdbc import SQLException
845 +from com.sun.star.sdbc.DataType import VARCHAR, CHAR, DECIMAL, DOUBLE, BIGINT, NUMERIC
846 +from com.sun.star.sdbc.ColumnValue import NO_NULLS, NULLABLE
847 +from com.sun.star.sdbcx.KeyType import PRIMARY, FOREIGN, UNIQUE
848 +from com.sun.star.sdbc.KeyRule import RESTRICT, CASCADE, NO_ACTION
850 +def suite(ctx,dburl):
851 + suite = unittest.TestSuite()
852 + suite.addTest(TestCase("testTables",ctx,dburl))
853 + suite.addTest(TestCase("testViews",ctx,dburl))
854 + suite.addTest(TestCase("testKeys",ctx,dburl))
855 + suite.addTest(TestCase("testUsers",ctx,dburl))
856 + suite.addTest(TestCase("testIndexes",ctx,dburl))
857 + return suite
859 +def nullable2Str( v ):
860 + if v == NO_NULLS:
861 + return "NOT NULL"
862 + return ""
864 +def autoIncremtent2Str( v ):
865 + if v:
866 + return "auto increment"
867 + return ""
869 +def dumpColumns( columns ):
870 + n = columns.getCount()
871 + print "Name\t type\t prec\t scale\t"
872 + for i in range( 0, n ):
873 + col = columns.getByIndex( i )
874 + print col.Name + "\t "+col.TypeName + "\t " + str(col.Precision) + "\t " + str(col.Scale) + "\t "+\
875 + str( col.DefaultValue ) + "\t " + str( col.Description ) + "\t " +\
876 + autoIncremtent2Str( col.IsAutoIncrement ) + "\t " + \
877 + nullable2Str( col.IsNullable )
880 +class TestCase(unittest.TestCase):
881 + def __init__(self,method,ctx,dburl):
882 + unittest.TestCase.__init__(self,method)
883 + self.ctx = ctx
884 + self.dburl = dburl
886 + def setUp( self ):
887 + self.driver = self.ctx.ServiceManager.createInstanceWithContext(
888 + 'org.openoffice.comp.connectivity.pq.Driver' , self.ctx )
889 + self.connection = self.driver.connect( self.dburl, () )
890 + ddl.executeDDLs( self.connection )
892 + def tearDown( self ):
893 + self.connection.close()
895 + def checkDescriptor( self, descriptor, name, typeName, type, prec, scale, defaultValue, desc ):
896 + self.failUnless( descriptor.Name == name )
897 + self.failUnless( descriptor.TypeName == typeName )
898 + self.failUnless( descriptor.Type == type )
899 + self.failUnless( descriptor.Precision == prec )
900 + self.failUnless( descriptor.Scale == scale )
901 +# print descriptor.DefaultValue + " == " + defaultValue
902 +# self.failUnless( descriptor.DefaultValue == defaultValue )
903 + self.failUnless( descriptor.Description == desc )
906 + def testKeys( self ):
907 + dd = self.driver.getDataDefinitionByConnection( self.connection )
908 + tables = dd.getTables()
909 + t = tables.getByName( "public.ordertab" )
910 + keys = t.getKeys()
911 + key = keys.getByName( "cust" )
912 + self.failUnless( key.Name == "cust" )
913 + self.failUnless( key.Type == FOREIGN )
914 + self.failUnless( key.ReferencedTable == "public.customer" )
915 + self.failUnless( key.UpdateRule == RESTRICT )
916 + self.failUnless( key.DeleteRule == CASCADE )
918 + keycolumns = keys.getByName( "ordertab_pkey" ).getColumns()
919 + self.failUnless( keycolumns.getElementNames() == (u"id",) )
921 + key = keys.getByName( "ordertab_pkey" )
922 + self.failUnless( key.Name == "ordertab_pkey" )
923 + self.failUnless( key.Type == PRIMARY )
924 + self.failUnless( key.UpdateRule == NO_ACTION )
925 + self.failUnless( key.DeleteRule == NO_ACTION )
927 + keys = tables.getByName( "public.customer" ).getKeys()
928 + key = keys.getByName( "customer_dummyserial_key" )
929 + self.failUnless( key.Name == "customer_dummyserial_key" )
930 + self.failUnless( key.Type == UNIQUE )
931 + self.failUnless( key.UpdateRule == NO_ACTION )
932 + self.failUnless( key.DeleteRule == NO_ACTION )
934 + keys = tables.getByName( "public.orderpos" ).getKeys()
935 + keyEnum = keys.createEnumeration()
936 + while keyEnum.hasMoreElements():
937 + key = keyEnum.nextElement()
938 + cols = key.getColumns()
939 + colEnum = cols.createEnumeration()
940 + while colEnum.hasMoreElements():
941 + col = colEnum.nextElement()
943 + def testViews( self ):
944 + dd = self.driver.getDataDefinitionByConnection( self.connection )
945 + views = dd.getViews()
947 + v = views.getByName( "public.customer2" )
948 + self.failUnless( v.Name == "customer2" )
949 + self.failUnless( v.SchemaName == "public" )
950 + self.failUnless( v.Command != "" )
952 + def testIndexes( self ):
953 + dd = self.driver.getDataDefinitionByConnection( self.connection )
954 + tables = dd.getTables()
955 + t = tables.getByName( "public.ordertab" )
956 + indexes = t.getIndexes()
957 + index = indexes.getByName( "ordertab_pkey" )
959 + self.failUnless( index.Name == "ordertab_pkey" )
960 + self.failUnless( index.IsPrimaryKeyIndex )
961 + self.failUnless( index.IsUnique )
962 + self.failUnless( not index.IsClustered )
964 + columns = index.getColumns()
965 + self.failUnless( columns.hasByName( "id" ) )
967 + self.failUnless( columns.getByIndex(0).Name == "id" )
969 + def checkRenameTable( self, t , tables):
970 + t.rename( "foo" )
971 + self.failUnless( tables.hasByName( "public.foo" ) )
973 + t.rename( "public.foo2" )
974 + self.failUnless( tables.hasByName( "public.foo2" ) )
976 + try:
977 + t.rename( "pqsdbc_test.foo2" )
978 + self.failUnless( tables.hasByName( "pqsdbc_test.foo2" ) )
979 + print "looks like a server 8.1 or later (changing a schema succeeded)"
980 + t.rename( "pqsdbc_test.foo" )
981 + self.failUnless( tables.hasByName( "pqsdbc_test.foo" ) )
982 + t.rename( "public.foo2" )
983 + self.failUnless( tables.hasByName( "public.foo2" ) )
984 + except SQLException,e:
985 + if e.Message.find( "support changing" ) >= 0:
986 + print "looks like a server prior to 8.1 (changing schema failed with Message [" + e.Message.replace("\n", " ") + "])"
987 + else:
988 + raise e
989 + tables.dropByName( "public.foo2" )
991 + def testTables( self ):
992 + dd = self.driver.getDataDefinitionByConnection( self.connection )
993 + tables = dd.getTables()
994 + t = tables.getByName( "public.customer" )
995 + self.failUnless( t.Name == "customer" )
996 + self.failUnless( t.SchemaName == "public" )
997 + self.failUnless( t.Type == "TABLE" )
999 + cols = t.getColumns()
1000 + self.failUnless( cols.hasByName( 'name' ) )
1001 + self.failUnless( cols.hasByName( 'id' ) )
1002 + col = cols.getByName( "dummyserial" )
1003 +# dumpColumns( cols )
1004 + self.checkDescriptor( cols.getByName( "id" ), "id", "bpchar", CHAR, 8, 0, "", "unique id" )
1005 + self.checkDescriptor( cols.getByName( "name" ), "name", "text", VARCHAR, 0, 0, "", "" )
1007 + dd = cols.createDataDescriptor()
1008 + dd.Name = "foo"
1009 + dd.TypeName = "CHAR"
1010 + dd.Type = CHAR
1011 + dd.Precision = 25
1012 + dd.IsNullable = NULLABLE
1013 + cols.appendByDescriptor( dd )
1015 + dd.Name = "foo2"
1016 + dd.TypeName = "DECIMAL"
1017 + dd.Type = DECIMAL
1018 + dd.Precision = 12
1019 + dd.Scale = 5
1020 + dd.DefaultValue = "2.3423"
1021 + dd.Description = "foo2 description"
1022 + cols.appendByDescriptor( dd )
1024 + dd.Name = "cash"
1025 + dd.TypeName = "MONEY"
1026 + dd.Type = DOUBLE
1027 +# dd.IsNullable = NO_NULLS
1028 + dd.DefaultValue = "'2.42'"
1029 + cols.appendByDescriptor( dd )
1031 + cols.refresh()
1033 + self.checkDescriptor( cols.getByName( "foo"), "foo", "bpchar", CHAR, 25,0,"","")
1034 + self.checkDescriptor(
1035 + cols.getByName( "foo2"), "foo2", "numeric", NUMERIC, 12,5,"2.3423","foo2 description")
1036 +# dumpColumns( cols )
1038 + datadesc = tables.createDataDescriptor()
1039 + datadesc.SchemaName = "public"
1040 + datadesc.Name = "blub"
1041 + datadesc.Description = "This describes blub"
1043 + tables.appendByDescriptor( datadesc )
1045 + # make the appended descriptors known
1046 + tables.refresh()
1048 + t = tables.getByName( "public.blub" )
1049 + self.failUnless( t.Name == "blub" )
1050 + self.failUnless( t.SchemaName == "public" )
1051 + self.failUnless( t.Description == "This describes blub" )
1053 + cols = t.getColumns()
1054 + dd = cols.createDataDescriptor()
1055 + dd.Name = "mytext"
1056 + dd.TypeName = "text"
1057 + dd.Type = VARCHAR
1058 + dd.IsNullable = NO_NULLS
1059 + cols.appendByDescriptor( dd )
1061 + cols.refresh()
1063 + dd.DefaultValue = "'myDefault'"
1064 + dd.Name = "mytext2"
1065 + dd.IsNullable = NULLABLE
1066 + dd.Description = "mytext-Description"
1067 + t.alterColumnByName( "mytext" , dd )
1069 + cols.refresh()
1071 + self.checkDescriptor( cols.getByName( "mytext2" ), "mytext2", "text", VARCHAR, 0,0,"'myDefault'","mytext-Description" )
1073 + t = tables.getByName( "public.customer2" )
1074 + self.checkRenameTable( t,tables )
1076 + t = tables.getByName( "public.blub" )
1077 + self.checkRenameTable( t,tables )
1081 + def testUsers( self ):
1082 + dd = self.driver.getDataDefinitionByConnection( self.connection )
1083 + users = dd.getUsers()
1084 + self.failUnless( "pqsdbc_joe" in users.getElementNames() )
1085 --- /dev/null 2007-08-27 20:56:43.916000000 +0200
1086 +++ connectivity/workben/postgresql/statement.py 2006-05-27 13:33:11.000000000 +0200
1087 @@ -0,0 +1,279 @@
1088 +#*************************************************************************
1090 +# $RCSfile: statement.py,v $
1092 +# $Revision: 1.1.2.5 $
1094 +# last change: $Author: jbu $ $Date: 2006/05/27 11:33:11 $
1096 +# The Contents of this file are made available subject to the terms of
1097 +# either of the following licenses
1099 +# - GNU Lesser General Public License Version 2.1
1100 +# - Sun Industry Standards Source License Version 1.1
1102 +# Sun Microsystems Inc., October, 2000
1104 +# GNU Lesser General Public License Version 2.1
1105 +# =============================================
1106 +# Copyright 2000 by Sun Microsystems, Inc.
1107 +# 901 San Antonio Road, Palo Alto, CA 94303, USA
1109 +# This library is free software; you can redistribute it and/or
1110 +# modify it under the terms of the GNU Lesser General Public
1111 +# License version 2.1, as published by the Free Software Foundation.
1113 +# This library is distributed in the hope that it will be useful,
1114 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1115 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1116 +# Lesser General Public License for more details.
1118 +# You should have received a copy of the GNU Lesser General Public
1119 +# License along with this library; if not, write to the Free Software
1120 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
1121 +# MA 02111-1307 USA
1124 +# Sun Industry Standards Source License Version 1.1
1125 +# =================================================
1126 +# The contents of this file are subject to the Sun Industry Standards
1127 +# Source License Version 1.1 (the "License"); You may not use this file
1128 +# except in compliance with the License. You may obtain a copy of the
1129 +# License at http://www.openoffice.org/license.html.
1131 +# Software provided under this License is provided on an "AS IS" basis,
1132 +# WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
1133 +# WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
1134 +# MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
1135 +# See the License for the specific provisions governing your rights and
1136 +# obligations concerning the Software.
1138 +# The Initial Developer of the Original Code is: Joerg Budischewski
1140 +# Copyright: 2000 by Sun Microsystems, Inc.
1142 +# All Rights Reserved.
1144 +# Contributor(s): Joerg Budischewski
1148 +#*************************************************************************
1149 +import unohelper
1150 +import unittest
1151 +import ddl
1152 +from com.sun.star.sdbc import SQLException, XArray
1153 +from com.sun.star.sdbc.DataType import VARCHAR
1154 +from com.sun.star.util import Date
1155 +from com.sun.star.util import Time
1156 +from com.sun.star.util import DateTime
1158 +# todo
1159 +class MyArray( unohelper.Base, XArray ):
1160 + def __init__( self, data ):
1161 + self.data = data
1162 + def getBaseType( self ):
1163 + return VARCHAR
1164 + def getBaseTypeName( self ):
1165 + return "varchar"
1166 + def getArray( self, foo ):
1167 + return self.data
1168 + def getArrayAtIndex( self, startIndex, count, foo ):
1169 + return self.data[startIndex:startIndex+count-1]
1170 + def getResultSet( self, foo):
1171 + return None
1172 + def getResultSetAtIndex( self, startIndex, count, foo ):
1173 + return None
1175 +def suite(ctx,dburl):
1176 + suite = unittest.TestSuite()
1177 + suite.addTest(TestCase("testRobustness",ctx,dburl))
1178 + suite.addTest(TestCase("testRow",ctx,dburl))
1179 + suite.addTest(TestCase("testNavigation",ctx,dburl))
1180 + suite.addTest(TestCase("testDatabaseMetaData",ctx,dburl))
1181 + suite.addTest(TestCase("testGeneratedResultSet",ctx,dburl))
1182 + suite.addTest(TestCase("testResultSetMetaData",ctx,dburl))
1183 + suite.addTest(TestCase("testArray",ctx,dburl))
1184 + return suite
1186 +def realEquals( a,b,eps ):
1187 + val = a - b
1188 + if val < 0:
1189 + val = -1. * val
1190 + return val < eps
1192 +class TestCase(unittest.TestCase):
1193 + def __init__(self,method,ctx,dburl):
1194 + unittest.TestCase.__init__(self,method)
1195 + self.ctx = ctx
1196 + self.dburl = dburl
1198 + def setUp(self):
1199 + self.driver = self.ctx.ServiceManager.createInstanceWithContext(
1200 + 'org.openoffice.comp.connectivity.pq.Driver' , self.ctx )
1201 + self.connection = self.driver.connect( self.dburl, () )
1202 + self.stmt = self.connection.createStatement()
1203 + try:
1204 + self.stmt.executeUpdate( "DROP TABLE firsttable" )
1205 + except SQLException,e:
1206 + pass
1208 + ddls = (
1209 + "BEGIN",
1210 + "CREATE TABLE firsttable (tString text,tInteger integer,tShort smallint,tLong bigint,tFloat real,"+
1211 + "tDouble double precision,tByteSeq bytea,tBool boolean, tDate date, tTime time, tTimestamp timestamp, tIntArray integer[], tStringArray text[], tSerial serial ) WITH OIDS",
1212 + "INSERT INTO firsttable VALUES ( 'foo', 70000, 12000, 70001, 2.4, 2.45, 'huhu', 'true', '1999-01-08','04:05:06','1999-01-08 04:05:06', '{2,3,4}', '{\"huhu\",\"hi\"}')",
1213 + "INSERT INTO firsttable VALUES ( 'foo2', 69999, 12001, 70002, -2.4, 2.55, 'huhu', 'false', '1999-01-08','04:05:06','1999-01-08 04:05:06', NULL , '{\"bla\"}' )",
1214 + "INSERT INTO firsttable VALUES ( 'foo2', 69999, 12001, 70002, -2.4, 2.55, 'huhu', null, '1999-01-08', '04:05:06','1999-01-08 04:05:06', '{}' , '{\"bl ubs\",\"bl\\\\\\\\a}}b\\\\\"a\",\"blub\"}' )",
1215 + "COMMIT" )
1216 + for i in ddls:
1217 + self.stmt.executeUpdate(i)
1219 + def tearDown(self):
1220 + self.stmt.close()
1221 + self.connection.close()
1223 + def testRow(self):
1224 + row = ("foo",70000,12000,70001,2.4,2.45, "huhu", True ,
1225 + Date(8,1,1999), Time(0,6,5,4),DateTime(0,6,5,4,8,1,1999) )
1226 + row2 = ("foo2",69999,12001,70002,-2.4,2.55, "huhu", False )
1228 + rs = self.stmt.executeQuery( "SELECT * from firsttable" )
1229 + self.failUnless( rs.next() )
1231 + self.failUnless( rs.getString(1) == row[0] )
1232 + self.failUnless( rs.getInt(2) == row[1] )
1233 + self.failUnless( rs.getShort(3) == row[2] )
1234 + self.failUnless( rs.getLong(4) == row[3] )
1235 + self.failUnless( realEquals(rs.getFloat(5), row[4], 0.001))
1236 + self.failUnless( realEquals(rs.getDouble(6), row[5], 0.00001))
1237 + self.failUnless( rs.getBytes(7) == row[6] )
1238 + self.failUnless( rs.getBoolean(8) == row[7] )
1239 + self.failUnless( rs.getDate(9) == row[8] )
1240 + self.failUnless( rs.getTime(10) == row[9] )
1241 + self.failUnless( rs.getTimestamp(11) == row[10] )
1243 + a = rs.getArray(12)
1244 + data = a.getArray( None )
1245 + self.failUnless( len( data ) == 3 )
1246 + self.failUnless( int(data[0] ) == 2 )
1247 + self.failUnless( int(data[1] ) == 3 )
1248 + self.failUnless( int(data[2] ) == 4 )
1250 + self.failUnless( rs.next() )
1252 + self.failUnless( rs.next() )
1253 + data = rs.getArray(13).getArray(None)
1254 + self.failUnless( data[0] == "bl ubs" )
1255 + self.failUnless( data[1] == "bl\\a}}b\"a" ) # check special keys
1256 + self.failUnless( data[2] == "blub" )
1258 + rs.getString(8)
1259 + self.failUnless( rs.wasNull() )
1260 + rs.getString(7)
1261 + self.failUnless( not rs.wasNull() )
1263 + self.failUnless( rs.findColumn( "tShort" ) == 3 )
1264 + rs.close()
1266 + def testNavigation( self ):
1267 + rs = self.stmt.executeQuery( "SELECT * from firsttable" )
1268 + self.failUnless( rs.isBeforeFirst() )
1269 + self.failUnless( not rs.isAfterLast() )
1270 + self.failUnless( rs.isBeforeFirst() )
1272 + self.failUnless( rs.next() )
1273 + self.failUnless( rs.isFirst() )
1274 + self.failUnless( not rs.isLast() )
1275 + self.failUnless( not rs.isBeforeFirst() )
1277 + self.failUnless( rs.next() )
1278 + self.failUnless( rs.next() )
1279 + self.failUnless( not rs.next() )
1280 + self.failUnless( rs.isAfterLast() )
1282 + rs.absolute( 1 )
1283 + self.failUnless( rs.isFirst() )
1285 + rs.absolute( 3 )
1286 + self.failUnless( rs.isLast() )
1288 + rs.relative( -1 )
1289 + self.failUnless( rs.getRow() == 2 )
1291 + rs.relative( 1 )
1292 + self.failUnless( rs.getRow() == 3 )
1294 + rs.close()
1296 + def testRobustness( self ):
1297 + rs = self.stmt.executeQuery( "SELECT * from firsttable" )
1299 + self.failUnlessRaises( SQLException, rs.getString , 1 )
1301 + rs.next()
1302 + self.failUnlessRaises( SQLException, rs.getString , 24 )
1303 + self.failUnlessRaises( SQLException, rs.getString , 0 )
1305 + self.connection.close()
1306 + self.failUnlessRaises( SQLException, rs.getString , 1 )
1307 + self.failUnlessRaises( SQLException, self.stmt.executeQuery, "SELECT * from firsttable" )
1308 + rs.close()
1311 + def testDatabaseMetaData( self ):
1312 + meta = self.connection.getMetaData()
1314 + self.failUnless( not meta.isReadOnly() )
1316 + def testGeneratedResultSet( self ):
1317 + self.stmt.executeUpdate(
1318 + "INSERT INTO firsttable VALUES ( 'foo3', 69998, 12001, 70002, -2.4, 2.55, 'huhu2')" )
1319 + #ddl.dumpResultSet( self.stmt.getGeneratedValues() )
1320 + rs = self.stmt.getGeneratedValues()
1321 + self.failUnless( rs.next() )
1322 + self.failUnless( rs.getInt( 14 ) == 4 )
1324 + def testResultSetMetaData( self ):
1325 + rs = self.stmt.executeQuery( "SELECT * from firsttable" )
1327 + # just check, if we get results !
1328 + meta = rs.getMetaData()
1330 + count = meta.getColumnCount()
1331 + for i in range( 1, count+1):
1332 + meta.isNullable( i )
1333 + meta.isCurrency( i )
1334 + meta.isCaseSensitive( i )
1335 + meta.isSearchable( i )
1336 + meta.isSigned( i )
1337 + meta.getColumnDisplaySize( i )
1338 + meta.getColumnName( i )
1339 + meta.getColumnLabel( i )
1340 + meta.getSchemaName( i )
1341 + meta.getPrecision( i )
1342 + meta.getScale( i )
1343 + meta.getTableName( i )
1344 + meta.getColumnTypeName( i )
1345 + meta.getColumnType( i )
1346 + meta.isReadOnly( i )
1347 + meta.isWritable( i )
1348 + meta.isDefinitelyWritable( i )
1349 + meta.getColumnServiceName( i )
1351 + def testArray( self ):
1352 + stmt = self.connection.prepareStatement(
1353 + "INSERT INTO firsttable VALUES ( 'insertedArray', 70000, 12000, 70001, 2.4, 2.45, 'huhu', 'true', '1999-01-08','04:05:06','1999-01-08 04:05:06', '{2,3,4}', ? )" )
1354 + myarray = ( "a", "\"c", "}d{" )
1355 + stmt.setArray( 1, MyArray( myarray ) )
1356 + stmt.executeUpdate()
1358 + stmt = self.connection.createStatement()
1359 + rs = stmt.executeQuery( "SELECT tStringArray FROM firsttable WHERE tString = 'insertedArray'" )
1360 + rs.next()
1361 + data = rs.getArray(1).getArray(None)
1362 + self.failUnless( data[0] == myarray[0] )
1363 + self.failUnless( data[1] == myarray[1] )
1364 + self.failUnless( data[2] == myarray[2] )