-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
gabor de mooij
committed
Jul 23, 2009
1 parent
2d80e8c
commit 3a9f06a
Showing
24 changed files
with
5,957 additions
and
2,402 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
|
||
|
||
/** | ||
* Object Oriented Database Bean class | ||
* Empty Type definition for bean processing | ||
* | ||
*/ | ||
class OODBBean { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
<?php | ||
|
||
/** | ||
* RedBean Can | ||
* @desc a Can contains beans and acts as n iterator, it also enables you to work with | ||
* large collections while remaining light-weight | ||
* @author gabordemooij | ||
*/ | ||
class RedBean_Can implements Iterator , ArrayAccess , SeekableIterator , Countable { | ||
|
||
/** | ||
* | ||
* @var array | ||
*/ | ||
private $collectionIDs = null; | ||
|
||
/** | ||
* | ||
* @var string | ||
*/ | ||
private $type = null; | ||
|
||
/** | ||
* | ||
* @var int | ||
*/ | ||
private $pointer = 0; | ||
|
||
/** | ||
* | ||
* @var int | ||
*/ | ||
private $num = 0; | ||
|
||
/** | ||
* Constructor | ||
* @param $type | ||
* @param $collection | ||
* @return RedBean_Can $instance | ||
*/ | ||
public function __construct( $type="", $collection = array() ) { | ||
|
||
$this->collectionIDs = $collection; | ||
$this->type = $type; | ||
$this->num = count( $this->collectionIDs ); | ||
} | ||
|
||
/** | ||
* Wraps an OODBBean in a RedBean_Decorator | ||
* @param OODBBean $bean | ||
* @return RedBean_Decorator $deco | ||
*/ | ||
public function wrap( $bean ) { | ||
|
||
$dclass = PRFX.$this->type.SFFX; | ||
$deco = new $dclass( floatval( $bean->id ) ); | ||
$deco->setData( $bean ); | ||
return $deco; | ||
|
||
} | ||
|
||
/** | ||
* Returns the number of beans in this can | ||
* @return int $num | ||
*/ | ||
public function count() { | ||
|
||
return $this->num; | ||
|
||
} | ||
|
||
/** | ||
* Returns all the beans inside this can | ||
* @return array $beans | ||
*/ | ||
public function getBeans() { | ||
|
||
$rows = RedBean_OODB::fastloader( $this->type, $this->collectionIDs ); | ||
|
||
$beans = array(); | ||
|
||
if (is_array($rows)) { | ||
foreach( $rows as $row ) { | ||
//Use the fastloader for optimal performance (takes row as data) | ||
$beans[] = $this->wrap( RedBean_OODB::getById( $this->type, $id , $row) ); | ||
} | ||
} | ||
|
||
return $beans; | ||
} | ||
|
||
public function slice( $begin=0, $end=0 ) { | ||
$this->collectionIDs = array_slice( $this->collectionIDs, $begin, $end); | ||
$this->num = count( $this->collectionIDs ); | ||
} | ||
|
||
/** | ||
* Returns the current bean | ||
* @return RedBean_Decorator $bean | ||
*/ | ||
public function current() { | ||
if (isset($this->collectionIDs[$this->pointer])) { | ||
$id = $this->collectionIDs[$this->pointer]; | ||
return $this->wrap( RedBean_OODB::getById( $this->type, $id ) ); | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Returns the key of the current bean | ||
* @return int $key | ||
*/ | ||
public function key() { | ||
return $this->pointer; | ||
} | ||
|
||
|
||
/** | ||
* Advances the internal pointer to the next bean in the can | ||
* @return int $pointer | ||
*/ | ||
public function next() { | ||
return ++$this->pointer; | ||
} | ||
|
||
/** | ||
* Sets the internal pointer to the previous bean in the can | ||
* @return int $pointer | ||
*/ | ||
public function prev() { | ||
if ($this->pointer > 0) { | ||
return ++$this->pointer; | ||
}else { | ||
return 0; | ||
} | ||
} | ||
|
||
/** | ||
* Resets the internal pointer to the first bean in the can | ||
* @return void | ||
*/ | ||
public function rewind() { | ||
$this->pointer=0; | ||
return 0; | ||
} | ||
|
||
/** | ||
* Sets the internal pointer to the specified key in the can | ||
* @param int $seek | ||
* @return void | ||
*/ | ||
public function seek( $seek ) { | ||
$this->pointer = (int) $seek; | ||
} | ||
|
||
/** | ||
* Checks there are any more beans in this can | ||
* @return boolean $morebeans | ||
*/ | ||
public function valid() { | ||
return ($this->num > ($this->pointer+1)); | ||
} | ||
|
||
/** | ||
* Checks there are any more beans in this can | ||
* Same as valid() but this method has a more descriptive name | ||
* @return boolean $morebeans | ||
*/ | ||
public function hasMoreBeans() { | ||
return $this->valid(); | ||
} | ||
|
||
/** | ||
* Makes it possible to use this object as an array | ||
* Sets the offset | ||
* @param $offset | ||
* @param $value | ||
* @return void | ||
*/ | ||
public function offsetSet($offset, $value) { | ||
$this->collectionIDs[$offset] = $value; | ||
} | ||
|
||
/** | ||
* Makes it possible to use this object as an array | ||
* Checks the offset | ||
* @param $offset | ||
* @return boolean $isset | ||
*/ | ||
public function offsetExists($offset) { | ||
return isset($this->collectionIDs[$offset]); | ||
} | ||
|
||
/** | ||
* Makes it possible to use this object as an array | ||
* Unsets the value at offset | ||
* @param $offset | ||
* @return void | ||
*/ | ||
public function offsetUnset($offset) { | ||
unset($this->collectionIDs[$offset]); | ||
} | ||
|
||
/** | ||
* Makes it possible to use this object as an array | ||
* Gets the bean at a given offset | ||
* @param $offset | ||
* @return RedBean_Decorator $bean | ||
*/ | ||
public function offsetGet($offset) { | ||
|
||
if (isset($this->collectionIDs[$offset])) { | ||
$id = $this->collectionIDs[$offset]; | ||
return $this->wrap( RedBean_OODB::getById( $this->type, $id ) ); | ||
} | ||
else { | ||
return null; | ||
} | ||
|
||
} | ||
|
||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
/** | ||
* Adapter for ADODB database layer AND RedBean | ||
* @author gabordemooij | ||
* | ||
*/ | ||
class RedBean_DBAdapter { | ||
|
||
/** | ||
* | ||
* @var ADODB | ||
*/ | ||
private $db = null; | ||
|
||
public static $log = array(); | ||
|
||
/** | ||
* | ||
* @param $database | ||
* @return unknown_type | ||
*/ | ||
public function __construct($database) { | ||
$this->db = $database; | ||
} | ||
|
||
/** | ||
* Escapes a string for use in a Query | ||
* @param $sqlvalue | ||
* @return unknown_type | ||
*/ | ||
public function escape( $sqlvalue ) { | ||
return $this->db->Escape($sqlvalue); | ||
} | ||
|
||
/** | ||
* Executes SQL code | ||
* @param $sql | ||
* @return unknown_type | ||
*/ | ||
public function exec( $sql ) { | ||
self::$log[] = $sql; | ||
return $this->db->Execute( $sql ); | ||
} | ||
|
||
/** | ||
* Multi array SQL fetch | ||
* @param $sql | ||
* @return unknown_type | ||
*/ | ||
public function get( $sql ) { | ||
self::$log[] = $sql; | ||
return $this->db->GetAll( $sql ); | ||
} | ||
|
||
/** | ||
* SQL row fetch | ||
* @param $sql | ||
* @return unknown_type | ||
*/ | ||
public function getRow( $sql ) { | ||
self::$log[] = $sql; | ||
return $this->db->GetRow( $sql ); | ||
} | ||
|
||
/** | ||
* SQL column fetch | ||
* @param $sql | ||
* @return unknown_type | ||
*/ | ||
public function getCol( $sql ) { | ||
self::$log[] = $sql; | ||
return $this->db->GetCol( $sql ); | ||
} | ||
|
||
/** | ||
* Retrieves a single cell | ||
* @param $sql | ||
* @return unknown_type | ||
*/ | ||
public function getCell( $sql ) { | ||
self::$log[] = $sql; | ||
$arr = $this->db->GetCol( $sql ); | ||
if ($arr && is_array($arr)) return ($arr[0]); else return false; | ||
} | ||
|
||
/** | ||
* Returns last inserted id | ||
* @return unknown_type | ||
*/ | ||
public function getInsertID() { | ||
// self::$log[] = $sql; | ||
return $this->db->getInsertID(); | ||
} | ||
|
||
/** | ||
* Returns number of affected rows | ||
* @return unknown_type | ||
*/ | ||
public function getAffectedRows() { | ||
// self::$log[] = $sql; | ||
return $this->db->Affected_Rows(); | ||
} | ||
|
||
/** | ||
* Unwrap the original database object | ||
* @return $database | ||
*/ | ||
public function getDatabase() { | ||
return $this->db; | ||
} | ||
|
||
/** | ||
* Return latest error message | ||
* @return string $message | ||
*/ | ||
public function getErrorMsg() { | ||
return $this->db->Errormsg(); | ||
} | ||
|
||
} |
Oops, something went wrong.