/** * Query the data source. * <p>This class makes no assumption about the content of the query * object, but the behavior of the resulting Cursor object should be * consistent between data source implementations. * @param mixed $query Some sort of query that will be understood by * the implemented data source. * @return Cursor The result of the query, FALSE otherwise (though this * is unlikely to occur because of the fatal error event when a query * fails). */ function &query($query) { if (!($rv =& parent::query($query))) { $result = mysql_query($query, $this->myLink); if ($result) { $rv = new MySQLCursor($result, $this); } else { LogError("MySQL query failed: \n" . $query . "\n" . mysql_error()); LogFatal("MySQL query failed"); $rv = FALSE; } } return $rv; }
public static function querySql($sql, $params, $form = '', $dsname = '') { $ds = new DataSource(); if ($form != '') { $fb = FormBuilder::load($form); $ds->builder = $fb; if ($dsname != '') { $field = $fb->findField(['name' => $dsname]); $ds->params = $field['params']; } } $ds->sql = $sql; return $ds->query($params); }
/** * Query the user input. Recognizes this minimal SQL statement form: * * select key1,key2,key3,etc * from assoc_array * where id_key = value * * TODO: Currently does not recognize update, insert, or delete queries * though a short leap of logic could imagine that update and insert * queries might thunk HTML output for displaying/editing a persistent * object. I'm not sure that logic can leap so far as to delete a an * object from the user. * * @param string $query An SQL select statment of the format noted * above. * @return Cursor The result of the query, FALSE otherwise (though this * is unlikely to occur because of the fatal error event when a query * fails). */ function &query($query) { if (!($rv =& parent::query($query))) { $parts = array(); if (eregi("select(.*)from(.*)where(.*)", $query, $parts)) { $fields = array(); $table = trim($parts[2]); $where = trim($parts[3]); $wparts = array(); if (eregi("(.*)=(.*)", $where, $wparts)) { #LogDebug( $wparts ); $idfield = ExtractFieldAlias($table, $wparts[1]); $idmatch = trim($wparts[2]); $tmp = array(); if (ereg("^\\'(.*)\\'\$", $idmatch, $tmp)) { $idmatch = $tmp[1]; } } elseif (eregi("(.*) is null", $where, $wparts)) { $idfield = ExtractFieldAlias($table, $wparts[1]); $idmatch = NULL; } else { LogError("Invalid user input where clause: {$query}"); LogFatal("Invalid user input where clause"); } $gotid = FALSE; foreach (split(',', $parts[1]) as $field) { $alias = ExtractFieldAlias($table, $field); if ($idfield == $alias) { $gotid = TRUE; } $fields[] = $alias; } } else { LogError("Invalid user input query: {$query}"); LogFatal("Invalid user input query"); } $rv = array(); if (array_key_exists($table, $_REQUEST) && is_array($tin = $_REQUEST[$table])) { #LogDebug( "--$idfield--" ); #LogDebug( $tin ); if (array_key_exists($idfield, $tin)) { if ($rec = ExtractRecordFromRequest($table, $idfield, $idmatch, $fields, $tin)) { $rv[] = $rec; } } else { foreach ($tin as $trow) { if ($rec = ExtractRecordFromRequest($table, $idfield, $idmatch, $fields, $trow)) { $rv[] = $rec; } } } } else { if ($rec = ExtractRecordFromRequest($table, $idfield, $idmatch, $fields, $_REQUEST)) { $rv[] = $rec; } } } $r = new ArrayCursor(&$rv, &$this); #LogDebug($r); return $r; }
/** * Implementation of load and import varies only slight, so both * methods refer to this helper method. * @param bool $do_load TRUE for load, FALSE for import. * @param ChunsuObject $loadme The object to load. * @param DataSource $source The data source to load the object from. * @return bool TRUE is successful, FALSE otherwise. */ function loadOrImport($do_load, &$loadme, $source) { $pkfield =& $this->config->get('primary-key'); $pka =& $pkfield->get('alias'); $core =& $loadme->getCore(); if (is_array($core) || is_object($core)) { $dummy =& $loadme->getDummy(); $pkval = $dummy->get($pka); } else { $pkval = $core; } if (!is_null($pkval)) { $gen = new SQLGenerator($loadme->getCore()); $cursor = $source->query($gen->select($this->config)); if ($cursor->getNext()) { $rec =& $cursor->get(); if ($do_load) { $loadme->setref($rec); $loadme->is_new = FALSE; } else { $loadme->bulkSet($rec); } return TRUE; // TODO: keep reading and handle conflicts if >1 record } } if (!$loadme->config->get('create-on-save')) { LogError("Cannot load object: no records found.\n" . $loadme->trace()); LogFatal("Cannot load object: no records found. "); } return TRUE; }
/** * Serialize a certain option key-value pair (meaning it has been updated) */ public function _serializeOption($optionKey, $optionValue) { // First sanitize $optionValue if ($this->_realmConfig[$optionKey]["type"] == "int") { if (!is_numeric($optionValue)) { throw "Bad option data. " . $this->_optionRealm . ".{$optionKey} expects numeric value, {$optionValue} given!"; } } else { if ($this->_realmConfig[$optionKey]["type"] == "boolean") { $optionValue = $optionValue ? '1' : '0'; } else { $optionValue = "'" . DataSource::escape($optionValue) . "'"; } } if (!defined("OPTIONS_SUPPRESS_DELETE")) { DataSource::query("delete from " . $this->_optionRealm . "_options where id=" . $this->_internalID . " and " . "optionname='" . $optionKey . "'"); } DataSource::query("insert into " . $this->_optionRealm . "_options values (" . $this->_internalID . "," . "'{$optionKey}',{$optionValue})"); }
public static function delete($name, $array) { $sql = "delete FROM " . $name; if ($array) { $first = true; $sql .= " where "; foreach ($array as $key => $value) { if (!$first) { $sql = " and "; } else { $first = false; } $sql .= $key . "='" . $value . "' "; } } LOG::trace("sql:{$sql}"); $ds = new DataSource(); $result = $ds->query($sql); if (!is_numeric($result)) { if ($ds->error()) { throw new Exception($ds->error()); } } return $result; }