July 25th, 2008

How to rename a column in Sybase ASE

Today I needed to rename a column in a Sybase ASE 12.5.1 database, and couldn’t seem to find the right syntax. My first few search results gave me syntax that was not actually relevant for my database. Two different pages led me to believe that some variation of this might work:

ALTER TABLE [table_name] RENAME [old_column_name] TO [new_column_name]

But that did not work for me. Upon closer inspection, I noticed that those two pages were for Sybase ASA, not ASE. I refined my search terms a bit, and found this:

sp_rename ‘[table_name].[old_column_name]’,'[new_column_name]’

Thanks to Rob Verschoor for posting this on dbforums.com!

March 27th, 2008

Recursively grep for a substring, open all results in TextPad with cursor positioned appropriately

I’ve been using Ext-JS on a new project, recently. It’s pretty neat, and the examples are impressive, but the documentation leaves a lot to be desired. I needed to make a section of a we page collapsible, and it seemed like the Ext.Panel class was the way to do that, but I was having trouble figuring out exactly how to get my existing HTML content into a collapsible Ext.Panel. Almost as a last resort, I ended up grepping my local ext-2.0/examples directory tree to find examples that instantiate Ext.Panel objects:

$ grep -Ri “new Ext.Panel” *
code-display.js: var panel = new Ext.Panel({
core/templates.js: var p = new Ext.Panel({
core/templates.js: var p2 = new Ext.Panel({
feed-viewer/MainPanel.js: this.preview = new Ext.Panel({
feed-viewer/MainPanel.js: tab = new Ext.Panel({z
[…]

This was not very useful. I needed to see the whole constructor invocation for each of those cases. So, I decided to grep again, showing just the filenames (using the -l parameter), so I could open all of those files in TextPad. The first part of that (showing just the filenames) was the easy part:

$ grep -Rli “new Ext.Panel” *
code-display.js
core/templates.js
feed-viewer/MainPanel.js
form/combos.js
form/custom.js
[…]

Next, I needed to change those file paths from cygwin/unix-style paths to windows paths, so they could be passed to TextPad on the command-line. Time for a for loop:

$ for f in `grep -Rli “new Ext.Panel” *`; do cygpath -w -a $f; done
c:\api\js\ext-2.0\examples\code-display.js
c:\api\js\ext-2.0\examples\core\templates.js
c:\api\js\ext-2.0\examples\feed-viewer\MainPanel.js
c:\api\js\ext-2.0\examples\form\combos.js
c:\api\js\ext-2.0\examples\form\custom.js
[…]

Okay, so I could have probably built an environment variable as I was looping through and converting these paths, but if I ever wanted to run this on a longer path, with more search results, that command-line could get extremely long.

So, I checked the TextPad help to see if I could pass in the name of a file containing full file paths for TextPad to open. Sure enough:

@filename
Open all the files that are listed, one per line, in the specified file. This overrides the option to load the workspace, specified on the General page of the Preferences dialog box.

You just need to put an at sign (@) before the filename, and TextPad will look at that file to find a list of files to open. So, I decided to create a temporary file, output the filenames found and converted by my set of commands (above) into that temporary file, and then run TextPad, passing the temporary filename preceded by an @ sign.

But wait! I noticed something else in the TextPad help that seemed like a cool idea:

Notes:

  • [...]
  • If the filename to be edited (not printed) is followed by “(
    <line>[,<col>])”, with no intervening spaces, the file will be opened with the cursor at that position. If
    <line> is a hex number (eg. 0×1a22), a hex view of the file will be created, with the cursor at that address.
eg. TEXTPAD.EXE -ac "Read me.txt"(51,20)
In this example TextPad will start up and open "Read me.txt" at line 51, column 20 and display it in a cascaded window.

So, I decided to figure out a way to put the filenames to open, as well as the row and column number to position the cursor at within each of those files, into the temporary file that I was going to pass to TextPad. I already knew how to get grep to output line numbers (using the -n parameter), so I thought that would be the easy part.

However, it seems that you can’t specify both the -l (show filenames) and -n (show line numbers) parameters on the grep commandline. No, -l does more than simply tell it to show the filename next to each matching line (-H does that). -l tells it to ONLY show the filenames. Here’s the -l parameter definition from the grep man page:

-l, --files-with-matches
Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match.

As far as I could tell, if I wanted line numbers and filenames, I needed to use -n and -H, and deal with the fact that the output would also include the text of the matching line. I also threw in -m 1 to limit the output to only one result per file, since the cursor can only be positioned in one place for each file. I didn’t need the -m previously, because the -l parameter already limited it to one result per file, since it only showed the filenames of each matching file. Here’s what the grep commandline and output looked like, at this point:

$ grep -RHn -m 1 “new Ext.Panel” *
code-display.js:11: var panel = new Ext.Panel({
core/templates.js:30: var p = new Ext.Panel({
feed-viewer/MainPanel.js:10: this.preview = new Ext.Panel({
form/combos.js:49: new Ext.Panel({
form/custom.js:40: var panel = new Ext.Panel({
[…]

At first, I thought the matching line text was just in my way, so I used sed to filter it out, and to replace the colon (:) between the filename and the line number with an open parenthesis, to prepare it for the format TextPad wanted:

$ grep -RHn -m 1 “new Ext.Panel” * | sed -e ’s/\(^[^:]\+\):\([0-9]\+\):.*$/\1(\2/g’
code-display.js(11
core/templates.js(30
feed-viewer/MainPanel.js(10
form/combos.js(49
form/custom.js(40
[…]

Next, I needed to get the offsets or column numbers for each matching line number that the previous command returned, to tell TextPad exactly where to put the cursor in each file. At first, I thought I could do this with grep, but the closest grep parameter seemed to be -b:

-b, --byte-offset
Print the byte offset within the input file before each line of output

However, -b gives the absolute byte offset starting from the very beginning of the file, rather than the offset within the matching lines. So, I had to find a different way to get the column offset within each matching line. This is when I realized that having the matching line text returned by my grep command could actually be useful. I figured I could just split that text out and count the characters leading up to the matching string with wc -c, among other things.

Anyways, after a lot of trial and error, a lot of re-checking man pages for bash, grep, wc, etc., I ended up with the following set of commands:

textpad $(for g in `for f in \`grep -Rli "new Ext.Panel" *\`; do (grep -Hn -m 1 "new Ext.Panel" $f | sed -e 's/\(^[^:]\+\):\([0-9]\+\):.*$/\1(\2/g'); done`; do echo `cygpath -w -a ${g/\(*/}`\(${g/*\(/},`grep -m 1 "new Ext.Panel" ${g/(*/} | sed -e 's/\t/ /g' -e 's/new Ext.Panel.*$//g' | wc -c`\); done) &

I’m sure this could be done more efficiently, but this was a fun challenge to take on, and I managed to find a way to do what I wanted to do. Feel free to leave a comment if you know a better way of doing this!

January 24th, 2008

Form usability problem on myCIGNA Request ID Card page

Ugh! I just wanted to request some replacement health insurance ID cards for my family, this morning, but the web form for that has issues. :(

First of all, they’ve got that annoying “feature” where it automatically tabs to the next field when you finish entering characters in the current field. This is something I’ve seen many times, over the years, particularly for phone number entry. In this form, it’s on all the Date of Birth fields.

For example, to enter the fictitious date ‘12/12/1234′, I only have to type ‘12121234′. After I enter the ‘12′ in the month field, it automatically advances to the day field. After I enter ‘12′ in the day field, it takes me to the year field, and after I enter ‘1234′ in the year field, it takes me to the next form input.

This behavior is mildly amusing, as long as I don’t make a mistake. If I need to go back and fix something, though, I have to fight with it because while I’m trying to backspace or use my left/right arrow keys to move the cursor to the digit I need to fix, the automatic tab “feature” is trying to advance me to the next form field.

Here’s a video showing me trying to correct a mistake in the year field:

Even though I know that the automatic tab “feature” is active, I still find myself trying to use arrow keys to get to the right position in the form field. I also usually press shift-tab to go up to the previous form field, but on this form, the automatic tab advances me out of the field as soon as I get back to it. How annoying!Now, in case you didn’t notice, there’s another problem with those Date of Birth form fields. Here’s a screenshot of the Employee Information section of the Request ID Card form:
myCIGNA Request ID Card form - Employee Information section

In the screenshot, if you look at the Employee Date of Birth fields, you can see where I’ve entered that fictitious date I previously spoke of, ‘12/12/1234′. You’d never know it, from looking at the page, though. It looks like I only entered ‘1/1/123′. I figured it had to be either a font size problem, or a form input size problem. When I did a ‘View Source’ on the page, I saw that it’s the latter:

<td align="left"><font color="#ff0000">* </font>Employee Date of Birth:</td>
<td><input name="EDOB_MM" size=”1″ tabindex=”14″ onKeyUp=”AutomaticTab(this.value,eval(’document.idcard.EDOB_DD’),2)” maxlength=”2″ onFocus=”window.status=’Please enter the month of birth.’;select(this)”> /
<input name=”EDOB_DD” size=”1″ tabindex=”15″ onKeyUp=”AutomaticTab(this.value,eval(’document.idcard.EDOB_YYYY’),2)” maxlength=”2″ onFocus=”window.status=’Please enter the date of birth.’;select(this)”> /
<input name=”EDOB_YYYY” size=”3″ tabindex=”16″ onKeyUp=”AutomaticTab(this.value,eval(’document.idcard.Member_first_name1′),4)” maxlength=”4″ onFocus=”window.status=’Please enter the year of birth.’;select(this)”><input type=”hidden” name=”EDOB” id=”EDOB” value=”"> mm / dd / yyyy</td>

The maxlength attributes are correct, but size attributes, which control how big the viewable text input is on the page, are all too small. Why the hell would someone make a form that hides one character from each date field?

You can also see the javascript calls to the annoying AutomaticTab “feature” in that code, above. Here’s the source for their AutomaticTab function:

function AutomaticTab(CurrentLocation,
    Destination,CurrentFieldLength)
{
var fieldlength;
fieldlength = CurrentLocation.length;
if( fieldlength == CurrentFieldLength)
Destination.focus();
}

Evil. Pure evil. Seriously, I can hit tab my damn self! I don’t need this dumb javascript to help me get from one field to another, especially when I need to use the cursor keys to move left/right within the fields, because not all the digits can be seen on these fields, thanks to the size/maxlength mismatch.

I’d like to think they only screwed up this one set of form fields on this page. Unfortunately, when I tried request cards for 4 family members, I ran into it again:
myCIGNA Request ID Card form - family members to request cards for

Yes, 4 more sets of date fields with annoying AutomaticTab calls and size/maxlength mismatches. *sigh*

These things were annoying, but did not stop me from completing the form. However, when I tried to submit it, I got this:
Please be sure the YEAR of birth is a 4 digit valid century year.

It focused and selected the Employee Date of Birth : Year field, but that looked fine (as far as I could tell, since only 3 digits were visible):
myCIGNA Request ID Card form - Employee Date of Birth with alleged invalid year

Since I knew the AutomaticTab function would get in the way if I tried to use the cursor keys within the year field, to verify all 4 digits, I disabled it by entering this in my address bar:
javascript:void(AutomaticTab=function(){})

Then, I was able to determine that the year field was fine. It contained ‘1234′, which is what I intended (for the purpose of this blog entry. I entered my real DOB info originally).

So, I looked at the page’s source code again, and found the place where that error message came from. It was in a function called “check_date”. Since I knew my date was valid, I disabled the date validation by putting this in my location bar:
javascript:void(check_date=function(){return true;})

This allowed me to submit the form. This is also a good example of why it’s important to not rely solely on client-side validation (javascript) when writing web applications. Javascript validation code can be viewed, modified, and even disabled. I could have submitted any year value I wanted, since I disabled their javascript date validation function. Hopefully, they have server-side validation as well, but I did not care to find out. I’m not interested in breaking my health insurance provider’s website (they’re pretty good at that, themselves!).

January 22nd, 2008

Firefox crashes when I close gmail, so I tried out Safari

This has been going on for way too long. It definitely started after Google released the new interface for gmail, but it didn’t happen immediately. Now, I’m not blaming Firefox, and I’m not blaming Google. I’m 95% certain that it’s the result of conflicts between one or more of my extensions. I’m always changing up my extensive (hehe) lineup of extensions, and many of those extensions dramatically impact the way the browser works. I love web development extensions, debugging tools, power toys, etc.

Thankfully, I recently read an entry on Mishoo’s blog, which started out describing some [presumably completely unrelated] Firefox performance issues, and ended with a short, positive review of Safari. I decided to give it a try, for curiosity’s sake. I tried Safari when the Windows version was first released, and I remember not being impressed, to say the least. I don’t remember what I didn’t like, but I do remember uninstalling it immediately, so it must have been pretty bad. I don’t use Opera every day, but I do respect it enough to keep it installed and reasonably up-to-date.

Anyways, I tried the newest version of Safari, version 3.0.4, and was pretty impressed. The interface was attractive, simple, and fairly intuitive. Just what one might expect from Apple, I guess. I’m not a big Apple fan; I do not own an iPod or an iPhone, I don’t use iTunes, and have never owned a Mac. But even I know that Apple has a reputation for attractive, simple, and intuitive products.

Using Safari hardly felt different from using Firefox, to me. Almost all I do miss my Firefox extensions, though, and Safari’s Bookmarks menu leaves a lot to be desired. But it is fast, and I almost all the websites I’ve been to have looked and acted just fine. Best of all, it doesn’t crash when I close gmail!

So, where I had been using Firefox for 99% of my personal browsing and Internet Explorer for 1%, I now find myself splitting that 99% pretty equally between Safari and Firefox (and IE for 1%, since some sites just demand it).

One of these days I’ll probably try running Firefox in Safe Mode, and if closing gmail doesn’t crash it, I’ll start disabling one extension after another, until I find the culprit. For now, though, I’m content using Safari part of the time.

December 27th, 2007

A couple of lesser-known Windows-key shortcuts

I’m a die-hard keyboard user, and always like to know when there’s a keyboard shortcut to accomplish something that might otherwise take a few strategic mouse clicks. The Windows key, while a bane to gamers’ existence, actually provides us with some useful functions.

I’ve known about several Windows key shortcuts for years, but there are a couple that I learned about more recently (sometime in 2007, I think), which I now use on a daily basis. The title of this post refers to these as ‘lesser-known’, but I base that purely on the fact that *I* did not know about them, not on any sort of user experience testing or published statistics.

Windows-D: Show Desktop

Hides all open windows, bringing the Windows desktop to the front. This duplicates the functionality of the ‘Show Desktop’ icon that exists in the Quick Launch toolbar by default, and the ‘Show the Desktop’ option in the context menu that appears when you right-click the taskbar. It does not actually minimize the other windows, and those windows can be re-focused via Alt-Tab or by clicking their taskbar entries.

Windows-L: Lock Computer

This duplicates the functionality of the ‘Lock Computer’ button in the Ctrl-Alt-Delete Windows Security dialog. On Windows XP systems that use Fast User Switching, it takes you back to the screen that lists user accounts.

I make it a habit of hitting Windows-L whenever I’m done using my PC at home, so that when my wife sits down to use it, she can just click her name and login (or resume her existing session). At work, it’s always a good idea to lock your computer, so I hit Windows-L whenever I get up to leave my desk.


Now, here are a few more well-known (i.e. I’ve known about them for a long time) ones:Windows-M: Minimize AllThis minimizes all open windows. Programs that hide out in the system tray when you minimize them will be sent to the system tray. Everything else will be minimized to the taskbar. This has the same effect of clicking the ‘Minimize’ icon in the top-right corner of all of your open windows.Windows-F: Search for Files and Folders

This pops up the Windows file search feature. This duplicates the functionality of clicking the Start Menu, clicking the ‘Search’ menu option, and clicking the ‘For Files or Folders…’ sub-menu option.

Windows key (by itself): Open the Start Menu

This pops up the Start Menu, just like what happens when you press Ctrl-Esc or click the start menu. I use this all the time.


Please note that I’ve still never used Windows Vista, so I am unfamiliar with any Windows-key shortcuts which may have been introduced with it. The shortcuts I’ve described, above, are valid in Windows XP Professional (and most of them were usable in previous versions of Windows as well). I did read about a change to the way the Start Menu works, in Vista, with the introduction of a smart search box.

If you know of any useful Windows-key shortcuts that I may have missed, for ANY version of Windows, please leave a reply and let me (and other readers) know!