Permalink
Browse files

Drop table implemented and tested

Signed-off-by: Pavan Kumar Sunkara <pavan.sss1991@gmail.com>
  • Loading branch information...
1 parent 11d650e commit ed267e6cc92127cde1f7721eac1e2ab8e25a6e0d @pksunkara committed Dec 5, 2010
Showing with 43 additions and 3 deletions.
  1. +29 −2 data/lib/db.php
  2. +1 −1 tests/create.php
  3. +13 −0 tests/drop.php
View
@@ -158,10 +158,39 @@ public function query($str)
$fields = explode(",",$str);
$this->createtable($table,$fields);
}
+ else if(preg_match("/^DROP TABLE [-a-zA-Z0-9_]+$/i",$str,$match)!=0 && $str==$match[0])
+ {
+ $table = substr($str,11);
+ $this->checkTableName($table);
+ $this->droptable($table);
+ }
else
self::$error->set("Not a valid mysql query. Query: ".$str);
}
+ protected function droptable($table)
+ {
+ $tbvl = array();
+
+ $fp = fopen(self::$db."mysql","r");
+ while(fscanf($fp,"%s\n",$hash))
+ {
+ $tbarr = $this->json->decode($hash);
+ if($tbarr->name!=$table)
+ array_push($tbvl,$tbarr);
+ }
+ fclose($fp);
+
+ unlink(self::$db.$table);
+
+ $tbfp = fopen(self::$db."mysql","w");
+ foreach($tbvl as $arr)
+ {
+ fwrite($tbfp,$this->json->encode($arr)."\n");
+ }
+ fclose($tbfp);
+ }
+
protected function createtable($table,$fields)
{
$tbvl = array();
@@ -177,9 +206,7 @@ protected function createtable($table,$fields)
break;
}
else
- {
array_push($tbvl,$tbarr);
- }
}
foreach($fields as $key=>$val)
View
@@ -8,6 +8,6 @@
include_once 'config.php';
-$db->query("CREATE TABLE shoppers (name string NULL,credit int DEFAULT '100')")
+$db->query("CREATE TABLE shoppers (name string NULL,credit int DEFAULT '100')");
?>
View
@@ -0,0 +1,13 @@
+<?php
+/* Name: PDQL
+ * Description: Php based Database and Query Language
+ * Author: Pavan Kumar Sunkara <pavan.sss1991@gmail.com>
+ * Copyright: Copyright (C) 2009 Sun Web dev, Inc.
+ * Licence: You may redistribute this under Creative Commons License
+ */
+
+include_once 'config.php';
+
+$db->query("DROP TABLE shoppers");
+
+?>

0 comments on commit ed267e6

Please sign in to comment.