Skip to content

Commit

Permalink
added and used AQueryWriter canCastToInt method for smart binding
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabor de Mooij, Buurtnerd committed Jun 14, 2011
1 parent 74e4a77 commit c236471
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 274 deletions.
50 changes: 26 additions & 24 deletions RedBean/Driver/PDO.php
Expand Up @@ -169,6 +169,30 @@ public function connect() {
}


protected function bindParams($s,$aValues) {
foreach($aValues as $key=>&$value) {
if (is_integer($key)) {
if (RedBean_QueryWriter_AQueryWriter::canBeTreatedAsInt($value) && $value < 2147483648) {
$s->bindParam($key+1,$value,PDO::PARAM_INT);
}
else {
$s->bindParam($key+1,$value,PDO::PARAM_STR);
}
}
else {

if (RedBean_QueryWriter_AQueryWriter::canBeTreatedAsInt($value) && $value < 2147483648) {
$s->bindParam($key,$value,PDO::PARAM_INT);
}
else {
$s->bindParam($key,$value,PDO::PARAM_STR);
}
}

}
}


/**
* Runs a query and fetches results as a multi dimensional array.
*
Expand Down Expand Up @@ -197,18 +221,8 @@ public function GetAll( $sql, $aValues=array() ) {
$s->execute($aValues);
}
else {
foreach($aValues as $key=>&$value) {
if (is_integer($key)) {
if (ctype_digit(strval($value)) && $value < 2147483648) { $s->bindParam($key+1,$value,PDO::PARAM_INT); }
else $s->bindParam($key+1,$value,PDO::PARAM_STR);
}
else {
$this->bindParams( $s, $aValues );

if (ctype_digit(strval($value)) && $value < 2147483648) $s->bindParam($key,$value,PDO::PARAM_INT);
else $s->bindParam($key,$value,PDO::PARAM_STR);
}

}
$s->execute();
}

Expand Down Expand Up @@ -360,19 +374,7 @@ public function Execute( $sql, $aValues=array() ) {
}
else {

foreach($aValues as $key=>&$value) {

if (is_integer($key)) {
if (ctype_digit(strval($value)) && $value < 2147483648) { $s->bindParam($key+1,$value,PDO::PARAM_INT); }
else $s->bindParam($key+1,$value,PDO::PARAM_STR);
}
else {

if (ctype_digit(strval($value)) && $value < 2147483648) $s->bindParam($key,$value,PDO::PARAM_INT);
else $s->bindParam($key,$value,PDO::PARAM_STR);
}

}
$this->bindParams( $s, $aValues );


$s->execute();
Expand Down
2 changes: 1 addition & 1 deletion RedBean/Plugin/Constraint.php
Expand Up @@ -256,7 +256,7 @@ private static function constraintSQLite($toolbox, $table, $table1, $table2, $pr


$sql1 = "
CREATE TRIGGER IF NOT EXISTS {$fkCode}a
CREATE TRIGGER IF NOT EXISTS {$fkCode}a
BEFORE DELETE ON $table1
FOR EACH ROW BEGIN
DELETE FROM $table WHERE $table.$property1 = OLD.$idfield1;
Expand Down
4 changes: 4 additions & 0 deletions RedBean/QueryWriter/AQueryWriter.php
Expand Up @@ -394,5 +394,9 @@ public function addIndex($table, $name, $column) {
try{ $this->adapter->exec("CREATE INDEX $name ON $table ($column) "); }catch(Exception $e){}
}

public static function canBeTreatedAsInt( $value ) {
return (strval($value)===strval(intval($value)));
}


}
2 changes: 1 addition & 1 deletion RedBean/redbean.inc.php
Expand Up @@ -102,7 +102,7 @@
require($dir."Plugin/BeanMachine.php");
require($dir."Plugin/QueryLogger.php");

require($dir."QueryWriter/NullWriter.php");


/* Developer Comfort */
require($dir."UnitOfWork.php");
Expand Down
39 changes: 36 additions & 3 deletions ltest.php
Expand Up @@ -123,8 +123,8 @@ public function onEvent($event, $info) {
}


$nullWriter = new RedBean_QueryWriter_NullWriter();
$redbean = new RedBean_OODB( $nullWriter );
//$nullWriter = new RedBean_QueryWriter_NullWriter();
//$redbean = new RedBean_OODB( $nullWriter );
/*
//Section A: Config Testing
testpack("CONFIG TEST");
Expand Down Expand Up @@ -498,7 +498,6 @@ public function onEvent($event, $info) {

$adapter->exec("DROP TRIGGER IF EXISTS fkb8317025deb6e03fc05abaabc748a503a ");
$adapter->exec("DROP TRIGGER IF EXISTS fkb8317025deb6e03fc05abaabc748a503b ");

//add cask 101 and whisky 12
$cask = $redbean->dispense("cask");
$whisky = $redbean->dispense("whisky");
Expand Down Expand Up @@ -1586,6 +1585,7 @@ function testViews($p) {




list( $mickey, $donald, $goofy ) = R::dispense("musician",3);
list( $vocals1, $vocals2, $keyboard1, $drums, $vocals3, $keyboard2 ) = R::dispense("bandmember",6);
list( $band1, $band2 ) = R::dispense("band",2);
Expand Down Expand Up @@ -1687,6 +1687,39 @@ function testViews($p) {



testpack("wipe and constraints");

R::exec(" drop table if exists page ");
R::exec(" drop table if exists book ");
R::exec(" drop table if exists book_page ");
R::exec(" drop table if exists prefix_page ");
R::exec(" drop table if exists prefix_book ");
R::exec(" drop table if exists prefix_book_page ");



R::exec("DROP TRIGGER IF EXISTS fkc2d4c7ea9e656a361bc08c9d072914cca ");
R::exec("DROP TRIGGER IF EXISTS fkc2d4c7ea9e656a361bc08c9d072914ccb ");

$page1 = R::dispense("page");
$book1 = R::dispense("book");
$page2 = R::dispense("page");
$book2 = R::dispense("book");
$page1->name = "page1";
$page2->name = "page2";
$book1->name = "book1";
$book2->name = "book2";

R::associate($book1,$page1);
R::associate($book2,$page2);
//exit;
asrt(count( R::getAll("select * from prefix_book_page")),2);
R::trash($book1);
asrt(count( R::getAll("select * from prefix_book_page")),1);
R::wipe("book");
asrt(count(R::getAll("select * from prefix_book_page")),0);




printtext("\nALL TESTS PASSED. REDBEAN SHOULD WORK FINE.\n");

0 comments on commit c236471

Please sign in to comment.