| 6 | 
           rodolico | 
           1 | 
           /* 
  | 
        
        
            | 
            | 
           2 | 
              mySQL Database for Computer Asset Management Program
  | 
        
        
            | 
            | 
           3 | 
              No records are deleted by default. If record is to be deleted
  | 
        
        
           | 38 | 
           rodolico | 
           4 | 
              it is marked as removed (removed not null)
  | 
        
        
           | 18 | 
           rodolico | 
           5 | 
              When joining tables (ie client_device) is modified, the old record is
  | 
        
        
           | 6 | 
           rodolico | 
           6 | 
              marked removed and a new record added, to give history of device
  | 
        
        
            | 
            | 
           7 | 
           */
  | 
        
        
            | 
            | 
           8 | 
              | 
        
        
            | 
            | 
           9 | 
           /* used as a configuration file for the app as a whole */
  | 
        
        
           | 18 | 
           rodolico | 
           10 | 
           drop table if exists _system;
  | 
        
        
            | 
            | 
           11 | 
           create table _system (
  | 
        
        
           | 6 | 
           rodolico | 
           12 | 
             _system_id      int unsigned not null auto_increment,
  | 
        
        
            | 
            | 
           13 | 
             group_name      varchar(64) NOT NULL COMMENT 'used to group keys together',
  | 
        
        
            | 
            | 
           14 | 
             key_name        varchar(64) NOT NULL COMMENT 'key into this value',
  | 
        
        
            | 
            | 
           15 | 
             theValue        text null COMMENT 'the actual value of this entry',
  | 
        
        
           | 22 | 
           rodolico | 
           16 | 
             primary key     (_system_id )
  | 
        
        
           | 6 | 
           rodolico | 
           17 | 
           )  COMMENT='Basically a configuration file equivilent to a windows INI ';
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           /* used by the auth class */
  | 
        
        
           | 18 | 
           rodolico | 
           20 | 
           drop table if exists _user;
  | 
        
        
            | 
            | 
           21 | 
           create table _user (
  | 
        
        
           | 38 | 
           rodolico | 
           22 | 
              _user_id       int unsigned not null auto_increment,
  | 
        
        
            | 
            | 
           23 | 
              username       varchar(32) not null comment 'user name for logging in',
  | 
        
        
            | 
            | 
           24 | 
              name           varchar(64) comment 'common name of user',
  | 
        
        
            | 
            | 
           25 | 
              email          varchar(64) comment 'email address of user',
  | 
        
        
            | 
            | 
           26 | 
              notes          text comment 'any notes about user',
  | 
        
        
            | 
            | 
           27 | 
              passwd         varchar(256) comment 'encrypted password of user',
  | 
        
        
            | 
            | 
           28 | 
              access         text comment 'sql to determine what records user can view',
  | 
        
        
            | 
            | 
           29 | 
              added          date not null comment 'Date record added to database',
  | 
        
        
            | 
            | 
           30 | 
              removed        date default null comment 'Date record marked as removed',
  | 
        
        
            | 
            | 
           31 | 
              primary key    ( _user_id )
  | 
        
        
           | 6 | 
           rodolico | 
           32 | 
           ) comment 'user access to program';
  | 
        
        
            | 
            | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
           /* used by the menu class */
  | 
        
        
           | 18 | 
           rodolico | 
           35 | 
           drop table if exists _menu;
  | 
        
        
            | 
            | 
           36 | 
           create table _menu (
  | 
        
        
           | 38 | 
           rodolico | 
           37 | 
              _menu_id       int unsigned not null auto_increment,
  | 
        
        
           | 19 | 
           rodolico | 
           38 | 
              parent_id      int unsigned default null  comment 'If this is a submenu the id of the parent' REFERENCES _menu(_menu_id),
  | 
        
        
           | 6 | 
           rodolico | 
           39 | 
              caption        varchar(20) not null comment 'The actual caption displayed',
  | 
        
        
           | 19 | 
           rodolico | 
           40 | 
              url            varchar(120) default null comment 'the url of the page/script to call or null if this contains sub-options',
  | 
        
        
           | 18 | 
           rodolico | 
           41 | 
              primary key    (_menu_id)
  | 
        
        
           | 6 | 
           rodolico | 
           42 | 
           ) comment 'We keep the entire menu structure here so modules can modify it';
  | 
        
        
            | 
            | 
           43 | 
              | 
        
        
            | 
            | 
           44 | 
              | 
        
        
           | 18 | 
           rodolico | 
           45 | 
           /* beginning of the actual tables used by the app, client, site and device */
  | 
        
        
           | 6 | 
           rodolico | 
           46 | 
              | 
        
        
           | 18 | 
           rodolico | 
           47 | 
           /*
  | 
        
        
            | 
            | 
           48 | 
              A device is owned by a client (entity). This is a list of available
  | 
        
        
            | 
            | 
           49 | 
              clients who can own things.
  | 
        
        
            | 
            | 
           50 | 
           */
  | 
        
        
            | 
            | 
           51 | 
           drop table if exists client;
  | 
        
        
            | 
            | 
           52 | 
           create table client (
  | 
        
        
           | 38 | 
           rodolico | 
           53 | 
              client_id      int unsigned not null auto_increment,
  | 
        
        
            | 
            | 
           54 | 
              name           varchar(64) comment 'Name of the client',
  | 
        
        
            | 
            | 
           55 | 
              notes          text comment 'Any notes you want to enter',
  | 
        
        
           | 22 | 
           rodolico | 
           56 | 
              internal_notes text comment 'private notes accessible only to technicians',
  | 
        
        
           | 38 | 
           rodolico | 
           57 | 
              added          date not null comment 'Date record added to database',
  | 
        
        
            | 
            | 
           58 | 
              removed        date default null comment 'Date record marked as removed',
  | 
        
        
            | 
            | 
           59 | 
              primary key    (client_id)
  | 
        
        
           | 6 | 
           rodolico | 
           60 | 
           ) comment 'Stores information about a particlar client/owner';
  | 
        
        
            | 
            | 
           61 | 
              | 
        
        
           | 18 | 
           rodolico | 
           62 | 
           /*
  | 
        
        
            | 
            | 
           63 | 
              A device can be on a site. This is a list of sites which can house
  | 
        
        
            | 
            | 
           64 | 
              devices
  | 
        
        
            | 
            | 
           65 | 
           */
  | 
        
        
            | 
            | 
           66 | 
           drop table if exists site;
  | 
        
        
            | 
            | 
           67 | 
           create table site (
  | 
        
        
           | 6 | 
           rodolico | 
           68 | 
              site_id     int unsigned not null auto_increment,
  | 
        
        
            | 
            | 
           69 | 
              name        varchar(64) comment 'Name of the site',
  | 
        
        
            | 
            | 
           70 | 
              notes       text comment 'Any notes you want to enter',
  | 
        
        
           | 38 | 
           rodolico | 
           71 | 
              added       date not null comment 'Date record added to database',
  | 
        
        
            | 
            | 
           72 | 
              removed     date default null comment 'Date record marked as removed',
  | 
        
        
           | 6 | 
           rodolico | 
           73 | 
              primary key (site_id)
  | 
        
        
            | 
            | 
           74 | 
           ) comment 'Stores information about a particlar physical site';
  | 
        
        
            | 
            | 
           75 | 
              | 
        
        
           | 18 | 
           rodolico | 
           76 | 
           /*
  | 
        
        
            | 
            | 
           77 | 
              The basis for the program. In our context, an asset is a computer or
  | 
        
        
            | 
            | 
           78 | 
              other device.
  | 
        
        
            | 
            | 
           79 | 
           */
  | 
        
        
            | 
            | 
           80 | 
           drop table if exists device;
  | 
        
        
            | 
            | 
           81 | 
           create table device (
  | 
        
        
           | 38 | 
           rodolico | 
           82 | 
              device_id   int unsigned not null auto_increment,
  | 
        
        
           | 19 | 
           rodolico | 
           83 | 
              name        varchar(64) not null comment 'name of the device or device',
  | 
        
        
            | 
            | 
           84 | 
              notes       text default null comment 'any notes we want to store',
  | 
        
        
           | 18 | 
           rodolico | 
           85 | 
              device_type_id int unsigned not null references device_type( device_type_id ),
  | 
        
        
           | 38 | 
           rodolico | 
           86 | 
              added       date not null comment 'Date record added to database',
  | 
        
        
            | 
            | 
           87 | 
              removed     date default null comment 'Date record marked as removed',
  | 
        
        
           | 18 | 
           rodolico | 
           88 | 
              primary key (device_id)
  | 
        
        
            | 
            | 
           89 | 
           ) comment 'stores information about an individual device or other device';
  | 
        
        
           | 6 | 
           rodolico | 
           90 | 
              | 
        
        
           | 18 | 
           rodolico | 
           91 | 
           /* 
  | 
        
        
            | 
            | 
           92 | 
              allows devices to be related to each other, ie a "part of" scheme, where 
  | 
        
        
            | 
            | 
           93 | 
              for example, a virtualized server may be 'part of' a physical machine.
  | 
        
        
            | 
            | 
           94 | 
              By using a child table, we can track movement across this relationship
  | 
        
        
            | 
            | 
           95 | 
           */
  | 
        
        
            | 
            | 
           96 | 
           drop table if exists device_device;
  | 
        
        
            | 
            | 
           97 | 
           create table device_device (
  | 
        
        
            | 
            | 
           98 | 
              device_device_id int unsigned not null auto_increment,
  | 
        
        
           | 38 | 
           rodolico | 
           99 | 
              device_id   int unsigned not null references device( device_id ),
  | 
        
        
            | 
            | 
           100 | 
              parent_id   int unsigned not null references device( device_id ),
  | 
        
        
            | 
            | 
           101 | 
              added       date not null comment 'Date record added to database',
  | 
        
        
            | 
            | 
           102 | 
              removed     date default null comment 'Date record marked as removed',
  | 
        
        
           | 18 | 
           rodolico | 
           103 | 
              primary key (device_device_id)
  | 
        
        
            | 
            | 
           104 | 
           ) comment 'joins device to another device';
  | 
        
        
           | 6 | 
           rodolico | 
           105 | 
              | 
        
        
            | 
            | 
           106 | 
           /*
  | 
        
        
           | 18 | 
           rodolico | 
           107 | 
              device_type is a child table of device, determining what type of device it is]
  | 
        
        
           | 6 | 
           rodolico | 
           108 | 
              such as computer, printer, router, whatever.
  | 
        
        
            | 
            | 
           109 | 
              flags currently used as 0 is non system and 1 is system, though this could
  | 
        
        
            | 
            | 
           110 | 
              be expanded in the future
  | 
        
        
            | 
            | 
           111 | 
           */
  | 
        
        
           | 18 | 
           rodolico | 
           112 | 
           drop table if exists device_type;
  | 
        
        
            | 
            | 
           113 | 
           create table device_type (
  | 
        
        
            | 
            | 
           114 | 
             device_type_id   int(10) unsigned NOT NULL auto_increment,
  | 
        
        
           | 38 | 
           rodolico | 
           115 | 
             name         varchar(64) not null COMMENT 'the visible displayed name',
  | 
        
        
            | 
            | 
           116 | 
             flags        int unsigned default 1 comment 'flags for this device type',
  | 
        
        
            | 
            | 
           117 | 
             added        date not null COMMENT 'date record was added',
  | 
        
        
            | 
            | 
           118 | 
             removed      date default NULL COMMENT 'date record was deleted/supserceded',
  | 
        
        
            | 
            | 
           119 | 
             primary key  (device_type_id)
  | 
        
        
           | 6 | 
           rodolico | 
           120 | 
           ) comment='simply a list of device types ie computer printer whatever';
  | 
        
        
            | 
            | 
           121 | 
              | 
        
        
            | 
            | 
           122 | 
              | 
        
        
            | 
            | 
           123 | 
           /* 
  | 
        
        
            | 
            | 
           124 | 
              this is actually a join with multiple tables, depending on what the
  | 
        
        
           | 18 | 
           rodolico | 
           125 | 
              name is associated with, client, site or device
  | 
        
        
           | 6 | 
           rodolico | 
           126 | 
              for example, if 'id' is client_id from the client table, then 'source'
  | 
        
        
            | 
            | 
           127 | 
              would be 'client' (name of the table);
  | 
        
        
            | 
            | 
           128 | 
           */
  | 
        
        
           | 18 | 
           rodolico | 
           129 | 
           drop table if exists alias;
  | 
        
        
            | 
            | 
           130 | 
           create table alias (
  | 
        
        
           | 6 | 
           rodolico | 
           131 | 
              alias_id int unsigned not null auto_increment,
  | 
        
        
           | 38 | 
           rodolico | 
           132 | 
              source      varchar(64) comment 'the table this alias comes from',
  | 
        
        
            | 
            | 
           133 | 
              id          int unsigned not null comment 'the client, site or device id',
  | 
        
        
            | 
            | 
           134 | 
              name        varchar(64) comment 'the alias for the device',
  | 
        
        
           | 18 | 
           rodolico | 
           135 | 
              primary key (alias_id)
  | 
        
        
            | 
            | 
           136 | 
           ) comment 'Allows client, site and device to have multiple names';
  | 
        
        
           | 6 | 
           rodolico | 
           137 | 
              | 
        
        
           | 18 | 
           rodolico | 
           138 | 
           /*
  | 
        
        
            | 
            | 
           139 | 
              links a site to a client. A site is "owned" by a client
  | 
        
        
            | 
            | 
           140 | 
           */
  | 
        
        
            | 
            | 
           141 | 
           drop table if exists client_site;
  | 
        
        
            | 
            | 
           142 | 
           create table client_site (
  | 
        
        
           | 6 | 
           rodolico | 
           143 | 
              client_site_id int unsigned not null auto_increment,
  | 
        
        
            | 
            | 
           144 | 
              client_id   int unsigned not null references client( client_id ),
  | 
        
        
            | 
            | 
           145 | 
              site_id     int unsigned not null references site( site_id ),
  | 
        
        
           | 38 | 
           rodolico | 
           146 | 
              added       date not null comment 'Date record added to database',
  | 
        
        
            | 
            | 
           147 | 
              removed     date default null comment 'Date record marked as removed',
  | 
        
        
           | 18 | 
           rodolico | 
           148 | 
              primary key (client_site_id)
  | 
        
        
           | 6 | 
           rodolico | 
           149 | 
           ) comment 'A client owns a site';
  | 
        
        
            | 
            | 
           150 | 
              | 
        
        
           | 18 | 
           rodolico | 
           151 | 
           /*
  | 
        
        
            | 
            | 
           152 | 
             A device is owned by a client. Ownership can be tracked by removing
  | 
        
        
            | 
            | 
           153 | 
             a device from one client and adding it to another.
  | 
        
        
            | 
            | 
           154 | 
           */
  | 
        
        
            | 
            | 
           155 | 
           drop table if exists client_device;
  | 
        
        
            | 
            | 
           156 | 
           create table client_device (
  | 
        
        
            | 
            | 
           157 | 
              client_device_id int unsigned not null auto_increment,
  | 
        
        
           | 6 | 
           rodolico | 
           158 | 
              client_id   int unsigned not null references client( client_id ),
  | 
        
        
           | 38 | 
           rodolico | 
           159 | 
              device_id   int unsigned not null references device( device_id ),
  | 
        
        
            | 
            | 
           160 | 
              added       date not null comment 'Date record added to database',
  | 
        
        
            | 
            | 
           161 | 
              removed     date default null comment 'Date record marked as removed',
  | 
        
        
           | 18 | 
           rodolico | 
           162 | 
              primary key (client_device_id)
  | 
        
        
            | 
            | 
           163 | 
           ) comment 'Links client and device tables';
  | 
        
        
           | 6 | 
           rodolico | 
           164 | 
              | 
        
        
           | 18 | 
           rodolico | 
           165 | 
           /*
  | 
        
        
            | 
            | 
           166 | 
              A device is at a location. Location history can be tracked by removing
  | 
        
        
            | 
            | 
           167 | 
              and adding a device to a new location
  | 
        
        
            | 
            | 
           168 | 
           */
  | 
        
        
            | 
            | 
           169 | 
           drop table if exists site_device;
  | 
        
        
            | 
            | 
           170 | 
           create table site_device (
  | 
        
        
            | 
            | 
           171 | 
              site_device_id int unsigned not null auto_increment,
  | 
        
        
           | 6 | 
           rodolico | 
           172 | 
              site_id     int unsigned not null references site( site_id ),
  | 
        
        
           | 38 | 
           rodolico | 
           173 | 
              device_id   int unsigned not null references device( device_id ),
  | 
        
        
            | 
            | 
           174 | 
              added       date not null comment 'Date record added to database',
  | 
        
        
            | 
            | 
           175 | 
              removed     date default null comment 'Date record marked as removed',
  | 
        
        
           | 18 | 
           rodolico | 
           176 | 
              primary key ( site_device_id )
  | 
        
        
            | 
            | 
           177 | 
           ) comment 'Links site and device tables';
  | 
        
        
           | 6 | 
           rodolico | 
           178 | 
              | 
        
        
           | 38 | 
           rodolico | 
           179 | 
           /* some useful views */
  | 
        
        
            | 
            | 
           180 | 
              | 
        
        
            | 
            | 
           181 | 
           /* active sites and their owner (from client table) */
  | 
        
        
            | 
            | 
           182 | 
           create or replace view view_site_owner as
  | 
        
        
            | 
            | 
           183 | 
              select
  | 
        
        
            | 
            | 
           184 | 
                 site.name         site_name,
  | 
        
        
            | 
            | 
           185 | 
                 site.site_id      site_id,
  | 
        
        
            | 
            | 
           186 | 
                 client.name       site_owner_name,
  | 
        
        
            | 
            | 
           187 | 
                 client.client_id  site_owner_id
  | 
        
        
            | 
            | 
           188 | 
              from
  | 
        
        
            | 
            | 
           189 | 
                 site
  | 
        
        
            | 
            | 
           190 | 
                 join client_site using (site_id)
  | 
        
        
            | 
            | 
           191 | 
                 join client using (client_id)
  | 
        
        
            | 
            | 
           192 | 
              where
  | 
        
        
            | 
            | 
           193 | 
                 1=1
  | 
        
        
            | 
            | 
           194 | 
                 and client_site.removed is null
  | 
        
        
            | 
            | 
           195 | 
                 and client.removed is null
  | 
        
        
            | 
            | 
           196 | 
                 and site.removed is null;
  | 
        
        
            | 
            | 
           197 | 
              | 
        
        
            | 
            | 
           198 | 
           create or replace view view_device_client_site as
  | 
        
        
            | 
            | 
           199 | 
              select
  | 
        
        
            | 
            | 
           200 | 
                 device.device_id  device_id,
  | 
        
        
            | 
            | 
           201 | 
                 client.client_id  client_id,
  | 
        
        
            | 
            | 
           202 | 
                 view_site_owner.site_id      site_id,
  | 
        
        
            | 
            | 
           203 | 
                 view_site_owner.site_owner_id   site_owner_id,
  | 
        
        
            | 
            | 
           204 | 
                 device.name       device_name,
  | 
        
        
            | 
            | 
           205 | 
                 client.name       client_name,
  | 
        
        
            | 
            | 
           206 | 
                 view_site_owner.site_name   site_name,
  | 
        
        
            | 
            | 
           207 | 
                 view_site_owner.site_owner_name site_owner_name
  | 
        
        
            | 
            | 
           208 | 
              from
  | 
        
        
            | 
            | 
           209 | 
                 device
  | 
        
        
            | 
            | 
           210 | 
                 join client_device using (device_id)
  | 
        
        
            | 
            | 
           211 | 
                 join client using (client_id)
  | 
        
        
            | 
            | 
           212 | 
                 join site_device using (device_id)
  | 
        
        
            | 
            | 
           213 | 
                 join view_site_owner using (site_id)
  | 
        
        
            | 
            | 
           214 | 
              where
  | 
        
        
            | 
            | 
           215 | 
                 1=1
  | 
        
        
            | 
            | 
           216 | 
                 and device.removed is null
  | 
        
        
            | 
            | 
           217 | 
                 and client.removed is null;
  | 
        
        
            | 
            | 
           218 | 
              | 
        
        
            | 
            | 
           219 | 
              | 
        
        
           | 6 | 
           rodolico | 
           220 | 
           /* add some indexes */
  | 
        
        
           | 38 | 
           rodolico | 
           221 | 
           alter table device add index (added,removed);
  | 
        
        
            | 
            | 
           222 | 
           alter table device add index (removed);
  | 
        
        
           | 18 | 
           rodolico | 
           223 | 
           alter table device add index (name);
  | 
        
        
            | 
            | 
           224 | 
           alter table device add index (device_type_id);
  | 
        
        
           | 6 | 
           rodolico | 
           225 | 
              | 
        
        
           | 18 | 
           rodolico | 
           226 | 
           alter table client_site add index ( client_id,site_id );
  | 
        
        
            | 
            | 
           227 | 
              | 
        
        
           | 38 | 
           rodolico | 
           228 | 
           alter table site add index (removed);
  | 
        
        
           | 6 | 
           rodolico | 
           229 | 
              | 
        
        
           | 38 | 
           rodolico | 
           230 | 
           alter table client add index (removed);
  | 
        
        
           | 6 | 
           rodolico | 
           231 | 
              | 
        
        
           | 18 | 
           rodolico | 
           232 | 
           /* preload some data */
  | 
        
        
           | 6 | 
           rodolico | 
           233 | 
              | 
        
        
           | 18 | 
           rodolico | 
           234 | 
           /* basic menu; home and logout */
  | 
        
        
            | 
            | 
           235 | 
           insert into _menu values ( null, null, 'Home Page', 'index.html' );
  | 
        
        
            | 
            | 
           236 | 
           insert into _menu values ( null, null, 'Log Out', 'logout.html' );
  | 
        
        
           | 6 | 
           rodolico | 
           237 | 
              | 
        
        
           | 18 | 
           rodolico | 
           238 | 
           /* one user, admin, with no password for initial access */
  | 
        
        
           | 38 | 
           rodolico | 
           239 | 
           insert into _user( username,added) values ( 'admin', now() );
  | 
        
        
           | 6 | 
           rodolico | 
           240 | 
              | 
        
        
           | 38 | 
           rodolico | 
           241 | 
           /* insert into device_type (name,flags, added ) values ( 'Computer',1,now() ); */
  |