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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbui.hxx"
26 
27 #ifndef _TOOLS_DEBUG_HXX
28 #include <tools/debug.hxx>
29 #endif
30 #ifndef _CPPUHELPER_EXC_HLP_HXX_
31 #include <cppuhelper/exc_hlp.hxx>
32 #endif
33 #ifndef TOOLS_DIAGNOSE_EX_H
34 #include <tools/diagnose_ex.h>
35 #endif
36 #ifndef _DBAUI_ADASTAT_HXX_
37 #include "AdabasStat.hxx"
38 #endif
39 #ifndef _COMPHELPER_TYPES_HXX_
40 #include <comphelper/types.hxx>
41 #endif
42 #ifndef _COM_SUN_STAR_SDBC_XSTATEMENT_HPP_
43 #include <com/sun/star/sdbc/XStatement.hpp>
44 #endif
45 #ifndef _COM_SUN_STAR_SDBC_XROW_HPP_
46 #include <com/sun/star/sdbc/XRow.hpp>
47 #endif
48 #ifndef _COM_SUN_STAR_SDBC_XRESULTSET_HPP_
49 #include <com/sun/star/sdbc/XResultSet.hpp>
50 #endif
51 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
52 #include <com/sun/star/beans/XPropertySet.hpp>
53 #endif
54 #ifndef DBAUI_ADABASSTAT_HRC
55 #include "AdabasStat.hrc"
56 #endif
57 #ifndef _DBU_DLG_HRC_
58 #include "dbu_dlg.hrc"
59 #endif
60 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
61 #include "dbustrings.hrc"
62 #endif
63 #ifndef DBAUI_TOOLS_HXX
64 #include "UITools.hxx"
65 #endif
66 #ifndef _CONNECTIVITY_DBTOOLS_HXX_
67 #include <connectivity/dbtools.hxx>
68 #endif
69 #ifndef _DBAUI_SQLMESSAGE_HXX_
70 #include "sqlmessage.hxx"
71 #endif
72 
73 using namespace dbaui;
74 DBG_NAME(OAdabasStatistics)
75 namespace dbaui
76 {
77 	using namespace dbtools;
78 	using namespace ::com::sun::star::uno;
79 	using namespace ::com::sun::star::beans;
80 	using namespace ::com::sun::star::sdbc;
81 	using namespace ::com::sun::star::lang;
82 
83 
84 
OAdabasStatistics(Window * pParent,const::rtl::OUString & _rUser,const Reference<::com::sun::star::sdbc::XConnection> & _xCurrentConnection,const Reference<XMultiServiceFactory> & _xFactory)85 OAdabasStatistics::OAdabasStatistics( Window* pParent,
86 									 const ::rtl::OUString& _rUser,
87 									 const Reference< ::com::sun::star::sdbc::XConnection >& _xCurrentConnection,
88 									 const Reference< XMultiServiceFactory >& _xFactory)
89 	: ModalDialog( pParent, ModuleRes(DLG_ADABASSTAT) )
90     ,m_FL_FILES(            this , ModuleRes(FL_FILES))
91 	,m_FT_SYSDEVSPACE(		this , ModuleRes(FT_SYSDEVSPACE))
92 	,m_ET_SYSDEVSPACE(		this , STR_ADABAS_HELP_SYSDEVSPACE,ModuleRes(ET_SYSDEVSPACE))
93 	,m_FT_TRANSACTIONLOG(	this , ModuleRes(FT_TRANSACTIONLOG))
94 	,m_ET_TRANSACTIONLOG(	this , STR_ADABAS_HELP_TRANSACT,ModuleRes(ET_TRANSACTIONLOG))
95 	,m_FT_DATADEVSPACE(		this , ModuleRes(FT_DATADEVSPACE))
96 	,m_LB_DATADEVS(			this , STR_ADABAS_HELP_DATADEVSPACES,ModuleRes(LB_DATADEVS))
97     ,m_FL_SIZES(            this , ModuleRes(FL_SIZES))
98 	,m_FT_SIZE(				this , ModuleRes(FT_SIZE))
99 	,m_ET_SIZE(				this , STR_ADABAS_HELP_SIZE,ModuleRes(ET_SIZE))
100 	,m_FT_FREESIZE(			this , ModuleRes(FT_FREESIZE))
101 	,m_ET_FREESIZE(			this , STR_ADABAS_HELP_FREESIZE,ModuleRes(ET_FREESIZE))
102 	,m_FT_MEMORYUSING(		this , ModuleRes(FT_MEMORYUSING))
103 	,m_ET_MEMORYUSING(		this , STR_ADABAS_HELP_MEMORYUSING,ModuleRes(ET_MEMORYUSING))
104 	,m_PB_OK(				this , ModuleRes(PB_OK))
105 	,m_xConnection(_xCurrentConnection)
106 	,m_bErrorShown(sal_False)
107 {
108 	DBG_CTOR(OAdabasStatistics,NULL);
109 
110 	FreeResource();
111 
112 	DBG_ASSERT(m_xConnection.is(),"No connection");
113 	if(m_xConnection.is())
114 	{
115 		Reference<XStatement> xStmt;
116 		Reference<XResultSet> xRes;
117 
118 		sal_Bool bCanSelect		= sal_False;
119 		::rtl::OUString aStmt;
120 		::rtl::OUString sSchema = _rUser.toAsciiUpperCase();
121 
122 		Reference<XDatabaseMetaData> xMetaData;
123 		// first read the sizes
124 		try
125 		{
126 			xMetaData = m_xConnection->getMetaData();
127 			bCanSelect = checkSystemTable(::rtl::OUString::createFromAscii("SERVERDBSTATISTICS"),sSchema);
128 
129 			if(bCanSelect)
130 			{
131 				aStmt = ::rtl::OUString::createFromAscii("SELECT SERVERDBSIZE, UNUSEDPAGES FROM ");
132 
133 				aStmt += ::dbtools::quoteTableName(xMetaData,sSchema,::dbtools::eInDataManipulation);
134 				aStmt += ::rtl::OUString::createFromAscii(".\"SERVERDBSTATISTICS\"");
135 
136 				xStmt = m_xConnection->createStatement();
137 				xRes = xStmt->executeQuery(aStmt);
138 
139 
140 				Reference<XRow> xRow(xRes,UNO_QUERY);
141 				// first the db sizes
142 				if(xRes.is() && xRes->next())
143 				{
144 					double nUsedPages = xRow->getInt(1) / 256;
145 					double nFreePages = xRow->getInt(2) / 256;
146 
147 					m_ET_SIZE.SetText(::rtl::OUString::valueOf((sal_Int32)nUsedPages));
148 					m_ET_FREESIZE.SetText(::rtl::OUString::valueOf((sal_Int32)nFreePages));
149 					m_ET_MEMORYUSING.SetValue(static_cast<sal_Int32>(((nUsedPages-nFreePages)/nUsedPages)*100));
150 				}
151 				else
152 					showError();
153 
154 				xRow = NULL;
155 			}
156 			else
157 				showError();
158 		}
159 		catch(const SQLException& )
160 		{
161             ::dbaui::showError( SQLExceptionInfo( ::cppu::getCaughtException() ), pParent, _xFactory );
162 		}
163         catch( const Exception& )
164         {
165             DBG_UNHANDLED_EXCEPTION();
166         }
167 		try
168 		{
169 			::comphelper::disposeComponent(xStmt);
170 		}
171         catch( const Exception& )
172         {
173             DBG_UNHANDLED_EXCEPTION();
174         }
175 
176 		// now fill the datadev spaces
177 		if(bCanSelect)
178 		{
179 			try
180 			{
181 				bCanSelect = checkSystemTable(::rtl::OUString::createFromAscii("DATADEVSPACES"),sSchema);
182 
183 				if(bCanSelect)
184 				{
185 					// then the db files
186 					aStmt = ::rtl::OUString::createFromAscii("SELECT DEVSPACENAME FROM ");
187 					aStmt += ::dbtools::quoteTableName(xMetaData,sSchema,::dbtools::eInDataManipulation);
188 					aStmt += ::rtl::OUString::createFromAscii(".\"DATADEVSPACES\"");
189 					xStmt = m_xConnection->createStatement();
190 					xRes = xStmt->executeQuery(aStmt);
191 
192 					Reference<XRow> xRow(xRes,UNO_QUERY);
193 					while(xRes.is() && xRes->next())
194 					{
195 						m_LB_DATADEVS.InsertEntry(xRow->getString(1));
196 					}
197 					if(!m_LB_DATADEVS.GetEntryCount())
198 						showError();
199 				}
200 				else
201 					showError();
202 			}
203 			catch(const SQLException& e)
204 			{
205 				::dbaui::showError(SQLExceptionInfo(e),pParent,_xFactory);
206 			}
207             catch( const Exception& )
208             {
209                 DBG_UNHANDLED_EXCEPTION();
210             }
211 			try
212 			{
213 				::comphelper::disposeComponent(xStmt);
214 			}
215             catch( const Exception& )
216             {
217                 DBG_UNHANDLED_EXCEPTION();
218             }
219 
220 			// now fill the sysdatadev spaces
221 			if(bCanSelect)
222 			{
223 				try
224 				{
225 					bCanSelect = checkSystemTable(::rtl::OUString::createFromAscii("CONFIGURATION"),sSchema);
226 
227 					if(bCanSelect)
228 					{
229 						aStmt = ::rtl::OUString::createFromAscii("SELECT * FROM ");
230 						aStmt += ::dbtools::quoteTableName(xMetaData,sSchema,::dbtools::eInDataManipulation);
231 						aStmt += ::rtl::OUString::createFromAscii(".CONFIGURATION WHERE DESCRIPTION LIKE 'SYS%DEVSPACE%NAME'");
232 						xStmt = m_xConnection->createStatement();
233 						xRes = xStmt->executeQuery(aStmt);
234 						if(xRes.is() && xRes->next())
235 						{
236 							Reference<XRow> xRow(xRes,UNO_QUERY);
237 							m_ET_SYSDEVSPACE.SetText(xRow->getString(2));
238 						}
239 						else
240 							showError();
241 
242 						aStmt = ::rtl::OUString::createFromAscii("SELECT * FROM ");
243 						aStmt += ::dbtools::quoteTableName(xMetaData,sSchema,::dbtools::eInDataManipulation);
244 						aStmt += ::rtl::OUString::createFromAscii(".CONFIGURATION WHERE DESCRIPTION = 'TRANSACTION LOG NAME'");
245 						xRes = xStmt->executeQuery(aStmt);
246 						if(xRes.is() && xRes->next())
247 						{
248 							Reference<XRow> xRow(xRes,UNO_QUERY);
249 							m_ET_TRANSACTIONLOG.SetText(xRow->getString(2));
250 						}
251 						else
252 							showError();
253 					}
254 					else
255 						showError();
256 				}
257 				catch(const SQLException& e)
258 				{
259 					::dbaui::showError(SQLExceptionInfo(e),pParent,_xFactory);
260 				}
261                 catch( const Exception& )
262                 {
263                     DBG_UNHANDLED_EXCEPTION();
264                 }
265 				try
266 				{
267 					::comphelper::disposeComponent(xStmt);
268 				}
269                 catch( const Exception& )
270                 {
271                     DBG_UNHANDLED_EXCEPTION();
272                 }
273 			}
274 		}
275 	}
276 
277 	m_ET_SYSDEVSPACE.SetSpecialReadOnly(sal_True);
278 	m_ET_TRANSACTIONLOG.SetSpecialReadOnly(sal_True);
279 	m_LB_DATADEVS.SetSpecialReadOnly(sal_True);
280 	m_ET_SIZE.SetSpecialReadOnly(sal_True);
281 	m_ET_FREESIZE.SetSpecialReadOnly(sal_True);
282 	m_ET_MEMORYUSING.SetSpecialReadOnly(sal_True);
283 }
284 //------------------------------------------------------------------------
~OAdabasStatistics()285 OAdabasStatistics::~OAdabasStatistics()
286 {
287 	DBG_DTOR(OAdabasStatistics,NULL);
288 }
289 // -----------------------------------------------------------------------------
checkSystemTable(const::rtl::OUString & _rsSystemTable,::rtl::OUString & _rsSchemaName)290 sal_Bool OAdabasStatistics::checkSystemTable(const ::rtl::OUString& _rsSystemTable, ::rtl::OUString& _rsSchemaName )
291 {
292 	sal_Bool bCanSelect = sal_False;
293 	Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
294 	if ( xMeta.is() )
295 	{
296 		Reference<XResultSet> xRes = xMeta->getTablePrivileges(Any(),::rtl::OUString::createFromAscii("%"),  _rsSystemTable);
297 		if(xRes.is())
298 		{
299 			Reference<XRow> xRow(xRes,UNO_QUERY);
300 			static const ::rtl::OUString sSelect = ::rtl::OUString::createFromAscii("SELECT");
301 			// first the db sizes
302 			while( xRow.is() && xRes->next() )
303 			{
304 				_rsSchemaName = xRow->getString(2);
305 				if(sSelect == xRow->getString(6) && !xRow->wasNull())
306 				{
307 					bCanSelect = sal_True;
308 					break;
309 				}
310 			}
311 			::comphelper::disposeComponent(xRes);
312 		}
313 	}
314 
315 	return bCanSelect;
316 }
317 // -----------------------------------------------------------------------------
showError()318 void OAdabasStatistics::showError()
319 {
320 	if(!m_bErrorShown)
321 	{
322 		OSQLMessageBox aMsg(GetParent(),GetText(),String(ModuleRes(STR_ADABAS_ERROR_SYSTEMTABLES)));
323 		aMsg.Execute();
324 		m_bErrorShown = sal_True;
325 	}
326 }
327 // -----------------------------------------------------------------------------
328 }
329 
330