home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 10 / ioProg_10.iso / soft / optima / samples.z / SQLAnyUtil.wxc < prev    next >
Encoding:
Text File  |  1996-09-17  |  9.0 KB  |  320 lines

  1. Save Format v1.3
  2. @begin ClassFile "SQLAnyUtil"
  3.  Exported 0;
  4.  
  5.  @begin UserFunction "SQLAnyUtil()"
  6.   GencodeSrcLine 15;
  7.   FunctionName "SQLAnyUtil::SQLAnyUtil()";
  8.  @end;
  9.  
  10.  @begin UserFunction "Prototype for SQLAnyUtil()"
  11.   Private 1;
  12.   GencodeSrcLine 22;
  13.   FunctionName "SQLAnyUtil::Prototype for SQLAnyUtil()";
  14.  @end;
  15.  
  16.  @begin UserFunction "~SQLAnyUtil()"
  17.   GencodeSrcLine 19;
  18.   FunctionName "SQLAnyUtil::~SQLAnyUtil()";
  19.  @end;
  20.  
  21.  @begin UserFunction "Prototype for ~SQLAnyUtil()"
  22.   Private 1;
  23.   GencodeSrcLine 24;
  24.   FunctionName "SQLAnyUtil::Prototype for ~SQLAnyUtil()";
  25.  @end;
  26.  
  27.  @begin UserFunction "DataSourceExists( WString const & source )"
  28.   GencodeSrcLine 23;
  29.   FunctionName "SQLAnyUtil::DataSourceExists( WString const & source )";
  30.  @end;
  31.  
  32.  @begin UserFunction "Prototype for DataSourceExists( WString const & source )"
  33.   Private 1;
  34.   GencodeSrcLine 26;
  35.   FunctionName "SQLAnyUtil::Prototype for DataSourceExists( WString const & source )";
  36.  @end;
  37.  
  38.  @begin UserFunction "DataSourceAdd( WString const & name, WString const & dbfile )"
  39.   GencodeSrcLine 52;
  40.   FunctionName "SQLAnyUtil::DataSourceAdd( WString const & name, WString const & dbfile )";
  41.  @end;
  42.  
  43.  @begin UserFunction "Prototype for DataSourceAdd( WString const & name, WString const & dbfile )"
  44.   Private 1;
  45.   GencodeSrcLine 28;
  46.   FunctionName "SQLAnyUtil::Prototype for DataSourceAdd( WString const & name, WString const & dbfile )";
  47.  @end;
  48.  
  49.  @begin UserFunction "GetSampleDBPath( void )"
  50.   GencodeSrcLine 109;
  51.   FunctionName "SQLAnyUtil::GetSampleDBPath( void )";
  52.  @end;
  53.  
  54.  @begin UserFunction "Prototype for GetSampleDBPath( void )"
  55.   Private 1;
  56.   GencodeSrcLine 30;
  57.   FunctionName "SQLAnyUtil::Prototype for GetSampleDBPath( void )";
  58.  @end;
  59.  
  60.  @begin UserFunction "ErrorMessage( WForm * parent, WString const & title, WQuery * query )"
  61.   GencodeSrcLine 126;
  62.   FunctionName "SQLAnyUtil::ErrorMessage( WForm * parent, WString const & title, WQuery * query )";
  63.  @end;
  64.  
  65.  @begin UserFunction "Prototype for ErrorMessage( WForm * parent, WString const & title, WQuery * query )"
  66.   Private 1;
  67.   GencodeSrcLine 32;
  68.   FunctionName "SQLAnyUtil::Prototype for ErrorMessage( WForm * parent, WString const & title, WQuery * query )";
  69.  @end;
  70.  
  71.  @begin HPPPrefixBlock
  72. @begin-code HPPPrefix
  73.  
  74. // Declarations added here will be included at the top of the .HPP file
  75.  
  76. @end-code;
  77.   GencodeSrcLine 11;
  78.  @end;
  79.  
  80.  @begin CPPPrefixBlock
  81. @begin-code CPPPrefix
  82.  
  83. // Code added here will be included at the top of the .CPP file
  84.  
  85. //  Include definitions for resources.
  86. #include "WRes.h"
  87.  
  88. @end-code;
  89.   GencodeSrcLine 11;
  90.  @end;
  91.  
  92.  @begin ClassContentsBlock
  93. @begin-code ClassContents
  94.  
  95.     public:
  96.         // add your public instance data here
  97.     private:
  98.         // add your private instance data here
  99.     protected:
  100.         // add your protected instance data here
  101.  
  102. @end-code;
  103.   GencodeSrcLine 16;
  104.  @end;
  105.  
  106. @begin-code GeneratedClassContents
  107.  
  108.  
  109. @end-code;
  110.  
  111. @begin-code Code "SQLAnyUtil::SQLAnyUtil()"
  112.  
  113. @@CLASSNAME@::@CLASSNAME@()
  114. {
  115.     
  116. }
  117.  
  118. @end-code;
  119.  
  120. @begin-code Code "SQLAnyUtil::Prototype for SQLAnyUtil()"
  121.  
  122.     public:
  123.         @@CLASSNAME@();
  124.  
  125. @end-code;
  126.  
  127. @begin-code Code "SQLAnyUtil::~SQLAnyUtil()"
  128.  
  129. @@CLASSNAME@::~@CLASSNAME@()
  130. {
  131.     
  132. }
  133.  
  134. @end-code;
  135.  
  136. @begin-code Code "SQLAnyUtil::Prototype for ~SQLAnyUtil()"
  137.  
  138.     public:
  139.         ~@CLASSNAME@();
  140.  
  141. @end-code;
  142.  
  143. @begin-code Code "SQLAnyUtil::DataSourceExists( WString const & source )"
  144.  
  145. // DataSourceExists
  146.  
  147. // This functions checks to see if a specified data source
  148. // has been previously registered.
  149.  
  150. static WBool @CLASSNAME@::DataSourceExists( 
  151.     WString const &         source )
  152. {
  153.     WRegistryKey            registry;
  154.     WString                    key;
  155.     WBool                    ret = FALSE;
  156.     WFilePath               val;
  157.     
  158.     key = "Software\\ODBC\\ODBC.INI\\ODBC Data Sources";
  159.     if( registry.Open( WRKeyCurrentUser, key ) ) {
  160.         if( registry.HasValue( source, WRegDataString ) ) {
  161.             key.Sprintf( "Software\\ODBC\\ODBC.INI\\%s", source.GetText() );
  162.             if( registry.HasValue( "driver", WRegDataString ) ) {
  163.                 val = registry.GetStringValue( "driver" );
  164.                 if( val.Exists() ) {
  165.                     ret = TRUE;
  166.                 }
  167.             }
  168.         }
  169.         registry.Close();
  170.     }
  171.     return( ret );
  172.    
  173. }
  174.  
  175. @end-code;
  176.  
  177. @begin-code Code "SQLAnyUtil::Prototype for DataSourceExists( WString const & source )"
  178.  
  179.     public:
  180.         static WBool DataSourceExists(   WString const &   source );
  181.  
  182. @end-code;
  183.  
  184. @begin-code Code "SQLAnyUtil::DataSourceAdd( WString const & name, WString const & dbfile )"
  185.  
  186. static WBool @CLASSNAME@::DataSourceAdd(
  187.     WString const &     name, 
  188.     WString const &     dbfile )
  189.     WRegistryKey        dreg;
  190.     WFilePath           driver;
  191.     WRegistryKey        registry;
  192.     WString             key;
  193.     WBool               ret = FALSE;
  194.     
  195.     // Add the data source name to the list of data sources available
  196.     
  197.     key = "Software\\ODBC\\ODBC.INI\\ODBC Data Sources";
  198.     if( registry.Open( WRKeyCurrentUser, key ) ) {
  199.         registry.SetStringValue( name, "Sybase SQL Anywhere 5.0" );
  200.         registry.Close();
  201.     }
  202.     
  203.     // Create the detailed entry for the new data source <name>
  204.     // AutoStop         "yes"
  205.     // Database File    <dbfile>
  206.     // Description      <name>
  207.     // Driver           <driver registered for SQL Anywhere>
  208.     // Start            <dbeng50.exe in the same directory as the driver>
  209.     
  210.     key.Sprintf( "Software\\ODBC\\ODBC.INI\\%s", name.GetText() );
  211.     if( registry.Create( WRKeyCurrentUser, key ) ) {
  212.         
  213.         registry.SetStringValue( "AutoStop", "yes" );
  214.         registry.SetStringValue( "DatabaseFile", dbfile );
  215.         registry.SetStringValue( "Description", name );
  216.         
  217.         // Fetch the driver info from the SQL Anywhere entry
  218.         
  219.         driver.Clear(); // Null
  220.         key.SetText( "Software\\ODBC\\ODBCINST.INI\\Sybase SQL Anywhere 5.0" );
  221.         if( dreg.Open( WRKeyLocalMachine, key ) ) {
  222.             driver = dreg.GetStringValue( "Driver" );
  223.             dreg.Close();
  224.         }
  225.         
  226.         if( !driver.GetNull() ) {
  227.             
  228.             // Use the driver path to set the Driver values.
  229.             registry.SetStringValue( "Driver", driver.GetText() );
  230.             
  231.             // Replace the driver file name with the engine file name for the Start value.
  232.             driver.MakePath( NULL, NULL, "dbeng50", "exe", _WFILEPATH_ALL );
  233.             registry.SetStringValue( "Start", driver );
  234.            
  235.             ret = TRUE;
  236.         }
  237.         
  238.         registry.Close();
  239.     }
  240.     return( ret );
  241. }
  242.  
  243. @end-code;
  244.  
  245. @begin-code Code "SQLAnyUtil::Prototype for DataSourceAdd( WString const & name, WString const & dbfile )"
  246.  
  247.     public:
  248.         static WBool DataSourceAdd(  WString const & name,  WString const & dbfile );
  249.  
  250. @end-code;
  251.  
  252. @begin-code Code "SQLAnyUtil::GetSampleDBPath( void )"
  253.  
  254. // GetSampleDBPath
  255.  
  256. // Return the path to the standard SQL Anywhere sample database
  257.  
  258. static WString @CLASSNAME@::GetSampleDBPath( void )
  259. {
  260.     WString             dbfile;
  261.     WRegistryKey        registry;
  262.     WString             key;
  263.     
  264.     key = "Software\\ODBC\\ODBC.INI\\SQL Anywhere 5.0 Sample";
  265.     if( registry.Open( WRKeyCurrentUser, key ) ) {
  266.         dbfile = registry.GetStringValue( "DatabaseFile" );
  267.         registry.Close();
  268.     }
  269.     return( dbfile );    
  270. }
  271.  
  272. @end-code;
  273.  
  274. @begin-code Code "SQLAnyUtil::Prototype for GetSampleDBPath( void )"
  275.  
  276.     public:
  277.         static WString GetSampleDBPath( void );
  278.  
  279. @end-code;
  280.  
  281. @begin-code Code "SQLAnyUtil::ErrorMessage( WForm * parent, WString const & title, WQuery * query )"
  282.  
  283. static void @CLASSNAME@::ErrorMessage( WForm * parent, WString const & title, WQuery * query )
  284. {
  285.     WDataErrorArray             errList;
  286.     
  287.     errList = query->GetErrorList();
  288.     for( WInt i = 0; i < errList.GetCount(); i++ ) {
  289.         WLong errCode  = errList[i].GetErrorCode();
  290.         WString errMsg = errList[i].GetErrorMessage();
  291.         WLong funcCode = errList[i].GetFunctionCode();
  292.         WLong natCode  = errList[i].GetNativeCode();
  293.         WString state  = errList[i].GetSQLState();
  294.         
  295.         // View the debug log to see these messages
  296.         WDBG(( "*** Update Error ***" ));
  297.         WDBG(( "Error code:    %ld", errCode ));
  298.         WDBG(( "Message:       '%s'", errMsg.GetText() ));
  299.         WDBG(( "Function code: %ld", funcCode ));
  300.         WDBG(( "Native code:   %ld", natCode ));
  301.         WDBG(( "SQL State:     '%s'", state.GetText() ));
  302.         
  303.         // Use the form title for the error message
  304.         WMessageBox::Messagef( 
  305.             parent, WMBLevelError, WMBButtonOk, title,
  306.             "%s", errMsg.GetText() );
  307.      }    
  308. }
  309.  
  310. @end-code;
  311.  
  312. @begin-code Code "SQLAnyUtil::Prototype for ErrorMessage( WForm * parent, WString const & title, WQuery * query )"
  313.  
  314.     public:
  315.         static void ErrorMessage( WForm * parent, WString const & title, WQuery * query );
  316.  
  317. @end-code;
  318. @end;
  319.