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/YViews.hxx"
21 : #include "mysql/YTables.hxx"
22 : #include <com/sun/star/sdbc/XRow.hpp>
23 : #include <com/sun/star/sdbc/XResultSet.hpp>
24 : #include <com/sun/star/sdbc/ColumnValue.hpp>
25 : #include <com/sun/star/sdbc/KeyRule.hpp>
26 : #include <com/sun/star/sdbcx/KeyType.hpp>
27 : #include <com/sun/star/sdbcx/CheckOption.hpp>
28 : #include "mysql/YCatalog.hxx"
29 : #include <comphelper/extract.hxx>
30 : #include "connectivity/dbtools.hxx"
31 : #include "connectivity/dbexception.hxx"
32 : #include <cppuhelper/interfacecontainer.h>
33 : #include "connectivity/sdbcx/VView.hxx"
34 : #include <comphelper/types.hxx>
35 : #include "TConnection.hxx"
36 :
37 : using namespace ::comphelper;
38 :
39 : using namespace ::cppu;
40 : using namespace connectivity;
41 : using namespace connectivity::mysql;
42 : using namespace ::com::sun::star::uno;
43 : using namespace ::com::sun::star::beans;
44 : using namespace ::com::sun::star::sdbcx;
45 : using namespace ::com::sun::star::sdbc;
46 : using namespace ::com::sun::star::container;
47 : using namespace ::com::sun::star::lang;
48 : using namespace dbtools;
49 : typedef connectivity::sdbcx::OCollection OCollection_TYPE;
50 :
51 0 : sdbcx::ObjectType OViews::createObject(const OUString& _rName)
52 : {
53 0 : OUString sCatalog,sSchema,sTable;
54 : ::dbtools::qualifiedNameComponents(m_xMetaData,
55 : _rName,
56 : sCatalog,
57 : sSchema,
58 : sTable,
59 0 : ::dbtools::eInDataManipulation);
60 0 : return new ::connectivity::sdbcx::OView(isCaseSensitive(),
61 : sTable,
62 : m_xMetaData,
63 : 0,
64 : OUString(),
65 : sSchema,
66 : sCatalog
67 0 : );
68 : }
69 :
70 0 : void OViews::impl_refresh( ) throw(RuntimeException)
71 : {
72 0 : static_cast<OMySQLCatalog&>(m_rParent).refreshTables();
73 0 : }
74 :
75 0 : void OViews::disposing(void)
76 : {
77 0 : m_xMetaData.clear();
78 0 : OCollection::disposing();
79 0 : }
80 :
81 0 : Reference< XPropertySet > OViews::createDescriptor()
82 : {
83 0 : Reference<XConnection> xConnection = static_cast<OMySQLCatalog&>(m_rParent).getConnection();
84 0 : connectivity::sdbcx::OView* pNew = new connectivity::sdbcx::OView(true, xConnection->getMetaData());
85 0 : return pNew;
86 : }
87 :
88 : // XAppend
89 0 : sdbcx::ObjectType OViews::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor )
90 : {
91 0 : createView(descriptor);
92 0 : return createObject( _rForName );
93 : }
94 :
95 : // XDrop
96 0 : void OViews::dropObject(sal_Int32 _nPos,const OUString& /*_sElementName*/)
97 : {
98 0 : if ( m_bInDrop )
99 0 : return;
100 :
101 0 : Reference< XInterface > xObject( getObject( _nPos ) );
102 0 : sal_Bool bIsNew = connectivity::sdbcx::ODescriptor::isNew( xObject );
103 0 : if (!bIsNew)
104 : {
105 0 : OUString aSql( "DROP VIEW" );
106 :
107 0 : Reference<XPropertySet> xProp(xObject,UNO_QUERY);
108 0 : aSql += ::dbtools::composeTableName( m_xMetaData, xProp, ::dbtools::eInTableDefinitions, false, false, true );
109 :
110 0 : Reference<XConnection> xConnection = static_cast<OMySQLCatalog&>(m_rParent).getConnection();
111 0 : Reference< XStatement > xStmt = xConnection->createStatement( );
112 0 : xStmt->execute(aSql);
113 0 : ::comphelper::disposeComponent(xStmt);
114 0 : }
115 : }
116 :
117 0 : void OViews::dropByNameImpl(const OUString& elementName)
118 : {
119 0 : m_bInDrop = sal_True;
120 0 : OCollection_TYPE::dropByName(elementName);
121 0 : m_bInDrop = sal_False;
122 0 : }
123 :
124 0 : void OViews::createView( const Reference< XPropertySet >& descriptor )
125 : {
126 0 : Reference<XConnection> xConnection = static_cast<OMySQLCatalog&>(m_rParent).getConnection();
127 :
128 0 : OUString aSql( "CREATE VIEW " );
129 0 : OUString sCommand;
130 :
131 0 : aSql += ::dbtools::composeTableName( m_xMetaData, descriptor, ::dbtools::eInTableDefinitions, false, false, true );
132 :
133 0 : aSql += " AS ";
134 0 : descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_COMMAND)) >>= sCommand;
135 0 : aSql += sCommand;
136 :
137 0 : Reference< XStatement > xStmt = xConnection->createStatement( );
138 0 : if ( xStmt.is() )
139 : {
140 0 : xStmt->execute(aSql);
141 0 : ::comphelper::disposeComponent(xStmt);
142 : }
143 :
144 : // insert the new view also in the tables collection
145 0 : OTables* pTables = static_cast<OTables*>(static_cast<OMySQLCatalog&>(m_rParent).getPrivateTables());
146 0 : if ( pTables )
147 : {
148 0 : OUString sName = ::dbtools::composeTableName( m_xMetaData, descriptor, ::dbtools::eInDataManipulation, false, false, false );
149 0 : pTables->appendNew(sName);
150 0 : }
151 0 : }
152 :
153 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|