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