/** * @param DatabasePostgres $db * @param string $table * @param string $field * @return null|PostgresField */ static function fromText(DatabasePostgres $db, $table, $field) { $q = <<<SQL SELECT attnotnull, attlen, conname AS conname, atthasdef, adsrc, COALESCE(condeferred, 'f') AS deferred, COALESCE(condeferrable, 'f') AS deferrable, CASE WHEN typname = 'int2' THEN 'smallint' WHEN typname = 'int4' THEN 'integer' WHEN typname = 'int8' THEN 'bigint' WHEN typname = 'bpchar' THEN 'char' ELSE typname END AS typname FROM pg_class c JOIN pg_namespace n ON (n.oid = c.relnamespace) JOIN pg_attribute a ON (a.attrelid = c.oid) JOIN pg_type t ON (t.oid = a.atttypid) LEFT JOIN pg_constraint o ON (o.conrelid = c.oid AND a.attnum = ANY(o.conkey) AND o.contype = 'f') LEFT JOIN pg_attrdef d on c.oid=d.adrelid and a.attnum=d.adnum WHERE relkind = 'r' AND nspname=%s AND relname=%s AND attname=%s; SQL; $table = $db->remappedTableName($table); $res = $db->query(sprintf($q, $db->addQuotes($db->getCoreSchema()), $db->addQuotes($table), $db->addQuotes($field))); $row = $db->fetchObject($res); if (!$row) { return null; } $n = new PostgresField(); $n->type = $row->typname; $n->nullable = $row->attnotnull == 'f'; $n->name = $field; $n->tablename = $table; $n->max_length = $row->attlen; $n->deferrable = $row->deferrable == 't'; $n->deferred = $row->deferred == 't'; $n->conname = $row->conname; $n->has_default = $row->atthasdef === 't'; $n->default = $row->adsrc; return $n; }
function ruleDef($table, $rule) { $q = <<<END SELECT definition FROM pg_rules \tWHERE schemaname = %s \t AND tablename = %s \t AND rulename = %s END; $r = $this->db->query(sprintf($q, $this->db->addQuotes($this->db->getCoreSchema()), $this->db->addQuotes($table), $this->db->addQuotes($rule))); $row = $this->db->fetchRow($r); if (!$row) { return null; } $d = $row[0]; return $d; }
/** * Transform the user's search string into a better form for tsearch2 * Returns an SQL fragment consisting of quoted text to search for. * * @param $term string * * @return string */ function parseQuery($term) { wfDebug("parseQuery received: {$term} \n"); ## No backslashes allowed $term = preg_replace('/\\\\/', '', $term); ## Collapse parens into nearby words: $term = preg_replace('/\\s*\\(\\s*/', ' (', $term); $term = preg_replace('/\\s*\\)\\s*/', ') ', $term); ## Treat colons as word separators: $term = preg_replace('/:/', ' ', $term); $searchstring = ''; $m = array(); if (preg_match_all('/([-!]?)(\\S+)\\s*/', $term, $m, PREG_SET_ORDER)) { foreach ($m as $terms) { if (strlen($terms[1])) { $searchstring .= ' & !'; } if (strtolower($terms[2]) === 'and') { $searchstring .= ' & '; } elseif (strtolower($terms[2]) === 'or' or $terms[2] === '|') { $searchstring .= ' | '; } elseif (strtolower($terms[2]) === 'not') { $searchstring .= ' & !'; } else { $searchstring .= " & {$terms['2']}"; } } } ## Strip out leading junk $searchstring = preg_replace('/^[\\s\\&\\|]+/', '', $searchstring); ## Remove any doubled-up operators $searchstring = preg_replace('/([\\!\\&\\|]) +(?:[\\&\\|] +)+/', "\$1 ", $searchstring); ## Remove any non-spaced operators (e.g. "Zounds!") $searchstring = preg_replace('/([^ ])[\\!\\&\\|]/', "\$1", $searchstring); ## Remove any trailing whitespace or operators $searchstring = preg_replace('/[\\s\\!\\&\\|]+$/', '', $searchstring); ## Remove unnecessary quotes around everything $searchstring = preg_replace('/^[\'"](.*)[\'"]$/', "\$1", $searchstring); ## Quote the whole thing $searchstring = $this->db->addQuotes($searchstring); wfDebug("parseQuery returned: {$searchstring} \n"); return $searchstring; }