xref: /trunk/main/connectivity/source/drivers/adabas/BColumns.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_connectivity.hxx"
30 #include "adabas/BColumns.hxx"
31 #include "connectivity/sdbcx/VColumn.hxx"
32 #include "connectivity/sdbcx/VColumn.hxx"
33 #include <com/sun/star/sdbc/XRow.hpp>
34 #include <com/sun/star/sdbc/XResultSet.hpp>
35 #include <com/sun/star/sdbc/DataType.hpp>
36 #include <com/sun/star/sdbc/ColumnValue.hpp>
37 #include "adabas/BTable.hxx"
38 #include "adabas/BTables.hxx"
39 #include "adabas/BCatalog.hxx"
40 #include <comphelper/types.hxx>
41 #include "connectivity/dbtools.hxx"
42 #include <comphelper/property.hxx>
43 
44 using namespace ::comphelper;
45 
46 
47 using namespace connectivity::adabas;
48 using namespace connectivity::sdbcx;
49 using namespace connectivity;
50 using namespace ::com::sun::star::uno;
51 using namespace ::com::sun::star::beans;
52 //  using namespace ::com::sun::star::sdbcx;
53 using namespace ::com::sun::star::sdbc;
54 using namespace ::com::sun::star::container;
55 using namespace ::com::sun::star::lang;
56 typedef connectivity::sdbcx::OCollection OCollection_TYPE;
57 
58 sdbcx::ObjectType OColumns::createObject(const ::rtl::OUString& _rName)
59 {
60     Reference< XResultSet > xResult = m_pTable->getMetaData()->getColumns(Any(),
61                                                             m_pTable->getSchema(),m_pTable->getTableName(),_rName);
62 
63     sdbcx::ObjectType xRet = NULL;
64     if(xResult.is())
65     {
66         Reference< XRow > xRow(xResult,UNO_QUERY);
67         while(xResult->next())
68         {
69             if(xRow->getString(4) == _rName)
70             {
71                 sal_Int32 nType             = xRow->getInt(5);
72                 ::rtl::OUString sTypeName   = xRow->getString(6);
73                 sal_Int32 nPrec             = xRow->getInt(7);
74                 OAdabasCatalog::correctColumnProperties(nPrec,nType,sTypeName);
75                 sal_Bool bAutoIncrement = sal_False;
76                 if ( !_rName.equalsAscii("DEFAULT") && !m_pTable->getSchema().equalsAscii("DOMAIN") && !m_pTable->getTableName().equalsAscii("COLUMNS") )
77                 {
78                     Reference< XStatement > xStmt = m_pTable->getMetaData()->getConnection()->createStatement(  );
79                     ::rtl::OUString sQuery(RTL_CONSTASCII_USTRINGPARAM("SELECT \"DEFAULT\" FROM DOMAIN.COLUMNS WHERE OWNER = '"));
80                     sQuery += m_pTable->getSchema();
81                     sQuery += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("' AND TABLENAME = '"));
82                     sQuery += m_pTable->getTableName() + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("' AND COLUMNNAME = '"));
83                     sQuery += _rName + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("'"));
84                     try
85                     {
86                         Reference< XResultSet > xResult2 = xStmt->executeQuery(sQuery);
87                         Reference< XRow > xRow2(xResult2,UNO_QUERY);
88                         if ( xRow2.is() && xResult2->next() )
89                             bAutoIncrement = xRow2->getString(1) == ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DEFAULT STAMP"));
90                     }
91                     catch(const Exception&)
92                     {
93                     }
94                 }
95 
96                 xRet = new OColumn(_rName,
97                                             sTypeName,
98                                             xRow->getString(13),
99                                             xRow->getString(12),
100                                             xRow->getInt(11),
101                                             nPrec,
102                                             xRow->getInt(9),
103                                             nType,
104                                             bAutoIncrement,sal_False,sal_False,sal_True);
105                 break;
106             }
107         }
108         ::comphelper::disposeComponent(xResult);
109     }
110 
111     return xRet;
112 }
113 
114 // -------------------------------------------------------------------------
115 void OColumns::impl_refresh() throw(RuntimeException)
116 {
117     m_pTable->refreshColumns();
118 }
119 // -------------------------------------------------------------------------
120 Reference< XPropertySet > OColumns::createDescriptor()
121 {
122     return new OColumn(sal_True);
123 }
124 // -------------------------------------------------------------------------
125 // XAppend
126 sdbcx::ObjectType OColumns::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
127 {
128     ::osl::MutexGuard aGuard(m_rMutex);
129     if ( m_pTable->isNew() )
130         return cloneDescriptor( descriptor );
131 
132     ::rtl::OUString aSql(RTL_CONSTASCII_USTRINGPARAM("ALTER TABLE "));
133     ::rtl::OUString sQuote  = m_pTable->getMetaData()->getIdentifierQuoteString(  );
134     const ::rtl::OUString& sDot = OAdabasCatalog::getDot();
135 
136     m_pTable->beginTransAction();
137     try
138     {
139         aSql += ::dbtools::quoteName(sQuote,m_pTable->getSchema()) + sDot + ::dbtools::quoteName(sQuote,m_pTable->getTableName());
140         aSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" ADD ("));
141         aSql += ::dbtools::quoteName(sQuote,_rForName);
142         aSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" "));
143         aSql += OTables::getColumnSqlType(descriptor);
144         aSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" )"));
145 
146         Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement();
147         xStmt->execute(aSql);
148         ::comphelper::disposeComponent(xStmt);
149 
150         m_pTable->alterNotNullValue(getINT32(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE))),_rForName);
151     }
152     catch(const Exception&)
153     {
154         m_pTable->rollbackTransAction();
155         throw;
156     }
157     m_pTable->endTransAction();
158 
159     return createObject( _rForName );
160 }
161 // -------------------------------------------------------------------------
162 // XDrop
163 void OColumns::dropObject(sal_Int32 /*_nPos*/,const ::rtl::OUString _sElementName)
164 {
165     OSL_ENSURE(m_pTable,"OColumns::dropByName: Table is null!");
166     if(!m_pTable->isNew())
167     {
168         ::rtl::OUString aSql(RTL_CONSTASCII_USTRINGPARAM("ALTER TABLE "));
169         ::rtl::OUString sQuote  = m_pTable->getMetaData()->getIdentifierQuoteString(  );
170         const ::rtl::OUString& sDot = OAdabasCatalog::getDot();
171 
172         aSql += ::dbtools::quoteName(sQuote,m_pTable->getSchema()) + sDot + ::dbtools::quoteName(sQuote,m_pTable->getTableName());
173         aSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" DROP "));
174         aSql += ::dbtools::quoteName(sQuote,_sElementName);
175 
176         Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement(  );
177         xStmt->execute(aSql);
178         ::comphelper::disposeComponent(xStmt);
179     }
180 }
181 // -----------------------------------------------------------------------------
182 
183 
184 
185