SorgentiSQL » Cronologia » Versione 2
Diego Sorrentino, 30-04-2019 11:19
1 | 1 | Diego Sorrentino | h1. Sorgenti SQL |
---|---|---|---|
2 | |||
3 | I sorgenti della piattaforma lato DB si trovano nel progetto, nella directory @db/@ |
||
4 | Si compongono di 4 differenti files: |
||
5 | * struct.sql |
||
6 | * function.add.sql |
||
7 | * function.edit.sql |
||
8 | * view.sql |
||
9 | |||
10 | h2. struct.sql |
||
11 | |||
12 | All'interno sono presenti tutte le definizioni delle tabelle e lo schema delle relazioni, in formato (testo) SQL. |
||
13 | 2 | Diego Sorrentino | Un +esempio+ di struttura di tabella: |
14 | 1 | Diego Sorrentino | <pre><code class="sql"> |
15 | CREATE TABLE province( |
||
16 | id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, |
||
17 | id_region SMALLINT UNSIGNED NOT NULL REFERENCES region(id), |
||
18 | code CHAR(2) UNIQUE NOT NULL, |
||
19 | name CHAR(50) UNIQUE NOT NULL, |
||
20 | PRIMARY KEY(id) |
||
21 | ); |
||
22 | </code></pre> |
||
23 | |||
24 | |||
25 | h2. function.add.sql e function.edit.sql |
||
26 | |||
27 | Definizione di tutte le *STORED FUNCTIONS* e *STORED PROCEDURES* utilizzate per _manipolare_ i dati. |
||
28 | Sono state utilizzate le *STORED FUNCTIONS* e *STORED PROCEDURES* per fornire un'interfaccia comune al sistema e aumentare il livello di sicurezza. |
||
29 | Le funzioni *DOVREBBERO* avere un prefisso comune per l'inserimento e modifica dei dati: *save_<entità>* ma, a causa di continue interruzioni durante l'evoluzione del software, molte funzioni ancora sono distinte in *new_<entità>* e *edit_<entità>* |
||
30 | 2 | Diego Sorrentino | Un +esempio+ di struttura di una *STORED FUNCTIONS*: |
31 | 1 | Diego Sorrentino | <pre><code class="sql"> |
32 | delimiter // |
||
33 | DROP FUNCTION IF EXISTS save_communication// |
||
34 | CREATE FUNCTION save_communication(_id INTEGER UNSIGNED, _subject TEXT, _body TEXT, _to_id_event INTEGER UNSIGNED, _to_all_subscribers BOOLEAN, _send_after TINYINT) RETURNS INT |
||
35 | BEGIN |
||
36 | IF (_id IS NULL) THEN |
||
37 | INSERT INTO communication (subject, body, to_id_event, to_all_subscribers, send_after) |
||
38 | VALUES (_subject, _body, _to_id_event, _to_all_subscribers, _send_after); |
||
39 | ELSE |
||
40 | UPDATE communication SET |
||
41 | added = NOW(), subject = _subject, body = _body, |
||
42 | send_after = _send_after |
||
43 | WHERE id = _id; |
||
44 | END IF; |
||
45 | |||
46 | RETURN 0; |
||
47 | END // |
||
48 | delimiter ; |
||
49 | </code></pre> |
||
50 | |||
51 | |||
52 | |||
53 | h2. view.sql |
||
54 | |||
55 | Definizione di tutte le *VISTE* utilizzate nel sistema. |
||
56 | Sono state utilizzate le *VISTE* per fornire un'interfaccia comune al sistema e aumentare il livello di sicurezza. |
||
57 | La viste sono, _solitamente_, identificate con il prefisso *list_<entità>* o *list_<entità_entità>*. |
||
58 | 2 | Diego Sorrentino | Un +esempio+ di struttura di una *VISTA*: |
59 | 1 | Diego Sorrentino | <pre><code class="sql"> |
60 | CREATE OR REPLACE VIEW list_communication AS |
||
61 | SELECT c.*, |
||
62 | e.id_loc, e.ads_id_event, e.mag_pref_value, CAST(e.depth AS DECIMAL(5,1)) AS depth, e.zone_name, e.n_quests AS counter, |
||
63 | lat_degree, lat_first, ROUND(lat_second) AS lat_second, |
||
64 | lon_degree, lon_first, ROUND(lon_second) AS lon_second, |
||
65 | e.event_date |
||
66 | FROM communication AS c |
||
67 | LEFT JOIN event AS e ON (c.to_id_event = e.id); |
||
68 | </code></pre> |