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
 
70 rodolico 8
DEFINITELY needs to be re-thought
9
 
34 rodolico 10
/*
70 rodolico 11
   attribute_table allows us to categorize the attributes based on what they are used for.
34 rodolico 12
   for example, allows us to have names out of client table, and names out of device table
13
   as two separate attributes.
14
*/
70 rodolico 15
drop table if exists attribute_table;
16
create table attribute_table (
17
   attribute_table_id int unsigned not null auto_increment,
18
   name           varchar(64) comment 'display name for category',
19
   primary key    ( attribute_category_id )
20
) comment 'which table attribute is for';
21
 
22
/*
23
   Allows us to group attributes into different categories, for example
24
   hardware (disk size, cpu, etc...), financial (cost, vendor), network,
25
   etc
26
*/
34 rodolico 27
drop table if exists attribute_category;
28
create table attribute_category (
29
   attribute_category_id int unsigned not null auto_increment,
30
   name           varchar(64) comment 'display name for category',
31
   primary key    ( attribute_category_id )
32
) comment 'categorizes attributes';
33
 
34
/*
35
   this will hold the actual names of the attributes, such as name, uuid, serial number, cpu information, etc...
36
*/
37
drop table if exists attribute;
38
create table attribute (
39
   attribute_id   int unsigned not null auto_increment,
40
   name           varchar(64) not null comment 'name for display' ,
70 rodolico 41
   attribute_category_id int unsigned not null auto_increment,
42
   attribute_table_id int unsigned not null references attribute_table(attribute_table_id),
37 rodolico 43
   display_in_summary boolean comment 'if true, indicates it should be displayed when device/site/client screen is open',
34 rodolico 44
   added          date,
45
   removed        date default null,
46
   primary key    (attribute_id),
70 rodolico 47
   index          (attribute_table_id, attribute_category_id)
34 rodolico 48
) comment = 'stores attribute names (not values) for other tables';
49
 
50
/*
51
   stores the actual values.
52
   NOTE the key field table_id can reference different tables depending on the category of the
53
   attribute in question
70 rodolico 54
 
55
   We can get the first line of a text field with
56
   select substring_index( value, '\n',1 ) from attribute_value where ...
34 rodolico 57
*/
58
drop table if exists attribute_value;
59
create table attribute_value (
60
   attribute_device_id int unsigned not null auto_increment,
61
   attribute_id   int unsigned not null references attribute(attribute_id),
62
   table_id       int unsigned not null comment 'reference to arbitrary table',
63
   value          text null default null comment 'the actual value stored',
64
   added          date,
65
   removed        date default null,
66
   index          (table_id,attribute_id),
67
   primary key    (attribute_device_id)
68
) comment 'attributes for devices';
69
 
70 rodolico 70
insert into attribute_category( name ) values ( 'owner' ), ( 'location' ), ( 'device' );
34 rodolico 71
 
72
/* add one, default, attribute of 'name' for all three tables defined */
73
insert into attribute( name,attribute_category_id )
74
   select
75
      'name',
76
      attribute_category.attribute_category_id
77
   from
78
      attribute_category;
79
 
80
/*
81
   some convenience views on the main three tables which might use these, device, site and client
82
*/
56 rodolico 83
drop view if exists view_attribute_owner;
34 rodolico 84
create view view_attribute_client as
85
   select
86
      client.client_id,
87
      client.name client,
88
      attribute.name attribute,
89
      attribute_value.value,
90
      attribute_value.added,
91
      attribute_value.removed
92
   from
93
      attribute_value
94
      join attribute using (attribute_id)
95
      join attribute_category using (attribute_category_id)
96
      join client on ( attribute_value.table_id = client.client_id)
97
   where
56 rodolico 98
      attribute_category.name = 'owner';
34 rodolico 99
 
56 rodolico 100
drop view if exists view_attribute_location;
34 rodolico 101
create view view_attribute_site as
102
   select
103
      site.site_id,
104
      site.name site,
105
      attribute.name attribute,
106
      attribute_value.value,
107
      attribute_value.added,
108
      attribute_value.removed
109
   from
110
      attribute_value
111
      join attribute using (attribute_id)
112
      join attribute_category using (attribute_category_id)
113
      join site on ( attribute_value.table_id = site.site_id)
114
   where
56 rodolico 115
      attribute_category.name = 'location';
34 rodolico 116
 
117
drop view if exists view_attribute_device;
118
create view view_attribute_device as
119
   select
120
      device.device_id,
121
      device.name device,
122
      attribute.name attribute,
123
      attribute_value.value,
124
      attribute_value.added,
125
      attribute_value.removed
126
   from
127
      attribute_value
128
      join attribute using (attribute_id)
129
      join attribute_category using (attribute_category_id)
130
      join device on ( attribute_value.table_id = device.device_id)
131
   where
132
      attribute_category.name = 'device';
133
 
134