Permalink
Browse files

Merge pull request #643 from Lynesth/patch-17

Can't select sqlite if not set (bis)
  • Loading branch information...
gabordemooij committed May 6, 2018
2 parents fcbaeaf + 71bbdf8 commit 647d1531fdec19bbb2230c42a7f4c27e85734594
Showing with 67 additions and 9 deletions.
  1. +15 −7 RedBeanPHP/QueryWriter/AQueryWriter.php
  2. +50 −0 testing/RedUNIT/Base/Finding.php
  3. +2 −2 testing/cli/runperf.php
@@ -377,6 +377,11 @@ private function makeSQLFromConditions( $conditions, &$bindings, $addSql = '' )
$values = array( $values );
}
+ $checkOODB = reset( $values );
+ if ( $checkOODB instanceof OODBBean && $checkOODB->getMeta( 'type' ) === $column && substr( $column, -3 ) != '_id' )
+ $column = $column . '_id';
+
+
$sql = $this->esc( $column );
$sql .= ' IN ( ';
@@ -386,6 +391,9 @@ private function makeSQLFromConditions( $conditions, &$bindings, $addSql = '' )
array_unshift($sqlConditions, $sql);
foreach ( $values as $k => $v ) {
+ if ( $v instanceof OODBBean ) {
+ $v = $v->id;
+ }
$values[$k] = strval( $v );
array_unshift( $bindings, $v );
@@ -395,6 +403,9 @@ private function makeSQLFromConditions( $conditions, &$bindings, $addSql = '' )
$slots = array();
foreach( $values as $k => $v ) {
+ if ( $v instanceof OODBBean ) {
+ $v = $v->id;
+ }
$slot = ':slot'.$counter++;
$slots[] = $slot;
$bindings[$slot] = strval( $v );
@@ -406,16 +417,13 @@ private function makeSQLFromConditions( $conditions, &$bindings, $addSql = '' )
}
$sql = '';
- $addSql = $this->glueSQLCondition( $addSql, !empty( $sqlConditions ) ? QueryWriter::C_GLUE_AND : NULL );
if ( !empty( $sqlConditions ) ) {
- $sql = implode( ' AND ', $sqlConditions );
- $sql = " WHERE ( $sql ) ";
-
- if ( $addSql ) $sql .= $addSql;
- } elseif ( $addSql ) {
- $sql = $addSql;
+ $sql .= " WHERE ( " . implode( ' AND ', $sqlConditions ) . ") ";
}
+ $addSql = $this->glueSQLCondition( $addSql, !empty( $sqlConditions ) ? QueryWriter::C_GLUE_AND : NULL );
+ if ( $addSql ) $sql .= $addSql;
+
return $sql;
}
@@ -599,6 +599,56 @@ public function testFindOrCreate()
asrt( (int) $book->price, 50 );
}
+ /**
+ * Tests OODBBean as conditions
+ *
+ * @return void
+ */
+ public function testFindLikeWithOODBBeans() {
+ R::nuke();
+ $book = R::dispense( 'book' );
+ $page = R::dispense( 'page' );
+ $page->book = $book;
+ R::store( $page );
+ $book2 = R::dispense( 'book' );
+ $page2 = R::dispense( 'page' );
+ $page2->book = $book2;
+ R::store( $page2 );
+ $pages = R::findLike( 'page', array( 'book_id' => array( 1, 2 ) ) );
+ $pagesWithOODB = R::findLike( 'page', array( 'book' => array( $book, $book2 ) ) );
+ asrt( count( $pagesWithOODB ), 2 );
+ asrt( json_encode($pagesWithOODB), json_encode($pages) );
+ asrt( reset( $pagesWithOODB )->id, $page->id );
+ asrt( end( $pagesWithOODB )->id, $page2->id );
+
+ $pages = R::findLike( 'page', array( 'book' => array( $book, $book2->id ) ) );
+ asrt( count( $pages ), 2 );
+ asrt( reset( $pages )->id, $page->id );
+ asrt( end( $pages )->id, $page2->id );
+
+ $pages = R::findLike( 'page', array( 'book_id' => array( $book->id, $book2 ) ) );
+ asrt( count( $pages ), 2 );
+ asrt( reset( $pages )->id, $page->id );
+ asrt( end( $pages )->id, $page2->id );
+
+ $pagesFail = R::findLike( 'page', array( 'book' => array( $book->id, $book2 ) ) );
+ asrt( count( $pagesFail ), 0 );
+
+ $book3 = R::dispense( 'book' );
+ $page3 = R::dispense( 'page' );
+ R::store( $page3 );
+ $page3->book = $book3;
+ $pagesFail = R::findLike( 'page', array( 'book' => $book3 ) );
+ asrt( count( $pagesFail ), 0 );
+
+ $pen = R::dispense( 'pen' );
+ R::store( $pen );
+ asrt( $pen->id, $book->id );
+ $pagesFail = R::findLike( 'page', array( 'book' => $pen ) );
+ asrt( count( $pagesFail ), 0 );
+
+ }
+
/**
* Tests the findLike method.
*
View
@@ -37,9 +37,9 @@
if ( isset( $ini['sqlite'] ) ) {
R::addDatabase( 'sqlite', 'sqlite:' . $ini['sqlite']['file'], NULL, NULL, FALSE );
+ R::selectDatabase( 'sqlite' );
}
-R::selectDatabase( 'sqlite' );
// Function to activate a driver
function activate_driver( $d )
@@ -93,4 +93,4 @@ function activate_driver( $d )
echo PHP_EOL."AVG: $a, MAX: $mx, MIN: $mn, TOTAL: $s, TIMES: $times ".PHP_EOL;
}
-}
+}

0 comments on commit 647d153

Please sign in to comment.