1 #*************************************************************************
3 # $RCSfile: metadata.py,v $
7 # last change: $Author: jbu $ $Date: 2006/05/27 11:33:11 $
9 # The Contents of this file are made available subject to the terms of
10 # either of the following licenses
12 # - GNU Lesser General Public License Version 2.1
13 # - Sun Industry Standards Source License Version 1.1
15 # Sun Microsystems Inc., October, 2000
17 # GNU Lesser General Public License Version 2.1
18 # =============================================
19 # Copyright 2000 by Sun Microsystems, Inc.
20 # 901 San Antonio Road, Palo Alto, CA 94303, USA
22 # This library is free software; you can redistribute it and/or
23 # modify it under the terms of the GNU Lesser General Public
24 # License version 2.1, as published by the Free Software Foundation.
26 # This library is distributed in the hope that it will be useful,
27 # but WITHOUT ANY WARRANTY; without even the implied warranty of
28 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 # Lesser General Public License for more details.
31 # You should have received a copy of the GNU Lesser General Public
32 # License along with this library; if not, write to the Free Software
33 # Foundation, Inc., 59 Temple Place, Suite 330, Boston,
37 # Sun Industry Standards Source License Version 1.1
38 # =================================================
39 # The contents of this file are subject to the Sun Industry Standards
40 # Source License Version 1.1 (the "License"); You may not use this file
41 # except in compliance with the License. You may obtain a copy of the
42 # License at http://www.openoffice.org/license.html.
44 # Software provided under this License is provided on an "AS IS" basis,
45 # WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
46 # WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
47 # MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
48 # See the License for the specific provisions governing your rights and
49 # obligations concerning the Software.
51 # The Initial Developer of the Original Code is: Ralph Thomas
53 # Copyright: 2000 by Sun Microsystems, Inc.
55 # All Rights Reserved.
57 # Contributor(s): Ralph Thomas, Joerg Budischewski
59 #*************************************************************************
64 from com
.sun
.star
.sdbc
.DataType
import SMALLINT
, INTEGER
, BIGINT
, DATE
, TIME
, TIMESTAMP
, NUMERIC
66 def dumpResultSet( rs
, count
):
67 # for i in range(1, count):
68 # sys.stdout.write(meta.getColumnName( i ) + "\t")
69 sys
.stdout
.write( "\n" )
71 for i
in range( 1, count
+1):
72 sys
.stdout
.write( rs
.getString( i
) + "\t" )
73 sys
.stdout
.write( "\n" )
80 suite
= unittest
.TestSuite()
81 suite
.addTest(TestCase("testDatabaseMetaData",ctx
,dburl
))
82 suite
.addTest(TestCase("testTypeGuess",ctx
,dburl
))
85 class TestCase(unittest
.TestCase
):
86 def __init__(self
,method
,ctx
,dburl
):
87 unittest
.TestCase
.__init
__(self
,method
)
93 self
.driver
= self
.ctx
.ServiceManager
.createInstanceWithContext(
94 'org.openoffice.comp.connectivity.pq.Driver' , self
.ctx
)
95 self
.connection
= self
.driver
.connect( self
.dburl
, () )
96 ddl
.executeDDLs( self
.connection
)
99 self
.connection
.close()
101 def testDatabaseMetaData( self
):
102 meta
= self
.connection
.getMetaData()
104 rs
= meta
.getTables( None, "public", "%", () )
105 # dumpResultSet( rs, 5)
107 rs
= meta
.getColumns( None, "%", "customer", "%" )
108 # dumpResultSet( rs, 18 )
110 rs
= meta
.getPrimaryKeys( None, "public" , "%" )
111 # dumpResultSet( rs , 6 )
112 rs
= meta
.getTablePrivileges( None, "public" , "%" )
113 # dumpResultSet( rs , 7 )
114 rs
= meta
.getColumns( None, "public" , "customer", "%" )
115 # dumpResultSet( rs , 18 )
116 rs
= meta
.getTypeInfo()
117 # dumpResultSet(rs, 18)
119 if rs
.getString(1) == "pqsdbc_short":
120 self
.failUnless( rs
.getInt(2) == SMALLINT
)
122 self
.failUnless( not rs
.isAfterLast() ) # domain type cannot be found
125 rs
= meta
.getIndexInfo( None, "public" , "customer", False, False )
126 # dumpResultSet( rs, 13 )
128 def testTypeGuess( self
):
129 stmt
= self
.connection
.createStatement()
130 rs
= stmt
.executeQuery( "SELECT sum(amount) FROM orderpos" )
131 meta
= rs
.getMetaData()
132 self
.failUnless( meta
.getColumnType(1) == BIGINT
)
134 stmt
= self
.connection
.createStatement()
135 rs
= stmt
.executeQuery( "SELECT sum(price) FROM product" )
136 meta
= rs
.getMetaData()
137 self
.failUnless( meta
.getColumnType(1) == NUMERIC
)
139 rs
= stmt
.executeQuery( "SELECT max(ttime) FROM firsttable" )
140 meta
= rs
.getMetaData()
141 self
.failUnless( meta
.getColumnType(1) == TIME
)
143 rs
= stmt
.executeQuery( "SELECT max(tdate) FROM firsttable" )
144 meta
= rs
.getMetaData()
145 self
.failUnless( meta
.getColumnType(1) == DATE
)
147 rs
= stmt
.executeQuery( "SELECT max(ttimestamp) FROM firsttable" )
148 meta
= rs
.getMetaData()
149 self
.failUnless( meta
.getColumnType(1) == TIMESTAMP
)
151 # print rs.getString( 1 )