Skip to content

Instantly share code, notes, and snippets.

@jeaguilar
Created June 15, 2017 19:42
Show Gist options
  • Save jeaguilar/2db5c15385fb6000ffb3a025ca016140 to your computer and use it in GitHub Desktop.
Save jeaguilar/2db5c15385fb6000ffb3a025ca016140 to your computer and use it in GitHub Desktop.
When moving from Lucee 4.5.x to Lucee 5.1.x queries that depended on Now() would no longer work. Using a MySQL log, I determined that queries were being sent with a millisecond value, which would not match records stored in a datetime column.
<cfscript>
try {
/*
Create the table which will save the timestamps
Use datetime(3) for a column which will save the millisecond value
CREATE TABLE `test_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`dta` datetime(3) DEFAULT NULL,
`dtb` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
*/
if (!isDefined("session.a")){
// Store a Now() value into the session and create a new timestamp using createDateTime
session.a = Now();
session.b = createDateTime( Year(session.a), Month(session.a), Day(session.a), Hour(session.a), Minute(session.a), Second(session.a) );
// Save it in the test_table
query name="ins" datasource="#application.dsn#" {
echo("INSERT INTO test_table (dta, dtb) VALUES (");
queryparam sqltype="timestamp" value=createODBCDateTime(session.a);
echo(", ");
queryparam sqltype="timestamp" value=createODBCDateTime(session.b);
echo(")");
}
// Dump it for good measure and note that neither shows a millisecond value
writeDump(session.a);
writeDump(session.b);
echo("<a href='?'>Query Table</a>");
} else if (!isDefined("url.purge")) {
// This query uses the Now() value stored in session.a and queries against the dta column with the datetime(3) datatype
query name="sela" datasource="#application.dsn#" {
echo("SELECT * FROM test_table WHERE dta = ");
queryparam sqltype="timestamp" value=createODBCDateTime(session.a);
}
// Expect one row to be returned
writeDump(sela);
// This query uses the timestamp value created with createDateTime stored in session.b and queries against the dtb column with the datetime datatype
query name="selb" datasource="#application.dsn#" {
echo("SELECT * FROM test_table WHERE dtb = ");
queryparam sqltype="timestamp" value=createODBCDateTime(session.b);
}
// Expect one row to be returned
writeDump(selb);
// This query uses the timestamp value created with createDateTime and queries against the dta column with the datetime(3) datatype
query name="selafrb" datasource="#application.dsn#" {
echo("SELECT * FROM test_table WHERE dta = ");
queryparam sqltype="timestamp" value=createODBCDateTime(session.b);
}
// Note that the output of the timestamp in the query does not show a millisecond value
writeDump(selafrb);
// This query uses the Now() value stored in session.a and queries against the dtb column with the datetime datatype
query name="selbfra" datasource="#application.dsn#" {
echo("SELECT * FROM test_table WHERE dtb = ");
queryparam sqltype="timestamp" value=createODBCDateTime(session.a);
}
// Note that the output of the timestamp in the query does not show a millisecond value
writeDump(selbfra);
// Purge the session
echo("<a href='?purge=true'>Purge Session</a>");
} else {
// Clear the session struct to restart the process
structDelete(session, "a");
structDelete(session, "b");
echo("<a href='?'>Restart</a>");
}
}
catch (Any excpt) {
writeDump(excpt);
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment