From 1f31949150f1e957fbb871afd8da44f7a50e9e34 Mon Sep 17 00:00:00 2001 From: Slim Amamou Date: Sun, 3 Feb 2008 23:33:51 +0100 Subject: [PATCH] tout fonctionne. mais pas tres bien. je dois encore ameliorer l'ihm --- bazdig/bazdig.css | 79 +++++++++++++++++++++++++++++++++++++++++++--- bazdig/console/index.php | 51 +++++++++++++++++++++++++++--- bazdig/db/index.php | 15 +++++---- bazdig/db/set/index.php | 5 +-- bazdig/history/index.php | 9 ++++-- bazdig/sql/exec/index.php | 16 +++++++--- lib/code.php | 14 +++++++- lib/database.php | 60 +++++++++++++++++++++++++++++++---- test/SqlCodeTest.php | 15 +++++++++ test/TableTest.php | 45 +++++++++++++++++++++++--- test/bazdig-test.db | Bin 5120 -> 5120 bytes test/query_string.php | 1 + 12 files changed, 274 insertions(+), 36 deletions(-) rewrite bazdig/bazdig.css (100%) rewrite bazdig/db/index.php (63%) create mode 100644 test/query_string.php diff --git a/bazdig/bazdig.css b/bazdig/bazdig.css dissimilarity index 100% index e0068cc..2ebdef8 100644 --- a/bazdig/bazdig.css +++ b/bazdig/bazdig.css @@ -1,5 +1,74 @@ -.button { border: 1px outset silver ; padding: 5px 10px; color: black; background-color: lightgrey; text-decoration:none} -.query { display: block; padding: 10px; text-decoration: none; color: black } -a:hover.query { background-color: yellow } -#ok { width: 100%; } -#browser { width: 30%; padding: 10px; vertical-align: top} +body { + font: 1em Verdana, Arial, sans-serif; + background-color: #FFFFFF; + color: #000000; + margin: 1em 0 0 1em; +} +#nav a { + border: 1px outset silver ; + padding: 5px 10px; + color: black; + background-color: lightgrey; + text-decoration:none +} +#settings {  + text-align: right; + padding: 10px; + width: 400px; +} +#settings label { + display: block; +} +#settings * input:focus { + background: yellow; +} +#queries a { + display: block; + padding: 10px; + text-decoration: none; + color: black +} +#queries a:hover { + background-color: yellow; +} +#ok { + display: block; + clear:both; + width: 100% +} +table tr td { + vertical-align: top; +} +#console { + float: left; + width: 70% +} +#schema ul { + list-style-type: none; + margin: 0; + padding: 0; +} +#schema * a { + display: block; + border: 1px inset silver ; + color: grey; + background-color: lightgrey; + text-decoration:none; + padding: 5px 10px; +} +#schema * a:hover { + border: 1px outset silver ; + color: black; +} +#schema ul li a { + font-weight: bold; +} +#schema ul li ul li a { + font-weight: normal; +} +#schema ul li ul { + display: none; +} +#schema ul li:hover ul { + display: block; +} diff --git a/bazdig/console/index.php b/bazdig/console/index.php index e0f8bb5..29e49e0 100644 --- a/bazdig/console/index.php +++ b/bazdig/console/index.php @@ -1,19 +1,62 @@ bazdig + + -

historydatabase

+ + +
- - + + +
$_SESSION['db_type'], 'name' => $_SESSION['db_name'], 'host' => $_SESSION['db_host']), $_SESSION['db_user'], $_SESSION['db_password']); + echo "

".$work_db->name.""; + echo " @". $work_db->host ."

"; + echo $work_db->httpGet($bazdig->get('/db/schema/')); +} + +?> +
+ + + + +
+ + diff --git a/bazdig/db/index.php b/bazdig/db/index.php dissimilarity index 63% index 72a1e83..c911c2e 100644 --- a/bazdig/db/index.php +++ b/bazdig/db/index.php @@ -1,6 +1,9 @@ -
- - - - -
+ +
+ + + + + + +
diff --git a/bazdig/db/set/index.php b/bazdig/db/set/index.php index 3ea73a4..a14b50f 100644 --- a/bazdig/db/set/index.php +++ b/bazdig/db/set/index.php @@ -1,11 +1,12 @@ -

consolesave

+ +
+
get_url() .'?q='. $q->code .'" >'. $q->code .''; + echo ''. $q->code .''; } +?> +
+
diff --git a/bazdig/sql/exec/index.php b/bazdig/sql/exec/index.php index 6fee79f..94d97fe 100644 --- a/bazdig/sql/exec/index.php +++ b/bazdig/sql/exec/index.php @@ -4,14 +4,22 @@ define('WARAQ_ROOT', '../../..'); require_once WARAQ_ROOT .'/'. 'ini.php'; - if (!$_SESSION['db']) { - header('Location: '. $bazdig->get('/db')->url ); + require "code.php"; + + if ($_GET['dbt']) { + $_SESSION['db_type'] = $_GET['dbt']; + $_SESSION['db_name'] = $_GET['dbn']; + $_SESSION['db_host'] = $_GET['dbh']; + $_SESSION['db_user'] = $_GET['dbu']; + $_SESSION['db_password'] = $_GET['dbp']; } - require "code.php"; + if (!$_SESSION['db_type'] or !$_GET['q']) { + header('Location: '. $bazdig->get('/console')->url ); + } $history_db = new PDO("sqlite:". $bazdig->getparam('db')->file); - $work_db = new PDO($_SESSION['db'], $_SESSION['db_user'], $_SESSION['db_password']); + $work_db = new BDB(array('type' => $_SESSION['db_type'], 'name' => $_SESSION['db_name'], 'host' => $_SESSION['db_host']), $_SESSION['db_user'], $_SESSION['db_password']); SqlCode::set_db($history_db); $query = new SqlCode(stripslashes($_GET['q'])); diff --git a/lib/code.php b/lib/code.php index d4aaa64..0e2a5b3 100644 --- a/lib/code.php +++ b/lib/code.php @@ -1,6 +1,7 @@ query($this->code); return $result; } + + function extractColumns() + { + eregi('create +table +[^(]+\(([^)]+)', $this->code, $strings); + $columns = split(',', $strings[1]); + for ($i=0; $i < count($columns); $i++) { + $c = new Column(firstWord($columns[$i])); + $columns[$i] = $c; + } + return $columns; + } } diff --git a/lib/database.php b/lib/database.php index 66b0a7a..d935486 100755 --- a/lib/database.php +++ b/lib/database.php @@ -2,8 +2,48 @@ define('WARAQ_ROOT', '..'); require_once WARAQ_ROOT .'/ini.php'; + require_once "code.php"; + class BDB extends PDO { + var $type, $name, $host, $user, $password; + + function __construct($dsn, $username = NULL, $password = NULL) + { + $this->user = $username; + $this->password = $password; + if (is_array($dsn)) { + $dbconfig = $dsn; + $this->type = $dbconfig['type']; + $this->name = $dbconfig['name']; + $this->host = $dbconfig['host']; + $dsn = $this->getDsn(); + } + parent::__construct($dsn,$username, $password); + } + + function getDsn() + { + $dsn = $this->type .':'; + if ("sqlite" == $this->type or "sqlite2" == $this->type) { + $dsn .= $this->name; + } else { + $dsn .= 'host='. $this->host; + $dsn .= ';dbname='. $this->name; + } + return $dsn; + } + + function httpGet($localResource) + { + $queryString = "dbt=". $this->type; + $queryString .= "&dbn=". $this->name; + $queryString .= "&dbh=". $this->host; + $queryString .= "&dbu=". $this->user; + $queryString .= "&dbp=". $this->password; + return file_get_contents($localResource->url .'?'. $queryString); + } + function listTables() { $dbType = $this->getAttribute(PDO::ATTR_DRIVER_NAME); @@ -15,7 +55,7 @@ $result = $this->query($query); $tables = array(); foreach ($result as $r) { - $tables []= new Table($r['name']); + $tables []= new Table($r[0]); } return $tables; @@ -35,17 +75,23 @@ function loadColumns($db) { + $this->columns = array(); $table = $this->name; $dbType = $db->getAttribute(PDO::ATTR_DRIVER_NAME); if ('sqlite' == $dbType || 'sqlite2' == $dbType) { - $result = $db->fetchColumnTypes($table); - foreach ($result as $column => $type) { - $this->columns []= new Column($column, $type); - } + $query = "select sql from sqlite_master where type='table' and name='$table'"; + $row = $db->query($query)->fetch(); + $createQuery = new SqlCode($row['sql']); + $this->columns = $createQuery->extractColumns(); } else { - $columns = $db->query("show columns from $table"); + $query = "show columns from $table"; + $result = $db->query($query); + foreach ($result as $column) { + $this->columns []= new Column($column['Field'], $column['Type']); + } } - + + return $this->columns; } } diff --git a/test/SqlCodeTest.php b/test/SqlCodeTest.php index 7d4dee5..cfb5637 100644 --- a/test/SqlCodeTest.php +++ b/test/SqlCodeTest.php @@ -18,6 +18,7 @@ class SqlCodeTest extends UnitTestCase { $this->sqlCode2->date = '2008-01-25'; $this->markkitDB =& new PDO("sqlite2:markkit-test.db"); $this->markkitQuery3 = new SqlCode("select * from marks where pageUrl like '%x'"); + $this->markkitQuery4 = new SqlCode("create table marks (pageUrl, text)"); } function tearDown() @@ -29,6 +30,20 @@ class SqlCodeTest extends UnitTestCase { $this->db->exec("delete from $tableName where id='$id'"); } + function test_extractColumns() + { + $result = firstWord(' test test2 test3 '); + $expected = 'test'; + $this->assertEqual($expected, $result); + $result = firstWord('test'); + $this->assertEqual($expected, $result); + $result = $this->markkitQuery4->extractColumns(); + $expected = 2; + $this->assertEqual($expected, count($result)); + $expected = array(new Column('pageUrl'), new Column('text')); + $this->assertEqual($expected, $result); + } + function test_search() { $result = SqlCode::search("test"); diff --git a/test/TableTest.php b/test/TableTest.php index f803fc6..3808993 100644 --- a/test/TableTest.php +++ b/test/TableTest.php @@ -7,27 +7,62 @@ class TableTest extends UnitTestCase { function setUp() { + define('WARAQ_ROOT', '..'); + require_once WARAQ_ROOT .'/ini.php'; require_once '../lib/database.php'; - $this->db =& new BDB("sqlite:bazdig-test.db"); + $this->dbsqlite =& new BDB("sqlite:bazdig-test.db"); + $this->dbmysql =& new BDB("mysql:host=localhost;dbname=markkit", "root"); + $this->dbmysql2 =& new BDB(array('type' => 'mysql', 'host' => 'localhost', 'name' => 'markkit'), "root"); + $this->dbsqlite2 =& new BDB(array('type' => 'sqlite', 'name' => 'bazdig-test.db')); $this->table = new Table('sql'); + $this->table2 = new Table('marks'); } function tearDown() { } + function test_httpGet() + { + global $bazdig; + + $result = $this->dbmysql2->httpGet($bazdig->get('/test/query_string.php')); + $expected = "dbt=mysql&dbn=markkit&dbh=localhost&dbu=root&dbp="; + $this->assertEqual($expected, $result); + } + + function test_getDsn() + { + $result = $this->dbmysql2->getDsn(); + $expected = "mysql:host=localhost;dbname=markkit"; + $this->assertEqual($expected, $result); + $result = $this->dbsqlite2->getDsn(); + $expected = "sqlite:bazdig-test.db"; + $this->assertEqual($expected, $result); + } + function test_listTables() { - $tables = $this->db->listTables(); - $expected = array($this->table); + $tables = $this->dbsqlite->listTables(); + $expected = array($this->table, new Table('bash')); + $this->assertEqual($expected, $tables); + $expected = array(new Table('ctrl_patch'), new Table('marks')); + $tables = $this->dbmysql->listTables(); + $this->assertEqual($expected, $tables); + $tables = $this->dbmysql2->listTables(); $this->assertEqual($expected, $tables); } function test_loadColumns() { - $this->table->loadColumns($this->db); - $expected = array(new Column('id'), new Column('date'), new Column('code')); + $this->table->loadColumns($this->dbsqlite); + $expected = array(new Column('id'), new Column('code'), new Column('date')); $this->assertEqual($expected, $this->table->columns); + $expected = array(new Column('id', 'varchar(50)'), new Column('creationDate', 'datetime'), new Column('pageUrl', 'varchar(255)'), new Column('text', 'varchar(255)'), new Column('owner', 'varchar(50)'), new Column ('startNodePath', 'varchar(128)'), new Column ('startOffset', 'int(11)'), new Column('endNodePath', 'varchar(128)'), new Column('endOffset', 'int(11)')); + $this->table2->loadColumns($this->dbmysql); + $this->assertEqual($expected, $this->table2->columns); + $this->table2->loadColumns($this->dbmysql2); + $this->assertEqual($expected, $this->table2->columns); } } diff --git a/test/bazdig-test.db b/test/bazdig-test.db index 75d5d91b42bf4a869b9afee6131410c6e40c3ce1..50ab8a3f718d7c71ea4ab1d9f96e5887bbf4c2f6 100644 GIT binary patch delta 45 qcwTGJXwaA-&G>Gkj3zs`v0;dzp_Q?rm7(S20Cq2A{^q6Z(L4YbnG3K0 delta 45 qcwTGJXwaA-%^0^)Mw6Y}$Rfnh#LC3f%Gi8z0J|45fAdoIXdVCra|=8G diff --git a/test/query_string.php b/test/query_string.php new file mode 100644 index 0000000..4f019e4 --- /dev/null +++ b/test/query_string.php @@ -0,0 +1 @@ +