Subversion Repositories computer_asset_manager_v2

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
34 rodolico 1
/*
2
   table structure for module attributes.
3
   Three tables; one to categorize attributes, one to hold the attribute names
4
   and one to link attribute to device with storage for a value
5
*/
6
 
7
 
8
/*
9
   attribute_category allows us to categorize the attributes based on what they are used for.
10
   for example, allows us to have names out of client table, and names out of device table
11
   as two separate attributes.
12
*/
13
drop table if exists attribute_category;
14
create table attribute_category (
15
   attribute_category_id int unsigned not null auto_increment,
16
   name           varchar(64) comment 'display name for category',
17
   primary key    ( attribute_category_id )
18
) comment 'categorizes attributes';
19
 
20
/*
21
   this will hold the actual names of the attributes, such as name, uuid, serial number, cpu information, etc...
22
*/
23
drop table if exists attribute;
24
create table attribute (
25
   attribute_id   int unsigned not null auto_increment,
26
   name           varchar(64) not null comment 'name for display' ,
27
   attribute_category_id int unsigned not null references attribute_category(attribute_category_id),
28
   added          date,
29
   removed        date default null,
30
   primary key    (attribute_id),
31
   index          (attribute_category_id)
32
) comment = 'stores attribute names (not values) for other tables';
33
 
34
/*
35
   stores the actual values.
36
   NOTE the key field table_id can reference different tables depending on the category of the
37
   attribute in question
38
*/
39
drop table if exists attribute_value;
40
create table attribute_value (
41
   attribute_device_id int unsigned not null auto_increment,
42
   attribute_id   int unsigned not null references attribute(attribute_id),
43
   table_id       int unsigned not null comment 'reference to arbitrary table',
44
   value          text null default null comment 'the actual value stored',
45
   added          date,
46
   removed        date default null,
47
   index          (table_id,attribute_id),
48
   primary key    (attribute_device_id)
49
) comment 'attributes for devices';
50
 
51
insert into attribute_category( name ) values ( 'client' ), ( 'site' ), ( 'device' );
52
 
53
/* add one, default, attribute of 'name' for all three tables defined */
54
insert into attribute( name,attribute_category_id )
55
   select
56
      'name',
57
      attribute_category.attribute_category_id
58
   from
59
      attribute_category;
60
 
61
/*
62
   some convenience views on the main three tables which might use these, device, site and client
63
*/
64
drop view if exists view_attribute_client;
65
create view view_attribute_client as
66
   select
67
      client.client_id,
68
      client.name client,
69
      attribute.name attribute,
70
      attribute_value.value,
71
      attribute_value.added,
72
      attribute_value.removed
73
   from
74
      attribute_value
75
      join attribute using (attribute_id)
76
      join attribute_category using (attribute_category_id)
77
      join client on ( attribute_value.table_id = client.client_id)
78
   where
79
      attribute_category.name = 'client';
80
 
81
drop view if exists view_attribute_site;
82
create view view_attribute_site as
83
   select
84
      site.site_id,
85
      site.name site,
86
      attribute.name attribute,
87
      attribute_value.value,
88
      attribute_value.added,
89
      attribute_value.removed
90
   from
91
      attribute_value
92
      join attribute using (attribute_id)
93
      join attribute_category using (attribute_category_id)
94
      join site on ( attribute_value.table_id = site.site_id)
95
   where
96
      attribute_category.name = 'site';
97
 
98
drop view if exists view_attribute_device;
99
create view view_attribute_device as
100
   select
101
      device.device_id,
102
      device.name device,
103
      attribute.name attribute,
104
      attribute_value.value,
105
      attribute_value.added,
106
      attribute_value.removed
107
   from
108
      attribute_value
109
      join attribute using (attribute_id)
110
      join attribute_category using (attribute_category_id)
111
      join device on ( attribute_value.table_id = device.device_id)
112
   where
113
      attribute_category.name = 'device';
114
 
115