ColdFusion MySQL Search Utility

There’s a legacy app I work on that currently requires adding 4 columns and a couple rows (in different tables) every time you add a row to another table. It doesn’t happen often and it would be a nightmare to refactor so I wrote little script that would search the database for any columns named ‘x’ as well as any fields with a value of ‘x’ and return an array of the offending tables.

I cleaned it up a little bit and cfc-ized it in case I ever have to do anything similar. I haven’t tested it at all aside from the one time I ran it today so use it at your own risk. It’s really simple to use, you just need to pass in the ColdFusion datasource name and the search term.

Example Usage:



Looking for column #search_term#



Looking for value  #search_term#

Download ColdFusion MySQL Search Utility!

PS: it won’t search numeric columns if you aren’t searching for a number.

Ruby Line / Curve…Thing

Remember these?

During a brief period in elementary school drawing these things was quite the cool thing to do. I’ve been messing around with the GD2 module a bit lately and I thought it’d be fun and easy to write a little script to generate one.

It was.

The drawing algorithm

@nodes.times do |i|
    adjustment = i * @node_size
    pen.move_to adjustment, 0
    pen.line_to 0, @size - adjustment
end

Example Usage:

#takes in the output image size and the number of nodes
c = Curve.new 180, 30
c.draw
c.export "test.gif"

Download it!

*Note: If your output size doesn’t divide evenly by the number of nodes then the image will actually reduce the size of the output image.

Ruby Resize Utility

I wanted to demo an ad rotator the other day at work and I wanted to load up a bunch of images of various sizes to make it look legit. It would have taken forever to do it ad by ad, so I wrote a little ruby script to automatically resize all the images in a directory. Then I used my directory listing utility to copy the files into my clipboard. Pasted into the database from there!

(Requires GD2)

Usage:

 r = Resizer.new input_path, output_path, width, height
 r.crop_files

Download it!

JavaScript Tile Puzzle

I was tinkering a bit while watching the tube tonight and I thought it might be fun to make a simple JavaScript tile puzzle. Might look terrible in other browsers.

First step was to write a little ruby script (using the GD Graphics Library) to read in a file and cut it up into individual tiles. (Photoshop is for wussies)

if __file__ = $0
  #pass the image name, tile size through the command line
  t = Tile_Cropper.new ARGV[0], ARGV[1].to_i
  t.crop_tiles
end

Then I used a slightly modified method I read about in Ben Nadel’s blog to scramble the tiles.


	
	
	
	
		
	
	

A little bit of CSS magic:

#puzzle {
	width:      500px;
	margin:     0px;
	padding:    0px;
}
.piece {
	list-style: none;
	display:    inline;
	margin:     0px;
	padding:    0px;
}
img {
	border:     1px solid gray;
}

And finally I used scriptaculous and prototype for the obnoxious drag-and-drop sorting. Not that I’m complaining. the Sortable methods weren’t intended to be used for something like this.

function init() {
	Sortable.create('puzzle');
}

Complete the puzzle to find out what I was watching!. Here, I’ll give you a hint. It’s Dexter.

Download the code.

3 Month Anniversay Re-Blogging Evaluation!

When I first fired up this blog I thought I’d be posting a lot about the kind of problems and solutions I bang my head against daily at work. But instead it’s evolved into me posting a lot of the little scripts that I play around with while I’m watching tv or doing laundry or whatever else.

Sometimes I worry that what I’m posting might give one the impression that I’m a bit of a programming simpleton. In order to offset this I’ve thought about posting about some more advanced topics, but my heart just isn’t in it. For whatever reason I’ve really been enjoying playing with and posting my little programs…so that’s what I’m going to do!

MySQL Capitalize Function

I wrote a little MySQL function to capitalize the first letter of every word in a string. I thought I’d share since I wasn’t able to google for it.

CREATE FUNCTION CAP_FIRST (input VARCHAR(255))

RETURNS VARCHAR(255)

DETERMINISTIC

BEGIN
	DECLARE len INT;
	DECLARE i INT;

	SET len   = CHAR_LENGTH(input);
	SET input = LOWER(input);
	SET i = 0;

	WHILE (i < len) DO
		IF (MID(input,i,1) = ' ' OR i = 0) THEN
			IF (i < len) THEN
				SET input = CONCAT(
					LEFT(input,i),
					UPPER(MID(input,i + 1,1)),
					RIGHT(input,len - i - 1)
				);
			END IF;
		END IF;
		SET i = i + 1;
	END WHILE;

	RETURN input;
END;

So running the following code...

SELECT CAP_FIRST(
	'this is totally like   @ TEST 1 right!' 
)

Returns the string "This Is Totally Like @ Test 1 Right!"

I would rather have regex'd it, but I couldn't find any sort of regex replace function in the docs.