Sunday, August 05, 2007

ColdFusion 8 and Apache Derby - Create and Drop Tables and Views

So today I decided to mess with the Embedded Derby Database in ColdFusion 8. I wanted to figure out how to check for table and view existence and how to create tables and views. So below I will explain how I did it along with my test code at then end.

OK, so first we have to create the new database, which is very easy as explained by Ben Forta in his Blog. The only thing it is actually easier because the final release does have the create checkbox so you do not have to add create=true to the advanced settings string.(Pictured Below)


Once you complete the steps above, your database is ready to use. So now we create some tables and views. First, I wanted to check the existence of tables and views so I can drop items if they exists. This is easy now with the new <cfdbinfo> tag, which lets you retrieve information about a data source, including details about the database, tables, queries, procedures, foreign keys, indexes, and version information about the database, driver, and JDBC. (info on cfdocs).

I then use the results to check if the tables I want to create currently exists, good practice as you are building your DB, because if you try to create a table that already exists the process will break. So therefore, I save the results to a list that I later use to check for existence (I wanted to just use ListFind rather then write a query of a query everytime I wanted to check). After that, I just used SQL code to create my tables and views. To find out more about Apache Derby go to Apache Derby Docs, I used the manuals to see how the CREATE STATEMENTS had to be formed.

I believe the code below explains it all. Now there is no more need for Access Databases for small projects, Woo Hoo!!!!! I included an image of the output after the code ....

<!--- Use cfdbinfo to get the tables of the database --->
<cfdbinfo type="tables" datasource="myDerbyDB" name="dbdata">
<!--- Check if USERS table exists --->
<cfquery name="tableExists" dbtype="query">SELECT * FROM dbdata WHERE TABLE_TYPE='TABLE'</cfquery>
<cfquery name="viewExists" dbtype="query">SELECT * FROM dbdata WHERE TABLE_TYPE='VIEW'</cfquery>
<cfscript>
// Save results into list values to check for existance
tables=valueList(tableExists.TABLE_NAME);
views=valueList(viewExists.TABLE_NAME);
</cfscript>
<!--- Drop Views First Due to Dependency Check --->
<cfif listFindNoCase(views,'VIEW_CARS_RELATED')>
<cfquery name="dropView1" datasource="myDerbyDB">DROP VIEW VIEW_CARS_RELATED</cfquery>
</cfif>
<cfif listFindNoCase(views,'VIEW_CARS_ALL')> <cfquery name="dropView1" datasource="myDerbyDB">DROP VIEW VIEW_CARS_ALL</cfquery>
</cfif>
<!--- Drop Tables if exists --->
<cfif listFindNoCase(tables,'CARS')>
<cfquery name="dropTables" datasource="myDerbyDB">DROP TABLE CARS</cfquery>
</cfif>
<cfif listFindNoCase(tables,'COMPANY')>
<cfquery name="dropTables" datasource="myDerbyDB">DROP TABLE COMPANY</cfquery>
</cfif>
<!--- Create Tables --->
<cfquery name="createTables1" datasource="myDerbyDB">
CREATE TABLE CARS
(
CARID INT NOT NULL GENERATED ALWAYS AS IDENTITY( START WITH 1,INCREMENT BY 1),
COMPANYID INT NOT NULL,
CARNAME VARCHAR(100),
PRIMARY KEY (CARID)
)
</cfquery>
<cfquery name="createTables2" datasource="myDerbyDB">
CREATE TABLE COMPANY
(
COMPANYID INT NOT NULL GENERATED ALWAYS AS IDENTITY( START WITH 1,INCREMENT BY 1),
COMPANYNAME VARCHAR(100),
PRIMARY KEY (COMPANYID)
)
</cfquery>
<!--- Create Views --->
<cfquery name="createView1" datasource="myDerbyDB">
CREATE VIEW VIEW_CARS_RELATED
AS
SELECT CARS.CARID,CARS.CARNAME,COMPANY.COMPANYNAME
FROM CARS
INNER JOIN COMPANY ON COMPANY.COMPANYID=CARS.COMPANYID
</cfquery>
<cfquery name="createView2" datasource="myDerbyDB">
CREATE VIEW VIEW_CARS_ALL
AS
SELECT CARS.CARID,CARS.CARNAME,COMPANY.COMPANYNAME
FROM CARS
LEFT OUTER JOIN COMPANY ON COMPANY.COMPANYID=CARS.COMPANYID
</cfquery>
<!--- Loop to create the first 10 cars --->
<cfloop from="1" to="10" index="i">
<cfquery name="insertCars" datasource="myDerbyDB">
INSERT INTO CARS (COMPANYID,CARNAME) VALUES (#i#,'CAR_#i#')
</cfquery>
</cfloop>
<!--- Loop to create the first 10 companies --->
<cfloop from="1" to="10" index="i">
<cfquery name="insertCompanies" datasource="myDerbyDB">
INSERT INTO COMPANY (COMPANYNAME) VALUES ('COMPANY_#i#')
</cfquery>
</cfloop>
<!--- Add one more record without relation to a company --->
<cfquery name="insertCar" datasource="myDerbyDB">
INSERT INTO CARS (COMPANYID,CARNAME) VALUES (0,'CAR_11')
</cfquery>
<!--- Get Cars with Companies --->
<cfquery name="getCars1" datasource="myDerbyDB">SELECT * FROM VIEW_CARS_RELATED</cfquery>
<cfquery name="getCars2" datasource="myDerbyDB">SELECT * FROM VIEW_CARS_ALL</cfquery>
<!--- Dump the queries --->
<cfdump var="#getCars1#">
<cfdump var="#getCars2#">

Saturday, July 21, 2007

Sirius Player For Mac!

So today I was browsing thru the apple downloads section and found SiriusMac, which is a slick easy to use Radio Interface that streams Sirius Radio to your mac and it is Free! This is great because normally, I have to open up a browser, navigate to the sirius site, select the player (I have a bookmark but still), login then select the station and there you go. I know it is not much but it is still tedious. I wrote a blog entry on this too because they stream using windows media on their site that didn't work well with my Mac which required me to get Flip4Mac WMV (another great application) and firefox would always send me an alert about a missing plug-in - drove me crazy. As far as Safari, it didn't work great either, sometimes the player would reposition itself - crazy. So lately, I started bringing up Sirius on my PC via parallels, as I always have it on while developing (so imaging the steps just to hear Sirius on my Mac).

This player is great, just go thru the simple set up and bam! You got Sirius radio on your mac (Sirius Subscription Required). I love it! I suggest if you use this app and really appreciate what they've done, that a small donation is made, they deserve it!

The application, I believe was built using python.

To get it just go to the following links...

Apple Downloads

or

SiriusMac Site

Until next time ...

UPDATE: SiriusMac is programmed entirely in AppleScript Studio.

Thursday, June 28, 2007

iTunes Store Album Browser Window function in HTML

Ok so I had a hard time figuring what to call this entry, so if I confuse many of you I apologize, but I did warn before that I am not a great writer. The reason for this entry is for a feature I was asked to do not to long ago and failed miserably at. The feature was to create a scroll area that was similar to iTunes album browse interface (pictured below).

Basically you have a content area that displays a certain amount of items and when you click on either the left or right arrow all the contents shift to show the new ones. This is easy to do in Flash, but I needed an HTML equivalent of it as this site was not in Flash. So first I set out to define the divs that would encompass this feature (picture below).

So what exactly is this above? Let me explain. The entire viewable area is what I defined as "frame" this will hold all the child elements such as the left and right buttons, the scrollable area ("scroller") and the content area ("scrollcontent"). Once this was set up I decided to use my favorite animation library (script.aculo.us) and javascript framework (prototype).

My first attempt I used was with the "Photoshop Masking" mentality. Basically, the scroller div would be a defined width div with overflow set as hidden and the content area would be wider child div displaying the content. The scroller div would act as the mask as I move the content area x position. This first attempt worked great on all browsers expect for GUESS WHO!? Yeap, IE. Every time I would call the animation the child div would go to the top level and the masking of the parent div would no longer be active. This only happened in IE and at the time I could not figure out why so I decided to go with another way to display the data. Of course, this drove me crazy and I needed to make it work, so eventually I ran into the "Panic - Coda" website, where they had implemented this exact feature. I needed to know how they did this, so I decided to inspect their javascript files.

I am not going to explain exactly how they did it all, instead I decided to take it to the simplest form. The example I created will allow you to go left and right with the arrows until you reach the beginning or end of which at that point the arrow that represents further scrolling will disappear (this is not available in theirs). Inspecting their files taught me the following.

  • ScrollLeft: gets or sets the number of pixels that an element's content is scrolled to the left. This is what makes this possible as it can be used in divs where there is an overflow, even if it is set to hidden. There is also a ScrollTop property if you want to scroll vertically.
  • The use of Robert Penner's Easing Equations, the source is in actionScript but is easily implemented as JavaScript functions/
  • The use of setInterval in javascript - I had no clue, just like actionScript

Compared to my first draft, this new one actually never moves the position of the scroll content div, instead it changes the scroll position of the scroller div. This now works in all browsers! Yeah!

In the end I created 2 examples, one implemented by using part of Panic's Coda Website javascript functions (again simplified) and the second one using the Yahoo UI Library. I know I said I favored script.aculo.us but in the end I am a Coldfusion Developer and by what I heard some of the new ajax functions are using this library, so why not take it for a spin. I have to say the Yahoo UI Library is pretty freaking slick. Anyways, click below to view the example, the source files do good of explaining how everything works but if anyone needs help understanding just let me know and I will clarify.

Until next time ...

Monday, June 18, 2007

URL Rewrite FOR IIS with RegEx support and it isFREE!!!

Well, as I was learning how to do SES (Search Engine Safe) URLs with Coldfusion and IIS , I learned that CF had a built in feature that allowed you to do this. The only problem was that it still required you to have the *.cfm file in your address and your variables/values following separated by "/" rather than the default url patterns.

ie. http://mysite.com/index.cfm/myVariable/itsValue/
translates to
http://mysite.com/index.cfm?myVariable=itsValue

Honestly, this was fine for me, but I wanted to have the ability to rewrite URLs outside of Coldfusion, like Apache's mod_rewrite, which in turn would allow me to create true pretty URLS.

ie. http://mysite.com/theValue/
translates to
http://mysite.com/index.cfm?theVariable=theValue

I did some research online regarding ISAPI filters for IIS but most lacked Regular Expression Support unless you were willing to fork out some extra money for paid for versions. Then, Ray Camden introduced me to IIRF (Ionic's ISAPI Rewrite Filter), a small, cheap, easy to use, URL rewriting ISAPI filter that combines a good price (free!) with good features.

This product is amazing, not only because of price (I did mention it was free right?) but implementation is easy. Their directions are thorough and supply enough examples that you will be up and running in minutes. Thanks for the info Ray!

Click here to go to Ionic's Site

Until next time ...

SES Not Enabled in CF8 by Default

This is a follow up for my previous post regarding the ability to create SES (Search Engine Safe) URLs with Coldfusion. If you are using the beta version of Coldfusion 8 (aka: Scorpio), these setting are not on by default. You will have to open up your web.xml file and make uncomment the following lines;
<!-- begin SES --->
  <servlet-mapping id="coldfusion_mapping_6">
     <servlet-name>CfmServlet</servlet-name>
     <url-pattern>*.cfml/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping id="coldfusion_mapping_7">
    <servlet-name>CfmServlet</servlet-name>
    <url-pattern>*.cfm/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping id="coldfusion_mapping_8">
    <servlet-name>CFCServlet</servlet-name>
    <url-pattern>*.cfc/*</url-pattern>
  </servlet-mapping>
<!--- end SES -->
You will also notice that the mappings are now called "coldfusion_mapping_#" rather than "macromedia_mapping_#" as noted in the adobe tech note : ColdFusion MX 7 and Search Engine Safe (SES) URLs You can find the web.xml in one of the two places:
  • Standalone Install:cf_root\wwwroot\WEB-INF
  • EAR\WAR installation:application root\cfusion-ear\cfusion-war\WEB-INF
Remember you must restart your server to have the changes take place.

Until next time ...