Permalink
Browse files

added and used AQueryWriter canCastToInt method for smart binding

  • Loading branch information...
1 parent 74e4a77 commit c2364714a3113947c05ce4f16e717a747f5ba15b Gabor de Mooij, Buurtnerd committed Jun 14, 2011
Showing with 71 additions and 274 deletions.
  1. +26 −24 RedBean/Driver/PDO.php
  2. +1 −1 RedBean/Plugin/Constraint.php
  3. +4 −0 RedBean/QueryWriter/AQueryWriter.php
  4. +1 −1 RedBean/redbean.inc.php
  5. +36 −3 ltest.php
  6. +0 −242 pgtest.php
  7. +1 −1 replica.xml
  8. +2 −2 test.php
View
50 RedBean/Driver/PDO.php
@@ -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.
*
@@ -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();
}
@@ -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();
View
2 RedBean/Plugin/Constraint.php
@@ -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;
View
4 RedBean/QueryWriter/AQueryWriter.php
@@ -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)));
+ }
+
}
View
2 RedBean/redbean.inc.php
@@ -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");
View
39 ltest.php
@@ -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");
@@ -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");
@@ -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);
@@ -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");
View
242 pgtest.php
@@ -85,249 +85,7 @@ public function onEvent($event, $info) {
}
}
-
try {
-
- $nullWriter = new RedBean_QueryWriter_NullWriter();
- $redbean = new RedBean_OODB( $nullWriter );
-
-//Section A: Config Testing
- testpack("CONFIG TEST");
-//Can we access the required exceptions?
- asrt(class_exists("RedBean_Exception_FailedAccessBean"),true);
- asrt(class_exists("RedBean_Exception_Security"),true);
- asrt(class_exists("RedBean_Exception_SQL"),true);
-
-
-//Section B: UNIT TESTING
- testpack("UNIT TEST RedBean OODB: Dispense");
- $page = $redbean->dispense("page");
- asrt(((bool)$page->getMeta("type")),true);
- asrt(isset($page->id),true);
- asrt(($page->getMeta("type")),"page");
- asrt(($page->id),0);
- try {
- $redbean->dispense("");
- fail();
- }catch(RedBean_Exception_Security $e) {
- pass();
- }
- try {
- $redbean->dispense(".");
- fail();
- }catch(RedBean_Exception_Security $e) {
- pass();
- }
- try {
- $redbean->dispense("-");
- fail();
- }catch(RedBean_Exception_Security $e) {
- pass();
- }
-
-
- testpack("UNIT TEST RedBean OODB: Check");
- $bean = $redbean->dispense("page");
- $bean->name = array("1");
- try {
- $redbean->store($bean);
- fail();
- }catch(RedBean_Exception_Security $e) {
- pass();
- }
- try {
- $redbean->check($bean);
- fail();
- }catch(RedBean_Exception_Security $e) {
- pass();
- }
- $bean->name = new RedBean_OODBBean;
- try {
- $redbean->store($bean);
- fail();
- }catch(RedBean_Exception_Security $e) {
- pass();
- }
- try {
- $redbean->check($bean);
- fail();
- }catch(RedBean_Exception_Security $e) {
- pass();
- }
- $prop = ".";
- $bean->$prop = 1;
- try {
- $redbean->store($bean);
- fail();
- }catch(RedBean_Exception_Security $e) {
- pass();
- }
- try {
- $redbean->check($bean);
- fail();
- }catch(RedBean_Exception_Security $e) {
- pass();
- }
- $prop = "-";
- $bean->$prop = 1;
- try {
- $redbean->store($bean);
- fail();
- }catch(RedBean_Exception_Security $e) {
- pass();
- }
- try {
- $redbean->check($bean);
- fail();
- }catch(RedBean_Exception_Security $e) {
- pass();
- }
-
-/*
- testpack("UNIT TEST RedBean OODB: Load");
- $bean = $redbean->load("typetest",2);
- $nullWriter->returnSelectRecord = array();
- asrt($nullWriter->selectRecordArguments[0],"typetest");
- asrt($nullWriter->selectRecordArguments[1],array(2));
- asrt($bean->id,0);
- $nullWriter->returnSelectRecord = array(array("name"=>"abc","id"=>3));
- $bean = $redbean->load("typetest",3);
- asrt($nullWriter->selectRecordArguments[0],"typetest");
- asrt($nullWriter->selectRecordArguments[1],array(3));
- asrt($bean->id,3);
- try {
- $bean = $redbean->load("typetest",-2);
- pass();
- }catch(RedBean_Exception_Security $e) {
- fail();
- }
- try {
- $bean = $redbean->load("typetest",0);
- pass();
- }catch(RedBean_Exception_Security $e) {
- fail();
- }
- try {
- $bean = $redbean->load("typetest",2.1);
- pass();
- }catch(RedBean_Exception_Security $e) {
- fail();
- }
- try {
- $bean = $redbean->load(" ",3);
- fail();
- }catch(RedBean_Exception_Security $e) {
- pass();
- }
- try {
- $bean = $redbean->load(".",3);
- fail();
- }catch(RedBean_Exception_Security $e) {
- pass();
- }
- try {
- $bean = $redbean->load("type.test",3);
- fail();
- }catch(RedBean_Exception_Security $e) {
- pass();
- }
-
- testpack("UNIT TEST RedBean OODB: Batch");
- $nullWriter->reset();
- $beans = $redbean->batch("typetest",array(2));
- $nullWriter->returnSelectRecord = array();
- asrt($nullWriter->selectRecordArguments[0],"typetest");
- asrt($nullWriter->selectRecordArguments[1],array(2));
- asrt(count($beans),0);
- $nullWriter->reset();
- $nullWriter->returnSelectRecord = array(array("name"=>"abc","id"=>3));
- $beans = $redbean->batch("typetest",array(3));
- asrt($nullWriter->selectRecordArguments[0],"typetest");
- asrt($nullWriter->selectRecordArguments[1],array(3));
- asrt(count($beans),1);
-
-
- testpack("UNIT TEST RedBean OODB: Store");
- $nullWriter->reset();
- $bean = $redbean->dispense("bean");
- $bean->name = "coffee";
- $nullWriter->returnScanType = 91239;
- $nullWriter->returnInsertRecord = 1234;
- asrt($redbean->store($bean),1234);
- asrt($nullWriter->getColumnsArgument,"bean");
- asrt($nullWriter->createTableArgument,"bean");
- asrt($nullWriter->scanTypeArgument,"coffee");
- asrt($nullWriter->codeArgument,NULL);
- asrt($nullWriter->addColumnArguments,array("bean","name",91239));
- asrt($nullWriter->insertRecordArguments,array("bean",array("name"),array(array("coffee"))));
- asrt($nullWriter->addUniqueIndexArguments,array());
- asrt($nullWriter->updateRecordArguments,array());
- asrt($nullWriter->widenColumnArguments,array());
- $nullWriter->reset();
- $bean = $redbean->dispense("bean");
- $bean->name = "chili";
- $bean->id=9876;
- $nullWriter->returnCode = 0;
- $nullWriter->returnScanType = 777;
- $nullWriter->returnTables=array("bean");
- $nullWriter->returnGetColumns=array("name"=>13);
- asrt($redbean->store($bean),9876);
- asrt($nullWriter->getColumnsArgument,"bean");
- asrt($nullWriter->createTableArgument,NULL);
- asrt($nullWriter->scanTypeArgument,"chili");
- asrt($nullWriter->codeArgument,13);
- asrt($nullWriter->addColumnArguments,array());
- asrt($nullWriter->insertRecordArguments,array());
- asrt($nullWriter->addUniqueIndexArguments,array());
- asrt($nullWriter->updateRecordArguments,array("bean",array(array("property"=>"name","value"=>"chili")),9876 ));
- asrt($nullWriter->widenColumnArguments,array("bean","name", 777));
-
- testpack("UNIT TEST RedBean OODB: Freeze");
- $nullWriter->reset();
- $redbean->freeze(true);
- $bean = $redbean->dispense("bean");
- $bean->name = "coffee";
- $nullWriter->returnScanType = 91239;
- $nullWriter->returnInsertRecord = 1234;
- asrt($redbean->store($bean),1234);
- asrt($nullWriter->getColumnsArgument,NULL);
- asrt($nullWriter->createTableArgument,NULL);
- asrt($nullWriter->scanTypeArgument,NULL);
- asrt($nullWriter->codeArgument,NULL);
- asrt($nullWriter->addColumnArguments,array());
- asrt($nullWriter->insertRecordArguments,array("bean",array("name"),array(array("coffee"))));
- asrt($nullWriter->addUniqueIndexArguments,array());
- asrt($nullWriter->updateRecordArguments,array());
- asrt($nullWriter->widenColumnArguments,array());
- $redbean->freeze(false);
-
-
-
-
-
- testpack("UNIT TEST RedBean OODBBean: import");
- $bean = new RedBean_OODBBean;
- $bean->import(array("a"=>1,"b"=>2));
- asrt($bean->a, 1);
- asrt($bean->b, 2);
-
-
-//Test observer
- testpack("UNIT TEST Observer Mechanism ");
- $observable = new ObservableMock();
- $observer = new ObserverMock();
- $observable->addEventListener("event1",$observer);
- $observable->addEventListener("event3",$observer);
- $observable->test("event1", "testsignal1");
- asrt($observer->event,"event1");
- asrt($observer->info,"testsignal1");
- $observable->test("event2", "testsignal2");
- asrt($observer->event,"event1");
- asrt($observer->info,"testsignal1");
- $observable->test("event3", "testsignal3");
- asrt($observer->event,"event3");
- asrt($observer->info,"testsignal3");
-*/
$adapter = $toolbox->getDatabaseAdapter();
$writer = $toolbox->getWriter();
$redbean = $toolbox->getRedBean();
View
2 replica.xml
@@ -72,7 +72,7 @@
<item type="php">RedBean/Plugin/Optimizer/Shrink.php</item>
<item type="php">RedBean/Plugin/Optimizer/Datetime.php</item>
<item type="php">RedBean/Plugin/QueryLogger.php</item>
- <item type="php">RedBean/QueryWriter/NullWriter.php</item>
+
<item type="php">RedBean/UnitOfWork.php</item>
<item type="php">RedBean/SimpleModel.php</item>
<item type="php">RedBean/ModelHelper.php</item>
View
4 test.php
@@ -134,8 +134,8 @@ function ID($id) {
return R::$writer->getIDField($table);
}
-$nullWriter = new RedBean_QueryWriter_NullWriter();
-$redbean = new RedBean_OODB( $nullWriter );
+//$nullWriter = new RedBean_QueryWriter_MySQL();
+$redbean = $toolbox->getRedBean(); //new RedBean_OODB( $nullWriter );
$linker = new RedBean_LinkManager( $toolbox );

0 comments on commit c236471

Please sign in to comment.