|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Features PowerBuilder Developer's View of SQL Anywhere DBMS – Part 2
A new level of productivity
By: Chris Pollach
Feb. 20, 2009 03:13 PM
When you start to develop mission-critical business applications using your favorite development tool, you should seriously consider iAnywhere's SQL Anywhere database management system to support the data management requirements. For this article I will focus on SQL Anywhere version 10 and PowerBuilder 11.0. Some of the features reviewed may not be supported in previous versions of these software releases. Where possible, I will try and identify features that are only supported in the current release and if there are any alternatives in previous versions. In Part 1 I discussed the installation of SQL Anywhere, developing a schema, and the Database Painter. In Part 2 I will continue the discussion on the Database Painter as well as tracking database changes. Database Painter Once you have the new or modified table changes completed in the associated table structure, move your mouse to the top of the Column layout pane where the headings are displayed and use the RHMB on any of the heading names. PowerBuilder should then display a pop-up menu with the "Save" or "Save Table as" options. You may now select one of these to have the table entity modified or added to the schema by the SQL Anywhere DDL compiler. It should be noted that PowerBuilder never touches your SQL Anywhere database, instead it always interfaces to one of three standard compilers: Data Definition Language (DDL), Data Manipulation Language (DML), or Database Control Language (DCL). All of these various compiler interactions are "brokered" for you automatically by the PowerBuilder Database Painter. Once a new table has been created, you may wish to add Primary Keys, Indexes, or even Foreign Keys. This is very simple in the Database Painter by using the RHMB on the table name in the tree control or once you have it visible in the object layout pane. At this point a pop-up menu will appear. Select the "New" option and a cascading menu should drop that allows you to create one of the features that I just mentioned (see Figure 1). I won't delve into the details of these DBMS features in this article but I do recommend that you read your DBMS manuals on these topic areas for further detailed information on these features and their ramifications. As a last comment on handling table changes, the PowerBuilder developer may be restricted from creating new tables/columns or even modifying existing column structures within a table. The PB Database Painter has an interesting option for the developer to interface to the Database Administration personnel. This allows you to provide them with the necessary DDL compiler input for DBA to apply these changes using their favorite database utility. Here is how it works: before pretending to create, modify, or delete an entity, turn on the Database Painters "Activity Log". You can find this option on the lower right-hand side of the Database Painter in the Pane marked "Activity Log." Activate this pane and use the RHMB anywhere within this pane. The resulting pop-up menu should include both a "Start Log" and "Stop Log" menu item. At this time, select the Start Log option to have PowerBuilder automatically record every change to your local SQL Anywhere database, for example, column changes, foreign keys, indexes, etc. When you have completed all your database maintenance tasks, return to the "Activity Log" pane and select the "Stop Log" menu option using the RHMB. The last remaining step will be to export the captured SQL in the log to an ASCII file and send this to your DBA. In older versions of PowerBuilder there was an "Export Log" option but for some reason the Sybase engineers seem to have removed that functionality. To counteract this deficiency, copy the contents of the Activity Log over to something like the MS-Windows NotePad utility and then save this to a .SQL file. The other option is to simply close the Database Painter, which will then prompt you to save the contents of the log file (strange but true). What I don't like about the latter approach is that I might not be finished with all my database changes at that point. You can get around this little caveat in the newer versions of PowerBuilder by opening a second Database Painter just to log the changes for a given entity. Then, when you close the second instance, you can save the SQL but still continue to work on other in-progress database maintenance items when returning to the first instance of the Database Painter (food for thought). Views Another easy technique to create a view is to use the SQL from within a current DataWindow to establish the database view. This can be done by entering the DataWindow Painter and selecting the SQL option to enter the SQL Painter. In the SQL Painter, use the menu item "Design=>Convert to Syntax" to reproduce the actual SQL Syntax that the DataWindow will execute at runtime. Copy this into the MS-Windows Clipboard and exit the SQL/DataWindow painters. Returning to the Database Painter, activate the "ISQL Session" Pane. Within this area, paste the SQL statement from the clipboard of what you discovered using the DataWindow Painter. To create a view from this SQL, add a "Create VIEW ("name") ... DDL header in front of the pasted SQL. Then press the "Execute" command button on your Database Painter toolbar to run the DDL and build the view. To test the view, expand the "Views" item on the DB Painter's Tree and locate the view you wish to test. Using the RHMB on the view entity, select the "Edit Data=>Grid" menu item from the resulting pop-up menu. This action will execute the actual view entity's SQL and show you the actual result set returned from the SQL Anywhere DBMS. Now is that not easy or what? To utilize the view database entity, all that is required is to build a DataWindow object and request that a view be the data source for the DataWindow's SQL (you'll see this option in the DataWindow Wizard). Select the new view you just created and that's all there is to it. This is a real productivity feature for developers as now you can build dozens of DataWindow objects and never have to construct the same SQL statement again. Triggers Events This feature is only accessible from Sybase Central but very easy to create using the wizard. To create an Event, launch Sybase Central and connect to your target database. Then use the RHMB on the Event item in the Sybase Central tree and select the "New=>Event" menu item from the resulting pop-up menu. The Event Wizard will be started and will guide you through the process of setting up an event. You may also use PowerBuilder's Database Painter to create an event, but you will have to know the DDL syntax for this. Once you have the event syntax ready though, paste this into the Database Painter's "ISQL Session" pane and press the "execute" toolbar button to create, modify, or delete the event. You can always view an "Event" from within the PowerBuilder Database Painter by highlighting the event from the tree control in the painter, then RHMB and select the "Properties" menu item from the resulting pop-up menu. The actual event syntax will now appear in the "Property" pane. To modify the event from within PowerBuilder, drag the event from the tree control to the "ISQL Session" pane in the Database Painter (see Figure 4). This will have PowerBuilder converse with the DDL compiler to deliver the exact Event syntax into the work area. The default will be a CREATE statement, so to modify the event, change the verb to ALTER and then modify the command between the Begin and End statements as required. When the modifications are completed, press the Execute button on the Database Painter's toolbar to invoke the DDL compiler (tip: you can then use Sybase Central to confirm the changes if you like). Stored Procedures To create a Stored Procedure in PowerBuilder is extremely easy. Again, you could use the Sybase Central utility, but with PB's Database Painter in concert with the SQL Painter this task can be made very easy. To start with, open the DB Painter in PB and activate the "ISQL Session" pane. Use the RHMB in the pane's work area and select the "Paste Special=>SQL=Select" from the resulting pop-up menu. This will now activate the SQL painter where you can graphically create any SQL statement you require to activate the desired result set (tip: use the "Preview" button on the SQL Painter's toolbar to test your SQL before returning to the DB painter). Once you are satisfied with the SQL and Result Set being returned by the DBMS, exit the SQL Painter and the final SQL statement will now appear in the ISQL Pane. To this returned statement, add a "Create Procedure Name as ..." in front of the current DDL statement and then press the "execute" button on the DB Painter's toolbar. This should now create the Stored Procedure in the SQL Anywhere repository. To test the new Stored Procedure that you created using the steps above, use the Tree control in the DB Painter and expand the Stored Procedure section, locate your new Stored Procedure, and with the RHMB, select the "Edit Data=>Grid" menu item from the resulting pop-up menu (see Figure 5). This will have PowerBuilder invoke the Stored Procedure and display the generated result set in the "Results" pane of the Database Painter. You may also use the ISQL session pane and type in an execute command to run the Stored Procedure. For example, "Execute sp_cip_test ( ) ;". For those Stored Procedures that have arguments, you may add these within the brackets after the Stored Procedure name in the execute statement. The real "icing on the cake," so to speak, for PB developers is when they are required to use the Stored Procedure in an application All they need to do is create a DataWindow using the Stored Procedure as its "data source." This is less than a minute's work in the DataWindow Wizard and the resulting DataWindow Object will execute the Stored Procedure using a "Declare, Prepare, Execute and Fetch loop" that the developer never needs to code (unlike in C++, C#, VB, Java, etc., languages where you need to "code your SQL brains out"). If you have created Stored Procedures for the Delete, Update, and Insert actions, map these in the DataWindow Painter. To do so, use the "Rows=Stored Procedure Update" menu item in the DataWindow Painter. The resulting dialog will allow the developer to map other various Stored Procedures to one of the three actions I just mentioned - including any necessary argument values. Now you have an object that will interact with your SQL Anywhere Stored Procedures effortlessly. Once created, the new Stored Procedure-based DataWindow object can be given to novice developers for easy use in their application. Database Debugging It should be noted that once you have completed debugging the Stored Procedure and would like the balance of the Result Set to continue back to the client, you need to exit the "debugging" mode in Sybase Central (see Figure 6). To do this, select the "Design" toolbar icon in Sybase Central. This should now deactivate the bugging session and allow the balance of the result set to be transmitted. Until this happens, however, don't worry that your PB or ISQL session has been frozen in the meantime. Stored Functions Once the Stored Function has been created, you may test the Stored Function in Sybase Central or using PowerBuilder's Database Painter by adding a Select statement like "SELECT "DBA"."fn_cip_test"();" and then pressing the Execute button. Your Stored Function should return a value in the "Result" pane of the Database Painter. Again, if the Stored Function is having some processing problems, return to Sybase Central and set up the appropriate debugging actions. Then run the Stored Function again and walk through the Stored Function code to locate the problematic section of the code when the debug becomes active. Similar to executing a Stored Procedure, you may add values within the brackets right after the Stored Function name to pass in optional required argument values. In your PowerBuilder application you may also treat a Stored Function like a Stored Procedure and declare it in-line within your PowerScript code. This allows you the maximum flexibility when passing in argument values and directing the result back to a program variable. PowerBuilder's SQL Painter can be called from your PowerScript coding panes to help you with the Declare Function statement. An example script might look like this for the Stored Function execution under program control: String ls_xml DECLARE myFunc PROCEDURE FOR fn_cip_test ( ) ; Tracking Database Changes Neither the SQL Anywhere DBMS nor its related Sybase Central utility possesses any SCC functionality. The ideal solution would be to marry PowerBuilder's SCM functionality (especially the free built-in one) with your SQL Anywhere database design, changes, and maintenance. This doesn't seem immediately obvious as PowerBuilder does not have a database "Target" to attach your SQL Script files to. You can create the DML, DCL, and DLL files from PB, Sybase Central, or your favorite Database Design tool but the approach I have often seen is to store these in a .SQL file within a folder structure located on a file server. It is possible though to have PowerBuilder track these items for you. For this approach to work though, you need to think "outside of the box" and use PB's "web" capabilities. This probably sounds extremely strange, but in actuality it is logical as web components are typically composed of text files such as XML, HTML, CSS, and JavaScript. If we use a "Web Target" in PowerBuilder, it will track any file type including .SQL. SQL Anywhere Web Target If you would like to compare versions of your work, you might like to activate the use of a "Differential" utility. I like to use the WinDiff utility from Microsoft as it comes with Visual Studio (Express is free) or you can download the WinDiff.exe for free from the MSDN website. Once you have this utility installed, you'll need to inform the PowerBuilder WorkSpace component where it is located. To do this you will need to use the "Advanced" button on the WorkSpace's property Source Control tab page (see Figure 7). This will take you to a dialog that allows you to map the location and parameters for the comparison utility. In my case the location is: C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\Bin\WinDiff.exe %s %s By adding the %s %s parameters to the EXE path and name, it instructs the DIFF utility to compare the current version of the file/component in your PowerBuilder IDE to the one last saved in your SCC project's work location. Once this mapping has been completed, you may close the WorkSpace Properties dialog. In the previous section we spoke about creating SQL files from any of your Database Painter work by using the "Activity Log" of PowerBuilder. You can also use Microsoft's NotePad, the PB NotePad feature, etc., to create an SQL file with your database schema statements included. Once you have completed the contents of the SQL file, just save this to your current PowerBuilder application's work folder. To have the SQL Anywhere Web Target pick up this file initially, use the RHMB on the SQL Anywhere 10 Web Target and choose the "Import Files" option from the resulting pop-up menu, then select the .SQL file from the next dialog. You should now see your SQL file logged under the PB Web Target. From here we need to register an initial copy (state) that you're the SCM. In PowerBuilder, just RHMB the SQL file in the System Tree and select the "Add to Source Control" menu item from the resulting pop-up menu. The SQL file should now have a green dot beside it indicating that the SCC software is now in control of this file. To make changes to the SQL file, you can now treat this like any normal PowerBuilder component change, so we will need to first RHMB the file and perform a "Check-Out" request. Once checked out, the SQL file will show a green check mark beside it. By double-clicking the SQL file, the PB NotePad editor will appear and you can make the necessary changes (or solicit the DB/SQL Painters for help in doing so). Once your changes have been completed, save your work back to the SQL file. To view the differences between the original and your current work-in-progress, RHMB the SQL file in the PB System Tree and select the "Show Differences" option in the resulting pop-up menu. This should now show you the current changes to the SQL script and how it differs from the last version (see Figure 8). If your latest changes are correct, process them in Sybase Central or within the Database Painter of PowerBuilder. Then, use the RHMB on the SQL file in the PB System Tree and select the "Check-In" option to save the current SQL statements in the SCC. When using commercial SCC packages, you may be optionally prompted during the Check-In process to provide required comments as to the nature of the changes. For other developers or DBA staff, they can see the historical information by selecting the "Show History" menu option when the RHMB is used on the SQL file. This will inform the person of all the changes, individuals who made them, date/time of the change, etc. You can even request that any version be compared to any other version. So, for example, you can request what the SQL file looked like last year (three revisions ago) versus today's current copy. It's a really nice feature to have and it has saved me a few times too. Reader Feedback: Page 1 of 1
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
|||||||||||||||||||||||||||||||||||||||||||||||||