196de5490SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
396de5490SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
496de5490SAndrew Rist * or more contributor license agreements. See the NOTICE file
596de5490SAndrew Rist * distributed with this work for additional information
696de5490SAndrew Rist * regarding copyright ownership. The ASF licenses this file
796de5490SAndrew Rist * to you under the Apache License, Version 2.0 (the
896de5490SAndrew Rist * "License"); you may not use this file except in compliance
996de5490SAndrew Rist * with the License. You may obtain a copy of the License at
1096de5490SAndrew Rist *
1196de5490SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
1296de5490SAndrew Rist *
1396de5490SAndrew Rist * Unless required by applicable law or agreed to in writing,
1496de5490SAndrew Rist * software distributed under the License is distributed on an
1596de5490SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1696de5490SAndrew Rist * KIND, either express or implied. See the License for the
1796de5490SAndrew Rist * specific language governing permissions and limitations
1896de5490SAndrew Rist * under the License.
1996de5490SAndrew Rist *
2096de5490SAndrew Rist *************************************************************/
2196de5490SAndrew Rist
2296de5490SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25*b63233d8Sdamjan #include "precompiled_dbui.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #ifndef _DBACCESS_UI_DIRECTSQL_HXX_
28cdf0e10cSrcweir #include "directsql.hxx"
29cdf0e10cSrcweir #endif
30cdf0e10cSrcweir #ifndef _DBACCESS_UI_DIRECTSQL_HRC_
31cdf0e10cSrcweir #include "directsql.hrc"
32cdf0e10cSrcweir #endif
33cdf0e10cSrcweir #ifndef _DBU_DLG_HRC_
34cdf0e10cSrcweir #include "dbu_dlg.hrc"
35cdf0e10cSrcweir #endif
36cdf0e10cSrcweir #ifndef _SV_MSGBOX_HXX
37cdf0e10cSrcweir #include <vcl/msgbox.hxx>
38cdf0e10cSrcweir #endif
39cdf0e10cSrcweir #ifndef _COMPHELPER_TYPES_HXX_
40cdf0e10cSrcweir #include <comphelper/types.hxx>
41cdf0e10cSrcweir #endif
42cdf0e10cSrcweir #ifndef _SV_MSGBOX_HXX
43cdf0e10cSrcweir #include <vcl/msgbox.hxx>
44cdf0e10cSrcweir #endif
45cdf0e10cSrcweir #ifndef _SV_SVAPP_HXX
46cdf0e10cSrcweir #include <vcl/svapp.hxx>
47cdf0e10cSrcweir #endif
48cdf0e10cSrcweir #ifndef _VOS_MUTEX_HXX_
49cdf0e10cSrcweir #include <vos/mutex.hxx>
50cdf0e10cSrcweir #endif
51cdf0e10cSrcweir #ifndef TOOLS_DIAGNOSE_EX_H
52cdf0e10cSrcweir #include <tools/diagnose_ex.h>
53cdf0e10cSrcweir #endif
54cdf0e10cSrcweir
55cdf0e10cSrcweir //........................................................................
56cdf0e10cSrcweir namespace dbaui
57cdf0e10cSrcweir {
58cdf0e10cSrcweir //........................................................................
59cdf0e10cSrcweir
60cdf0e10cSrcweir using namespace ::com::sun::star::uno;
61cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
62cdf0e10cSrcweir using namespace ::com::sun::star::lang;
63cdf0e10cSrcweir
64cdf0e10cSrcweir //====================================================================
65cdf0e10cSrcweir //= LargeEntryListBox
66cdf0e10cSrcweir //====================================================================
67cdf0e10cSrcweir class LargeEntryListBox : public ListBox
68cdf0e10cSrcweir {
69cdf0e10cSrcweir public:
70cdf0e10cSrcweir LargeEntryListBox( Window* _pParent, const ResId& _rId );
71cdf0e10cSrcweir
72cdf0e10cSrcweir protected:
73cdf0e10cSrcweir virtual void UserDraw( const UserDrawEvent& rUDEvt );
74cdf0e10cSrcweir };
75cdf0e10cSrcweir
76cdf0e10cSrcweir //--------------------------------------------------------------------
LargeEntryListBox(Window * _pParent,const ResId & _rId)77cdf0e10cSrcweir LargeEntryListBox::LargeEntryListBox( Window* _pParent, const ResId& _rId )
78cdf0e10cSrcweir :ListBox(_pParent, _rId )
79cdf0e10cSrcweir {
80cdf0e10cSrcweir EnableUserDraw(sal_True);
81cdf0e10cSrcweir }
82cdf0e10cSrcweir
83cdf0e10cSrcweir //--------------------------------------------------------------------
UserDraw(const UserDrawEvent & _rUDEvt)84cdf0e10cSrcweir void LargeEntryListBox::UserDraw( const UserDrawEvent& _rUDEvt )
85cdf0e10cSrcweir {
86cdf0e10cSrcweir if (LISTBOX_ENTRY_NOTFOUND == _rUDEvt.GetItemId())
87cdf0e10cSrcweir ListBox::UserDraw( _rUDEvt );
88cdf0e10cSrcweir else
89cdf0e10cSrcweir _rUDEvt.GetDevice()->DrawText( _rUDEvt.GetRect(), GetEntry( _rUDEvt.GetItemId() ), TEXT_DRAW_LEFT | TEXT_DRAW_VCENTER | TEXT_DRAW_ENDELLIPSIS);
90cdf0e10cSrcweir }
91cdf0e10cSrcweir
92cdf0e10cSrcweir //====================================================================
93cdf0e10cSrcweir //= DirectSQLDialog
94cdf0e10cSrcweir //====================================================================
DBG_NAME(DirectSQLDialog)95cdf0e10cSrcweir DBG_NAME(DirectSQLDialog)
96cdf0e10cSrcweir //--------------------------------------------------------------------
97cdf0e10cSrcweir DirectSQLDialog::DirectSQLDialog( Window* _pParent, const Reference< XConnection >& _rxConn )
98cdf0e10cSrcweir :ModalDialog(_pParent, ModuleRes(DLG_DIRECTSQL))
99cdf0e10cSrcweir ,m_aFrame (this, ModuleRes(FL_SQL))
100cdf0e10cSrcweir ,m_aSQLLabel (this, ModuleRes(FT_SQL))
101cdf0e10cSrcweir ,m_aSQL (this, ModuleRes(ME_SQL))
102cdf0e10cSrcweir ,m_aExecute (this, ModuleRes(PB_EXECUTE))
103cdf0e10cSrcweir ,m_aHistoryLabel (this, ModuleRes(FT_HISTORY))
104cdf0e10cSrcweir ,m_pSQLHistory(new LargeEntryListBox(this, ModuleRes(LB_HISTORY)))
105cdf0e10cSrcweir ,m_aStatusFrame (this, ModuleRes(FL_STATUS))
106cdf0e10cSrcweir ,m_aStatus (this, ModuleRes(ME_STATUS))
107cdf0e10cSrcweir ,m_aButtonSeparator (this, ModuleRes(FL_BUTTONS))
108cdf0e10cSrcweir ,m_aHelp (this, ModuleRes(PB_HELP))
109cdf0e10cSrcweir ,m_aClose (this, ModuleRes(PB_CLOSE))
110cdf0e10cSrcweir ,m_nHistoryLimit(20)
111cdf0e10cSrcweir ,m_nStatusCount(1)
112cdf0e10cSrcweir ,m_xConnection(_rxConn)
113cdf0e10cSrcweir {
114cdf0e10cSrcweir DBG_CTOR(DirectSQLDialog,NULL);
115cdf0e10cSrcweir
116cdf0e10cSrcweir FreeResource();
117cdf0e10cSrcweir
118cdf0e10cSrcweir m_aSQL.GrabFocus();
119cdf0e10cSrcweir
120cdf0e10cSrcweir m_aExecute.SetClickHdl(LINK(this, DirectSQLDialog, OnExecute));
121cdf0e10cSrcweir m_aClose.SetClickHdl(LINK(this, DirectSQLDialog, OnClose));
122cdf0e10cSrcweir m_pSQLHistory->SetSelectHdl(LINK(this, DirectSQLDialog, OnListEntrySelected));
123cdf0e10cSrcweir m_pSQLHistory->SetDropDownLineCount(10);
124cdf0e10cSrcweir
125cdf0e10cSrcweir // add a dispose listener to the connection
126cdf0e10cSrcweir Reference< XComponent > xConnComp(m_xConnection, UNO_QUERY);
127cdf0e10cSrcweir OSL_ENSURE(xConnComp.is(), "DirectSQLDialog::DirectSQLDialog: invalid connection!");
128cdf0e10cSrcweir if (xConnComp.is())
129cdf0e10cSrcweir startComponentListening(xConnComp);
130cdf0e10cSrcweir
131cdf0e10cSrcweir m_aSQL.SetModifyHdl(LINK(this, DirectSQLDialog, OnStatementModified));
132cdf0e10cSrcweir OnStatementModified(&m_aSQL);
133cdf0e10cSrcweir }
134cdf0e10cSrcweir
135cdf0e10cSrcweir //--------------------------------------------------------------------
~DirectSQLDialog()136cdf0e10cSrcweir DirectSQLDialog::~DirectSQLDialog()
137cdf0e10cSrcweir {
138cdf0e10cSrcweir {
139cdf0e10cSrcweir ::osl::MutexGuard aGuard(m_aMutex);
140cdf0e10cSrcweir stopAllComponentListening();
141cdf0e10cSrcweir }
142cdf0e10cSrcweir delete m_pSQLHistory;
143cdf0e10cSrcweir
144cdf0e10cSrcweir DBG_DTOR(DirectSQLDialog,NULL);
145cdf0e10cSrcweir }
146cdf0e10cSrcweir
147cdf0e10cSrcweir //--------------------------------------------------------------------
_disposing(const EventObject & _rSource)148cdf0e10cSrcweir void DirectSQLDialog::_disposing( const EventObject& _rSource )
149cdf0e10cSrcweir {
150cdf0e10cSrcweir ::vos::OGuard aSolarGuard(Application::GetSolarMutex());
151cdf0e10cSrcweir ::osl::MutexGuard aGuard(m_aMutex);
152cdf0e10cSrcweir
153cdf0e10cSrcweir OSL_ENSURE(Reference< XConnection >(_rSource.Source, UNO_QUERY).get() == m_xConnection.get(),
154cdf0e10cSrcweir "DirectSQLDialog::_disposing: where does this come from?");
155cdf0e10cSrcweir (void)_rSource;
156cdf0e10cSrcweir
157cdf0e10cSrcweir {
158cdf0e10cSrcweir String sMessage(ModuleRes(STR_DIRECTSQL_CONNECTIONLOST));
159cdf0e10cSrcweir ErrorBox aError(this, WB_OK, sMessage);
160cdf0e10cSrcweir aError.Execute();
161cdf0e10cSrcweir }
162cdf0e10cSrcweir
163cdf0e10cSrcweir PostUserEvent(LINK(this, DirectSQLDialog, OnClose));
164cdf0e10cSrcweir }
165cdf0e10cSrcweir
166cdf0e10cSrcweir //--------------------------------------------------------------------
getHistorySize() const167cdf0e10cSrcweir sal_Int32 DirectSQLDialog::getHistorySize() const
168cdf0e10cSrcweir {
169cdf0e10cSrcweir CHECK_INVARIANTS("DirectSQLDialog::getHistorySize");
170cdf0e10cSrcweir return m_aStatementHistory.size();
171cdf0e10cSrcweir }
172cdf0e10cSrcweir
173cdf0e10cSrcweir //--------------------------------------------------------------------
implEnsureHistoryLimit()174cdf0e10cSrcweir void DirectSQLDialog::implEnsureHistoryLimit()
175cdf0e10cSrcweir {
176cdf0e10cSrcweir CHECK_INVARIANTS("DirectSQLDialog::implEnsureHistoryLimit");
177cdf0e10cSrcweir
178cdf0e10cSrcweir if (getHistorySize() <= m_nHistoryLimit)
179cdf0e10cSrcweir // nothing to do
180cdf0e10cSrcweir return;
181cdf0e10cSrcweir
182cdf0e10cSrcweir sal_Int32 nRemoveEntries = getHistorySize() - m_nHistoryLimit;
183cdf0e10cSrcweir while (nRemoveEntries--)
184cdf0e10cSrcweir {
185cdf0e10cSrcweir m_aStatementHistory.pop_front();
186cdf0e10cSrcweir m_aNormalizedHistory.pop_front();
187cdf0e10cSrcweir m_pSQLHistory->RemoveEntry((sal_uInt16)0);
188cdf0e10cSrcweir }
189cdf0e10cSrcweir }
190cdf0e10cSrcweir
191cdf0e10cSrcweir //--------------------------------------------------------------------
implAddToStatementHistory(const String & _rStatement)192cdf0e10cSrcweir void DirectSQLDialog::implAddToStatementHistory(const String& _rStatement)
193cdf0e10cSrcweir {
194cdf0e10cSrcweir CHECK_INVARIANTS("DirectSQLDialog::implAddToStatementHistory");
195cdf0e10cSrcweir
196cdf0e10cSrcweir // add the statement to the history
197cdf0e10cSrcweir m_aStatementHistory.push_back(_rStatement);
198cdf0e10cSrcweir
199cdf0e10cSrcweir // normalize the statement, and remember the normalized form, too
200cdf0e10cSrcweir String sNormalized(_rStatement);
201cdf0e10cSrcweir sNormalized.SearchAndReplaceAll((sal_Unicode)'\n', ' ');
202cdf0e10cSrcweir m_aNormalizedHistory.push_back(sNormalized);
203cdf0e10cSrcweir
204cdf0e10cSrcweir // add the normalized version to the list box
205cdf0e10cSrcweir m_pSQLHistory->InsertEntry(sNormalized);
206cdf0e10cSrcweir
207cdf0e10cSrcweir // ensure that we don't exceed the history limit
208cdf0e10cSrcweir implEnsureHistoryLimit();
209cdf0e10cSrcweir }
210cdf0e10cSrcweir
211cdf0e10cSrcweir #ifdef DBG_UTIL
212cdf0e10cSrcweir //--------------------------------------------------------------------
impl_CheckInvariants() const213cdf0e10cSrcweir const sal_Char* DirectSQLDialog::impl_CheckInvariants() const
214cdf0e10cSrcweir {
215cdf0e10cSrcweir if (m_aStatementHistory.size() != m_aNormalizedHistory.size())
216cdf0e10cSrcweir return "statement history is inconsistent!";
217cdf0e10cSrcweir
218cdf0e10cSrcweir if (!m_pSQLHistory)
219cdf0e10cSrcweir return "invalid listbox!";
220cdf0e10cSrcweir
221cdf0e10cSrcweir if (m_aStatementHistory.size() != m_pSQLHistory->GetEntryCount())
222cdf0e10cSrcweir return "invalid listbox entry count!";
223cdf0e10cSrcweir
224cdf0e10cSrcweir if (!m_xConnection.is())
225cdf0e10cSrcweir return "have no connection!";
226cdf0e10cSrcweir
227cdf0e10cSrcweir return NULL;
228cdf0e10cSrcweir }
229cdf0e10cSrcweir #endif
230cdf0e10cSrcweir
231cdf0e10cSrcweir //--------------------------------------------------------------------
implExecuteStatement(const String & _rStatement)232cdf0e10cSrcweir void DirectSQLDialog::implExecuteStatement(const String& _rStatement)
233cdf0e10cSrcweir {
234cdf0e10cSrcweir CHECK_INVARIANTS("DirectSQLDialog::implExecuteStatement");
235cdf0e10cSrcweir
236cdf0e10cSrcweir ::osl::MutexGuard aGuard(m_aMutex);
237cdf0e10cSrcweir
238cdf0e10cSrcweir String sStatus;
239cdf0e10cSrcweir try
240cdf0e10cSrcweir {
241cdf0e10cSrcweir // create a statement
242cdf0e10cSrcweir Reference< XStatement > xStatement = m_xConnection->createStatement();
243cdf0e10cSrcweir OSL_ENSURE(xStatement.is(), "DirectSQLDialog::implExecuteStatement: no statement returned by the connection!");
244cdf0e10cSrcweir
245cdf0e10cSrcweir // execute it
246cdf0e10cSrcweir if (xStatement.is())
247cdf0e10cSrcweir xStatement->execute(_rStatement);
248cdf0e10cSrcweir
24907a3d7f1SPedro Giffuni // successful
250cdf0e10cSrcweir sStatus = String(ModuleRes(STR_COMMAND_EXECUTED_SUCCESSFULLY));
251cdf0e10cSrcweir
252cdf0e10cSrcweir // dispose the statement
253cdf0e10cSrcweir ::comphelper::disposeComponent(xStatement);
254cdf0e10cSrcweir }
255cdf0e10cSrcweir catch(const SQLException& e)
256cdf0e10cSrcweir {
257cdf0e10cSrcweir sStatus = e.Message;
258cdf0e10cSrcweir }
259cdf0e10cSrcweir catch( const Exception& )
260cdf0e10cSrcweir {
261cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION();
262cdf0e10cSrcweir }
263cdf0e10cSrcweir
264cdf0e10cSrcweir // add the status text
265cdf0e10cSrcweir addStatusText(sStatus);
266cdf0e10cSrcweir }
267cdf0e10cSrcweir
268cdf0e10cSrcweir //--------------------------------------------------------------------
addStatusText(const String & _rMessage)269cdf0e10cSrcweir void DirectSQLDialog::addStatusText(const String& _rMessage)
270cdf0e10cSrcweir {
271cdf0e10cSrcweir String sAppendMessage = String::CreateFromInt32(m_nStatusCount++);
272cdf0e10cSrcweir sAppendMessage += String::CreateFromAscii(": ");
273cdf0e10cSrcweir sAppendMessage += _rMessage;
274cdf0e10cSrcweir sAppendMessage += String::CreateFromAscii("\n\n");
275cdf0e10cSrcweir
276cdf0e10cSrcweir String sCompleteMessage = m_aStatus.GetText();
277cdf0e10cSrcweir sCompleteMessage += sAppendMessage;
278cdf0e10cSrcweir m_aStatus.SetText(sCompleteMessage);
279cdf0e10cSrcweir
280cdf0e10cSrcweir m_aStatus.SetSelection(Selection(sCompleteMessage.Len(), sCompleteMessage.Len()));
281cdf0e10cSrcweir }
282cdf0e10cSrcweir
283cdf0e10cSrcweir //--------------------------------------------------------------------
executeCurrent()284cdf0e10cSrcweir void DirectSQLDialog::executeCurrent()
285cdf0e10cSrcweir {
286cdf0e10cSrcweir CHECK_INVARIANTS("DirectSQLDialog::executeCurrent");
287cdf0e10cSrcweir
288cdf0e10cSrcweir String sStatement = m_aSQL.GetText();
289cdf0e10cSrcweir
290cdf0e10cSrcweir // execute
291cdf0e10cSrcweir implExecuteStatement(sStatement);
292cdf0e10cSrcweir
293cdf0e10cSrcweir // add the statement to the history
294cdf0e10cSrcweir implAddToStatementHistory(sStatement);
295cdf0e10cSrcweir
296cdf0e10cSrcweir m_aSQL.SetSelection(Selection());
297cdf0e10cSrcweir m_aSQL.GrabFocus();
298cdf0e10cSrcweir }
299cdf0e10cSrcweir
300cdf0e10cSrcweir //--------------------------------------------------------------------
switchToHistory(sal_Int32 _nHistoryPos,sal_Bool _bUpdateListBox)301cdf0e10cSrcweir void DirectSQLDialog::switchToHistory(sal_Int32 _nHistoryPos, sal_Bool _bUpdateListBox)
302cdf0e10cSrcweir {
303cdf0e10cSrcweir CHECK_INVARIANTS("DirectSQLDialog::switchToHistory");
304cdf0e10cSrcweir
305cdf0e10cSrcweir if ((_nHistoryPos >= 0) && (_nHistoryPos < getHistorySize()))
306cdf0e10cSrcweir {
307cdf0e10cSrcweir // set the text in the statement editor
308cdf0e10cSrcweir String sStatement = m_aStatementHistory[_nHistoryPos];
309cdf0e10cSrcweir m_aSQL.SetText(sStatement);
310cdf0e10cSrcweir OnStatementModified(&m_aSQL);
311cdf0e10cSrcweir
312cdf0e10cSrcweir if (_bUpdateListBox)
313cdf0e10cSrcweir {
314cdf0e10cSrcweir // selecte the normalized statement in the list box
315cdf0e10cSrcweir m_pSQLHistory->SelectEntryPos((sal_uInt16)_nHistoryPos);
316cdf0e10cSrcweir OSL_ENSURE(m_pSQLHistory->GetSelectEntry() == m_aNormalizedHistory[_nHistoryPos],
317cdf0e10cSrcweir "DirectSQLDialog::switchToHistory: inconsistent listbox entries!");
318cdf0e10cSrcweir }
319cdf0e10cSrcweir
320cdf0e10cSrcweir m_aSQL.GrabFocus();
321cdf0e10cSrcweir m_aSQL.SetSelection(Selection(sStatement.Len(), sStatement.Len()));
322cdf0e10cSrcweir }
323cdf0e10cSrcweir else
324cdf0e10cSrcweir OSL_ENSURE(sal_False, "DirectSQLDialog::switchToHistory: invalid position!");
325cdf0e10cSrcweir }
326cdf0e10cSrcweir
327cdf0e10cSrcweir //--------------------------------------------------------------------
328cdf0e10cSrcweir IMPL_LINK( DirectSQLDialog, OnStatementModified, void*, /*NOTINTERESTEDIN*/ )
329cdf0e10cSrcweir {
330cdf0e10cSrcweir m_aExecute.Enable(0 != m_aSQL.GetText().Len());
331cdf0e10cSrcweir return 0L;
332cdf0e10cSrcweir }
333cdf0e10cSrcweir
334cdf0e10cSrcweir //--------------------------------------------------------------------
335cdf0e10cSrcweir IMPL_LINK( DirectSQLDialog, OnClose, void*, /*NOTINTERESTEDIN*/ )
336cdf0e10cSrcweir {
337cdf0e10cSrcweir EndDialog( RET_OK );
338cdf0e10cSrcweir return 0L;
339cdf0e10cSrcweir }
340cdf0e10cSrcweir
341cdf0e10cSrcweir //--------------------------------------------------------------------
342cdf0e10cSrcweir IMPL_LINK( DirectSQLDialog, OnExecute, void*, /*NOTINTERESTEDIN*/ )
343cdf0e10cSrcweir {
344cdf0e10cSrcweir executeCurrent();
345cdf0e10cSrcweir return 0L;
346cdf0e10cSrcweir }
347cdf0e10cSrcweir
348cdf0e10cSrcweir //--------------------------------------------------------------------
349cdf0e10cSrcweir IMPL_LINK( DirectSQLDialog, OnListEntrySelected, void*, /*NOTINTERESTEDIN*/ )
350cdf0e10cSrcweir {
351cdf0e10cSrcweir if (!m_pSQLHistory->IsTravelSelect())
352cdf0e10cSrcweir {
353cdf0e10cSrcweir const sal_uInt16 nSelected = m_pSQLHistory->GetSelectEntryPos();
354cdf0e10cSrcweir if (LISTBOX_ENTRY_NOTFOUND != nSelected)
355cdf0e10cSrcweir switchToHistory(nSelected, sal_False);
356cdf0e10cSrcweir }
357cdf0e10cSrcweir return 0L;
358cdf0e10cSrcweir }
359cdf0e10cSrcweir
360cdf0e10cSrcweir //........................................................................
361cdf0e10cSrcweir } // namespace dbaui
362cdf0e10cSrcweir //........................................................................
363cdf0e10cSrcweir
364