1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 #include "adabas/BColumns.hxx"
27 #include "connectivity/sdbcx/VColumn.hxx"
28 #include "connectivity/sdbcx/VColumn.hxx"
29 #include <com/sun/star/sdbc/XRow.hpp>
30 #include <com/sun/star/sdbc/XResultSet.hpp>
31 #include <com/sun/star/sdbc/DataType.hpp>
32 #include <com/sun/star/sdbc/ColumnValue.hpp>
33 #include "adabas/BTable.hxx"
34 #include "adabas/BTables.hxx"
35 #include "adabas/BCatalog.hxx"
36 #include <comphelper/types.hxx>
37 #include "connectivity/dbtools.hxx"
38 #include <comphelper/property.hxx>
39
40 using namespace ::comphelper;
41
42
43 using namespace connectivity::adabas;
44 using namespace connectivity::sdbcx;
45 using namespace connectivity;
46 using namespace ::com::sun::star::uno;
47 using namespace ::com::sun::star::beans;
48 // using namespace ::com::sun::star::sdbcx;
49 using namespace ::com::sun::star::sdbc;
50 using namespace ::com::sun::star::container;
51 using namespace ::com::sun::star::lang;
52 typedef connectivity::sdbcx::OCollection OCollection_TYPE;
53
createObject(const::rtl::OUString & _rName)54 sdbcx::ObjectType OColumns::createObject(const ::rtl::OUString& _rName)
55 {
56 Reference< XResultSet > xResult = m_pTable->getMetaData()->getColumns(Any(),
57 m_pTable->getSchema(),m_pTable->getTableName(),_rName);
58
59 sdbcx::ObjectType xRet = NULL;
60 if(xResult.is())
61 {
62 Reference< XRow > xRow(xResult,UNO_QUERY);
63 while(xResult->next())
64 {
65 if(xRow->getString(4) == _rName)
66 {
67 sal_Int32 nType = xRow->getInt(5);
68 ::rtl::OUString sTypeName = xRow->getString(6);
69 sal_Int32 nPrec = xRow->getInt(7);
70 OAdabasCatalog::correctColumnProperties(nPrec,nType,sTypeName);
71 sal_Bool bAutoIncrement = sal_False;
72 if ( !_rName.equalsAscii("DEFAULT") && !m_pTable->getSchema().equalsAscii("DOMAIN") && !m_pTable->getTableName().equalsAscii("COLUMNS") )
73 {
74 Reference< XStatement > xStmt = m_pTable->getMetaData()->getConnection()->createStatement( );
75 ::rtl::OUString sQuery(RTL_CONSTASCII_USTRINGPARAM("SELECT \"DEFAULT\" FROM DOMAIN.COLUMNS WHERE OWNER = '"));
76 sQuery += m_pTable->getSchema();
77 sQuery += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("' AND TABLENAME = '"));
78 sQuery += m_pTable->getTableName() + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("' AND COLUMNNAME = '"));
79 sQuery += _rName + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("'"));
80 try
81 {
82 Reference< XResultSet > xResult2 = xStmt->executeQuery(sQuery);
83 Reference< XRow > xRow2(xResult2,UNO_QUERY);
84 if ( xRow2.is() && xResult2->next() )
85 bAutoIncrement = xRow2->getString(1) == ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DEFAULT STAMP"));
86 }
87 catch(const Exception&)
88 {
89 }
90 }
91
92 xRet = new OColumn(_rName,
93 sTypeName,
94 xRow->getString(13),
95 xRow->getString(12),
96 xRow->getInt(11),
97 nPrec,
98 xRow->getInt(9),
99 nType,
100 bAutoIncrement,sal_False,sal_False,sal_True);
101 break;
102 }
103 }
104 ::comphelper::disposeComponent(xResult);
105 }
106
107 return xRet;
108 }
109
110 // -------------------------------------------------------------------------
impl_refresh()111 void OColumns::impl_refresh() throw(RuntimeException)
112 {
113 m_pTable->refreshColumns();
114 }
115 // -------------------------------------------------------------------------
createDescriptor()116 Reference< XPropertySet > OColumns::createDescriptor()
117 {
118 return new OColumn(sal_True);
119 }
120 // -------------------------------------------------------------------------
121 // XAppend
appendObject(const::rtl::OUString & _rForName,const Reference<XPropertySet> & descriptor)122 sdbcx::ObjectType OColumns::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
123 {
124 ::osl::MutexGuard aGuard(m_rMutex);
125 if ( m_pTable->isNew() )
126 return cloneDescriptor( descriptor );
127
128 ::rtl::OUString aSql(RTL_CONSTASCII_USTRINGPARAM("ALTER TABLE "));
129 ::rtl::OUString sQuote = m_pTable->getMetaData()->getIdentifierQuoteString( );
130 const ::rtl::OUString& sDot = OAdabasCatalog::getDot();
131
132 m_pTable->beginTransAction();
133 try
134 {
135 aSql += ::dbtools::quoteName(sQuote,m_pTable->getSchema()) + sDot + ::dbtools::quoteName(sQuote,m_pTable->getTableName());
136 aSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" ADD ("));
137 aSql += ::dbtools::quoteName(sQuote,_rForName);
138 aSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" "));
139 aSql += OTables::getColumnSqlType(descriptor);
140 aSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" )"));
141
142 Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement();
143 xStmt->execute(aSql);
144 ::comphelper::disposeComponent(xStmt);
145
146 m_pTable->alterNotNullValue(getINT32(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISNULLABLE))),_rForName);
147 }
148 catch(const Exception&)
149 {
150 m_pTable->rollbackTransAction();
151 throw;
152 }
153 m_pTable->endTransAction();
154
155 return createObject( _rForName );
156 }
157 // -------------------------------------------------------------------------
158 // XDrop
dropObject(sal_Int32,const::rtl::OUString _sElementName)159 void OColumns::dropObject(sal_Int32 /*_nPos*/,const ::rtl::OUString _sElementName)
160 {
161 OSL_ENSURE(m_pTable,"OColumns::dropByName: Table is null!");
162 if(!m_pTable->isNew())
163 {
164 ::rtl::OUString aSql(RTL_CONSTASCII_USTRINGPARAM("ALTER TABLE "));
165 ::rtl::OUString sQuote = m_pTable->getMetaData()->getIdentifierQuoteString( );
166 const ::rtl::OUString& sDot = OAdabasCatalog::getDot();
167
168 aSql += ::dbtools::quoteName(sQuote,m_pTable->getSchema()) + sDot + ::dbtools::quoteName(sQuote,m_pTable->getTableName());
169 aSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" DROP "));
170 aSql += ::dbtools::quoteName(sQuote,_sElementName);
171
172 Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
173 xStmt->execute(aSql);
174 ::comphelper::disposeComponent(xStmt);
175 }
176 }
177 // -----------------------------------------------------------------------------
178
179
180
181