Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "mysql/YTables.hxx"
21 : #include "mysql/YViews.hxx"
22 : #include "mysql/YTable.hxx"
23 : #include <com/sun/star/sdbc/XRow.hpp>
24 : #include <com/sun/star/sdbc/XResultSet.hpp>
25 : #include <com/sun/star/sdbc/ColumnValue.hpp>
26 : #include <com/sun/star/sdbcx/Privilege.hpp>
27 : #include <com/sun/star/sdbc/KeyRule.hpp>
28 : #include <com/sun/star/sdbcx/KeyType.hpp>
29 : #include "mysql/YCatalog.hxx"
30 : #include <comphelper/extract.hxx>
31 : #include "connectivity/dbtools.hxx"
32 : #include "connectivity/dbexception.hxx"
33 : #include <cppuhelper/interfacecontainer.h>
34 : #include <comphelper/types.hxx>
35 : #include "TConnection.hxx"
36 :
37 : using namespace ::comphelper;
38 : using namespace connectivity;
39 : using namespace ::cppu;
40 : using namespace connectivity::mysql;
41 : using namespace ::com::sun::star::uno;
42 : using namespace ::com::sun::star::beans;
43 : using namespace ::com::sun::star::sdbcx;
44 : using namespace ::com::sun::star::sdbc;
45 : using namespace ::com::sun::star::container;
46 : using namespace ::com::sun::star::lang;
47 : using namespace dbtools;
48 :
49 0 : sdbcx::ObjectType OTables::createObject(const ::rtl::OUString& _rName)
50 : {
51 0 : ::rtl::OUString sCatalog,sSchema,sTable;
52 0 : ::dbtools::qualifiedNameComponents(m_xMetaData,_rName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
53 :
54 0 : static const ::rtl::OUString s_sTableTypeView("VIEW");
55 0 : static const ::rtl::OUString s_sTableTypeTable("TABLE");
56 0 : static const ::rtl::OUString s_sAll("%");
57 :
58 0 : Sequence< ::rtl::OUString > sTableTypes(3);
59 0 : sTableTypes[0] = s_sTableTypeView;
60 0 : sTableTypes[1] = s_sTableTypeTable;
61 0 : sTableTypes[2] = s_sAll; // just to be sure to include anything else ....
62 :
63 0 : Any aCatalog;
64 0 : if ( !sCatalog.isEmpty() )
65 0 : aCatalog <<= sCatalog;
66 0 : Reference< XResultSet > xResult = m_xMetaData->getTables(aCatalog,sSchema,sTable,sTableTypes);
67 :
68 0 : sdbcx::ObjectType xRet = NULL;
69 0 : if ( xResult.is() )
70 : {
71 0 : Reference< XRow > xRow(xResult,UNO_QUERY);
72 0 : if ( xResult->next() ) // there can be only one table with this name
73 : {
74 : sal_Int32 nPrivileges = Privilege::DROP |
75 : Privilege::REFERENCE |
76 : Privilege::ALTER |
77 : Privilege::CREATE |
78 : Privilege::READ |
79 : Privilege::DELETE |
80 : Privilege::UPDATE |
81 : Privilege::INSERT |
82 0 : Privilege::SELECT;
83 :
84 : OMySQLTable* pRet = new OMySQLTable( this
85 : ,static_cast<OMySQLCatalog&>(m_rParent).getConnection()
86 : ,sTable
87 0 : ,xRow->getString(4)
88 0 : ,xRow->getString(5)
89 : ,sSchema
90 : ,sCatalog
91 0 : ,nPrivileges);
92 0 : xRet = pRet;
93 : }
94 0 : ::comphelper::disposeComponent(xResult);
95 : }
96 :
97 0 : return xRet;
98 : }
99 : // -------------------------------------------------------------------------
100 0 : void OTables::impl_refresh( ) throw(RuntimeException)
101 : {
102 0 : static_cast<OMySQLCatalog&>(m_rParent).refreshTables();
103 0 : }
104 : // -------------------------------------------------------------------------
105 0 : void OTables::disposing(void)
106 : {
107 0 : m_xMetaData.clear();
108 0 : OCollection::disposing();
109 0 : }
110 : // -------------------------------------------------------------------------
111 0 : Reference< XPropertySet > OTables::createDescriptor()
112 : {
113 0 : return new OMySQLTable(this,static_cast<OMySQLCatalog&>(m_rParent).getConnection());
114 : }
115 : // -------------------------------------------------------------------------
116 : // XAppend
117 0 : sdbcx::ObjectType OTables::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
118 : {
119 0 : createTable(descriptor);
120 0 : return createObject( _rForName );
121 : }
122 : // -------------------------------------------------------------------------
123 : // XDrop
124 0 : void OTables::dropObject(sal_Int32 _nPos,const ::rtl::OUString _sElementName)
125 : {
126 0 : Reference< XInterface > xObject( getObject( _nPos ) );
127 0 : sal_Bool bIsNew = connectivity::sdbcx::ODescriptor::isNew( xObject );
128 0 : if (!bIsNew)
129 : {
130 0 : Reference< XConnection > xConnection = static_cast<OMySQLCatalog&>(m_rParent).getConnection();
131 :
132 :
133 0 : ::rtl::OUString sCatalog,sSchema,sTable;
134 0 : ::dbtools::qualifiedNameComponents(m_xMetaData,_sElementName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
135 :
136 0 : ::rtl::OUString aSql( "DROP " );
137 :
138 0 : Reference<XPropertySet> xProp(xObject,UNO_QUERY);
139 0 : sal_Bool bIsView = xProp.is() && ::comphelper::getString(xProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))) == ::rtl::OUString("VIEW");
140 0 : if(bIsView) // here we have a view
141 0 : aSql += ::rtl::OUString("VIEW ");
142 : else
143 0 : aSql += ::rtl::OUString("TABLE ");
144 :
145 : ::rtl::OUString sComposedName(
146 0 : ::dbtools::composeTableName( m_xMetaData, sCatalog, sSchema, sTable, sal_True, ::dbtools::eInDataManipulation ) );
147 0 : aSql += sComposedName;
148 0 : Reference< XStatement > xStmt = xConnection->createStatement( );
149 0 : if ( xStmt.is() )
150 : {
151 0 : xStmt->execute(aSql);
152 0 : ::comphelper::disposeComponent(xStmt);
153 : }
154 : // if no exception was thrown we must delete it from the views
155 0 : if ( bIsView )
156 : {
157 0 : OViews* pViews = static_cast<OViews*>(static_cast<OMySQLCatalog&>(m_rParent).getPrivateViews());
158 0 : if ( pViews && pViews->hasByName(_sElementName) )
159 0 : pViews->dropByNameImpl(_sElementName);
160 0 : }
161 0 : }
162 0 : }
163 : // -------------------------------------------------------------------------
164 0 : ::rtl::OUString OTables::adjustSQL(const ::rtl::OUString& _sSql)
165 : {
166 0 : ::rtl::OUString sSQL = _sSql;
167 0 : static const ::rtl::OUString s_sUNSIGNED("UNSIGNED");
168 0 : sal_Int32 nIndex = sSQL.indexOf(s_sUNSIGNED);
169 0 : while(nIndex != -1 )
170 : {
171 0 : sal_Int32 nParen = sSQL.indexOf(')',nIndex);
172 0 : sal_Int32 nPos = nIndex + s_sUNSIGNED.getLength();
173 0 : ::rtl::OUString sNewUnsigned( sSQL.copy(nPos,nParen - nPos + 1));
174 0 : sSQL = sSQL.replaceAt(nIndex,s_sUNSIGNED.getLength()+sNewUnsigned.getLength(),sNewUnsigned + s_sUNSIGNED);
175 0 : nIndex = sSQL.indexOf(s_sUNSIGNED,nIndex + s_sUNSIGNED.getLength()+sNewUnsigned.getLength());
176 0 : }
177 0 : return sSQL;
178 : }
179 : // -------------------------------------------------------------------------
180 0 : void OTables::createTable( const Reference< XPropertySet >& descriptor )
181 : {
182 0 : const Reference< XConnection > xConnection = static_cast<OMySQLCatalog&>(m_rParent).getConnection();
183 0 : static const ::rtl::OUString s_sCreatePattern("(M,D)");
184 0 : const ::rtl::OUString aSql = adjustSQL(::dbtools::createSqlCreateTableStatement(descriptor,xConnection,this,s_sCreatePattern));
185 0 : Reference< XStatement > xStmt = xConnection->createStatement( );
186 0 : if ( xStmt.is() )
187 : {
188 0 : xStmt->execute(aSql);
189 0 : ::comphelper::disposeComponent(xStmt);
190 0 : }
191 0 : }
192 : // -----------------------------------------------------------------------------
193 0 : void OTables::appendNew(const ::rtl::OUString& _rsNewTable)
194 : {
195 0 : insertElement(_rsNewTable,NULL);
196 :
197 : // notify our container listeners
198 0 : ContainerEvent aEvent(static_cast<XContainer*>(this), makeAny(_rsNewTable), Any(), Any());
199 0 : OInterfaceIteratorHelper aListenerLoop(m_aContainerListeners);
200 0 : while (aListenerLoop.hasMoreElements())
201 0 : static_cast<XContainerListener*>(aListenerLoop.next())->elementInserted(aEvent);
202 0 : }
203 : // -----------------------------------------------------------------------------
204 0 : ::rtl::OUString OTables::getNameForObject(const sdbcx::ObjectType& _xObject)
205 : {
206 : OSL_ENSURE(_xObject.is(),"OTables::getNameForObject: Object is NULL!");
207 0 : return ::dbtools::composeTableName( m_xMetaData, _xObject, ::dbtools::eInDataManipulation, false, false, false );
208 : }
209 : // -----------------------------------------------------------------------------
210 0 : void OTables::addComment(const Reference< XPropertySet >& descriptor,::rtl::OUStringBuffer& _rOut)
211 : {
212 0 : ::rtl::OUString sDesc;
213 0 : descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DESCRIPTION)) >>= sDesc;
214 0 : if ( !sDesc.isEmpty() )
215 : {
216 0 : _rOut.appendAscii(" COMMENT '");
217 0 : _rOut.append(sDesc);
218 0 : _rOut.appendAscii("'");
219 0 : }
220 0 : }
221 :
222 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|