Databases and Drivers

evantahler edited this page May 1, 2011 · 5 revisions

Databases and Drivers in DAVE

DAVE is designed to be used with multiple database types. At the time of this writing, mySQL and Mongo are supported to provide an example of both a relational and document database. The code that is specific to each database is stored in /API/DB/DRIVERS/[Database_name] and is is configured by setting the database type in CONFIG.php. Every database driver needs an init.php, which will load the remaning files. The high-level goal of the driver are:

  • Ensure a database connection
  • create the global $DBOBJ
  • Set the _DAVE methods
  • Set and database-specific methods
  • ensure that SCHEMA.php is up to date (relational DB's only)

Hopefully, looking at the example drivers will provide insight into each file's purpose, but here are the goals of each file:

init.php

  • Load remaning DB files in the proper order, ensure a connection

ConnectToDatabse.php

  • Connect to the Database
  • Create $DBOBJ and provide a consistant interface

DirectDBFunctions.php

  • Create the collection of of functions which will talk to the database directly (Other than the _DAVE methods).
  • Some methods (like start transaction) may not be applicable to all database types. If this is the case, return true.
  • Note that the entire collection of methods below is required:
    • _GetAPIRequestsCount()
    • _LogAPIRequest()
    • _DBSetCache()
    • _DBGetCache()
    • _StartTransaction()
    • _CreateDBSaveState()
    • _RestoreDBSaveState()
    • _TruncateTable()
    • _CleanSessions()
    • _CleanLog()
    • _CleanCache()
    • _CountRowsInTable()
    • _FindDBMaxValue()
    • _FindDBMinValue()

DAVE.php

  • Using the functions created in ConnectToDatabase.php and DirectDBFunctions.php, DAVE builds the _ADD, _EDIT, _VIEW, and _DELETE methods for the API.
  • These methods should follow the consistant structure of passing ($TableName, $Parameters) (and the optional $Settings for _VIEW).
  • These methods are where variable uniqueness is maintained and required variables are checked.
  • These methods should return results as an array with the first entry being true or false, and the second being the array of objects found or a human readable error message accordingly IE _view might return array(true,array(obj1, obj2)) and _delete might return array(false,"that object cannot be found")
  • These methods should also escape input properly

Driver specific notes and philosophy

MySQL

  • Designed to read table/col information from the database
  • Denoting a column as unique or non_null will be understood by DAVE, and those checks will be preformed by the API before interacting with the Database
  • You don't need to configure SCHEMA.php
  • All column names are translated into acceptable $POST_VARIABLES and will be allowed by the API.

Mongo

  • Designed to allow maximum flexibility, and maintain the non-relational goal of using undefined attributes on object, The Mongo driver will not attempt to read any "column" level information from the Database
  • SCHEMA.php will need to be created by hand for each table/collection of objects. The META KEY should always be _ID for mongoDB.
  • When creating SCHEMA.php, you can list all attributes (if you know them), or only those that need to be either unique or are requires. Other attributes passed into _ADD or _EDIT actions will be properly stored.
  • Due to not pre-parsing the Databases' schema, DAVE cannot predetermine the collection of $POST_VARIABLES to accept. DAVE will still include those variables found in SCHEMA.php, but be sure to add additional variables you might want to POST_VARIABLES.php