franta-hg@16
|
1 |
package cz.frantovo.sql.vyuka.dao;
|
franta-hg@16
|
2 |
|
franta-hg@16
|
3 |
import cz.frantovo.sql.vyuka.dao.VyukaSuperDAO.DATABAZE;
|
franta-hg@16
|
4 |
import cz.frantovo.sql.vyuka.dto.Hlaska;
|
franta-hg@16
|
5 |
import cz.frantovo.sql.vyuka.dto.Hlaska.Typ;
|
franta-hg@16
|
6 |
import cz.frantovo.sql.vyuka.dto.Tabulka;
|
franta-hg@16
|
7 |
import cz.frantovo.sql.vyuka.dto.Uzivatel;
|
franta-hg@16
|
8 |
import cz.frantovo.sql.vyuka.dto.VysledekSQL;
|
franta-hg@16
|
9 |
import java.sql.Connection;
|
franta-hg@16
|
10 |
import java.sql.PreparedStatement;
|
franta-hg@16
|
11 |
import java.sql.ResultSet;
|
franta-hg@16
|
12 |
import java.sql.SQLException;
|
franta-hg@16
|
13 |
import java.util.logging.Level;
|
franta-hg@16
|
14 |
|
franta-hg@16
|
15 |
/**
|
franta-hg@16
|
16 |
* Pro spouštění uživatelových příkazů.
|
franta-hg@16
|
17 |
* @author fiki
|
franta-hg@16
|
18 |
*/
|
franta-hg@16
|
19 |
public class PiskovisteDAO extends VyukaSuperDAO {
|
franta-hg@16
|
20 |
|
franta-hg@16
|
21 |
TipyDAO tipy = new TipyDAO();
|
franta-hg@16
|
22 |
HistorieDAO historie = new HistorieDAO();
|
franta-hg@16
|
23 |
|
franta-hg@16
|
24 |
public VysledekSQL vykonejSQL(String sql, Uzivatel uzivatel) {
|
franta-hg@16
|
25 |
VysledekSQL v = new VysledekSQL();
|
franta-hg@16
|
26 |
if (historie.ulozPrikaz(sql, uzivatel)) {
|
franta-hg@16
|
27 |
|
franta-hg@16
|
28 |
Connection db = getSpojeni(DATABAZE.PISKOVISTE);
|
franta-hg@16
|
29 |
if (db == null) {
|
franta-hg@16
|
30 |
v.getHlasky().add(new Hlaska("Došlo k chybě spojení.", Typ.Chyba));
|
franta-hg@16
|
31 |
} else {
|
franta-hg@16
|
32 |
PreparedStatement ps = null;
|
franta-hg@16
|
33 |
ResultSet rs = null;
|
franta-hg@16
|
34 |
try {
|
franta-hg@16
|
35 |
ps = db.prepareStatement(sql);
|
franta-hg@17
|
36 |
boolean isRS = ps.execute();
|
franta-hg@16
|
37 |
|
franta-hg@17
|
38 |
if (isRS) {
|
franta-hg@17
|
39 |
rs = ps.getResultSet();
|
franta-hg@17
|
40 |
v.getTabulky().add(zpracujVysledek(rs));
|
franta-hg@16
|
41 |
|
franta-hg@17
|
42 |
while (ps.getMoreResults()) {
|
franta-hg@17
|
43 |
rs = ps.getResultSet();
|
franta-hg@17
|
44 |
v.getTabulky().add(zpracujVysledek(rs));
|
franta-hg@16
|
45 |
}
|
franta-hg@16
|
46 |
} else {
|
franta-hg@17
|
47 |
/** TODO: zobrazit počet aktualizovaných? */
|
franta-hg@17
|
48 |
v.getHlasky().add(new Hlaska("SQL příkaz byl proveden, ale nevrátil žádná data.", Typ.Varovani));
|
franta-hg@16
|
49 |
}
|
franta-hg@16
|
50 |
|
franta-hg@16
|
51 |
} catch (SQLException e) {
|
franta-hg@16
|
52 |
log.log(Level.SEVERE, "SQL chyba při vykonávání uživatelského dotazu.", e);
|
franta-hg@16
|
53 |
v.getHlasky().add(new Hlaska("Chybné SQL: " + e.getMessage(), Typ.Chyba));
|
franta-hg@16
|
54 |
} catch (Exception e) {
|
franta-hg@16
|
55 |
log.log(Level.SEVERE, "Chyba při vykonávání uživatelského dotazu.", e);
|
franta-hg@16
|
56 |
v.getHlasky().add(new Hlaska("Došlo k chybě dotazu.", Typ.Chyba));
|
franta-hg@16
|
57 |
} finally {
|
franta-hg@16
|
58 |
zavri(db, ps, rs);
|
franta-hg@16
|
59 |
}
|
franta-hg@16
|
60 |
}
|
franta-hg@16
|
61 |
|
franta-hg@16
|
62 |
v.getHlasky().add(new Hlaska(tipy.getTip(), Typ.Tip, false));
|
franta-hg@16
|
63 |
|
franta-hg@16
|
64 |
} else {
|
franta-hg@16
|
65 |
v.getHlasky().add(new Hlaska("Došlo k chybě historie.", Typ.Chyba));
|
franta-hg@16
|
66 |
}
|
franta-hg@16
|
67 |
return v;
|
franta-hg@16
|
68 |
}
|
franta-hg@17
|
69 |
|
franta-hg@17
|
70 |
private Tabulka zpracujVysledek(ResultSet rs) throws SQLException {
|
franta-hg@17
|
71 |
Tabulka t = new Tabulka();
|
franta-hg@17
|
72 |
|
franta-hg@17
|
73 |
int pocetSloupecku = rs.getMetaData().getColumnCount();
|
franta-hg@17
|
74 |
String[] zahlavi = new String[pocetSloupecku];
|
franta-hg@17
|
75 |
t.setZahlavi(zahlavi);
|
franta-hg@17
|
76 |
for (int i = 0; i < pocetSloupecku; i++) {
|
franta-hg@17
|
77 |
zahlavi[i] = rs.getMetaData().getColumnName(i + 1);
|
franta-hg@17
|
78 |
}
|
franta-hg@17
|
79 |
|
franta-hg@17
|
80 |
while (rs.next()) {
|
franta-hg@17
|
81 |
Object[] hodnoty = new Object[pocetSloupecku];
|
franta-hg@17
|
82 |
for (int i = 0; i < pocetSloupecku; i++) {
|
franta-hg@17
|
83 |
hodnoty[i] = rs.getObject(i + 1);
|
franta-hg@17
|
84 |
}
|
franta-hg@17
|
85 |
t.getHodnoty().add(hodnoty);
|
franta-hg@17
|
86 |
}
|
franta-hg@17
|
87 |
|
franta-hg@17
|
88 |
return t;
|
franta-hg@17
|
89 |
}
|
franta-hg@16
|
90 |
}
|