xref: /trunk/main/dbaccess/source/ui/misc/dsmeta.cxx (revision 96de5490)
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 #include "dsmeta.hxx"
25 #include <connectivity/DriversConfig.hxx>
26 #include "dsntypes.hxx"
27 #include <comphelper/processfactory.hxx>
28 /** === begin UNO includes === **/
29 /** === end UNO includes === **/
30 
31 #include <map>
32 
33 //........................................................................
34 namespace dbaui
35 {
36 //........................................................................
37 
38 	/** === begin UNO using === **/
39     using namespace dbaccess;
40     using namespace ::com::sun::star;
41 	/** === end UNO using === **/
42 
43     struct FeatureSupport
44     {
45         // authentication mode of the data source
46         AuthenticationMode      eAuthentication;
47 
48         FeatureSupport()
49             :eAuthentication( AuthUserPwd )
50         {
51         }
52 
53         FeatureSupport( AuthenticationMode _Auth )
54             :eAuthentication( _Auth )
55         {
56         }
57     };
58 
59     struct FeatureMapping
60     {
61         /// one of the items from dsitems.hxx
62         ItemID          nItemID;
63         const sal_Char* pAsciiFeatureName;
64     };
65 
66 	//====================================================================
67 	//= global tables
68 	//====================================================================
69 	//--------------------------------------------------------------------
70     static const FeatureMapping* lcl_getFeatureMappings()
71     {
72         static const FeatureMapping s_aMappings[] = {
73             { DSID_AUTORETRIEVEENABLED,     "GeneratedValues" },
74             { DSID_AUTOINCREMENTVALUE,      "GeneratedValues" },
75             { DSID_AUTORETRIEVEVALUE,       "GeneratedValues" },
76             { DSID_SQL92CHECK,              "UseSQL92NamingConstraints" },
77             { DSID_APPEND_TABLE_ALIAS,      "AppendTableAliasInSelect" },
78             { DSID_AS_BEFORE_CORRNAME,      "UseKeywordAsBeforeAlias" },
79             { DSID_ENABLEOUTERJOIN,         "UseBracketedOuterJoinSyntax" },
80             { DSID_IGNOREDRIVER_PRIV,       "IgnoreDriverPrivileges" },
81             { DSID_PARAMETERNAMESUBST,      "ParameterNameSubstitution" },
82             { DSID_SUPPRESSVERSIONCL,       "DisplayVersionColumns" },
83             { DSID_CATALOG,                 "UseCatalogInSelect" },
84             { DSID_SCHEMA,                  "UseSchemaInSelect" },
85             { DSID_INDEXAPPENDIX,           "UseIndexDirectionKeyword" },
86             { DSID_DOSLINEENDS,             "UseDOSLineEnds" },
87             { DSID_BOOLEANCOMPARISON,       "BooleanComparisonMode" },
88             { DSID_CHECK_REQUIRED_FIELDS,   "FormsCheckRequiredFields" },
89             { DSID_IGNORECURRENCY,          "IgnoreCurrency" },
90             { DSID_ESCAPE_DATETIME,         "EscapeDateTime" },
91             { DSID_PRIMARY_KEY_SUPPORT,     "PrimaryKeySupport" },
92             { DSID_RESPECTRESULTSETTYPE,    "RespectDriverResultSetType" },
93             { DSID_MAX_ROW_SCAN,            "MaxRowScan" },
94             { 0, NULL }
95         };
96         return s_aMappings;
97     }
98 
99 	//--------------------------------------------------------------------
100     static const FeatureSet& lcl_getFeatureSet( const ::rtl::OUString _rURL )
101     {
102         typedef ::std::map< ::rtl::OUString, FeatureSet, ::comphelper::UStringLess >    FeatureSets;
103         static FeatureSets s_aFeatureSets;
104         if ( s_aFeatureSets.empty() )
105         {
106             ::connectivity::DriversConfig aDriverConfig( ::comphelper::getProcessServiceFactory() );
107             const uno::Sequence< ::rtl::OUString > aPatterns = aDriverConfig.getURLs();
108             for (   const ::rtl::OUString* pattern = aPatterns.getConstArray();
109                     pattern != aPatterns.getConstArray() + aPatterns.getLength();
110                     ++pattern
111                 )
112             {
113                 FeatureSet aCurrentSet;
114                 const ::comphelper::NamedValueCollection aCurrentFeatures( aDriverConfig.getFeatures( *pattern ).getNamedValues() );
115 
116                 const FeatureMapping* pFeatureMapping = lcl_getFeatureMappings();
117                 while ( pFeatureMapping->pAsciiFeatureName )
118                 {
119                     if ( aCurrentFeatures.has( pFeatureMapping->pAsciiFeatureName ) )
120                         aCurrentSet.put( pFeatureMapping->nItemID );
121                     ++pFeatureMapping;
122                 }
123 
124                 s_aFeatureSets[ *pattern ] = aCurrentSet;
125             }
126         }
127 
128         OSL_ENSURE( s_aFeatureSets.find( _rURL ) != s_aFeatureSets.end(), "invalid URL/pattern!" );
129         return s_aFeatureSets[ _rURL ];
130     }
131 
132 	//--------------------------------------------------------------------
133     static AuthenticationMode getAuthenticationMode( const ::rtl::OUString& _sURL )
134     {
135         DECLARE_STL_USTRINGACCESS_MAP( FeatureSupport, Supported);
136         static Supported s_aSupport;
137         if ( s_aSupport.empty() )
138         {
139             ::connectivity::DriversConfig aDriverConfig(::comphelper::getProcessServiceFactory());
140             const uno::Sequence< ::rtl::OUString > aURLs = aDriverConfig.getURLs();
141             const ::rtl::OUString* pIter = aURLs.getConstArray();
142             const ::rtl::OUString* pEnd = pIter + aURLs.getLength();
143             for(;pIter != pEnd;++pIter)
144             {
145                 FeatureSupport aInit( AuthNone );
146                 const ::comphelper::NamedValueCollection& aMetaData = aDriverConfig.getMetaData(*pIter);
147                 if ( aMetaData.has("Authentication") )
148                 {
149                     ::rtl::OUString sAuth;
150                     aMetaData.get("Authentication") >>= sAuth;
151                     if ( sAuth.equalsAscii("UserPassword") )
152                         aInit = AuthUserPwd;
153                     else if ( sAuth.equalsAscii("Password") )
154                         aInit = AuthPwd;
155                 }
156                 s_aSupport.insert(Supported::value_type(*pIter,aInit));
157             }
158         }
159         OSL_ENSURE(s_aSupport.find(_sURL) != s_aSupport.end(),"Illegal URL!");
160         return s_aSupport[ _sURL ].eAuthentication;
161     }
162 
163 	//====================================================================
164 	//= DataSourceMetaData_Impl
165 	//====================================================================
166     class DataSourceMetaData_Impl
167     {
168     public:
169         DataSourceMetaData_Impl( const ::rtl::OUString& _sURL );
170 
171         inline ::rtl::OUString getType() const { return m_sURL; }
172 
173     private:
174         const ::rtl::OUString m_sURL;
175     };
176 
177 	//--------------------------------------------------------------------
178     DataSourceMetaData_Impl::DataSourceMetaData_Impl( const ::rtl::OUString& _sURL )
179         :m_sURL( _sURL )
180     {
181     }
182 
183 	//====================================================================
184 	//= DataSourceMetaData
185 	//====================================================================
186 	//--------------------------------------------------------------------
187     DataSourceMetaData::DataSourceMetaData( const ::rtl::OUString& _sURL )
188         :m_pImpl( new DataSourceMetaData_Impl( _sURL ) )
189     {
190     }
191 
192 	//--------------------------------------------------------------------
193     DataSourceMetaData::~DataSourceMetaData()
194     {
195     }
196 
197 	//--------------------------------------------------------------------
198     const FeatureSet& DataSourceMetaData::getFeatureSet() const
199     {
200         return lcl_getFeatureSet( m_pImpl->getType() );
201     }
202 
203 	//--------------------------------------------------------------------
204     AuthenticationMode  DataSourceMetaData::getAuthentication( const ::rtl::OUString& _sURL )
205     {
206         return getAuthenticationMode( _sURL );
207     }
208 
209 //........................................................................
210 } // namespace dbaui
211 //........................................................................
212