Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RDBCform.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RDBCform.C
1 // $Id: RDBCform.C,v 1.1.1.1 2004/02/18 20:58:02 dave Exp $
2 //
3 // This file is part of the RDBC
4 // Author: Valeriy Onuchin <onuchin@sirius.ihep.su>
6 //
7 // An example of Simple Form used for browsing of tables
8 //
10 //
11 // Usage:
12 //
13 // root[0] gSystem->Load("libRDBC"); // load library
14 // root[1] .L RDBCform.C // load macro
15 // or
16 // root[1] .L RDBCform.C++ // compile and load macro
17 //
18 // root[2] new DbForm(); // create form
19 //
21 //
22 // To compile standalone executable program use:
23 //
24 // 1. Compile shared lib module RDBCform_C.so by:
25 //
26 // root[0] gSystem->Load("libRDBC");
27 // root[1] .L RDBCform.C++
28 //
29 // 2. Comiple executable by:
30 //
31 // $ g++ -O -o RDBCform RDBCform.C `root-config --cflags --glibs` -lRDBC -DSTANDALONE
32 //
33 // Check the main() code at the bottom of this script
34 //
35 // 3. Run it (RDBCform_C.so should be in current dir):
36 //
37 // $ ./RDBCform&
38 //
40 //
41 // Warnings:
42 //
43 // - this macro uses TGxxx classes which don't work with win32
44 // - some tables ( for example Oracle system tables) can
45 // be browsed only with forward_only cursor.
46 // Use DbForm::SetForwardOnly() method to force this
47 // setting.
48 //
49 //Begin_Html
50 /*
51 <img src="oform.gif">
52 */
53 //End_Html
54 
55 #include <RDBC/TSQLConnection.h>
56 #include <RDBC/TSQLStatement.h>
57 #include <RDBC/TSQLDriverManager.h>
58 #include <RDBC/TSQLResultSet.h>
61 
62 #include <TTimer.h>
63 #include <TGCanvas.h>
64 #include <TGToolBar.h>
65 #include <TGStatusBar.h>
66 #include <TGMsgBox.h>
67 #include <TVirtualX.h>
68 #include <TROOT.h>
69 #include <TInterpreter.h>
70 #include <TGMenu.h>
71 #include <TG3DLine.h>
72 #include <RQ_OBJECT.h>
73 #include <TSystem.h>
74 #include <TGTextEntry.h>
75 #include <TGButton.h>
76 #include <TGFrame.h>
77 #include <TGLabel.h>
78 
79 
80 class DbForm* gForm = 0; // db form available from interpreter
81 
83 //
84 // Password Dialog widget used for entering
85 // Data Source Name, User Name and Password
86 //
89 {
90 RQ_OBJECT()
91 
92 private:
93  TGMainFrame* fMain;
94  TGTextEntry* fDSNEntry; // data source name text entry
95  TGTextEntry* fUserEntry; // username text entry
96  TGTextEntry* fPswdEntry; // password text entry
97  TGTextButton* fOKButton; // OK button
98  TGTextButton* fCancelButton; // Cancel button
99  TGLabel* fDSNLabel;
100  TGLabel* fPswLabel;
101  TGLabel* fUsrLabel;
102  TGLabel* fInfoLabel;
103  TString fDSN; // last entered DSN
104  TString fUsername; // last entered username
105  TString fPassword; // last enterd password
106 
107 public:
108  PasswordDialog();
109  ~PasswordDialog();
110 
111  void Hide();
112  void Show();
113  void Clear();
114 
115  TGMainFrame* GetMainFrame() const { return fMain; }
116  TString GetDSN() const { return fDSN; }
117  TString GetUsername() const { return fUsername; }
118  TString GetPassword() const { return fPassword; }
119 
120  void Print(Option_t* opt="") const;
121  void Entered( const Text_t* dsn=0,
122  const Text_t* pwd=0,
123  const Text_t* usr=0 ); // *SIGNAL*
124 
125  // slots
126  void DSNEntered();
127  void PasswordEntered();
128  void UsernameEntered();
129 };
130 
131 //____________________________________________________________________
133 {
134  // constructor
135 
136  fMain = new TGMainFrame(gClient->GetRoot(),500,200);
137  fMain->SetWMSizeHints(500,200,500,200,0,0);
138  fMain->SetWindowName("Enter DSN, Password, User Name");
139  fMain->SetIconName("Enter DSN, Password ,User Name");
140 
141  // create username text entry
142  fDSNEntry = new TGTextEntry(fMain,"");
143  fDSNEntry->Resize(350,20);
144  fDSNEntry->Move(100,50);
145  fDSNLabel = new TGLabel(fMain,"DSN:");
146  fDSNLabel->Move(30,52);
147 
148  fUserEntry = new TGTextEntry(fMain,"");
149  fUserEntry->Resize(350,20);
150  fUserEntry->Move(100,90);
151  fUserEntry->SetMaxLength(20); // maximum 20 symbols
152  fUsrLabel = new TGLabel(fMain,"User Name:");
153  fUsrLabel->Move(30,92);
154 
155  // create password text entry
156  fPswdEntry = new TGTextEntry(fMain,"");
157  fPswdEntry->Resize(350,20);
158  fPswdEntry->Move(100,130);
159  fPswdEntry->SetMaxLength(20); // maximum 20 symbols
160 
161  // set password echo mode
162  fPswdEntry->SetEchoMode(TGTextEntry::kPassword);
163 
164  fPswLabel = new TGLabel(fMain,"Password:");
165  fPswLabel->Move(30,132);
166 
167  fInfoLabel = new TGLabel(fMain,
168  "Type DSN, Username and Password to log on");
169 
170  fInfoLabel->Move(30,20);
171 
172  fOKButton = new TGTextButton(fMain,"OK");
173  fOKButton->Move(200,170);
174  fOKButton->Resize(50,20);
175 
176  fCancelButton = new TGTextButton(fMain,"Cancel");
177  fCancelButton->Move(300,170);
178  fCancelButton->Resize(50,20);
179 
180  fMain->MapSubwindows();
181  fMain->MapRaised();
182  fMain->SetWMPosition(300,300);
183  fMain->ChangeOptions(fMain->GetOptions() | kFixedSize);
184 
185  //*** Make connections between signals and handlers
186 
187  fDSNEntry->Connect("ReturnPressed()","PasswordDialog",
188  this,"DSNEntered");
189 
190  // Connects TGTextEntry's signal "ReturnPressed" from
191  // fUserEntry to PasswordDialog::UsernameEntered() method
192  // of "this" object
193 
194  fUserEntry->Connect("ReturnPressed()","PasswordDialog",
195  this,"UsernameEntered");
196 
197  // Connects TGTextEntry's signal "ReturnPressed" from
198  // fPswdEntry to PasswordDialog::UsernameEntered() method
199  // of "this" object.
200 
201  fPswdEntry->Connect( "ReturnPressed()","PasswordDialog",
202  this,"PasswordEntered()");
203 
204  fCancelButton->Connect("Pressed()","PasswordDialog",
205  this,"Hide()");
206 
207  fCancelButton->Connect("Pressed()","PasswordDialog",
208  this,"Clear()");
209 
210  fOKButton->Connect("Released()","PasswordDialog",
211  this,"PasswordEntered()");
212 
213  fDSNEntry->Connect("CursorOutDown()","TGTextEntry",fUserEntry,"SetFocus()");
214  fUserEntry->Connect("CursorOutDown()","TGTextEntry",fPswdEntry,"SetFocus()");
215  fUserEntry->Connect("CursorOutUp()","TGTextEntry",fDSNEntry,"SetFocus()");
216  fPswdEntry->Connect("CursorOutUp()","TGTextEntry",fUserEntry,"SetFocus()");
217 }
218 
219 //____________________________________________________________________
221 {
222  //
223 
224  fMain->MapRaised();
225 }
226 
227 //____________________________________________________________________
229 {
230  //
231 
232  fMain->UnmapWindow();
233 }
234 
235 //____________________________________________________________________
237 {
238  //
239 
240  fDSNEntry->Clear();
241  fPswdEntry->Clear();
242  fUserEntry->Clear();
243 }
244 
245 //____________________________________________________________________
247 {
248  // destructor
249 
250  delete fDSNEntry;
251  delete fDSNLabel;
252  delete fUserEntry;
253  delete fPswdEntry;
254  delete fPswLabel;
255  delete fUsrLabel;
256  delete fInfoLabel;
257  delete fOKButton;
258  delete fCancelButton;
259  delete fMain;
260 }
261 
262 //____________________________________________________________________
263 void PasswordDialog::Entered(const Text_t* dsn,const Text_t* usr,
264  const Text_t* pwd)
265 {
266  // This signal is emitted when username and password entered.
267 
268  TString DSN = dsn;
269  TString USR = usr;
270  TString PWD = pwd;
271 
272  // User can add some validation procedure below
273  if( DSN==fDSN && USR==fUsername && PWD==fPassword ) return;
274  if( !DSN.IsNull() && !USR.IsNull() ) {
275 
276  long args[3];
277 
278  args[0] = (long)DSN.Data();
279  args[1] = (long)USR.Data();
280  args[2] = (long)PWD.Data();
281 
282  fDSN = DSN; // copy dsn
283  fUsername = USR; // copy username
284  fPassword = PWD; // copy password
285 
286  Emit("Entered(Text_t*,Text_t*,Text_t*)",args);
287  }
288 }
289 
290 //____________________________________________________________________
292 {
293  // Handles ReturnPressed signal from dsn text entry.
294  // ... just move keyboard focus to username text entry.
295 
296  // move keyboard focus to password text entry
297  fUserEntry->SetFocus();
298 
299  // select text in the password text entry
300  fUserEntry->SelectAll();
301 }
302 
303 //____________________________________________________________________
305 {
306  // Handles ReturnPressed signal from username text entry.
307  // ... just move keyboard focus to password text entry.
308 
309  // move keyboard focus to password text entry
310  fPswdEntry->SetFocus();
311 
312  // select text in the password text entry
313  fPswdEntry->SelectAll();
314 }
315 
316 //____________________________________________________________________
318 {
319  // Handles ReturnPressed signal from password text entry
320 
321  TString dsn = fDSNEntry->GetText();
322  TString usr = fUserEntry->GetText();
323  TString pwd = fPswdEntry->GetText();
324 
325  Clear();
326  fDSNEntry->SetFocus(); // move back keyboard focus to username entry
327  Entered(dsn.Data(),usr.Data(),pwd.Data()); // emit signal
328  fDSN="";
329  fUsername="";
330  fPassword="";
331 }
332 
333 //____________________________________________________________________
334 void PasswordDialog::Print(Option_t*) const
335 {
336  // Prints entered dsn, username and password
337 
338  printf("\nDSN: %s",fDSN.Data());
339  printf("\nUsername: %s\n",fUsername.Data());
340  printf("Password: %s\n",fPassword.Data());
341 }
342 
345 {
346 RQ_OBJECT()
347 
348 private:
349  TGLabel* fLabel; // column name
350  TGTextEntry* fEntry; // column value as text
351  TGCompositeFrame* fMain; //
352  TGLayoutHints* fLayoutHints1; // hints for text
353  TGLayoutHints* fLayoutHints2; // hints for label
354  Int_t fColumnIndex; // column index
355  TString fColumnName; // column name
356  TString fColumnValue; // column value
357 
358 public:
359  DbFormEntry(TGWindow* p,Int_t index,
360  const TString& value, const TString& name);
361  ~DbFormEntry();
362 
363  void Updated(Int_t index,const Text_t* value); //*SIGNAL*
364  void SetColumnValue(const TString& value)
365  { fColumnValue = value; fEntry->SetText(value.Data()); }
366 
367  TGCompositeFrame* GetMain() const { return fMain; }
368 };
369 
370 //____________________________________________________________________
372  const TString& value,const TString& name)
373 {
374  // ctor
375 
376  fMain = new TGCompositeFrame(p,32, 32,kHorizontalFrame);
377  fLayoutHints1 = new TGLayoutHints( kLHintsRight | kLHintsExpandX,
378  5, 100, 5, 5);
379  fLayoutHints2 = new TGLayoutHints( kLHintsNormal,
380  100, 5, 5, 5);
381 
384  fColumnName = name;
385 
386  fLabel = new TGLabel(fMain,fColumnName.Data());
387  fEntry = new TGTextEntry(fMain,fColumnValue.Data());
388  fEntry->SetEnabled(kFALSE); // disable
389  fMain->AddFrame(fLabel,fLayoutHints2);
390  fMain->AddFrame(fEntry,fLayoutHints1);
391 
392  fEntry->Connect("ReturnPressed()","DbFormEntry",this,
393  "Updated(Int_t,Text_t*)");
394 
395 }
396 
397 //____________________________________________________________________
399 {
400  // dtor with sanity check
401 
402  if(fLabel) fLabel->UnmapWindow();
403  if(fEntry) fEntry->UnmapWindow();
404  if(fLabel) delete fLabel;
405  if(fEntry) delete fEntry;
406  if(fLayoutHints1) delete fLayoutHints1;
407  if(fLayoutHints2) delete fLayoutHints2;
408  if(fMain) delete fMain;
409 }
410 
411 //____________________________________________________________________
412 void DbFormEntry::Updated(Int_t index,const Text_t* value)
413 {
414  // emit signal when text changed in the text entry
415  // (not used so far)
416 
417  long args[2];
418 
419  TString newValue = fEntry->GetText();
420 
421  if(fColumnValue!=newValue) {
422  args[0] = (long)fColumnIndex;
423  args[1] = (long)newValue.Data();
424  Emit("Updated(Int_t,Text_t*)",args);
425  }
426 }
427 
429 
431 class DbForm
432 {
433 RQ_OBJECT()
434 
435 private:
438  enum EHelp { kAbout };
439 
440  // GUI part
441  TGMainFrame* fMain; // main frame
442  TGPopupMenu* fFileMenu; // file popup menu
443  TGPopupMenu* fGotoMenu; // go popup menu
444  TGPopupMenu* fHelpMenu; // help menu
445  TGTextEntry* fQueryTextEntry; // SQL query text entry
446  TGTextButton* fExecuteQueryButton; // excute query button
447  TGTextButton* fCancelQueryButton; // cancel query button
448  TGTextButton* fNextButton; // next button
449  TGTextButton* fPrevButton; // prev button
450  TGTextButton* fLastButton; // last button
451  TGTextButton* fFirstButton; // first button
452  TTimer* fNextButtonTimer; // auotorepeat timer for next button
453  TTimer* fPrevButtonTimer; // auotorepeat timer for prev button
454  TGCompositeFrame* fButtonGroup; // button group for next,prev,first,last bttns
455  TGStatusBar* fStatusBar; // status bar
456  TGMenuBar* fMenuBar; // menu bar
457  TGLayoutHints* fMenuBarLayout; // layout hints
458  TGLayoutHints* fMenuBarItemLayout; // layout hints
459  TGLayoutHints* fMenuBarHelpLayout; // layout hints
460  TGHorizontal3DLine* fToolBarSep; // separator
461  TGToolBar* fToolBar; // composite rame for execute query text entry and bttns
462  TGLayoutHints* fBarLayout; // layout hints
463  TGLayoutHints* fQueryTextLayout; // layout for query text entry
464  TGCanvas* fCanvasWindow; // window with sliders
465  TGCompositeFrame* fContainer; //
466  DbFormEntry** fEntries; // form entries
467  PasswordDialog* fPD; // password dialog used to enter dsn,uid,pwd
468 
469  // db part
470  Bool_t fConnected; // kTRUE if connected to db
471  TString fDSN; // dsn name
472  Int_t fRstype; // result set type
473  Int_t fRsconc; // result set concurrency
474  TSQLConnection* fConnection; // connection to db
475  TSQLStatement* fStatement; // latest statement
476  TSQLResultSet* fResultSet; // latest result set
477  TSQLResultSetMetaData* fResultSetMetaData; // result set meta data
478 
479  void DisplayResults();
480  void UpdateColumn(Int_t index,const Text_t* value);
481  void ClearCanvas();
482 
483 public:
484  DbForm();
485  ~DbForm();
486 
488  void SetForwardOnly();
492 
493  void Hide() { fMain->UnmapWindow(); }
494  void Show() { fMain->MapRaised(); }
495  TGMainFrame* GetMainFrame() const { return fMain; }
496 
497  // slots
498  void HandleFileMenu(Int_t id);
499  void HandleGotoMenu(Int_t id);
500  void HandleDisconnect();
501  void HandleConnect();
502  void HandleExecuteQuery();
503  void First();
504  void Last();
505  void Next();
506  void Prev();
507  void AcceleratePrevButton();
508  void AccelerateNextButton();
509  void Connect(const Text_t* dsn, const Text_t* uid, const Text_t* pwd);
510 
511 };
512 
514 //____________________________________________________________________
516 {
517  // constructor
518 
519  TGLayoutHints *lo = 0;
520 
521  fConnection = 0; // connection to db
522  fStatement = 0; // latest statement
523  fResultSet = 0; // latest result set
524  fResultSetMetaData = 0; // result set meta data
525  fConnected = kFALSE;
526 
527  if(gForm) gInterpreter->DeleteGlobal(gForm); //delete gForm;
528 
529  fMain = new TGMainFrame(gClient->GetRoot(),500,500);
530  fMain->SetWMSizeHints(500,500,500,500,0,0);
531  fMain->SetWindowName("Simple Database Form");
532  fMain->SetIconName("Simple of Database Form");
533  fMain->SetClassHints("DbForm", "DbForm");
534  fMain->SetMWMHints(kMWMDecorAll, kMWMFuncAll, kMWMInputModeless);
535 
536  fFileMenu = new TGPopupMenu(gClient->GetRoot());
537  fFileMenu->AddEntry("&Connect",kConnect);
538  fFileMenu->AddEntry("&Disconnect",kDisconnect);
539  fFileMenu->AddSeparator();
540  fFileMenu->AddEntry("&Exit",kExit);
541 
542  fGotoMenu = new TGPopupMenu(gClient->GetRoot());
543  fGotoMenu->AddEntry("&First",kFirst);
544  fGotoMenu->AddEntry("&Prev",kPrev);
545  fGotoMenu->AddEntry("&Next",kNext);
546  fGotoMenu->AddEntry("&Last",kLast);
547 
548  fHelpMenu = new TGPopupMenu(gClient->GetRoot());
549  fHelpMenu->AddEntry("&About",kAbout);
550 
551  fMenuBarLayout = new TGLayoutHints( kLHintsTop |
552  kLHintsLeft |
553  kLHintsExpandX,5,5,3,1);
554 
555  fMenuBarItemLayout = new TGLayoutHints(kLHintsTop |
556  kLHintsLeft,0,20,3,1);
557 
558  fMenuBarHelpLayout = new TGLayoutHints(kLHintsTop |
559  kLHintsRight,0,5,3,1);
560 
561  fMenuBar = new TGMenuBar(fMain,1,1,kHorizontalFrame);
562  fMenuBar->AddPopup("&File",fFileMenu, fMenuBarItemLayout);
563  fMenuBar->AddPopup("&Go",fGotoMenu, fMenuBarItemLayout);
564  fMenuBar->AddPopup("&Help",fHelpMenu, fMenuBarHelpLayout);
565  fMain->AddFrame(fMenuBar, fMenuBarLayout);
566 
567  fToolBarSep = new TGHorizontal3DLine(fMain);
568  fBarLayout = new TGLayoutHints(kLHintsTop | kLHintsExpandX);
569  fMain->AddFrame(fToolBarSep,fBarLayout);
570 
571  fToolBar = new TGToolBar(fMain,60,20, kHorizontalFrame);
572  fMain->AddFrame(fToolBar,fBarLayout);
573 
574  fQueryTextEntry = new TGTextEntry(fToolBar,"");
576 
577  fExecuteQueryButton = new TGTextButton(fToolBar,"Query");
578  fCancelQueryButton = new TGTextButton(fToolBar,"Cancel");
579 
582 
583  fStatusBar = new TGStatusBar(fMain, 60, 10);
584  int parts[] = { 25, 75 };
585  fStatusBar->SetParts(parts, 2);
586  lo = new TGLayoutHints(kLHintsBottom | kLHintsExpandX, 0, 0, 3, 0);
587  fMain->AddFrame(fStatusBar,lo);
588  fButtonGroup = new TGCompositeFrame(fMain,60,10,kHorizontalFrame);
589  fMain->AddFrame(fButtonGroup,lo);
590 
591  fFirstButton = new TGTextButton(fButtonGroup,"First",kFirst);
592  fPrevButton = new TGTextButton(fButtonGroup,"Prev",kPrev);
593  fNextButton = new TGTextButton(fButtonGroup,"Next",kNext);
594  fLastButton = new TGTextButton(fButtonGroup,"Last",kLast);
595 
600 
601  fCanvasWindow = new TGCanvas(fMain, 200, 400);
602  lo = new TGLayoutHints( kLHintsBottom |
603  kLHintsExpandX |
604  kLHintsExpandY, 3, 3, 3, 3);
605  fMain->AddFrame(fCanvasWindow,lo);
606 
607  fContainer = new TGCompositeFrame(fCanvasWindow->GetViewPort(), 10, 10,
608  kHorizontalFrame);
609 
610  fContainer->SetLayoutManager(new TGVerticalLayout(fContainer));
611  fCanvasWindow->SetContainer(fContainer);
612 
613  fMain->MapSubwindows();
614  fMain->SetWMPosition(300,300);
615  fMain->Layout();
616  fMain->MapRaised();
617 
618  TSQL::SetHandler("Catch(TSQLException*)");
620 
621  // connect signals
622  TQObject::Connect(fCancelQueryButton,"Pressed()","TGTextEntry",
623  fQueryTextEntry,"Clear()");
624 
625  fFileMenu->Connect("Activated(Int_t)","DbForm",this,
626  "HandleFileMenu(Int_t)");
627 
628  fGotoMenu->Connect("Activated(Int_t)","DbForm",this,
629  "HandleGotoMenu(Int_t)");
630 
631  fExecuteQueryButton->Connect("Released()","DbForm",this,
632  "HandleExecuteQuery()");
633 
634  fQueryTextEntry->Connect("ReturnPressed()","DbForm",this,
635  "HandleExecuteQuery()");
636 
637  fNextButtonTimer = new TTimer(250);
638  fPrevButtonTimer = new TTimer(250);
639 
640  fFirstButton->Connect("Released()","DbForm",this,"First()");
641  fLastButton->Connect("Released()","DbForm",this,"Last()");
642 
643  // create accelerating autorepeat effect for Next/Prev buttons
644  fNextButton->Connect("Pressed()","TTimer",fNextButtonTimer,"TurnOn()");
645  fNextButton->Connect("Released()","TTimer",fNextButtonTimer,"TurnOff()");
646  fNextButton->Connect("Released()","TTimer",fNextButtonTimer,"SetTime(=250)");
647  fNextButtonTimer->Connect("Timeout()","DbForm",this,"Next()"); //
648  fNextButtonTimer->Connect("Timeout()","DbForm",this,"AccelerateNextButton()"); //
649 
650  fPrevButton->Connect("Pressed()","TTimer",fPrevButtonTimer,"TurnOn()");
651  fPrevButton->Connect("Released()","TTimer",fPrevButtonTimer,"TurnOff()");
652  fPrevButton->Connect("Released()","TTimer",fPrevButtonTimer,"SetTime(=250)");
653  fPrevButtonTimer->Connect("Timeout()","DbForm",this,"Prev()");
654  fPrevButtonTimer->Connect("Timeout()","DbForm",this,"AcceleratePrevButton()"); //
655 
656  gForm = this;
657  fPD = 0;
658 }
659 
660 //____________________________________________________________________
662 {
663  // accelerator
664 
665  Long_t nt = fNextButtonTimer->GetTime();
666  fNextButtonTimer->SetTime(nt-10);
667 }
668 
669 //____________________________________________________________________
671 {
672  // accelerator
673 
674  Long_t pt = fPrevButtonTimer->GetTime();
675  fPrevButtonTimer->SetTime(pt-10);
676 }
677 
678 //____________________________________________________________________
680 {
681  // destructor
682 
683  delete fNextButtonTimer;
684  delete fPrevButtonTimer;
685  delete fContainer;
686  delete fCanvasWindow;
687  delete fQueryTextLayout;
688  delete fBarLayout;
689  delete fToolBar;
690  delete fToolBarSep;
691  delete fMenuBarHelpLayout;
692  delete fMenuBarItemLayout;
693  delete fMenuBarLayout;
694  delete fMenuBar;
695  delete fStatusBar;
696  delete fButtonGroup;
697  delete fFirstButton;
698  delete fLastButton;
699  delete fPrevButton;
700  delete fNextButton;
701  delete fCancelQueryButton;
702  delete fExecuteQueryButton;
703  delete fQueryTextEntry;
704  delete fHelpMenu;
705  delete fGotoMenu;
706  delete fFileMenu;
708  delete fPD;
709  delete fMain;
710 }
711 
712 //____________________________________________________________________
713 void DbForm::UpdateColumn(Int_t index,const Text_t* value)
714 {
715  // handler method used when column was updated
716  // (not used yet)
717 
718  TString str=value;
719 
720  if( fResultSet &&
721  !fResultSet->IsBeforeFirst() &&
722  !fResultSet->IsAfterLast() ) fResultSet->UpdateString(index,str);
723 }
724 
725 //____________________________________________________________________
727 {
728  // scroll to first row
729 
730  fResultSet->First();
731  DisplayResults();
732 }
733 
734 //____________________________________________________________________
736 {
737  // scroll to last row
738 
739  fResultSet->Last();
740  DisplayResults();
741 }
742 
743 //____________________________________________________________________
745 {
746  // scroll to next row
747 
748  fResultSet->Next();
749  DisplayResults();
750 }
751 
752 //____________________________________________________________________
754 {
755  // scroll to previous row
756 
757  fResultSet->Previous();
758  DisplayResults();
759 }
760 
761 //____________________________________________________________________
763 {
764  // display new row
765 
766  char rowstr[15];
767  Int_t row = fResultSet->GetRow();
768  sprintf(rowstr,"Row: %d",row);
769  fStatusBar->SetText(rowstr,0);
770 
771  if(fResultSet->IsAfterLast()) {
772  fGotoMenu->DisableEntry(kNext);
773  fNextButton->SetState(kButtonDisabled);
774  } else {
775  fGotoMenu->EnableEntry(kNext);
776  if(fNextButton->GetState()==kButtonDisabled) fNextButton->SetState(kButtonUp);
777  }
778 
779  if(fResultSet->IsBeforeFirst()) {
780  fGotoMenu->DisableEntry(kPrev);
781  fPrevButton->SetState(kButtonDisabled);
782  } else {
783  fGotoMenu->EnableEntry(kPrev);
784  if(fPrevButton->GetState()==kButtonDisabled) fPrevButton->SetState(kButtonUp);
785  }
786 
788 
789  Int_t ncollumns = fResultSetMetaData->GetColumnCount();
790 
791  TString value;
792 
793  for( int i=1; i <= ncollumns; ++i ) {
795  value = fResultSet->GetString(i);
796  } else {
797  value = "";
798  }
799  fEntries[i-1]->SetColumnValue(value);
800  }
801 }
802 
803 //____________________________________________________________________
805 {
806  // handler for File menu popup actions
807 
808  switch(id) {
809  case kConnect:
810  fConnected = kFALSE;
811 
812  if(!fPD) {
813  fPD = new PasswordDialog();
814  fPD->Connect("Entered(Text_t*,Text_t*,Text_t*)",
815  "DbForm",this,
816  "Connect(Text_t*,Text_t*,Text_t*)");
817  } else {
818  fPD->Show();
819  }
820  break;
821  case kDisconnect:
823  break;
824  case kExit:
826  gSystem->Exit(0);
827  break;
828  default:
829  break;
830  }
831 }
832 
833 //____________________________________________________________________
835 {
836  // handler for Go menu popup actions
837 
838  switch(id) {
839  case kFirst:
840  First();
841  break;
842  case kLast:
843  Last();
844  break;
845  case kPrev:
846  Prev();
847  break;
848  case kNext:
849  Next();
850  break;
851  default:
852  break;
853  }
854 }
855 
856 //____________________________________________________________________
858 {
859  // clear canvas
860 
861  Int_t ncollumns;
862 
863  if(fResultSetMetaData) { // clear canvas window
864  TGCompositeFrame* cont = (TGCompositeFrame*)fCanvasWindow->GetContainer();
865  TList* list = cont->GetList();
866  ncollumns = fResultSetMetaData->GetColumnCount();
867 
868  for( int i=1; i <= ncollumns; ++i ) {
869  delete fEntries[i-1];
870  }
871 
872  list->Clear();
873  cont->MapSubwindows();
874  cont->Layout();
875  }
876 }
877 
878 //____________________________________________________________________
880 {
881  // handler for use to execute SQL query
882 
883  TString sql = fQueryTextEntry->GetText();
884  Int_t ncollumns;
885  char rowstr[15];
886 
887  if(sql.IsNull()) return; // validate sql
888 
889  ClearCanvas();
890 
891  if(fStatement) delete fStatement;
893 
894  if(!fStatement) {
895  return;
896  }
897 
899 
900  if(fResultSet) {
901  fStatusBar->SetText(sql.Data(),1);
902  fResultSetMetaData = fResultSet->GetMetaData(); // new metadata
903  if(fRstype != kTYPE_FORWARD_ONLY) fGotoMenu->EnableEntry(kFirst);
904  if(fRstype != kTYPE_FORWARD_ONLY) fGotoMenu->EnableEntry(kPrev);
905  fGotoMenu->EnableEntry(kNext);
906  fGotoMenu->EnableEntry(kLast);
907  if(fRstype != kTYPE_FORWARD_ONLY) fFirstButton->SetState(kButtonUp);
908  if(fRstype != kTYPE_FORWARD_ONLY) fPrevButton->SetState(kButtonUp);
909  fNextButton->SetState(kButtonUp);
910  fLastButton->SetState(kButtonUp);
911 
912  Int_t row = fResultSet->GetRow();
913  sprintf(rowstr,"Row: %d",row);
914  fStatusBar->SetText(rowstr,0);
915 
916  ncollumns = fResultSetMetaData->GetColumnCount();
917  Int_t type;
918 
919  fEntries = new DbFormEntryPtr[ncollumns];
920 
921  for( int i=1; i <= ncollumns; ++i ) {
923  fEntries[i-1] = new DbFormEntry(fCanvasWindow->GetContainer(),i,
924  TString(""),fResultSetMetaData->GetColumnName(i));
925 
926  fCanvasWindow->AddFrame(fEntries[i-1]->GetMain(), fMenuBarLayout);
927 
928 // fEntries[i-1]->Connect("Updated(Int_t,Text_t*)",
929 // "DbForm",this,"UpdateColumn(Int_t,Text_t*)");
930  }
931 
933  fMain->MapSubwindows();
934  fMain->Layout();
935  } else {
936  sql = "Failed to execute " + sql;
937  fStatusBar->SetText(sql.Data(),1);
938  fGotoMenu->DisableEntry(kFirst);
939  fGotoMenu->DisableEntry(kPrev);
940  fGotoMenu->DisableEntry(kNext);
941  fGotoMenu->DisableEntry(kLast);
942  fFirstButton->SetState(kButtonDisabled);
943  fPrevButton->SetState(kButtonDisabled);
944  fNextButton->SetState(kButtonDisabled);
945  fLastButton->SetState(kButtonDisabled);
946  fQueryTextEntry->SetFocus();
947  fStatusBar->SetText("",0);
948  }
949 }
950 
951 //____________________________________________________________________
953 {
954  // handler for disconnect from db
955 
956  ClearCanvas();
958  fConnection = 0;
959  fConnected = kFALSE;
960  fGotoMenu->DisableEntry(kFirst);
961  fGotoMenu->DisableEntry(kPrev);
962  fGotoMenu->DisableEntry(kNext);
963  fGotoMenu->DisableEntry(kLast);
964  fFirstButton->SetState(kButtonDisabled);
965  fPrevButton->SetState(kButtonDisabled);
966  fNextButton->SetState(kButtonDisabled);
967  fLastButton->SetState(kButtonDisabled);
968  fExecuteQueryButton->SetState(kButtonDisabled);
969  fCancelQueryButton->SetState(kButtonDisabled);
970  fFileMenu->DisableEntry(kDisconnect);
971  fQueryTextEntry->SetEnabled(kFALSE);
972  fQueryTextEntry->Clear();
973  fStatusBar->SetText("Disconnected",1);
974  fStatusBar->SetText("",0);
976  fResultSet=0;
977  fStatement=0;
978 }
979 
980 //____________________________________________________________________
982 {
983  // handler used to connect to db
984 
985  fExecuteQueryButton->SetState(kButtonUp);
986  fCancelQueryButton->SetState(kButtonUp);
987  fFileMenu->EnableEntry(kDisconnect);
988 
989  TString info = "Connected to DSN - ";
990  info += fDSN;
991  fStatusBar->SetText(info.Data(),1);
992 
994 
999  } else {
1000  SetForwardOnly();
1001  }
1002 
1005  } else {
1007  }
1008 
1009  fQueryTextEntry->SetEnabled(kTRUE);
1010  fQueryTextEntry->SetFocus();
1011 }
1012 
1013 //____________________________________________________________________
1014 void DbForm::Connect(const Text_t* dsn,const Text_t* uid,const Text_t* pwd)
1015 {
1016  // password dialog
1017 
1018  HandleDisconnect();
1019 
1021  fConnected = fConnection != 0;
1022 
1023  if(fConnected) {
1024  fDSN = dsn;
1025  fPD->Hide();
1026  HandleConnect();
1027  }
1028 }
1029 
1030 //____________________________________________________________________
1032 {
1033  //
1034 
1036  fPrevButton->SetState(kButtonDisabled);
1037  fFirstButton->SetState(kButtonDisabled);
1038  fLastButton->SetState(kButtonDisabled);
1039  fGotoMenu->DisableEntry(kFirst);
1040  fGotoMenu->DisableEntry(kPrev);
1041  fGotoMenu->DisableEntry(kLast);
1042 }
1043 
1044 //____________________________________________________________________
1046 {
1047  // exception handling function
1048  // can be switched off by TSQL::UnsetHandler()
1049 
1050  EMsgBoxIcon icontype = kMBIconExclamation;
1051  Int_t buttons = 0;
1052  Int_t retval;
1053 
1054  TString str = e->GetMessage();
1055  TGMsgBox* errb;
1056 
1057  if(gForm) {
1058  errb = new TGMsgBox(gClient->GetRoot(),gForm->GetMainFrame(),
1059  "Error", str.Data(),icontype, buttons, &retval);
1060  } else {
1061  printf("%s\n",str.Data());
1062  }
1063 }
1064 
1066 void RDBCform()
1067 {
1068  new DbForm();
1069 }
1070 
1072 #ifdef STANDALONE
1073 
1074 #include <TROOT.h>
1075 #include <TApplication.h>
1076 #include <TSystem.h>
1077 #include <TInterpreter.h>
1078 
1079 //---- Main program ------------------------------------------------------------
1080 
1081 TROOT root("RDBCform", "GUI form for table browsing");
1082 
1083 int main(int argc, char **argv)
1084 {
1085  TApplication theApp("App", &argc, argv);
1086 
1087  if (gROOT->IsBatch()) {
1088  fprintf(stderr, "%s: cannot run in batch mode\n", argv[0]);
1089  return 1;
1090  }
1091  gDebug =1;
1092  gSystem->Load("libRDBC");
1093  gSystem->Load("./RDBCform_C");
1094  gInterpreter->ProcessLine("gForm = new DbForm();");
1095  theApp.Run();
1096  return 0;
1097 }
1098 #endif