Permalink
Browse files

cleanup, renamiing, documentation

  • Loading branch information...
1 parent 63c921e commit 5bbde0cb7c6078633a595f63c018cb19ebb254b8 @evantahler committed Apr 25, 2011
View
@@ -1,7 +1,7 @@
favicon.gif
favicon.ico
API/CONFIG.php
-API/DB/TABLES.php
+API/DB/SCHEMA.php
API/log/*
SPEC/log/*
SERVER/log/*
View
@@ -37,44 +37,16 @@ function GetCache($Key)
elseif($CONFIG['CacheType'] == "DB")
{
+ // Look in DirectDBFunctions for this DB Driver
function SetCache($Key, $Value, $ThisCacheTime = null)
{
- global $CONFIG, $DBObj;
- if ($ThisCacheTime == null) { $ThisCacheTime = $CONFIG['CacheTime']; }
-
- $ExpireTime = time() + $ThisCacheTime;
-
- $Status = $DBObj->GetStatus();
- if ($Status === true)
- {
- $Connection = $DBObj->GetConnection();
- $SQL = 'INSERT INTO `'.$CONFIG['CacheTable'].'` (`Key`, `Value`, `ExpireTime`) VALUES ("'.mysql_real_escape_string($Key,$Connection).'", "'.mysql_real_escape_string(serialize($Value),$Connection).'", "'.mysql_real_escape_string($ExpireTime,$Connection).'");' ;
- $DBObj->Query($SQL);
- $Status = $DBObj->GetStatus();
- if ($Status === true){return true;}
- else { return false; }
- }
- else { return false; }
+ return _DBSetCache($Key, $Value, $ThisCacheTime);
}
+ // Look in DirectDBFunctions for this DB Driver
function GetCache($Key)
{
- global $CONFIG, $DBObj;
-
- $Status = $DBObj->GetStatus();
- if ($Status === true)
- {
- $Connection = $DBObj->GetConnection();
- $SQL = 'SELECT `Value` FROM `'.$CONFIG['CacheTable'].'` WHERE (`Key` = "'.mysql_real_escape_string($Key,$Connection).'" AND `ExpireTime` >= "'.mysql_real_escape_string(time(),$Connection).'") LIMIT 1;' ;
- $DBObj->Query($SQL);
- $Status = $DBObj->GetStatus();
- if ($Status === true){
- $Results = $DBObj->GetResults();
- return unserialize($Results[0]['Value']);
- }
- else { return false; }
- }
- else { return false; }
+ return _DBGetCache($Key);
}
}
View
@@ -12,7 +12,7 @@ RESERVERD VARIABLES THAT ARE SET BY THIS CONFIG FILE OR IN OTHER PARTS OF THE AP
- $POST_VARIABLES : Collection of names of variables the API should ingest
- $ACTIONS : Hash of API's Actions
- $ERROR : The globabl state variable (100 is the OK state)
-- $DBObj : The database connection object
+- $DBOBJ : The database connection object
- $OUTPUT : The array to be formatted and returned to the user
- $TABLES : The table description object
***********************************************/
@@ -27,8 +27,8 @@ unset($path);
// Set the ERROR. This is used by all steps to ensure that nothing has perviously gone awry, and allows the next step to excecute. All steps will first check to make sure that ERROR == 100.
$ERROR = 100;
-$CONFIG['systemTimeZone'] = "America/Los_Angeles";
-date_default_timezone_set($CONFIG['systemTimeZone']);
+$CONFIG['SystemTimeZone'] = "America/Los_Angeles";
+date_default_timezone_set($CONFIG['SystemTimeZone']);
$CONFIG['DefaultOutputType'] = "JSON" ; // XML, PHP, VAR, SOAP, LINE, CONSOLE, or JSON
$CONFIG['XML_ROOT_NODE'] = "XML"; // what is the root node of your XML output called?
$CONFIG['RequestLimitPerHour'] = 1000; // limit how many times a specific IP can use the API per hour. Set it to 0 to have no limit
@@ -58,25 +58,25 @@ $CONFIG['LogTable'] = "log";
/*********************************************************/
// database
-$CONFIG["DBType"] = "MYSQL";
+$CONFIG["DBType"] = "MYSQL"; // DB types require a 'driver' to be defined within DB/DRIVERS. Check the MYSQL folder for examples.
+$CONFIG['TableConfigFile'] = $CONFIG['App_dir']."DB/SCHEMA.php"; // The DB is periodically read and schema is written here for faster subsequent access
+$CONFIG['TableConfigRefreshTime'] = 60; // time in seconds for this application to re-poll mySQL for table layout information. 0 will never poll
+$CONFIG['DBLogFile'] = $CONFIG['LogFolder']."SQL.txt"; // comment me out for no logging of mySQL commands
$CONFIG['DB'] = "daveapi"; // the name of the database in use
+$CONFIG['LOG_DB'] = $CONFIG['DB']; // There can be a seperate DB in use for logging. The LogTable table is expected to be within this database.
$CONFIG['dbhost'] = "127.0.0.1";
$CONFIG['dbuser'] = "root";
$CONFIG['dbpass'] = "";
-$CONFIG['LOG_DB'] = $CONFIG['DB']; // There can be a seperate DB in use for logging. The LogTable table is expected to be within this database.
-
-$CONFIG['MySQLLogFile'] = $CONFIG['LogFolder']."SQL.txt"; // comment me out for no logging of mySQL commands
-
/*********************************************************/
// CRON
$CONFIG['CronLogFile'] = $CONFIG['LogFolder']."CRON_LOG.txt";
$CONFIG['MaxLogFileSize'] = 1048576 * 1; // 1MB
$CONFIG['LogsToCheck'] = array(); // log files that might get big that you want to automatically truncate
$CONFIG['LogsToCheck'][] = $CONFIG['CronLogFile'];
-$CONFIG['LogsToCheck'][] = $CONFIG['MySQLLogFile'];
+$CONFIG['LogsToCheck'][] = $CONFIG['DBLogFile'];
/*********************************************************/
// CACHE
View
@@ -13,7 +13,7 @@
$parts = explode("/",__FILE__);
$ThisFile = $parts[count($parts) - 1];
chdir(substr(__FILE__,0,(strlen(__FILE__) - strlen($ThisFile))));
-require_once("load_enviorment.php"); unset($parts); unset($ThisFile);
+require_once("LoadEnv.php"); unset($parts); unset($ThisFile);
load_tasks();
View
@@ -20,12 +20,12 @@
if ($result === false)
{
$SQL = 'SELECT * FROM `developers` WHERE (`APIKey` = "'.$PARAMS["APIKey"].'") LIMIT 1;';
- $Status = $DBObj->GetStatus();
+ $Status = $DBOBJ->GetStatus();
if ($Status === true)
{
- $DBObj->Query($SQL);
- $Status = $DBObj->GetStatus();
- if ($Status === true){ $Results = $DBObj->GetResults();}
+ $DBOBJ->Query($SQL);
+ $Status = $DBOBJ->GetStatus();
+ if ($Status === true){ $Results = $DBOBJ->GetResults();}
else{ $ERROR = $Status; }
}
else { $ERROR = $Status; }
View
@@ -53,6 +53,23 @@ function run_task($TaskName, $PARAMS = array())
return $TaskLog."\r\n";
}
+function reload_tables()
+{
+ global $ERROR, $DBOBJ, $CONFIG, $TABLES;
+
+ $Status = $DBOBJ->GetStatus();
+ if ($Status === true)
+ {
+ $TABLES = array();
+ @unlink($CONFIG['TableConfigFile']);
+ require($CONFIG['App_dir']."DB/DRIVERS/".$CONFIG["DBType"]."/TableConfig.php"); // requiring again will force a re-load
+ }
+ else
+ {
+ $ERROR = "DB Cannot be reached: ".$Status;
+ }
+}
+
function create_session()
{
$key = md5( uniqid() );
@@ -5,22 +5,42 @@
https://github.com/evantahler/PHP-DAVE-API
Evan Tahler | 2011
-I am the mySQL connection class
+I am the mySQL connection class. To create a simmilar class for other DB types, please note the functions that that DBConnection class should contain and thier implamentation:
-Here's an example:
+- __construct():
+ - Can be passsed an additional $DB than the default.
+ - Returns true on sucess
+ - Returns false on failure, logs error to $This->Status
+- mysql_log(): Will log "queries" to file
+- CheckForSpecialStrings(): Will inspect queries for special strings ($CONFIG['SpecialStrings']) and replace. This is to fix situations where user might post "0" as input, etc
+- query(): preforms the DB operation
+ - Returns true on sucess
+ - Returns false on failure, logs error to $This->Status
+- GetLastInsert(): returns the auto incramanet ID of the row added
+- NumRowsEffected(): returns a count of the rows effected by the "Edit" command
+- GetConnection(): returns the connction object if applicable
+- GetStatus(): returns the last status message
+- GetResults(): returns the result of the last query()
+- close(): closes the DB connection
- $DBObj = new DBConnection();
- $Status = $DBObj->GetStatus();
+//////////////
+
+Example useage:
+
+ $DBOBJ = new DBConnection();
+ $Status = $DBOBJ->GetStatus();
if ($Status === true)
{
- $DBObj->Query($SQL);
- $Status = $DBObj->GetStatus();
- if ($Status === true){ $Results = $DBObj->GetResults();}
- // Do stuff with the $Results array
+ $DBOBJ->Query($SQL);
+ $Status = $DBOBJ->GetStatus();
+ if ($Status === true){
+ $Results = $DBOBJ->GetResults();
+ // Do stuff with the $Results array
+ }
else{ $ERROR = $Status; }
}
else { $ERROR = $Status; }
- $DBObj->close();
+ $DBOBJ->close();
use the GetLastInsert() function to get the deatils of an entry you just added.
@@ -66,9 +86,9 @@ private function mysql_log($line)
if ($host == ""){$host = "local_system";}
$line = date("Y-m-d H:i:s")." | ".$host." | ".$line;
- if (strlen($CONFIG['MySQLLogFile']) > 0)
+ if (strlen($CONFIG['DBLogFile']) > 0)
{
- $LogFileHandle = fopen($CONFIG['MySQLLogFile'], 'a');
+ $LogFileHandle = fopen($CONFIG['DBLogFile'], 'a');
if($LogFileHandle)
{
fwrite($LogFileHandle, ($line."\r\n"));
Oops, something went wrong.

0 comments on commit 5bbde0c

Please sign in to comment.