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