Rob Gonda's Blog

Database Data Integrity: The Basics

Applications have usually multiple tiers, such as user interface, business model, and data. It is not an uncommon practice to rely on the application level for data validation, since in most cases, it is a single application that interacts with one particular database. However, what would happen if one field got 'corrupt', because of a bug in the application? What would happen if multiple applications accessed the same data, and but one was missing a particular validation rule?

The data is held in a database, and it should be the database that maintained and verified integrity. You must be thinks, of course, you define datatypes and  table relations, but that's not enough. How many times have you -- or your dba -- defined a gender field as a char(1), or a uuid primary key as a char(35)? What makes you think that somehow, let's call it glitch, the gender field couldn't end up with a 'x' value, when you're only accepting 'm' or 'f'? or the id key could end up with '400', when taking only uuids. Chances are in fact, that your application in fact may be validating a uuid datatype, since it's native to ColdFusion, and anything other than a uuid key will break your application.

In fact, and without pointing at anyone, I've downloaded and played with dozens of open-source applications that use a uuid as a primary key, providing a sql schema script that does not validate that a proper uuid is being placed.

What is the solution? Constraints; and they should always be used. A constraint is nothing but a rule that all data have to comply, enforced at the database level. The most common constraints is the foreign key, where the database automatically enforce one column value (fk) to match any column value of a different table (pk), which are automatically created when you declare foreign keys

However, you can also create your own constraints, for example, gender have to be 'm', or 'f'; a credit card expiration have to be between 2005 and 2020, number of children have to be between 0 and 20, and so on. Your datatype can only restrict so much, but you can add additional validation to ensure that if falls into your business logic.

You can add constrains using sql scripts, or using sql GUI.

The following examples are for Microsoft SQL, but should be able to adapt them to any database that supports constraints.

To add a constraint to a gender field with datatype char(1), simple open the table in design mode, click the constraint icon (top toolbar, right icon, shape of a grid) and click the new button. The constraint expression must return a boolean; type ([gender] = 'f' or [gender] = 'm'). Following, you may assign any name to this constraint, commonly prefixed by CK_ for check. (see screen shot)

To add the same constraints by scripting, you may open a script window or the query analyzer and type the following

ALTER TABLE [dbo].[users] ADD
    CONSTRAINT [CK_users_gender] CHECK ([gender] = 'f' or [gender] = 'm')
GO


After this is in place, anytime you try to insert or alter the gender field with any character other than 'f' or 'm', the database will throw an error, which you can catch with the constraint name.

Tomorrow I will post how to validate regular expressions, including UUID datatypes.

CFEclipse 1.3 Released

Good news: CFEclipse 1.3 is out; thanks Mark! It is one of the many Eclipse plugins I use daily, and by far the best tool to write ColdFusion code. 

jQuery 1.1.1 update

If you're using jQuery 1.1, download immediately the 1.1.1 update. There are a few minor bugs that were fixed, but one was major for me. (see the full list here).
The first item: Setting the numerical value of a css property failed, for example: .css(”opacity”,0.5) (also occurred with zIndex, fontWeight), which by the way only affected IE, was causing my interface drag and drop not to work. For those of you not familiar with interface, it's a collection of components made in form of plugins that facilitate the development of Rich User Interfaces.
The particular problem I ran into was defining draggables. The code looks as follows:

$('#drag2').Draggable(
    {
        zIndex:     1000,
        ghosting:    true,
        opacity:     0.7
    }
);


Behind the scenes, this invokes the $().css('zIndex', 1000) function, which for some bizarre reason was translated into setting zIndex to 1000px; go figure. What amazes me with that Firefox, the all standard based web browser, actually ran this code, and IE, the all forgiving-stimulating-bad-code-practice was the one that broke.

Anyways, the jQuery team quickly released an update and I have not experienced any bugs since. Keep it up.



AjaxCFC for jQuery Alpha2 Release

AjaxCFC for jQuery is almost ready and I just uploaded to RIAForge the new Alpha 2 release. You may sync up your SVN repository or if you don't use source control, use RIAForge's zip download utility to fetch the latest version. Once you download the zip file, the jQuery related code is located in the branches/jquery folder.

I am using the just released jQuery 1.1, so if you drop this code on an old site using jQuery, you probably want to download the back-compatibility plugin.

Like I mentioned in my alpha 1 post, I included a plugin to maintain DWR syntax compatibility, and to illustrate it, I updated a few examples to use this code instead of DWR. Thus far, the only significant different I found is when returning a query using JSON serialization method (default on this release). The syntax to access the query columns is a little different, as you will see in the examples and soon-to-come documentation.

Among some other changes, I updated logging, error trapping, security checks, constructor methods, serialization capabilities, and more.

Most of these changes affect the DWR and jQuery version since the Ajax.cfc core file is shared between them.

Constructor method: many of you tried to extend your existing domain model objects and provide remote methods inside them. Although I don't particularly agree with this practice, I don't want to restrict it. AjaxCFC used to rely on it's built-in init method constructor, which interfered with you own component once extended, so I renamed the AjaxCFC to allow your objects to contain an init() method.

Error trapping: AjaxCFC catches most errors in your components and by default, alert you with the error message. I added the file and line number since sometimes the message was not enough. I also added a method to optionally popup a window showing the error dump, which is extremely useful because you can see the full trace... to enable this just invoke the setPopupErrorDump(true) method inside your CFC.

Security Check (for jQuery version only): jQuery sends additional x-header data in the http request, so I added an option to check for it, to prevent access to the CFC if it's accessed by any method other than an XmlHttpRequest. To enable it just invoke the setCheckHttpRequestData(true) method inside your CFC. In fact, by default AjaxCFC ships with all the security methods turned off, for easier debugging. It is advisable to turn them on by calling: setAllowedVerbs('post'); setCheckHTTPReferer(true); setPopupErrorDump(false); setCheckHttpRequestData(true);

Serialization (for jQuery only): The new AjaxCFC is able to serialize using pure JS, JSON, or WDDX. I haven't fully benchmarked the performance differences, so for now just use the one you like the most.

Logging: I updated the log4javascript version, plus I added a little more information in the request logs.

Documentation will be included with the beta release.

Please note that despite some comments out there, this release does not mean that AjaxCFC switched to jQuery. This release is a branch using a different Ajax engine, and both versions will continue to be updated and supported.

That's it for now, please report any issues using RIAForge's bug tracker.

Enjoy.

jQuery 1.1 final released today

Ladies and gents, jQuery 1.1 is out. It's 10-20x faster than 1.0.x due to significant API refactoring, which means that all your 1.0.x may not work; for this, you may use the jQuery 1.0 Compatibility Plugin to keep 1.0-style functionality in 1.1... check it out

Ajax, JavaScript, CSS, DOM: Get Firebug 1.0b8

Quick note for those who jumped ahead and downloaded Firebug 1.0 beta: updates will not show up automatically. For instance, I first downloaded 1.0b1 and it's already up to 1.0b8, with many, yes, many bug fixes.

For those of you new to Firebug, it you do any type of web development, you need this extension. Jack Slocum (mastermind behind YUI-EXT) quote: "Thanks to the FireBug Profiler, I was able to trace the bottlenecks and test different execution plans to see what was the fastest." when he was writing the all new DomQuery.

Web Developer 1.1 for Firefox

Web Developer, ironically, it's probably one my favorite Firefox extensions... it's up there right next to Firebug. They released a new version (1.1), which contains tons of nice features... one that I've been wanting for a while is collapsable JavaScript files view. Here's a list of the new features with this release.

  • Add a status icon that indicates whether there are CSS errors on the page
  • Add close buttons to all the extension toolbars
  • Add support for displaying the compressed and uncompressed file sizes in 'View Document Size'
  • Add support for entering the ruler dimensions for the 'Display Ruler' feature
  • Add support for explaining why certain features are unavailable
  • Add support for saving the viewport resizing option
  • Add support for the 'ping' attribute to 'Display Link Details'
  • Add support for using multiple Web Developer sidebars at the same time
  • Add the features
    • Disable Proxy
    • Disable Strict JavaScript Warnings
    • Display Abbreviations
    • Display Page Magnifier
    • Display Page Validation
    • Outline Images With Oversized Dimensions
    • Outline Links With Ping Attributes
    • Outline Table Captions
    • Validate Local Accessibility
  • Change the JavaScript status icon when JavaScript is disabled
  • Improve the 'Edit CSS' feature
    • Add Mozilla/Seamonkey support
    • Add search functionality
  • Improve the 'Edit HTML' feature
    • Add Mozilla/Seamonkey support
    • Add search functionality
  • Improve the 'View Cookie Information' feature
    • Add support for deleting cookies
    • Add support for editing cookies
  • Improve the 'View Style Information' feature
    • Add support for syntax highlighting
    • Allow the path to the highlighted element to be copied
  • Improve the design of the output of the features
  • Tons of Bug Fixes.

jQuery v1.1 beta released today

jQuery v1.1 beta was released today and final release is scheduled for this w/e -- how exciting! Since they streamlined the API to massively improve speed, they also released a back-compatibility plugin so you can easily just drop this new API to upgrade.

John Resig also blogged about the new selector speed and compared it to Jack Slocum's new DOMQuery.

Adobe Flex 2.0.1

Adobe released an update for Flex: 2.0.1. I just wrote (and lost) a review of the new changes, and it's passed 2am, so I'm not writing it again ... In summary, it now has official support for Macs, it has new JIT support for adding/removing modules in runtime, it's moving towards Apollo integration. You can see the official release notes and also check out Ted's detailed review.

jQuery 1.1 alpha released today

jQuery v1.1 alpha was just released. Among the major changes, you would find that

  • Its selectors are 10-20x faster than those in jQuery 1.0.4.
  • The documentation has been completely revamped.
  • The complexity of the API has dropped by 47%.
  • It has a ton of bug fixes.
  • It has a bunch of great new features.
Download

The following methods have been renamed/reorganized in this version, here is how you can continue to use them, as you would expect:

Old Way (1.0.x) New Way (1.1)
.ancestors() .parents()
.width() .css(”width”)
.height() .css(”height”)
.top() .css(”top”)
.left() .css(”left”)
.position() .css(”position”)
.float() .css(”float”)
.overflow() .css(”overflow”)
.color() .css(”color”)
.background() .css(”background”)
.id() .attr(”id”)
.title() .attr(”title”)
.name() .attr(”name”)
.href() .attr(”href”)
.src() .attr(”src”)
.rel() .attr(”rel”)
.oneblur(fn) .one(”blur”,fn)
.onefocus(fn) .one(”focus”,fn)
.oneload(fn) .one(”load”,fn)
.oneresize(fn) .one(”resize”,fn)
.onescroll(fn) .one(”scroll”,fn)
.oneunload(fn) .one(”unload”,fn)
.oneclick(fn) .one(”click”,fn)
.onedblclick(fn) .one(”dblclick”,fn)
.onemousedown(fn) .one(”mousedown”,fn)
.onemouseup(fn) .one(”mouseup”,fn)
.onemousemove(fn) .one(”mousemove”,fn)
.onemouseover(fn) .one(”mouseover”,fn)
.onemouseout(fn) .one(”mouseout”,fn)
.onechange(fn) .one(”change”,fn)
.onereset(fn) .one(”reset”,fn)
.oneselect(fn) .one(”select”,fn)
.onesubmit(fn) .one(”submit”,fn)
.onekeydown(fn) .one(”keydown”,fn)
.onekeypress(fn) .one(”keypress”,fn)
.onekeyup(fn) .one(”keyup”,fn)
.oneerror(fn) .one(”error”,fn)
.unblur(fn) .unbind(”blur”,fn)
.unfocus(fn) .unbind(”focus”,fn)
.unload(fn) .unbind(”load”,fn)
.unresize(fn) .unbind(”resize”,fn)
.unscroll(fn) .unbind(”scroll”,fn)
.ununload(fn) .unbind(”unload”,fn)
.unclick(fn) .unbind(”click”,fn)
.undblclick(fn) .unbind(”dblclick”,fn)
.unmousedown(fn) .unbind(”mousedown”,fn)
.unmouseup(fn) .unbind(”mouseup”,fn)
.unmousemove(fn) .unbind(”mousemove”,fn)
.unmouseover(fn) .unbind(”mouseover”,fn)
.unmouseout(fn) .unbind(”mouseout”,fn)
.unchange(fn) .unbind(”change”,fn)
.unreset(fn) .unbind(”reset”,fn)
.unselect(fn) .unbind(”select”,fn)
.unsubmit(fn) .unbind(”submit”,fn)
.unkeydown(fn) .unbind(”keydown”,fn)
.unkeypress(fn) .unbind(”keypress”,fn)
.unkeyup(fn) .unbind(”keyup”,fn)
.unerror(fn) .unbind(”error”,fn)

However, before you get too alarmed, I know they will release a 'back-compatibility' plugin, which would help you transition from 1.0.x to 1.1.

I am really excited, benchmarks will come soon, but this release is way faster and supperior, so go ahead, spread the word. I will probably release the beta version of AjaxCFC for jQuery with the official 1.1 release.

This blog is running version 5.9.003. Contact Blog Owner