Revert "j'ai corrigé un bug qui empechait le schema browser de fonctionner avec les...
[bazdig.git] / lib / database.php
blob87648d3b0db0b1df53da07fce2f253327ccd78e3
1 <?php
2 define('WARAQ_ROOT', '..');
3 require_once WARAQ_ROOT .'/ini.php';
5 require_once "code.php";
7 class BDB extends PDO
9 var $type, $name, $host, $user, $password;
11 function __construct($dsn, $username = NULL, $password = NULL)
13 $this->user = $username;
14 $this->password = $password;
15 if (is_array($dsn)) {
16 $dbconfig = $dsn;
17 $this->type = $dbconfig['type'];
18 $this->name = $dbconfig['name'];
19 $this->host = $dbconfig['host'];
20 $dsn = $this->getDsn();
22 parent::__construct($dsn,$username, $password);
25 function getDsn()
27 $dsn = $this->type .':';
28 if ("sqlite" == $this->type or "sqlite2" == $this->type) {
29 $dsn .= $this->name;
30 } else {
31 $dsn .= 'host='. $this->host;
32 $dsn .= ';dbname='. $this->name;
34 return $dsn;
37 function httpGet($localResource)
39 $queryString = "dbt=". $this->type;
40 $queryString .= "&dbn=". $this->name;
41 $queryString .= "&dbh=". $this->host;
42 $queryString .= "&dbu=". $this->user;
43 $queryString .= "&dbp=". $this->password;
44 return file_get_contents($localResource->url .'?'. $queryString);
47 function listTables()
49 $dbType = $this->getAttribute(PDO::ATTR_DRIVER_NAME);
50 if ('sqlite' == $dbType || 'sqlite2' == $dbType) {
51 $query = "select name from sqlite_master where type='table'";
52 } else {
53 $query = "show tables";
55 $result = $this->query($query);
56 $tables = array();
57 foreach ($result as $r) {
58 $tables []= new Table($r[0]);
61 return $tables;
65 class Table
67 var $name;
68 var $columns;
70 function __construct($name, $columns = array())
72 $this->name = $name;
73 $this->columns = $columns;
76 function loadColumns($db)
78 $this->columns = array();
79 $table = $this->name;
80 $dbType = $db->getAttribute(PDO::ATTR_DRIVER_NAME);
81 if ('sqlite' == $dbType || 'sqlite2' == $dbType) {
82 $query = "select sql from sqlite_master where type='table' and name='$table'";
83 $row = $db->query($query)->fetch();
84 $createQuery = new SqlCode($row['sql']);
85 $this->columns = $createQuery->extractColumns();
86 } else {
87 $query = "show columns from $table";
88 $result = $db->query($query);
89 foreach ($result as $column) {
90 $this->columns []= new Column($column['Field'], $column['Type']);
94 return $this->columns;
99 class Column
101 var $name;
102 var $type;
104 function __construct($name, $type = NULL)
106 $this->name = $name;
107 $this->type = $type;
111 function columnNames($row)
112 $names = array();
113 if (is_array($row)) $names = array_keys($row);
114 return $names;