<?php
require_once('database.inc.php');   # load database library


# create table benchmarkSet {{{
$tableName = 'benchmarkSet';

$fields = "
    id I AUTO KEY,
    name C(100),
    description C(2000)
";

createTable($tableName, $fields);
# }}}


# create table benchmark {{{
$tableName = 'benchmark';

$fields = "
    id I AUTO KEY,
    name C(100) CONSTRAINTS 'UNIQUE',
    description C(2000)
";

createTable($tableName, $fields);
# }}}


# create table benchmarkInstance {{{
$tableName = 'benchmarkInstance';

$fields = "
    id I AUTO KEY,
    benchmarkId I CONSTRAINTS 'FOREIGN KEY REFERENCES benchmark',
    benchmarkSetId I CONSTRAINTS 'FOREIGN KEY REFERENCES benchmarkSet',
    formatId I CONSTRAINTS 'FOREIGN KEY REFERENCES format',
    description C(2000),
    fileName C(100)
";

createTable($tableName, $fields);
# }}}


# create table format {{{
$tableName = 'format';

$fields = "
    id I AUTO KEY,
    name C(100) CONSTRAINTS 'UNIQUE',
    description C(2000)
";

createTable($tableName, $fields);
# }}}


# create table formatAttribute {{{
$tableName = 'formatAttribute';

$fields = "
    formatId I CONSTRAINTS 'FOREIGN KEY REFERENCES format',
    attributeId I CONSTRAINTS 'FOREIGN KEY REFERENCES attribute'
";

createTable($tableName, $fields);
# }}}


# create table attribute {{{
$tableName = 'attribute';

$fields = "
    id I AUTO KEY,
    name C(100) CONSTRAINTS 'UNIQUE',
    description C(2000)
";

createTable($tableName, $fields);
# }}}


# create table benchmarkInstanceAttribute {{{
$tableName = 'benchmarkInstanceAttribute';

$fields = "
    benchmarkInstanceId I CONSTRAINTS 'FOREIGN KEY REFERENCES benchmark',
    attributeId I CONSTRAINTS 'FOREIGN KEY REFERENCES attribute',
    value C(100)
";

createTable($tableName, $fields);
# }}}


# create table b DB user {{{
$tableName = 'bDBUser';

$fields = "
    id I AUTO KEY,
    login C(100) CONSTRAINTS 'UNIQUE',
    password C(100),
    canChangeOwnPassword I,
    canEditDatabase I,
    canEditUserAccounts I
";

createTable($tableName, $fields);

# create the admin user if there is no user yet {{{
$db = connect();

$db->StartTrans();

$recordSet = $db->Execute('SELECT count(*) FROM bDbUser');
$row = $recordSet->FetchRow();

if ($row['count'] == 0)
{
    $db->Execute("INSERT INTO bDbUser " . 
                 "(login, password, canEditDatabase, canEditUserAccounts, canChangeOwnPassword) " .
                 "VALUES ('$_GET[login]', '$_GET[password]', 1, 1, 1)");
}

$db->CompleteTrans();
# }}}

# }}}


# create table session user {{{
$tableName = 'sessionUser';

$fields = "
    sessionId C(100),
    userId I,
    createdTime T
";

createTable($tableName, $fields);
# }}}
?>
