![]() |
Database »
Database »
Utilities
Beginner
License: The GNU General Public License (GPL)
SQL Editor for Database DevelopersBy ElmueSQL editor with syntax parser, direct editing, code execution, database backup, table comparison, script generation, time measurement |
C#, SQL, .NETVS.NET2003, DBA, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
SqlBuilder is a very useful, powerful and intelligent C# tool for all developers and teams, which works with databases.
SELECT are wrapped automatically in an intelligent way onto multiple lines and are indented. The parsing happens while entering text. Normally you have to pay a lot of money for this functionality. DBCC FREEPROCCACHE ...) and then execute the SQL command. So you can measure the time an SQL command needs when it is first executed before the server has cached query results. The difference may be a factor of 1:10 or more! SELECT or EXECUTE queries at the same time which return a dataset with 2 tables and then compare the 2 query results for exact equality. SqlBuilder will tell you if the two results differ even if the lines in the tables appear in a different order. If you wish, SqlBuilder can remove all equal lines from the tables and leave only the different ones. So you can check after a modification you have made on a procedure if it still returns the same data as before. SqlEditor. If you have already assigned a default program for SQL files, this will not be changed but in the context menu of Explorer (right click on a file) you will find a new entry "Open with SqlBuilder". Although the source code is VERY complex, you will find a VERY clean and well structured code with plenty of comments written by a very experienced programmer.
(Coding effort: more than half a year!)
Suppose that you work on a project with multiple release versions of which each has its own folder on your local disk. Choose the folder of the current release in your server project. SqlBuilder will create two subfolders in the working directory: DatabaseBackup and Scripts.
SqlBuilder stores an XML file in the working directory with the server settings (server name, user name, password, etc.) If you work with multiple SQL servers, create a working directory for each server.
It is strongly recommended to ALWAYS additionally add a temporary folder in which you can experiment with SQL commands which are NOT stored in Subversion or CVS.
The next step is adding the SQL files or system objects on which you will work:
The Toolbar in the main window offers the following functionality: (from left to right)
Click the leftmost toolbar button and this window will open:

No matter if you:
SqlBuilder will add it to the Filelist of the main window:
There is a big difference between
It is very important that you understand the following:
Adding a system object like a *.PROC, *.FUNC, *.VIEW, *.TRIG file means that all changes to SQL code are stored on the SQL server!
SqlBuilder creates a dummy file (like fn_GetUserById.func) in your working directory which does NOT contain any SQL code!
On the other hand if you add a *.SQL or *.TABL file (like Test.sql above), all changes on SQL code will be stored locally in this file.
You will nearly never use the type "File" (*.sql) except for testing (executing) SQL code under development. Do NOT abuse type "File" (*.sql) to store the definitions of procedures, functions or modifications on tables, etc.!!
With CTRL + TAB you can rapidly switch between all the items in the Filelist that have been selected recently.
For each item in the Filelist, SqlBuilder will create the appropriate file in a subfolder of your Scripts folder. The Scripts folder will have a subfolder for each database.

You and all your colleagues will check in the entire content of your Scripts folder into Subversion or CVS. When the new release is ready, you click the button "Build Script" and SqlBuilder will build one huge Compound SQL Script.
This script contains:
Then you pass the compound script to your clients so they can update their SQL server to the latest version.
IMPORTANT:
To work with the SQL Editor you must know the keyborad shortcuts. Click the keyboard help button!!
The SQL editor stores all your changes in an Undo buffer which remains intact even after working on another file meanwhile. With CTRL-Z you can always return to the last workstate.
In a File or Table you can select any part of the SQL code and execute it with CTRL + E. If nothing is selected, the entire code will be executed.
Procedures, functions, views, triggers will always be executed entirely when hitting CTRL + E which will store them on the server.
It is recommended to have at least one Temp file in your Temp working directory where you execute SQL code for testing purposes.
With CTRL + TAB you can rapidly switch between all the items in the Filelist that have been selected recently.
If the result of a SQL query is a scalar value or if the query has no result at all, this will be displayed below in the main window:

If the result is one or multiple tables, these will be displayed in the ResultView window:
You always see the execution time with a real exactness of 1 millisecond. (Performance Counter)
If you want to see the execution time after clearing the server cache, use CTRL + B instead of CTRL + E.
CTRL + B disables all server optimizations, like buffering the query results, so you will see the worst case execution time.
The Toolbar of the Result View window offers the following functionality: (from left to right)
In the ResultView window you can compare the contents of two tables.
To use it, you must send two SQL commands to the server at the same time.
Example:
EXEC proc_GetUserData 877, 1
EXEC proc_GetUserData_New 877, 1
The SQL server will return a dataset with two tables. If proc_GetUserData is a stored procedure and proc_GetUserData_New is a procedure which you have modified, you can test how your modifications affect the returned data.

The Table Editor is a very powerful tool in SqlBuilder. It allows you to edit tables on the SQL server as if they were Excel tables. You can set the cursor into a cell and modify its value. When you are done with all changes you save the changes. After closing the TableEditor you will see all the SQL code that has been executed in the *.TABL file in the main window. These changes will automatically be included into the compound script if you didn't delete them.
The column's background color changes if the column has a UNIQUE or PRIMARY key. The column's text color depends on the data type (string, integer etc..)
The Toolbar of the Table Editor offers the following functionality: (from left to right)

The Table Designer is a very powerful tool that allows you to view / modify:
varchar(10) into varchar(50)) The job of the Table Designer is very complicated because Microsoft's SQL is so incredibly primitive. You will see that when you study the SQL commands which have been executed after modifying an existing primary key in a column which is referenced by foreign keys.
Autogenerated code example:

As you see, SqlBuilder tries to generate universal SQL scripts:
But if you modify Constraints this will not be possible because the names of the Constraints differ from server to server.
Again: Microsoft's SQL is so incredible primitive that there is no universal way to delete a Constraint without knowing its name.
Commands like the following are not accepted by Microsoft SQL server:ALTER TABLE [TableName] DROP UNIQUE (ColumnName)
ALTER TABLE [TableName] DROP PRIMARY KEY
The developer comments for procedures, functions, views and triggers are ONLY for internal use for you and your colleagues!
They will neither be written into the compound script nor stored on the server.
Before hitting the "Build Script" button, you should re-order the Filelist in the main window by drag and drop. The compound script will be built in the same order as you see the entries in the Filelist.
If the compound script creates a new procedure which depends on a new function you must FIRST create the function, THEN the procedure to avoid errors on the SQL Server of your clients. Use the developer comment field not to forget this:

The developer comments are stored in the local files *.Proc, *.View, *.Func, *.Trig.
With the next time you check-in int Subversion / CVS the comments will be available for your colleagues.
SqlBuilder can search directly in all databases for a text to be contained in a procedure, function, table column etc... The results are displayed as a list of the found system objects with their content beside. You open the Database search from the toolbar in the main window.

It is recommended to use the button "Database Backup" (in the toolbar of the main window) at least once a week and check in the entire folder DatabaseBackup into CVS/subversion to backup your work on procedures etc...
Even if your SQL server has a backup system and you fully trust it, the advantage is that you can compare the procedures, functions, views etc. with older versions by using the built-in compare functions of CVS / Subversion (or another compare tool like Araxis Merge) and you can verify which procedures have changed and what the differences are to older releases.
The files in the backup folder have the date of their creation on the SQL server. Sadly the date of the last modification is not stored on Microsoft SQL Server.
The SQL parser is the heart of the application and the most complicated part. Writing a parser for HTML code or for C++ code is extremely simple in comparison to an SQL parser which is a challenge.
While other programming languages have fixed syntax rules (e.g. in HTML every tag starts with <TAG> and ends with </TAG> or in C++, every command has the form function(argument, argument); and every line ends with a semicolon), SQL is a "language" without rules.
SELECT may have a WHERE clause or not. It may have a JOIN clause or not, etc. It simply ends where no more clauses follow. LEFT) may be a keyword (in JOIN LEFT) or a function (in left(string, count)). Finally, SQL is not a programming "language" at all. And this may be abused by inexperienced programmers to produce code which seems to come directly out of hell. The parser of SqlBuilder is capable of cleaning up any ugly SQL code.
It works in 5 steps:
ParseItem which is stored in a double linked chain. SELECT, CASE, CREATE, BEGIN, END,.... RtfHtmlBuilder (see below) with the parsed data to create an RTF document, HTML code or plain text from the parsed data. Only the first step works with plain text. The following steps work with objects and this makes the parsing very fast so it can happen while entering text. If you enter a new letter while the previous parsing did not yet finish, the previous parsing is cancelled and parsing is started anew. All this runs in the background invisible for the user!
SqlBuilder has a class with the name Defaults. Here you can define YOUR preferred default settings for the entire program like the colours for the parser, the timeout or the default SQL server if not yet specified by the user.....
SqlBuilder has some special control classes which you can copy and use in other C# applications.
This class is derived from RichTextBox and extends it with more functionality:
RtfHtmlBuilder If you ever used Microsoft's RichTextBox control and tried to display coloured text, you already know that:
richTextBox.SelectionColor = Color.Red;
richTextBox.SelectionIndent = 5;
richTextBox.AppendText("Text");
....
is EXTREMELY slow. It is so incredibly slow that for the display of a text of 50 KB, the user has to wait 20 seconds!
RtfHtmlBuilder is an ultra-fast RTF, HTML and PlainText creator class.
rtfBuilder.SelectionColor = Color.Red;
rtfBuilder.SelectionIndent = 5;
rtfBuilder.AppendText("Text");
.....
string s_Rtf = rtfBuilder.BuildRtf(new Font("Microsoft Sans Serif", 17));
richTextBoxEx.ReplaceRtf(s_Rtf); // flicker free text replacement
This class is derived from ListView and allows drag and drop of the items in the view to change their order.
This class paints the datagrid cells in different colors and has some more features. It has five workarounds built in for really ugly Microsoft bugs in this control. The DataGrid is by far the most buggy control in .NET framework!
This class allows input of passwords which cannot be spied out with an API spy or a .NET spy. The password is stored internally as encrypted string.
The textbox shows the password as plain text while you are typing it, otherwise it displays 12 stars no matter how long the password is.
P.S. On my homepage, you can download an SQL book for beginners in CHM format (German) and much more utilities.
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 18 Jun 2008 Editor: Sean Ewington |
Copyright 2008 by Elmue Everything else Copyright © CodeProject, 1999-2010 Web18 | Advertise on the Code Project |