require_once(dirname(__FILE__).'/../db.php');
if (empty($_SERVER['DOCUMENT_ROOT'])) {
- $_REQUEST['db'] = 'rent'; $name = 'tenant';
- $_REQUEST['db'] = 'rt'; $name = 'classes';
+ $_REQUEST['db'] = 'rent'; $name = 'test';
+ #$_REQUEST['db'] = 'rt'; $name = 'classes';
require_once('/opt/rent/www/config/db.php');
- #$db = new db('mysql:host=big;port=3306;dbname=rent;user=nico;password=');
print_r($Db->tables());
$Table = $Db->table($name);
- print_r($Table->sql());
+ print_r($Table->fields());
+ #print_r($Table->sql());
}
if (!defined('DB_HTML_EDIT')) define('DB_HTML_EDIT','Edit');
*
*/
function sql() {
+ if (isset($this->sql)) return $this->sql;
if ($this->db->type == 'sqlite') {
$sql = "SELECT sql FROM sqlite_master WHERE name='$this->name'";
} elseif ($this->db->type == 'mysql') {
} else {
err('table.sql(): Unknow db type: '.$this->db->type);
}
- return $this->db->query($sql);
+ $this->sql = $this->db->query($sql);
+ return $this->sql;
}
/*
a.attname AS name,
pg_catalog.format_type(a.atttypid, a.atttypmod) AS type,
CASE a.attnotnull WHEN 't' then 1 ELSE 0 END AS notnull,
-(SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128) FROM pg_catalog.pg_attrdef d WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) AS default,
+(SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128) FROM pg_catalog.pg_attrdef d WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) AS pg_default,
(SELECT 1 FROM pg_index i WHERE a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey) AND i.indrelid = '$this->name'::regclass AND i.indisprimary) as pk
FROM pg_catalog.pg_attribute a WHERE a.attrelid = (SELECT c.oid FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname='$this->name' AND pg_catalog.pg_table_is_visible(c.oid) ) AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum";
'autoincrement' => false,
);
- $this->fields[$row['name']]['type'] = $row['type'];
+ $this->fields[$row['name']]['type'] = strtolower($row['type']);
if (isset($row['notnull'])) {
$this->fields[$row['name']]['null'] = $row['notnull'] == '0' ? 1 : 0;
}
+ # sqlite autoincrement
+ if ($this->db->type == 'sqlite' and preg_match('/[\(,]'.$row['name'].' [^\),]+ AUTOINCREMENT/',$this->sql())) {
+ $this->fields[$row['name']]['autoincrement'] = true;
+ }
+
+ # mysql autoincrement
+ if (isset($row['Extra']) and $row['Extra'] == 'auto_increment') {
+ $this->fields[$row['name']]['autoincrement'] = true;
+ }
+
+ # Postgres autoincrement / default
+ if (isset($row['pg_default'])) {
+
+ if (preg_match('/^nextval\(/',$row['pg_default'])) {
+ $this->fields[$row['name']]['autoincrement'] = true;
+
+ } elseif (preg_match("/^''/",$row['pg_default'])) {
+ $this->fields[$row['name']]['default'] = '';
+
+ } else {
+ $this->fields[$row['name']]['default'] = $row['pg_default'];
+
+ }
+ }
+
+ # sqlite default
foreach (array('dflt_value') as $f) {
if (!isset($row[$f])) continue;
$this->fields[$row['name']]['default'] = $row[$f];