Permalink
Browse files

issue12: RB should play well with MySQL strict - dumped dtyp

  • Loading branch information...
gabor de mooij
gabor de mooij committed Dec 7, 2009
1 parent 34d806d commit 635df0d10b9f62ae1c4deee6555aa400e0e18303
Showing with 37 additions and 38 deletions.
  1. +34 −33 RedBean/QueryWriter/MySQL.php
  2. +3 −5 test.php
@@ -87,7 +87,7 @@ class RedBean_QueryWriter_MySQL implements RedBean_QueryWriter {
* Supported Column Types
*/
public $typeno_sqltype = array(
- RedBean_QueryWriter_MySQL::C_DATATYPE_BOOL=>" SET('1') ",
+ RedBean_QueryWriter_MySQL::C_DATATYPE_BOOL=>" TINYINT(1) UNSIGNED ",
RedBean_QueryWriter_MySQL::C_DATATYPE_UINT8=>" TINYINT(3) UNSIGNED ",
RedBean_QueryWriter_MySQL::C_DATATYPE_UINT32=>" INT(11) UNSIGNED ",
RedBean_QueryWriter_MySQL::C_DATATYPE_DOUBLE=>" DOUBLE ",
@@ -103,7 +103,7 @@ class RedBean_QueryWriter_MySQL implements RedBean_QueryWriter {
* constants (magic numbers)
*/
public $sqltype_typeno = array(
- "set('1')"=>RedBean_QueryWriter_MySQL::C_DATATYPE_BOOL,
+ "tinyint(1) unsigned"=>RedBean_QueryWriter_MySQL::C_DATATYPE_BOOL,
"tinyint(3) unsigned"=>RedBean_QueryWriter_MySQL::C_DATATYPE_UINT8,
"int(11) unsigned"=>RedBean_QueryWriter_MySQL::C_DATATYPE_UINT32,
"double" => RedBean_QueryWriter_MySQL::C_DATATYPE_DOUBLE,
@@ -164,20 +164,6 @@ public function check($table) {
public function __construct( RedBean_Adapter $adapter, $frozen = false ) {
$this->adapter = $adapter;
if (!$frozen) {
- $this->adapter->exec("DROP TABLE IF EXISTS `dtyp`");
- try{$this->adapter->exec("SET SESSION SQL_MODE=''");}catch(Exception $e){}
- $this->adapter->exec("
- CREATE TABLE IF NOT EXISTS `dtyp` (
- `id` int(11) unsigned NOT NULL auto_increment,
- `booleanset` set('1'),
- `tinyintus` tinyint(3) unsigned NOT NULL,
- `intus` int(11) unsigned NOT NULL,
- `doubles` double NOT NULL,
- `varchar255` varchar(255) NOT NULL,
- `text` text NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
- ");
$this->adapter->exec("
CREATE TABLE IF NOT EXISTS `__log` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
@@ -237,23 +223,38 @@ public function getColumns( $table ) {
* @return integer $type
*/
public function scanType( $value ) {
- $this->adapter->exec( "truncate table dtyp" );
- $v = "\"".$value."\"";
- $checktypeSQL = "insert into dtyp VALUES(null,$v,$v,$v,$v,$v,$v )";
- $this->adapter->exec( $checktypeSQL );
- $id = $this->adapter->getInsertID();
- $types = $this->dtypes;
- array_pop($types);
- $readtypeSQL = "SELECT ".implode(",",$types)." FROM dtyp WHERE id = $id ";
- $row = $this->adapter->getRow($readtypeSQL);;
- $tp = 0;
- foreach($row as $t=>$tv) {
- if (strval($tv) === strval($value)) {
- return $tp;
- }
- $tp++;
- }
- return $tp;
+
+ if (is_bool($value) || is_null($value)) {
+ return RedBean_QueryWriter_MySQL::C_DATATYPE_BOOL;
+ }
+
+ if (is_integer($value) && $value >= 0 && $value <= 255 ) {
+ return RedBean_QueryWriter_MySQL::C_DATATYPE_UINT8;
+ }
+
+ if (is_integer($value) && $value >= 0 && $value <= 4294967295 ) {
+ return RedBean_QueryWriter_MySQL::C_DATATYPE_UINT32;
+ }
+
+ if (is_float($value) || $value < 0) {
+ return RedBean_QueryWriter_MySQL::C_DATATYPE_DOUBLE;
+ }
+ if (is_string($value) && strlen($value) <= 255) {
+ return RedBean_QueryWriter_MySQL::C_DATATYPE_TEXT8;
+ }
+ if (is_string($value) && strlen($value) <= 65536) {
+ return RedBean_QueryWriter_MySQL::C_DATATYPE_TEXT16;
+ }
+
+ if (is_string($value) && strlen($value) > 65536) {
+ return RedBean_QueryWriter_MySQL::C_DATATYPE_TEXT32;
+ }
+
+ return 0;
+
+
+
+
}
/**
View
@@ -722,7 +722,6 @@ public function onEvent($event, $info) {
$redbean3 = new RedBean_Plugin_Cache( $redbean, $toolbox );
$movie = $redbean3->dispense("movie");
$movie->name = "cached movie 1";
-//$pdo->setDebugMode(1);
$movieid = $redbean3->store($movie);
asrt(($querycounter->counter>0),true);
$querycounter->counter=0;
@@ -856,7 +855,6 @@ public function onEvent($event, $info) {
$page->name = "Just Another Page In a Table";
$cols = $writer->getColumns("page");
asrt($cols["name"],"varchar(254)");
-//$pdo->setDebugMode(1);
$redbean->store( $page );
pass(); //no crash?
$cols = $writer->getColumns("page");
@@ -872,14 +870,15 @@ public function onEvent($event, $info) {
$redbean->addEventListener("update", $optimizer);
$writer = $toolbox->getWriter();
$cols = $writer->getColumns("one");
+
asrt($cols["col"],"text");
-$one->col = NULL;
+$one->col = true;
$redbean->store($one);
$cols = $writer->getColumns("one");
asrt($cols["col"],"text");
$redbean->store($one);
$cols = $writer->getColumns("one");
-asrt($cols["col"],"set('1')");
+asrt($cols["col"],"tinyint(1) unsigned");
$one->col = str_repeat('a long text',100);
$redbean->store($one);
@@ -1024,7 +1023,6 @@ public function onEvent($event, $info) {
$writer->deleteRecord("testtable", $id);
$row = $writer->selectRecord("testtable", array($id));
asrt($row,NULL);
-//$pdo->setDebugMode(1);
$writer->addColumn("testtable", "c2", 2);
try{ $writer->addUniqueIndex("testtable", array("c1","c2")); fail(); //should fail, no content length blob
}catch(RedBean_Exception_SQL $e){ pass(); }

0 comments on commit 635df0d

Please sign in to comment.