1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
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/.
12 #include "Catalog.hxx"
15 #include <TConnection.hxx>
17 #include <connectivity/dbtools.hxx>
19 #include <com/sun/star/sdbc/XRow.hpp>
20 #include <com/sun/star/sdbc/ColumnValue.hpp>
21 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
22 #include <comphelper/types.hxx>
24 using namespace ::connectivity
;
25 using namespace ::connectivity::firebird
;
26 using namespace ::connectivity::sdbcx
;
27 using namespace ::cppu
;
28 using namespace ::osl
;
30 using namespace ::com::sun::star
;
31 using namespace ::com::sun::star::beans
;
32 using namespace ::com::sun::star::container
;
33 using namespace ::com::sun::star::lang
;
34 using namespace ::com::sun::star::sdbc
;
35 using namespace ::com::sun::star::sdbcx
;
36 using namespace ::com::sun::star::uno
;
39 //----- OCollection -----------------------------------------------------------
40 void Tables::impl_refresh()
42 static_cast<Catalog
&>(m_rParent
).refreshTables();
45 ObjectType
Tables::createObject(const OUString
& rName
)
47 // Only retrieving a single table, so table type is irrelevant (param 4)
48 uno::Reference
< XResultSet
> xTables
= m_xMetaData
->getTables(Any(),
51 uno::Sequence
< OUString
>());
54 throw RuntimeException("Could not acquire table.");
56 uno::Reference
< XRow
> xRow(xTables
,UNO_QUERY_THROW
);
59 throw RuntimeException();
61 ObjectType
xRet(new Table(this,
63 m_xMetaData
->getConnection(),
64 xRow
->getString(3), // Name
65 xRow
->getString(4), // Type
66 xRow
->getString(5))); // Description / Remarks / Comments
69 throw RuntimeException("Found more tables than expected.");
74 OUString
Tables::createStandardColumnPart(const Reference
< XPropertySet
>& xColProp
,const Reference
< XConnection
>& _xConnection
)
76 Reference
<XDatabaseMetaData
> xMetaData
= _xConnection
->getMetaData();
78 ::dbtools::OPropertyMap
& rPropMap
= OMetaConnection::getPropMap();
80 bool bIsAutoIncrement
= false;
81 xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_ISAUTOINCREMENT
)) >>= bIsAutoIncrement
;
83 const OUString sQuoteString
= xMetaData
->getIdentifierQuoteString();
84 OUStringBuffer aSql
= ::dbtools::quoteName(sQuoteString
,::comphelper::getString(xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_NAME
))));
86 // check if the user enter a specific string to create autoincrement values
87 OUString sAutoIncrementValue
;
88 Reference
<XPropertySetInfo
> xPropInfo
= xColProp
->getPropertySetInfo();
90 if ( xPropInfo
.is() && xPropInfo
->hasPropertyByName(rPropMap
.getNameByIndex(PROPERTY_ID_AUTOINCREMENTCREATION
)) )
91 xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_AUTOINCREMENTCREATION
)) >>= sAutoIncrementValue
;
95 aSql
.append(dbtools::createStandardTypePart(xColProp
, _xConnection
));
96 // Add character set for (VAR)BINARY (fix) types:
97 // (VAR) BINARY is distinguished from other CHAR types by its character set.
98 // Octets is a special character set for binary data.
99 if ( xPropInfo
.is() && xPropInfo
->hasPropertyByName(rPropMap
.getNameByIndex(
103 xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_TYPE
))
105 if(aType
== DataType::BINARY
|| aType
== DataType::VARBINARY
)
108 aSql
.append("CHARACTER SET OCTETS");
112 if ( bIsAutoIncrement
&& !sAutoIncrementValue
.isEmpty())
115 aSql
.append(sAutoIncrementValue
);
117 // AutoIncrement "IDENTITY" is implicitly "NOT NULL"
118 else if(::comphelper::getINT32(xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_ISNULLABLE
))) == ColumnValue::NO_NULLS
)
119 aSql
.append(" NOT NULL");
121 return aSql
.makeStringAndClear();
124 uno::Reference
< XPropertySet
> Tables::createDescriptor()
126 // There is some internal magic so that the same class can be used as either
127 // a descriptor or as a normal table. See VTable.cxx for the details. In our
128 // case we just need to ensure we use the correct constructor.
129 return new Table(this, m_rMutex
, m_xMetaData
->getConnection());
132 //----- XAppend ---------------------------------------------------------------
133 ObjectType
Tables::appendObject(const OUString
& rName
,
134 const uno::Reference
< XPropertySet
>& rDescriptor
)
136 /* OUString sSql(::dbtools::createSqlCreateTableStatement(rDescriptor,
137 m_xMetaData->getConnection())); */
138 OUStringBuffer
aSqlBuffer("CREATE TABLE ");
139 OUString sCatalog
, sSchema
, sComposedName
, sTable
;
140 const Reference
< XConnection
>& xConnection
= m_xMetaData
->getConnection();
142 ::dbtools::OPropertyMap
& rPropMap
= OMetaConnection::getPropMap();
144 rDescriptor
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_CATALOGNAME
)) >>= sCatalog
;
145 rDescriptor
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_SCHEMANAME
)) >>= sSchema
;
146 rDescriptor
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_NAME
)) >>= sTable
;
148 sComposedName
= ::dbtools::composeTableName(m_xMetaData
, sCatalog
, sSchema
, sTable
, true, ::dbtools::EComposeRule::InTableDefinitions
);
149 if ( sComposedName
.isEmpty() )
150 ::dbtools::throwFunctionSequenceException(xConnection
);
152 aSqlBuffer
.append(sComposedName
);
153 aSqlBuffer
.append(" (");
156 Reference
<XColumnsSupplier
> xColumnSup(rDescriptor
,UNO_QUERY
);
157 Reference
<XIndexAccess
> xColumns(xColumnSup
->getColumns(),UNO_QUERY
);
158 // check if there are columns
159 if(!xColumns
.is() || !xColumns
->getCount())
160 ::dbtools::throwFunctionSequenceException(xConnection
);
162 Reference
< XPropertySet
> xColProp
;
164 sal_Int32 nCount
= xColumns
->getCount();
165 for(sal_Int32 i
=0;i
<nCount
;++i
)
167 if ( (xColumns
->getByIndex(i
) >>= xColProp
) && xColProp
.is() )
169 aSqlBuffer
.append(createStandardColumnPart(xColProp
,xConnection
));
170 aSqlBuffer
.append(",");
173 OUString sSql
= aSqlBuffer
.makeStringAndClear();
175 const OUString sKeyStmt
= ::dbtools::createStandardKeyStatement(rDescriptor
,xConnection
);
176 if ( !sKeyStmt
.isEmpty() )
180 if ( sSql
.endsWith(",") )
181 sSql
= sSql
.replaceAt(sSql
.getLength()-1, 1, ")");
186 m_xMetaData
->getConnection()->createStatement()->execute(sSql
);
188 return createObject(rName
);
191 //----- XDrop -----------------------------------------------------------------
192 void Tables::dropObject(sal_Int32 nPosition
, const OUString
& sName
)
194 uno::Reference
< XPropertySet
> xTable(getObject(nPosition
));
196 if (ODescriptor::isNew(xTable
))
199 OUStringBuffer
sSql("DROP ");
202 xTable
->getPropertyValue("Type") >>= sType
;
205 const OUString sQuoteString
= m_xMetaData
->getIdentifierQuoteString();
206 sSql
.append(::dbtools::quoteName(sQuoteString
,sName
));
208 m_xMetaData
->getConnection()->createStatement()->execute(sSql
.makeStringAndClear());
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */