/** * Remove an object. * @param ChunsuObject $removeme The object to remove. * @param DataSource $source The data source to remove the object from. * @return bool TRUE is successful, FALSE otherwise. */ function remove(&$removeme, &$source) { if (IsLogEnabled('DEBUG')) { LogDebug("removing " . get_class($removeme), $this); } return FALSE; }
/** * Save an object. * @param ChunsuObject $saveme The object to save. * @param DataSource $source The data source to save the object to. * @return bool TRUE is successful, FALSE otherwise. */ function save(&$saveme, &$source) { parent::save($saveme, $source); if ($saveme->isChanged()) { if ($saveme->config->get('create-on-save') && $saveme->is_new) { $saveme->commit(); // flatten changes into core $gen = new SQLGenerator($saveme->getCore()); $savequeries = $gen->replace($this->config); } else { $params = $saveme->changeset; $pk = $this->config->getPrimaryKey(); $pkalias = $pk->get('alias'); $params[$pkalias] = $saveme->get($pkalias); foreach ($this->config->getForeignKeys() as $fk) { $fkalias = $fk->get('alias'); $params[$fkalias] = $saveme->get($fkalias); } $gen = new SQLGenerator($params); $savequeries = $gen->update($this->config); } foreach ($savequeries as $sq) { $cursor =& $source->query($sq); if (!$cursor->getNext()) { LogError("save query failed! saving " . print_r($saveme, TRUE)); return FALSE; } } $saveme->is_new = FALSE; if ($pkval = $cursor->get('generated-id')) { $pkfield =& $this->config->get('primary-key'); $pka =& $pkfield->get('alias'); // for "replace into", treat this save as a load $saveme->is_loaded = TRUE; $opkval = $saveme->get($pka); if ($opkval == 'new') { $opkval = NULL; } if (!is_null($opkval) && $pkval != $opkval) { LogError("Primary key mismatch: {$pkval} != {$opkval} saving " . $saveme->trace()); LogFatal("Primary key mismatch: {$pkval} != {$opkval}"); } // reset the primary key with the value assigned by the // data source $saveme->set($pkval, $pka); $saveme->commit(); if (IsLogEnabled('DEBUG')) { LogDebug("set {$pka} to {$pkval}: " . $saveme->trace()); } } if ($rows = $cursor->get('affected-rows') > 1) { LogWarning($rows . " records written saving " . print_r($saveme, TRUE)); } } return TRUE; }
/** * Shut down the data source. * <p>When a fatal log event is not encountered, but FALSE is returned, * it means that the shutdown mechanism has not been implemented for * the data source a particular level. This behavior is only expected * when calling DataSource::shutdown() explicitly. * @return bool TRUE if successful, FALSE otherwise. */ function shutdown() { if ($this->isConnected) { if (IsLogEnabled('DEBUG')) { LogDebug("shutting down: " . print_r($this, TRUE), $this); } $this->isConnected = FALSE; return FALSE; } else { if (IsLogEnabled('DEBUG')) { LogDebug("already shut down", $this); } return TRUE; } }
/** * Get a configuration object. * @param mixed $initialConfig The external configuration object or array to * use for setting up initial values. May be an existing Configuration * object, in which case no other is constructed. */ function &GetConfigurationRef(&$initialConfig) { global $GETCONFIGURATION_HELPER; if (is_a($initialConfig, "AtsumiObject")) { $chelp = $initialConfig; } else { $GETCONFIGURATION_HELPER->setref($initialConfig); $chelp = $GETCONFIGURATION_HELPER; } if ($chelp->has('configuration-class')) { $configclass = $chelp->get('configuration-class'); } else { $configclass = 'Configuration'; } if (!class_exists($configclass)) { if (IsLogEnabled('ERROR')) { LogError("Configuration class {$configclass} is not defined:\n" . print_r($initialConfig, TRUE)); } LogFatal("Invalid configuration"); $rv = FALSE; return $rv; } else { if (is_a($initialConfig, $configclass)) { $rv =& $initialConfig; } elseif (is_a($initialConfig, 'Configuration')) { $ic =& $initialConfig->get(); $rv = new $configclass($ic); } else { $rv = new $configclass($initialConfig); } if (!is_a($rv, 'Configuration')) { if (IsLogEnabled('ERROR')) { LogError("{$configclass} is not a Configuration subclass:\n" . print_r($initialConfig, TRUE)); } LogFatal("Invalid configuration"); $rv = FALSE; } return $rv; } }
/** * Create an AtsumiService object from a configuration. * @param mixed $config Either a Configuration object or something else * that may be used as the core representation of a Configurtion. * @param string $targetClass The class of service to be created. Target * class should be a subclass of AtsumiService, but a superclass of the * object returned by GetService. The class of the returned object is * specified in the configuration as 'service-class'. * @return AtsumiServie A service implementation. Returns FALSE if the * service could not be created and the fatal error handler doesn't kill * the script. */ function GetService(&$config, $targetClass) { if (!class_exists($targetClass)) { if (IsLogEnabled('ERROR')) { LogError("Target class {$targetClass} does not exist\n" . print_r($config, TRUE)); } LogFatal("Invalid service target class"); return FALSE; } $cf =& GetConfigurationRef($config); $serviceclass = $cf->get('service-class'); if (!$cf->has('service-class')) { if (IsLogEnabled('ERROR')) { LogError("Configuration does not specify a service class:\n" . "Please set the 'service-class' configuration option.\n" . "Target: {$targetClass}\n" . print_r($cf, TRUE)); } LogFatal("Invalid service configuration"); return FALSE; } elseif (!class_exists($serviceclass)) { if (IsLogEnabled('ERROR')) { LogError("Serivce class {$serviceclass} is not defined:\n" . "Target: {$targetClass}\n" . print_r($cf, TRUE)); } LogFatal("Invalid service configuration"); return FALSE; } else { $rv = new $serviceclass($cf); if (!is_a($rv, $targetClass)) { if (IsLogEnabled('ERROR')) { LogError("Configuration does not specify a service class:\n" . "Class {$serviceclass} is not a subclass of {$targetClass}.\n" . print_r($rv, TRUE) . print_r($cf, TRUE)); } LogFatal("Invalid service configuration"); return FALSE; } elseif (!is_a($rv, 'AtsumiService')) { if (IsLogEnabled('ERROR')) { LogError("Configuration does not specify a service class:\n" . "Class {$targetClass} is not a subclass of AtsumiService.\n" . print_r($rv, TRUE) . print_r($cf, TRUE)); } LogFatal("Invalid service configuration"); return FALSE; } else { return $rv; } } }