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 "ado/AKeys.hxx"
31 #ifndef _CONNECTIVITY_ADO_INDEX_HXX_
32 #include "ado/AKey.hxx"
33 #endif
34 #include <com/sun/star/sdbc/XRow.hpp>
35 #include <com/sun/star/sdbc/XResultSet.hpp>
36 #include <com/sun/star/sdbcx/KeyType.hpp>
37 #include <com/sun/star/sdbc/KeyRule.hpp>
38 #include "ado/AConnection.hxx"
39 #include <comphelper/types.hxx>
40 #include "ado/Awrapado.hxx"
41 #include <comphelper/property.hxx>
42 #include <connectivity/dbexception.hxx>
43 #include "resource/ado_res.hrc"
44 
45 using namespace ::comphelper;
46 using namespace connectivity;
47 using namespace connectivity::ado;
48 using namespace com::sun::star::uno;
49 using namespace com::sun::star::lang;
50 using namespace com::sun::star::beans;
51 using namespace com::sun::star::sdbc;
52 using namespace com::sun::star::sdbcx;
53 using namespace com::sun::star::container;
54 
55 sdbcx::ObjectType OKeys::createObject(const ::rtl::OUString& _rName)
56 {
57 	return new OAdoKey(isCaseSensitive(),m_pConnection,m_aCollection.GetItem(_rName));
58 }
59 // -------------------------------------------------------------------------
60 void OKeys::impl_refresh() throw(RuntimeException)
61 {
62 	m_aCollection.Refresh();
63 }
64 // -------------------------------------------------------------------------
65 Reference< XPropertySet > OKeys::createDescriptor()
66 {
67 	return new OAdoKey(isCaseSensitive(),m_pConnection);
68 }
69 // -------------------------------------------------------------------------
70 // XAppend
71 sdbcx::ObjectType OKeys::appendObject( const ::rtl::OUString&, const Reference< XPropertySet >& descriptor )
72 {
73 	OAdoKey* pKey = NULL;
74 	if ( !getImplementation( pKey, descriptor ) || pKey == NULL)
75         m_pConnection->throwGenericSQLException( STR_INVALID_KEY_DESCRIPTOR_ERROR,static_cast<XTypeProvider*>(this) );
76 
77 	// To pass as column parameter to Key's Apppend method
78 	OLEVariant vOptional;
79 	vOptional.setNoArg();
80 
81 #if OSL_DEBUG_LEVEL > 0
82     KeyTypeEnum eKey =
83 #endif
84 	    OAdoKey::Map2KeyRule(getINT32(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))));
85 #if OSL_DEBUG_LEVEL > 0
86     (void)eKey;
87 #endif
88 
89 	WpADOKey aKey = pKey->getImpl();
90 	::rtl::OUString sName = aKey.get_Name();
91 	if(!sName.getLength())
92 		aKey.put_Name(::rtl::OUString::createFromAscii("PrimaryKey") );
93 
94 	ADOKeys* pKeys = m_aCollection;
95 	if ( FAILED(pKeys->Append(OLEVariant((ADOKey*)aKey),
96 							adKeyPrimary, // must be every time adKeyPrimary
97 							vOptional)) )
98 	{
99 		ADOS::ThrowException(*m_pConnection->getConnection(),static_cast<XTypeProvider*>(this));
100 		// just make sure that an SQLExceptionis thrown here
101         m_pConnection->throwGenericSQLException( STR_INVALID_KEY_DESCRIPTOR_ERROR,static_cast<XTypeProvider*>(this) );
102 	}
103 
104 	return new OAdoKey(isCaseSensitive(),m_pConnection,pKey->getImpl());
105 }
106 // -------------------------------------------------------------------------
107 // XDrop
108 void OKeys::dropObject(sal_Int32 /*_nPos*/,const ::rtl::OUString _sElementName)
109 {
110 	if(!m_aCollection.Delete(OLEVariant(_sElementName)))
111 		ADOS::ThrowException(*m_pConnection->getConnection(),static_cast<XTypeProvider*>(this));
112 }
113 // -----------------------------------------------------------------------------
114 
115 
116