Flash to RDB

Code sample

This Actionscript extract shows how chord data queries are handled.

When the host movie needs data, it calls the request function. Data is cached locally, to prevent repeat searches.
// initialise timeline variables
cache = new Array();  // local chord cache
match = new Array();  // search matches

// declare functions
// note: "root" is the desired chord's musical root, eg A
//       "type" is the chord's type, eg maj7
function request(root, type)
{
  // request chord data.
  // previously requested data will be cached locally.

  if (isCached(root, type))
  {
    // the chord is in the cache.
    // collect the cached data
    // and send it back to root
    _root.searchResult = collectFromCache();

    // display the results
    _root.displayResult();
  }
  else
  {
    // the target chord is not in the cache,
    // so search for it in the database
    fetchData(root, type);
  }
}

function isCached(root, type)
{
  // search the local cache for the target chord

  // refresh the match array
  delete match;
  match = new Array();

  // do the search
  var i;
  for (i = 0; i < cache.length; i++)
  {
    if (cache[i][0] == root && cache[i][1] == type)
    {
      // match found
      match.push(i);
    }
  }
  if (match.length) 
    return true;
  else
    return false;
}

function collectFromCache()
{
  // collect chord data from the local cache
  if (!match.length)
    return null;

  chordsFound = new Array();
  var row;
  for (row = 0; row < match.length; row++)
  {
    chordsFound.push(cache[match[row]]);
  }

  return chordsFound;
}

function fetchData(root, type)
{
  // search the database
  trace("in fetchData()...");

  // query MySQL for the data
  loadVariables("http://www.chordbook.com/search.php?root="
                 +root+"&type="+type, "", "POST");

  // note: the search results are caught,
  // cached and collected 
  // in this clip's data event handler
}