/** * Gets related beans of type $type for bean $bean * @param RedBean_OODBBean $bean * @param string $type * @return array $ids */ public function related(RedBean_OODBBean $bean, $type) { $table = $this->getTable(array($bean->getMeta("type"), $type)); if ($type == $bean->getMeta("type")) { // echo "<b>CROSS</b>"; $type .= "2"; $cross = 1; } else { $cross = 0; } $targetproperty = $type . "_id"; $property = $bean->getMeta("type") . "_id"; $sqlFetchKeys = " SELECT " . $this->adapter->escape($targetproperty) . " FROM `{$table}` WHERE " . $this->adapter->escape($property) . "\n\t\t\t= " . $this->adapter->escape($bean->id); if ($cross) { $sqlFetchKeys .= " UNION SELECT " . $this->adapter->escape($property) . " \n\t\t\tFROM `{$table}`\n\t\t\tWHERE " . $this->adapter->escape($targetproperty) . " = " . $this->adapter->escape($bean->id); } try { return $this->adapter->getCol($sqlFetchKeys); } catch (RedBean_Exception_SQL $e) { if ($e->getSQLState() != "42S02" && $e->getSQLState() != "42S22") { throw $e; } return array(); } }
/** * * @param RedBean_OODBBean $parent * @return array $childObjects */ public function children(RedBean_OODBBean $parent) { try { $ids = $this->adapter->getCol("SELECT id FROM\n\t\t\t`" . $parent->getMeta("type") . "`\n\t\t\tWHERE `" . $this->property . "` = " . intval($parent->id) . "\n\t\t"); } catch (RedBean_Exception_SQL $e) { return array(); } return $this->oodb->batch($parent->getMeta("type"), $ids); }
/** * Convenience function to execute Queries directly. * Executes SQL. * * @param string $sql sql * @param array $values values * * @return array $results */ public static function getCol($sql, $values = array()) { if (!self::$redbean->isFrozen()) { try { $rs = RedBean_Facade::$adapter->getCol($sql, $values); } catch (RedBean_Exception_SQL $e) { if (self::$writer->sqlStateIn($e->getSQLState(), array(RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_COLUMN, RedBean_QueryWriter::C_SQLSTATE_NO_SUCH_TABLE))) { return array(); } else { throw $e; } } return $rs; } else { return RedBean_Facade::$adapter->getCol($sql, $values); } }
/** * Returns all tables in the database * * @return array $tables tables */ public function getTables() { return $this->adapter->getCol("select table_name from information_schema.tables\n\t\twhere table_schema = 'public'"); }
/** * Returns all tables in the database * @return array $tables */ public function getTables() { return $this->adapter->getCol("show tables"); }
public function selectByCrit($select, $table, $column, $value, $withUnion = false) { $select = $this->noKW($this->adapter->escape($select)); $table = $this->noKW($this->adapter->escape($table)); $column = $this->noKW($this->adapter->escape($column)); $value = $this->adapter->escape($value); $sql = "SELECT {$select} FROM {$table} WHERE {$column} = ? "; $values = array($value); if ($withUnion) { $sql .= " UNION SELECT {$column} FROM {$table} WHERE {$select} = ? "; $values[] = $value; } return $this->adapter->getCol($sql, $values); }
/** * Convenience function to execute Queries directly. * Executes SQL. * * @param string $sql sql * @param array $values values * * @return array $results */ public static function getCol( $sql, $values=array() ) { return self::secureExec(function($sql, $values) { return R::$adapter->getCol( $sql, $values ); }, array(),$sql, $values); }