getCell() public static method

Executes SQL.
public static getCell ( string $sql, array $bindings = [] ) : string
$sql string SQL query to execute
$bindings array a list of values to be bound to query parameters
return string
Esempio n. 1
0
 /**
  * Test SQLite table rebuilding.
  *
  * @return void
  */
 public function testRebuilder()
 {
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     $book = R::dispense('book');
     $page = R::dispense('page');
     $book->xownPage[] = $page;
     $id = R::store($book);
     $book = R::load('book', $id);
     asrt(count($book->xownPage), 1);
     asrt((int) R::getCell('SELECT COUNT(*) FROM page'), 1);
     R::trash($book);
     asrt((int) R::getCell('SELECT COUNT(*) FROM page'), 0);
     $book = R::dispense('book');
     $page = R::dispense('page');
     $book->xownPage[] = $page;
     $id = R::store($book);
     $book = R::load('book', $id);
     asrt(count($book->xownPage), 1);
     asrt((int) R::getCell('SELECT COUNT(*) FROM page'), 1);
     $book->added = 2;
     R::store($book);
     $book->added = 'added';
     R::store($book);
     R::trash($book);
     asrt((int) R::getCell('SELECT COUNT(*) FROM page'), 0);
 }
Esempio n. 2
0
 /**
  * Test whether we can use SQL filters and
  * whether they are being applied properly for
  * different types of SELECT queries in the QueryWriter.
  */
 public function testSQLFilters()
 {
     R::nuke();
     AQueryWriter::setSQLFilters(array(QueryWriter::C_SQLFILTER_READ => array('book' => array('title' => ' LOWER(book.title) ')), QueryWriter::C_SQLFILTER_WRITE => array('book' => array('title' => ' UPPER(?) '))));
     $book = R::dispense('book');
     $book->title = 'story';
     R::store($book);
     asrt(R::getCell('SELECT title FROM book WHERE id = ?', array($book->id)), 'STORY');
     $book = $book->fresh();
     asrt($book->title, 'story');
     $library = R::dispense('library');
     $library->sharedBookList[] = $book;
     R::store($library);
     $library = $library->fresh();
     $books = $library->sharedBookList;
     $book = reset($books);
     asrt($book->title, 'story');
     $otherBook = R::dispense('book');
     $otherBook->sharedBook[] = $book;
     R::store($otherBook);
     $otherBook = $otherBook->fresh();
     $books = $otherBook->sharedBookList;
     $book = reset($books);
     asrt($book->title, 'story');
     $links = $book->ownBookBookList;
     $link = reset($links);
     $link->shelf = 'x13';
     AQueryWriter::setSQLFilters(array(QueryWriter::C_SQLFILTER_READ => array('book' => array('title' => ' LOWER(book.title) '), 'book_book' => array('shelf' => ' LOWER(book_book.shelf) ')), QueryWriter::C_SQLFILTER_WRITE => array('book' => array('title' => ' UPPER(?) '), 'book_book' => array('shelf' => ' UPPER(?) '))));
     R::store($link);
     asrt(R::getCell('SELECT shelf FROM book_book WHERE id = ?', array($link->id)), 'X13');
     $otherBook = $otherBook->fresh();
     unset($book->sharedBookList[$otherBook->id]);
     R::store($book);
     AQueryWriter::setSQLFilters(array());
 }
Esempio n. 3
0
    /**
     * Test adding foreign keys.
     *
     * @return void
     */
    public function testAddingForeignKey()
    {
        R::nuke();
        $sql = '
			CREATE TABLE book (
				id INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT,
				PRIMARY KEY ( id )
			)
			ENGINE = InnoDB
		';
        R::exec($sql);
        $sql = '
			CREATE TABLE page (
				id INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT,
				book_id INT( 11 ) UNSIGNED NOT NULL,
				PRIMARY KEY ( id )
			)
			ENGINE = InnoDB
		';
        R::exec($sql);
        $numOfFKS = R::getCell('
			SELECT COUNT(*)
			FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
			WHERE TABLE_NAME = "page" AND DELETE_RULE = "CASCADE"');
        asrt((int) $numOfFKS, 0);
        $writer = R::getWriter();
        //Can we add a foreign key with cascade?
        $writer->addFK('page', 'book', 'book_id', 'id', TRUE);
        $numOfFKS = R::getCell('
			SELECT COUNT(*)
			FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
			WHERE TABLE_NAME = "page" AND DELETE_RULE = "CASCADE"');
        asrt((int) $numOfFKS, 1);
        //dont add it twice
        $writer->addFK('page', 'book', 'book_id', 'id', TRUE);
        $numOfFKS = R::getCell('
			SELECT COUNT(*)
			FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
			WHERE TABLE_NAME = "page" AND DELETE_RULE = "CASCADE"');
        asrt((int) $numOfFKS, 1);
        //even if different
        $writer->addFK('page', 'book', 'book_id', 'id', FALSE);
        $numOfFKS = R::getCell('
			SELECT COUNT(*)
			FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
			WHERE TABLE_NAME = "page" ');
        asrt((int) $numOfFKS, 1);
        //Now add non-dep key
        R::nuke();
        $sql = '
			CREATE TABLE book (
				id INT( 11 ) UNSIGNED NULL AUTO_INCREMENT,
				PRIMARY KEY ( id )
			)
			ENGINE = InnoDB
		';
        R::exec($sql);
        $sql = '
			CREATE TABLE page (
				id INT( 11 ) UNSIGNED AUTO_INCREMENT,
				book_id INT( 11 ) UNSIGNED NULL,
				PRIMARY KEY ( id )
			)
			ENGINE = InnoDB
		';
        R::exec($sql);
        $numOfFKS = R::getCell('
			SELECT COUNT(*)
			FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
			WHERE TABLE_NAME = "page" AND DELETE_RULE = "CASCADE"');
        asrt((int) $numOfFKS, 0);
        //even if different
        $writer->addFK('page', 'book', 'book_id', 'id', FALSE);
        $numOfFKS = R::getCell('
			SELECT COUNT(*)
			FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
			WHERE TABLE_NAME = "page" AND DELETE_RULE = "CASCADE"');
        asrt((int) $numOfFKS, 0);
        $numOfFKS = R::getCell('
			SELECT COUNT(*)
			FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
			WHERE TABLE_NAME = "page" AND DELETE_RULE = "SET NULL"');
        asrt((int) $numOfFKS, 1);
        $writer->addFK('page', 'book', 'book_id', 'id', TRUE);
        $numOfFKS = R::getCell('
			SELECT COUNT(*)
			FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
			WHERE TABLE_NAME = "page" ');
    }
Esempio n. 4
0
 /**
  * Test common Facade usage scenarios.
  *
  * @return void
  */
 public function testCommonUsageFacade()
 {
     $toolbox = R::getToolBox();
     $adapter = $toolbox->getDatabaseAdapter();
     $writer = $toolbox->getWriter();
     $redbean = $toolbox->getRedBean();
     $pdo = $adapter->getDatabase();
     $a = new AssociationManager($toolbox);
     asrt(R::getRedBean() instanceof OODB, TRUE);
     asrt(R::getToolBox() instanceof ToolBox, TRUE);
     asrt(R::getDatabaseAdapter() instanceof Adapter, TRUE);
     asrt(R::getWriter() instanceof QueryWriter, TRUE);
     $book = R::dispense("book");
     asrt($book instanceof OODBBean, TRUE);
     $book->title = "a nice book";
     $id = R::store($book);
     asrt($id > 0, TRUE);
     $book = R::load("book", (int) $id);
     asrt($book->title, "a nice book");
     asrt(R::load('book', 999)->title, NULL);
     R::freeze(TRUE);
     try {
         R::load('bookies', 999);
         fail();
     } catch (\Exception $e) {
         pass();
     }
     R::freeze(FALSE);
     $author = R::dispense("author");
     $author->name = "me";
     R::store($author);
     $book9 = R::dispense("book");
     $author9 = R::dispense("author");
     $author9->name = "mr Nine";
     $a9 = R::store($author9);
     $book9->author_id = $a9;
     $bk9 = R::store($book9);
     $book9 = R::load("book", $bk9);
     $author = R::load("author", $book9->author_id);
     asrt($author->name, "mr Nine");
     R::trash($author);
     R::trash($book9);
     pass();
     $book2 = R::dispense("book");
     $book2->title = "second";
     R::store($book2);
     $book3 = R::dispense("book");
     $book3->title = "third";
     R::store($book3);
     asrt(count(R::find("book")), 3);
     asrt(count(R::findAll("book")), 3);
     asrt(count(R::findAll("book", " LIMIT 2")), 2);
     asrt(count(R::find("book", " id=id ")), 3);
     asrt(count(R::find("book", " title LIKE ?", array("third"))), 1);
     asrt(count(R::find("book", " title LIKE ?", array("%d%"))), 2);
     // Find without where clause
     asrt(count(R::findAll('book', ' order by id')), 3);
     R::trash($book3);
     R::trash($book2);
     asrt(count(R::getAll("SELECT * FROM book ")), 1);
     asrt(count(R::getCol("SELECT title FROM book ")), 1);
     asrt((int) R::getCell("SELECT 123 "), 123);
     $book = R::dispense("book");
     $book->title = "not so original title";
     $author = R::dispense("author");
     $author->name = "Bobby";
     R::store($book);
     $aid = R::store($author);
     $author = R::findOne("author", " name = ? ", array("Bobby"));
 }
Esempio n. 5
0
 /**
  * Stored and reloads spatial data to see if the
  * value is preserved correctly.
  *
  * @return void
  */
 protected function setGetSpatial($data)
 {
     R::nuke();
     $place = R::dispense('place');
     $place->location = $data;
     R::store($place);
     asrt(R::getCell('SELECT AsText(location) FROM place LIMIT 1'), $data);
 }
Esempio n. 6
0
 /**
  * Test selecting.
  * 
  * @return void
  */
 public function testSelects()
 {
     $rooms = R::dispense('room', 2);
     $rooms[0]->kind = 'suite';
     $rooms[1]->kind = 'classic';
     $rooms[0]->number = 6;
     $rooms[1]->number = 7;
     R::store($rooms[0]);
     R::store($rooms[1]);
     $rooms = R::getAssoc('SELECT * FROM room WHERE id < -999');
     asrt(is_array($rooms), TRUE);
     asrt(count($rooms), 0);
     $rooms = R::getAssoc('SELECT ' . R::getWriter()->esc('number') . ', kind FROM room ORDER BY kind ASC');
     foreach ($rooms as $key => $room) {
         asrt($key === 6 || $key === 7, TRUE);
         asrt($room == 'classic' || $room == 'suite', TRUE);
     }
     $rooms = R::getDatabaseAdapter()->getAssoc('SELECT kind FROM room');
     foreach ($rooms as $key => $room) {
         asrt($room == 'classic' || $room == 'suite', TRUE);
         asrt($room, $key);
     }
     $rooms = R::getAssoc('SELECT `number`, kind FROM rooms2 ORDER BY kind ASC');
     asrt(count($rooms), 0);
     asrt(is_array($rooms), TRUE);
     // GetCell should return NULL in case of exception
     asrt(NULL, R::getCell('SELECT dream FROM fantasy'));
 }
Esempio n. 7
0
 /**
  * Test Facade transactions.
  * 
  * @return void
  * 
  * @throws\Exception
  */
 public function testTransactionInFacade()
 {
     testpack('Test transaction in facade');
     $bean = R::dispense('bean');
     $bean->name = 'a';
     R::store($bean);
     R::trash($bean);
     R::freeze(TRUE);
     $bean = R::dispense('bean');
     $bean->name = 'a';
     R::store($bean);
     asrt(R::count('bean'), 1);
     R::trash($bean);
     asrt(R::count('bean'), 0);
     $bean = R::dispense('bean');
     $bean->name = 'a';
     $id = R::transaction(function () use(&$bean) {
         return R::transaction(function () use(&$bean) {
             return R::store($bean);
         });
     });
     asrt((int) $id, (int) $bean->id);
     R::trash($bean);
     $bean = R::dispense('bean');
     $bean->name = 'a';
     $id = R::transaction(function () use(&$bean) {
         return R::store($bean);
     });
     asrt((int) $id, (int) $bean->id);
     R::trash($bean);
     $bean = R::dispense('bean');
     $bean->name = 'a';
     try {
         R::transaction(function () use($bean) {
             R::store($bean);
             R::transaction(function () {
                 throw new \Exception();
             });
         });
     } catch (\Exception $e) {
         pass();
     }
     asrt(R::count('bean'), 0);
     $bean = R::dispense('bean');
     $bean->name = 'a';
     try {
         R::transaction(function () use($bean) {
             R::transaction(function () use($bean) {
                 R::store($bean);
                 throw new \Exception();
             });
         });
     } catch (\Exception $e) {
         pass();
     }
     asrt(R::count('bean'), 0);
     $bean = R::dispense('bean');
     $bean->name = 'a';
     try {
         R::transaction(function () use($bean) {
             R::transaction(function () use($bean) {
                 R::store($bean);
             });
         });
     } catch (\Exception $e) {
         pass();
     }
     asrt(R::count('bean'), 1);
     R::freeze(FALSE);
     try {
         R::transaction('nope');
         fail();
     } catch (\Exception $e) {
         pass();
     }
     testpack('Test Camelcase 2 underscore');
     $names = array('oneACLRoute' => 'one_acl_route', 'ALLUPPERCASE' => 'alluppercase', 'clientServerArchitecture' => 'client_server_architecture', 'camelCase' => 'camel_case', 'peer2peer' => 'peer2peer', 'fromUs4You' => 'from_us4_you', 'lowercase' => 'lowercase', 'a1A2b' => 'a1a2b');
     $bean = R::dispense('bean');
     foreach ($names as $name => $becomes) {
         $bean->{$name} = 1;
         asrt(isset($bean->{$becomes}), TRUE);
     }
     testpack('Misc Tests');
     R::debug(1);
     flush();
     ob_start();
     R::exec('SELECT 123');
     $out = ob_get_contents();
     ob_end_clean();
     flush();
     pass();
     asrt(strpos($out, 'SELECT 123') !== FALSE, TRUE);
     R::debug(0);
     flush();
     ob_start();
     R::exec('SELECT 123');
     $out = ob_get_contents();
     ob_end_clean();
     flush();
     pass();
     asrt($out, '');
     R::debug(0);
     pass();
     testpack('test to string override');
     $band = R::dispense('band');
     $str = strval($band);
     asrt($str, 'bigband');
     testpack('test whether we can use isset/set in model');
     $band->setProperty('property1', 123);
     asrt($band->property1, 123);
     asrt($band->checkProperty('property1'), TRUE);
     asrt($band->checkProperty('property2'), FALSE);
     $band = new \Model_Band();
     $bean = R::dispense('band');
     $bean->property3 = 123;
     $band->loadBean($bean);
     $bean->property4 = 345;
     $band->setProperty('property1', 123);
     asrt($band->property1, 123);
     asrt($band->checkProperty('property1'), TRUE);
     asrt($band->checkProperty('property2'), FALSE);
     asrt($band->property3, 123);
     asrt($band->property4, 345);
     testpack('Can we pass a\\PDO object to Setup?');
     $pdo = new \PDO('sqlite:test.db');
     R::addDatabase('pdo', $pdo);
     R::selectDatabase('pdo');
     R::getCell('SELECT 123;');
     testpack('Test array interface of beans');
     $bean = R::dispense('bean');
     $bean->hello = 'hi';
     $bean->world = 'planet';
     asrt($bean['hello'], 'hi');
     asrt(isset($bean['hello']), TRUE);
     asrt(isset($bean['bye']), FALSE);
     $bean['world'] = 'sphere';
     asrt($bean->world, 'sphere');
     foreach ($bean as $key => $el) {
         if ($el == 'sphere' || $el == 'hi' || $el == 0) {
             pass();
         } else {
             fail();
         }
         if ($key == 'hello' || $key == 'world' || $key == 'id') {
             pass();
         } else {
             fail();
         }
     }
     asrt(count($bean), 3);
     unset($bean['hello']);
     asrt(count($bean), 2);
     asrt(count(R::dispense('countable')), 1);
     // Otherwise untestable...
     $bean->setBeanHelper(new SimpleFacadeBeanHelper());
     R::getRedBean()->setBeanHelper(new SimpleFacadeBeanHelper());
     pass();
     // Test whether properties like owner and shareditem are still possible
     testpack('Test Bean Interface for Lists');
     $bean = R::dispense('bean');
     // Must not be list, because first char after own is lowercase
     asrt(is_array($bean->owner), FALSE);
     // Must not be list, because first char after shared is lowercase
     asrt(is_array($bean->shareditem), FALSE);
     asrt(is_array($bean->own), FALSE);
     asrt(is_array($bean->shared), FALSE);
     asrt(is_array($bean->own_item), FALSE);
     asrt(is_array($bean->shared_item), FALSE);
     asrt(is_array($bean->{'own item'}), FALSE);
     asrt(is_array($bean->{'shared Item'}), FALSE);
 }
Esempio n. 8
0
 /**
  * Test basic and complex common usage scenarios for
  * relations and associations.
  *
  * @return void
  */
 public function testScenarios()
 {
     list($q1, $q2) = R::dispense('quote', 2);
     list($pic1, $pic2) = R::dispense('picture', 2);
     list($book, $book2, $book3) = R::dispense('book', 4);
     list($topic1, $topic2, $topic3, $topic4, $topic5) = R::dispense('topic', 5);
     list($page1, $page2, $page3, $page4, $page5, $page6, $page7) = R::dispense('page', 7);
     $q1->text = 'lorem';
     $q2->text = 'ipsum';
     $book->title = 'abc';
     $book2->title = 'def';
     $book3->title = 'ghi';
     $page1->title = 'pagina1';
     $page2->title = 'pagina2';
     $page3->title = 'pagina3';
     $page4->title = 'pagina4';
     $page5->title = 'pagina5';
     $page6->title = 'cover1';
     $page7->title = 'cover2';
     $topic1->name = 'holiday';
     $topic2->name = 'cooking';
     $topic3->name = 'gardening';
     $topic4->name = 'computing';
     $topic5->name = 'christmas';
     // Add one page to the book
     $book->ownPage[] = $page1;
     $id = R::store($book);
     asrt(count($book->ownPage), 1);
     asrt(reset($book->ownPage)->getMeta('type'), 'page');
     $book = R::load('book', $id);
     asrt(count($book->ownPage), 1);
     asrt(reset($book->ownPage)->getMeta('type'), 'page');
     // Performing an own addition
     $book->ownPage[] = $page2;
     $id = R::store($book);
     $book = R::load('book', $id);
     asrt(count($book->ownPage), 2);
     // Performing a deletion
     $book = R::load('book', $id);
     unset($book->ownPage[1]);
     $id = R::store($book);
     $book = R::load('book', $id);
     asrt(count($book->ownPage), 1);
     asrt(reset($book->ownPage)->getMeta('type'), 'page');
     asrt(R::count('page'), 2);
     //still exists
     asrt(reset($book->ownPage)->id, '2');
     // Doing a change in one of the owned items
     $book->ownPage[2]->title = 'page II';
     $id = R::store($book);
     $book = R::load('book', $id);
     asrt(reset($book->ownPage)->title, 'page II');
     // Change by reference now... don't copy!
     $refToPage2 = $book->ownPage[2];
     $refToPage2->title = 'page II b';
     $id = R::store($book);
     $book = R::load('book', $id);
     asrt(reset($book->ownPage)->title, 'page II b');
     // Doing all actions combined
     $book->ownPage[] = $page3;
     R::store($book);
     $book = R::load('book', $id);
     unset($book->ownPage[2]);
     // And test custom key
     $book->ownPage['customkey'] = $page4;
     $book->ownPage[3]->title = "THIRD";
     R::store($book);
     $book = R::load('book', $id);
     asrt(count($book->ownPage), 2);
     $p4 = $book->ownPage[4];
     $p3 = $book->ownPage[3];
     asrt($p4->title, 'pagina4');
     asrt($p3->title, 'THIRD');
     // Test replacing an element
     $book = R::load('book', $id);
     $book->ownPage[4] = $page5;
     R::store($book);
     $book = R::load('book', $id);
     asrt(count($book->ownPage), 2);
     $p5 = $book->ownPage[5];
     asrt($p5->title, 'pagina5');
     // Other way around - single bean
     asrt($p5->book->title, 'abc');
     asrt(R::load('page', 5)->book->title, 'abc');
     asrt(R::load('page', 3)->book->title, 'abc');
     // Add the other way around - single bean
     $page1->id = 0;
     $page1->book = $book2;
     $page1 = R::load('page', R::store($page1));
     asrt($page1->book->title, 'def');
     $b2 = R::load('book', $id);
     asrt(count($b2->ownPage), 2);
     // Remove the other way around - single bean
     unset($page1->book);
     R::store($page1);
     $b2 = R::load('book', $book2->id);
     asrt(count($b2->ownPage), 1);
     //does not work
     $page1->book = NULL;
     R::store($page1);
     $b2 = R::load('book', $book2->id);
     asrt(count($b2->ownPage), 0);
     //works
     // Re-add the page
     $b2->ownPage[] = $page1;
     R::store($b2);
     $b2 = R::load('book', $book2->id);
     asrt(count($b2->ownPage), 1);
     // Different, less elegant way to remove
     $page1 = reset($b2->ownPage);
     $page1->book_id = NULL;
     R::store($page1);
     $b2 = R::load('book', $book2->id);
     asrt(count($b2->ownPage), 0);
     // Re-add the page
     $b2->ownPage[] = $page1;
     R::store($b2);
     $b2 = R::load('book', $book2->id);
     asrt(count($b2->ownPage), 1);
     // Another less elegant way to remove
     $page1->book = NULL;
     R::store($page1);
     $cols = R::getColumns('page');
     asrt(isset($cols['book']), FALSE);
     $b2 = R::load('book', $book2->id);
     asrt(count($b2->ownPage), 0);
     // Re-add the page
     $b2->ownPage[] = $page1;
     R::store($b2);
     $b2 = R::load('book', $book2->id);
     asrt(count($b2->ownPage), 1);
     // Another less elegant... just plain ugly... way to remove
     $page1->book = FALSE;
     R::store($page1);
     $cols = R::getColumns('page');
     asrt(isset($cols['book']), FALSE);
     $b2 = R::load('book', $book2->id);
     asrt(count($b2->ownPage), 0);
     // Re-add the page
     $b2->ownPage[] = $page1;
     R::store($b2);
     $b2 = R::load('book', $book2->id);
     asrt(count($b2->ownPage), 1);
     // You are not allowed to re-use the field for something else
     foreach (array(1, -2.1, array(), TRUE, 'NULL', new \stdClass(), 'just a string', array('a' => 1), 0) as $value) {
         try {
             $page1->book = $value;
             fail();
         } catch (RedException $e) {
             pass();
         }
     }
     // Test fk, not allowed to set to 0
     $page1 = reset($b2->ownPage);
     $page1->book_id = 0;
     // Even uglier way, but still needs to work
     $page1 = reset($b2->ownPage);
     $page1->book_id = NULL;
     R::store($b2);
     $b2 = R::load('book', $book2->id);
     asrt(count($b2->ownPage), 0);
     // Test shared items
     $book = R::load('book', $id);
     $book->sharedTopic[] = $topic1;
     $id = R::store($book);
     // Add an item
     asrt(count($book->sharedTopic), 1);
     asrt(reset($book->sharedTopic)->name, 'holiday');
     $book = R::load('book', $id);
     asrt(count($book->sharedTopic), 1);
     asrt(reset($book->sharedTopic)->name, 'holiday');
     // Add another item
     $book->sharedTopic[] = $topic2;
     $id = R::store($book);
     $tidx = R::store(R::dispense('topic'));
     $book = R::load('book', $id);
     asrt(count($book->sharedTopic), 2);
     $t1 = $book->sharedTopic[1];
     $t2 = $book->sharedTopic[2];
     asrt($t1->name, 'holiday');
     asrt($t2->name, 'cooking');
     // Remove an item
     unset($book->sharedTopic[2]);
     asrt(count($book->sharedTopic), 1);
     $id = R::store($book);
     $book = R::load('book', $id);
     asrt(count($book->sharedTopic), 1);
     asrt(reset($book->sharedTopic)->name, 'holiday');
     // Add and change
     $book->sharedTopic[] = $topic3;
     $book->sharedTopic[1]->name = 'tropics';
     $id = R::store($book);
     $book = R::load('book', $id);
     asrt(count($book->sharedTopic), 2);
     asrt($book->sharedTopic[1]->name, 'tropics');
     testids($book->sharedTopic);
     R::trash(R::load('topic', $tidx));
     $id = R::store($book);
     $book = R::load('book', $id);
     // Delete without save
     unset($book->sharedTopic[1]);
     $book = R::load('book', $id);
     asrt(count($book->sharedTopic), 2);
     $book = R::load('book', $id);
     // Delete without init
     asrt(R::count('topic'), 3);
     unset($book->sharedTopic[1]);
     $id = R::store($book);
     asrt(R::count('topic'), 3);
     asrt(count($book->sharedTopic), 1);
     asrt(count($book2->sharedTopic), 0);
     // Add same topic to other book
     $book2->sharedTopic[] = $topic3;
     asrt(count($book2->sharedTopic), 1);
     $id2 = R::store($book2);
     asrt(count($book2->sharedTopic), 1);
     $book2 = R::load('book', $id2);
     asrt(count($book2->sharedTopic), 1);
     // Get books for topic
     asrt($topic3->countShared('book'), 2);
     $t3 = R::load('topic', $topic3->id);
     asrt(count($t3->sharedBook), 2);
     // Nuke an own-array, replace entire array at once without getting first
     $page2->id = 0;
     $page2->title = 'yet another page 2';
     $page4->id = 0;
     $page4->title = 'yet another page 4';
     $book = R::load('book', $id);
     $book->ownPage = array($page2, $page4);
     R::store($book);
     $book = R::load('book', $id);
     asrt(count($book->ownPage), 2);
     asrt(reset($book->ownPage)->title, 'yet another page 2');
     asrt(end($book->ownPage)->title, 'yet another page 4');
     testids($book->ownPage);
     // Test with alias format
     $book3->cover = $page6;
     $idb3 = R::store($book3);
     $book3 = R::load('book', $idb3);
     $justACover = $book3->fetchAs('page')->cover;
     asrt($book3->cover instanceof OODBBean, TRUE);
     asrt($justACover->title, 'cover1');
     // No page property
     asrt(isset($book3->page), FALSE);
     // Test doubling and other side effects ... should not occur..
     $book3->sharedTopic = array($topic1, $topic2);
     $book3 = R::load('book', R::store($book3));
     $book3->sharedTopic = array();
     $book3 = R::load('book', R::store($book3));
     asrt(count($book3->sharedTopic), 0);
     $book3->sharedTopic[] = $topic1;
     $book3 = R::load('book', R::store($book3));
     // Added only one, not more?
     asrt(count($book3->sharedTopic), 1);
     asrt(intval(R::getCell("select count(*) from book_topic where book_id = {$idb3}")), 1);
     // Add the same
     $book3->sharedTopic[] = $topic1;
     $book3 = R::load('book', R::store($book3));
     asrt(count($book3->sharedTopic), 1);
     asrt(intval(R::getCell("select count(*) from book_topic where book_id = {$idb3}")), 1);
     $book3->sharedTopic['differentkey'] = $topic1;
     $book3 = R::load('book', R::store($book3));
     asrt(count($book3->sharedTopic), 1);
     asrt(intval(R::getCell("select count(*) from book_topic where book_id = {$idb3}")), 1);
     // Ugly assign, auto array generation
     $book3->ownPage[] = $page1;
     $book3 = R::load('book', R::store($book3));
     asrt(count($book3->ownPage), 1);
     asrt(intval(R::getCell("select count(*) from page where book_id = {$idb3} ")), 1);
     $book3 = R::load('book', $idb3);
     $book3->ownPage = array();
     // No change until saved
     asrt(intval(R::getCell("select count(*) from page where book_id = {$idb3} ")), 1);
     $book3 = R::load('book', R::store($book3));
     asrt(intval(R::getCell("select count(*) from page where book_id = {$idb3} ")), 0);
     asrt(count($book3->ownPage), 0);
     $book3 = R::load('book', $idb3);
     /**
      * Why do I need to do this ---> why does trash() not set id -> 0?
      * Because you unset() so trash is done on origin not bean
      */
     $page1->id = 0;
     $page2->id = 0;
     $page3->id = 0;
     $book3->ownPage[] = $page1;
     $book3->ownPage[] = $page2;
     $book3->ownPage[] = $page3;
     $book3 = R::load('book', R::store($book3));
     asrt(intval(R::getCell("select count(*) from page where book_id = {$idb3} ")), 3);
     asrt(count($book3->ownPage), 3);
     unset($book3->ownPage[$page2->id]);
     $book3->ownPage[] = $page3;
     $book3->ownPage['try_to_trick_ya'] = $page3;
     $book3 = R::load('book', R::store($book3));
     asrt(intval(R::getCell("select count(*) from page where book_id = {$idb3} ")), 2);
     asrt(count($book3->ownPage), 2);
     // Delete and re-add
     $book3 = R::load('book', $idb3);
     unset($book3->ownPage[10]);
     $book3->ownPage[] = $page1;
     $book3 = R::load('book', R::store($book3));
     asrt(count($book3->ownPage), 2);
     $book3 = R::load('book', $idb3);
     unset($book3->sharedTopic[1]);
     $book3->sharedTopic[] = $topic1;
     $book3 = R::load('book', R::store($book3));
     asrt(count($book3->sharedTopic), 1);
     // Test performance
     $logger = R::debug(true, 1);
     $book = R::load('book', 1);
     $book->sharedTopic = array();
     R::store($book);
     // No more than 1 update
     asrt(count($logger->grep('UPDATE')), 1);
     $book = R::load('book', 1);
     $logger->clear();
     print_r($book->sharedTopic, 1);
     // No more than 1 select
     asrt(count($logger->grep('SELECT')), 1);
     $logger->clear();
     $book->sharedTopic[] = $topic1;
     $book->sharedTopic[] = $topic2;
     asrt(count($logger->grep('SELECT')), 0);
     R::store($book);
     $book->sharedTopic[] = $topic3;
     // Now do NOT clear all and then add one, just add the one
     $logger->clear();
     R::store($book);
     $book = R::load('book', 1);
     asrt(count($book->sharedTopic), 3);
     // No deletes
     asrt(count($logger->grep("DELETE FROM")), 0);
     $book->sharedTopic['a'] = $topic3;
     unset($book->sharedTopic['a']);
     R::store($book);
     $book = R::load('book', 1);
     asrt(count($book->sharedTopic), 3);
     // No deletes
     asrt(count($logger->grep("DELETE FROM")), 0);
     $book->ownPage = array();
     R::store($book);
     asrt(count($book->ownPage), 0);
     $book->ownPage[] = $page1;
     $book->ownPage['a'] = $page2;
     asrt(count($book->ownPage), 2);
     R::store($book);
     unset($book->ownPage['a']);
     asrt(count($book->ownPage), 2);
     unset($book->ownPage[11]);
     R::store($book);
     $book = R::load('book', 1);
     asrt(count($book->ownPage), 1);
     $aPage = $book->ownPage[10];
     unset($book->ownPage[10]);
     $aPage->title .= ' changed ';
     $book->ownPage['anotherPage'] = $aPage;
     $logger->clear();
     R::store($book);
     // if ($db=="mysql") asrt(count($logger->grep("SELECT")),0);
     $book = R::load('book', 1);
     asrt(count($book->ownPage), 1);
     $ap = reset($book->ownPage);
     asrt($ap->title, "pagina1 changed ");
     // Fix udiff instead of diff
     $book3->ownPage = array($page3, $page1);
     $i = R::store($book3);
     $book3 = R::load('book', $i);
     asrt(intval(R::getCell("select count(*) from page where book_id = {$idb3} ")), 2);
     asrt(count($book3->ownPage), 2);
     $pic1->name = 'aaa';
     $pic2->name = 'bbb';
     R::store($pic1);
     R::store($q1);
     $book3->ownPicture[] = $pic1;
     $book3->ownQuote[] = $q1;
     $book3 = R::load('book', R::store($book3));
     // two own-arrays -->forgot array_merge
     asrt(count($book3->ownPicture), 1);
     asrt(count($book3->ownQuote), 1);
     asrt(count($book3->ownPage), 2);
     $book3 = R::load('book', R::store($book3));
     unset($book3->ownPicture[1]);
     $book3 = R::load('book', R::store($book3));
     asrt(count($book3->ownPicture), 0);
     asrt(count($book3->ownQuote), 1);
     asrt(count($book3->ownPage), 2);
     $book3 = R::load('book', R::store($book3));
     $NOTE = 0;
     $quotes = R::dispense('quote', 10);
     foreach ($quotes as &$justSomeQuote) {
         $justSomeQuote->note = 'note' . ++$NOTE;
     }
     $pictures = R::dispense('picture', 10);
     foreach ($pictures as &$justSomePic) {
         $justSomePic->note = 'note' . ++$NOTE;
     }
     $topics = R::dispense('topic', 10);
     foreach ($topics as &$justSomeTopic) {
         $justSomeTopic->note = 'note' . ++$NOTE;
     }
     for ($j = 0; $j < 10; $j++) {
         // Do several mutations
         for ($x = 0; $x < rand(1, 20); $x++) {
             modgr($book3, $quotes, $pictures, $topics);
         }
         $qbefore = count($book3->ownQuote);
         $pbefore = count($book3->ownPicture);
         $tbefore = count($book3->sharedTopic);
         $qjson = json_encode($book->ownQuote);
         $pjson = json_encode($book->ownPicture);
         $tjson = json_encode($book->sharedTopic);
         $book3 = R::load('book', R::store($book3));
         asrt(count($book3->ownQuote), $qbefore);
         asrt(count($book3->ownPicture), $pbefore);
         asrt(count($book3->sharedTopic), $tbefore);
         asrt(json_encode($book->ownQuote), $qjson);
         asrt(json_encode($book->ownPicture), $pjson);
         asrt(json_encode($book->sharedTopic), $tjson);
         testids($book->ownQuote);
         testids($book->ownPicture);
         testids($book->sharedTopic);
     }
 }
Esempio n. 9
0
 /**
  * Run tests
  */
 private function runOnce($n = TRUE)
 {
     $books = R::dispense('book', 10);
     $pages = R::dispense('page', 10);
     $readers = R::dispense('reader', 10);
     $texts = R::dispense('text', 10);
     $i = 0;
     foreach ($books as $book) {
         $book->name = 'book-' . $i++;
     }
     $i = 0;
     foreach ($pages as $page) {
         $page->name = 'page-' . $i++;
     }
     $i = 0;
     foreach ($readers as $reader) {
         $reader->name = 'reader-' . $i++;
     }
     $i = 0;
     foreach ($texts as $text) {
         $text->content = 'lorem ipsum -' . $i++;
     }
     foreach ($texts as $text) {
         $pages[array_rand($pages)]->ownText[] = $text;
     }
     foreach ($pages as $page) {
         $books[array_rand($books)]->ownPage[] = $page;
     }
     foreach ($readers as $reader) {
         $books[array_rand($books)]->sharedReader[] = $reader;
     }
     $i = $noOfReaders = $noOfPages = $noOfTexts = 0;
     foreach ($books as $key => $book) {
         $i++;
         $noOfPages += count($book->ownPage);
         $noOfReaders += count($book->sharedReader);
         foreach ($book->ownPage as $page) {
             $noOfTexts += count($page->ownText);
         }
         $arr = R::exportAll($book);
         echo "\nIntermediate info: " . json_encode($arr) . ": Totals = {$i},{$noOfPages},{$noOfReaders},{$noOfTexts} ";
         $this->compare($book, $arr[0]);
         $copiedBook = R::dup($book);
         $copiedBookArray = R::exportAll($copiedBook);
         $this->compare($book, $copiedBookArray[0]);
         $copiedBookArrayII = $copiedBook->export();
         $this->compare($book, $copiedBookArrayII);
         $copyFromCopy = R::dup($copiedBook);
         $copyFromCopyArray = R::exportAll($copyFromCopy);
         $this->compare($book, $copyFromCopyArray[0]);
         $copyFromCopyArrayII = $copyFromCopy->export();
         $this->compare($book, $copyFromCopyArrayII);
         $id = R::store($book);
         $copiedBook = R::dup($book);
         R::store($book);
         //should not be damaged
         $copiedBookArray = R::exportAll($copiedBook);
         $originalBookArray = R::exportAll($book);
         $this->compare($copiedBook, $copiedBookArray[0]);
         $this->compare($book, $originalBookArray[0]);
         $book = R::load('book', $id);
         $this->compare($book, $originalBookArray[0]);
         $copiedBook = R::dup($book);
         $this->compare($copiedBook, $copiedBook->export());
         R::store($copiedBook);
         $this->compare($copiedBook, $copiedBook->export());
         $copyFromCopy = R::dup($copiedBook);
         $this->compare($copyFromCopy, $copyFromCopy->export());
         R::store($copyFromCopy);
         $newPage = R::dispense('page');
         $newPage->name = 'new';
         $copyFromCopy->ownPage[] = $newPage;
         $modifiedCopy = R::dup($copyFromCopy);
         $exportMod = R::exportAll($modifiedCopy);
         $this->compare($modifiedCopy, $exportMod[0]);
         asrt(count($modifiedCopy->ownPage), count($copiedBook->ownPage) + 1);
         R::store($modifiedCopy);
         if ($n) {
             asrt((int) R::getCell('SELECT count(*) FROM book'), $i * 4);
             asrt((int) R::getCell('SELECT count(*) FROM page'), $noOfPages * 4 + $i);
             asrt((int) R::getCell('SELECT count(*) FROM text'), $noOfTexts * 4);
             asrt((int) R::getCell('SELECT count(*) FROM book_reader'), $noOfReaders * 4);
             asrt((int) R::getCell('SELECT count(*) FROM reader'), $noOfReaders);
         }
     }
     if ($n) {
         asrt($noOfTexts, 10);
         asrt($noOfReaders, 10);
         asrt($noOfPages, 10);
         asrt($i, 10);
     }
 }
Esempio n. 10
0
    /**
     * Test adding foreign keys.
     * 
     * @return void
     */
    public function testAddingForeignKey()
    {
        R::nuke();
        $database = R::getCell('SELECT current_database()');
        $sql = 'CREATE TABLE book (
			id SERIAL PRIMARY KEY
		)';
        R::exec($sql);
        $sql = 'CREATE TABLE page (
			id SERIAL PRIMARY KEY,
			book_id INTEGER
		)';
        R::exec($sql);
        $writer = R::getWriter();
        $sql = "\n\t\t\tSELECT \n\t\t\t\tCOUNT(*) \n\t\t\tFROM information_schema.key_column_usage AS k \n\t\t\tLEFT JOIN information_schema.table_constraints AS c ON c.constraint_name = k.constraint_name \n\t\t\tWHERE k.table_catalog = '{$database}'\n\t\t\t\tAND k.table_schema = 'public' \n\t\t\t\tAND k.table_name = 'page' \n\t\t\t\tAND c.constraint_type = 'FOREIGN KEY'";
        $numFKS = R::getCell($sql);
        asrt((int) $numFKS, 0);
        $writer->addFK('page', 'page', 'book_id', 'id', TRUE);
        $sql = "\n\t\t\tSELECT \n\t\t\t\tCOUNT(*) \n\t\t\tFROM information_schema.key_column_usage AS k \n\t\t\tLEFT JOIN information_schema.table_constraints AS c ON c.constraint_name = k.constraint_name \n\t\t\tWHERE k.table_catalog = '{$database}'\n\t\t\t\tAND k.table_schema = 'public' \n\t\t\t\tAND k.table_name = 'page' \n\t\t\t\tAND c.constraint_type = 'FOREIGN KEY'";
        $numFKS = R::getCell($sql);
        asrt((int) $numFKS, 1);
        //dont add twice
        $writer->addFK('page', 'page', 'book_id', 'id', TRUE);
        $sql = "\n\t\t\tSELECT \n\t\t\t\tCOUNT(*) \n\t\t\tFROM information_schema.key_column_usage AS k \n\t\t\tLEFT JOIN information_schema.table_constraints AS c ON c.constraint_name = k.constraint_name \n\t\t\tWHERE k.table_catalog = '{$database}'\n\t\t\t\tAND k.table_schema = 'public' \n\t\t\t\tAND k.table_name = 'page' \n\t\t\t\tAND c.constraint_type = 'FOREIGN KEY'";
        $numFKS = R::getCell($sql);
        asrt((int) $numFKS, 1);
        //even if it is different
        $writer->addFK('page', 'page', 'book_id', 'id', FALSE);
        $sql = "\n\t\t\tSELECT \n\t\t\t\tCOUNT(*) \n\t\t\tFROM information_schema.key_column_usage AS k \n\t\t\tLEFT JOIN information_schema.table_constraints AS c ON c.constraint_name = k.constraint_name \n\t\t\tWHERE k.table_catalog = '{$database}'\n\t\t\t\tAND k.table_schema = 'public' \n\t\t\t\tAND k.table_name = 'page' \n\t\t\t\tAND c.constraint_type = 'FOREIGN KEY'";
        $numFKS = R::getCell($sql);
        asrt((int) $numFKS, 1);
        R::nuke();
        $sql = 'CREATE TABLE book (
			id SERIAL PRIMARY KEY
		)';
        R::exec($sql);
        $sql = 'CREATE TABLE page (
			id SERIAL PRIMARY KEY,
			book_id INTEGER
		)';
        R::exec($sql);
        $writer = R::getWriter();
        $sql = "\n\t\t\tSELECT \n\t\t\t\tCOUNT(*) \n\t\t\tFROM information_schema.key_column_usage AS k \n\t\t\tLEFT JOIN information_schema.table_constraints AS c ON c.constraint_name = k.constraint_name \n\t\t\tWHERE k.table_catalog = '{$database}'\n\t\t\t\tAND k.table_schema = 'public' \n\t\t\t\tAND k.table_name = 'page' \n\t\t\t\tAND c.constraint_type = 'FOREIGN KEY'";
        $numFKS = R::getCell($sql);
        asrt((int) $numFKS, 0);
        $writer->addFK('page', 'page', 'book_id', 'id', FALSE);
        $sql = "\n\t\t\tSELECT \n\t\t\t\tCOUNT(*) \n\t\t\tFROM information_schema.key_column_usage AS k \n\t\t\tLEFT JOIN information_schema.table_constraints AS c ON c.constraint_name = k.constraint_name \n\t\t\tWHERE k.table_catalog = '{$database}'\n\t\t\t\tAND k.table_schema = 'public' \n\t\t\t\tAND k.table_name = 'page' \n\t\t\t\tAND c.constraint_type = 'FOREIGN KEY'";
        $numFKS = R::getCell($sql);
        asrt((int) $numFKS, 1);
    }
Esempio n. 11
0
 public function beforeCreate()
 {
     $pos = R::getCell("SELECT MAX(position) FROM setting");
     $this->set('position', (int) $pos + 1);
 }
Esempio n. 12
0
 public function beforeCreate()
 {
     if (is_null($this->get('position'))) {
         $this->set('position', (int) R::getCell("SELECT MAX(position) FROM " . $this->getTableName()) + 1);
     }
 }