<cfscript>

	// Let's clear the tokens table and reset the AUTO_INCREMENT value.
	truncateTokens()

	// Create our first token - we know this will succeed since we just cleared the table.
	value = createUniqueId();
	id1 = createTokenOrIgnore( value );

	dump( id1 );

	// These will all be no-ops, since we're trying to insert the same token over and
	// over. As such, ZERO will be returned.
	dump( createTokenOrIgnore( value ) );
	dump( createTokenOrIgnore( value ) );
	dump( createTokenOrIgnore( value ) );
	dump( createTokenOrIgnore( value ) );

	// Now, let's try to insert a new token, which will result in a new AUTO_INCREMENT ID.
	id2 = createTokenOrIgnore( value & "new" );

	dump( id2 );

	// ------------------------------------------------------------------------------- //
	// ------------------------------------------------------------------------------- //

	/**
	* I insert the given token value and return the associated ID. If the token is already
	* in the table, ZERO is returned.
	*/
	public numeric function createTokenOrIgnore( required string value ) {

		```
		<cfquery name="local.results" result="local.metaResults">
			INSERT IGNORE INTO
				token
			SET
				value = <cfqueryparam value="#value#" sqltype="varchar" />
			;
		</cfquery>
		```

		return( val( metaResults?.generatedKey ) );

	}


	/**
	* I truncate the tokens table, resetting the AUTO_INCREMENT value.
	*/
	public void function truncateTokens() {

		```
		<cfquery name="local.results" result="local.metaResults">
			TRUNCATE TABLE
				token
			;
		</cfquery>
		```

	}

</cfscript>