1+ package ca .sozoservers .dev .database ;
2+
3+ import java .io .File ;
4+ import java .lang .reflect .Field ;
5+ import java .sql .*;
6+
7+ import ca .sozoservers .dev .database .models .DatabaseModel ;
8+ import ca .sozoservers .dev .database .models .DatabaseModel .Constraints ;
9+ import ca .sozoservers .dev .database .models .DatabaseModel .DataType ;
10+ import ca .sozoservers .dev .database .models .DatabaseModel .SqlValue ;
11+
12+ public class Database {
13+
14+ private static File db ;
15+
16+ public Database (File dbPath ) {
17+ db = dbPath ;
18+ }
19+
20+ public static boolean createDatabase () {
21+ try (Connection conn = connect ()) {
22+ if (!db .exists ()) {
23+ DatabaseMetaData meta = conn .getMetaData ();
24+ meta .getDriverName ();
25+ }
26+ }catch (SQLException ex ) {
27+ ex .printStackTrace ();
28+ return false ;
29+ }
30+ return true ;
31+ }
32+
33+ public static boolean createTable (DatabaseModel ... models ) {
34+ try (Connection conn = connect (); Statement stmt = conn .createStatement ()) {
35+ for (DatabaseModel model : models ) {
36+ stmt .execute (model .getSQL (SqlValue .Table ));
37+ }
38+ return true ;
39+ } catch (SQLException ex ) {
40+ ex .printStackTrace ();
41+ }
42+ return false ;
43+ }
44+
45+ public static boolean set (DatabaseModel model ) {
46+
47+ try (Connection conn = Database .connect ();
48+ PreparedStatement pstmt = conn .prepareStatement (model .getSQL (SqlValue .Set ));) {
49+ Field [] fields = model .getClass ().getDeclaredFields ();
50+ int count = 0 ;
51+ for (Field field : fields ) {
52+ field .setAccessible (true );
53+ if (field .isAnnotationPresent (DataType .class )) {
54+ count ++;
55+ pstmt .setObject (count , field .get (model ));
56+ }
57+ }
58+ pstmt .executeUpdate ();
59+ pstmt .closeOnCompletion ();
60+ conn .close ();
61+ } catch (SQLException | IllegalArgumentException | IllegalAccessException ex ) {
62+ ex .printStackTrace ();
63+ return false ;
64+ }
65+ return true ;
66+
67+ }
68+
69+ public static boolean get (DatabaseModel model , boolean supressError ) {
70+
71+ try (Connection conn = Database .connect ();
72+ PreparedStatement pstmt = conn .prepareStatement (model .getSQL (SqlValue .Get ));) {
73+ Field [] fields = model .getClass ().getDeclaredFields ();
74+ for (Field field : fields ) {
75+ field .setAccessible (true );
76+ if (field .isAnnotationPresent (Constraints .class )) {
77+ if (field .getAnnotation (Constraints .class ).value ().equalsIgnoreCase ("PRIMARY KEY" )) {
78+ pstmt .setObject (1 , field .get (model ));
79+ break ;
80+ }
81+ }
82+ }
83+ ResultSet rs = pstmt .executeQuery ();
84+ for (Field field : fields ) {
85+ if (field .isAnnotationPresent (DataType .class )) {
86+ field .set (model , rs .getObject (field .getName ()));
87+ }
88+ }
89+ rs .close ();
90+ pstmt .closeOnCompletion ();
91+ conn .close ();
92+ } catch (SQLException | IllegalArgumentException | IllegalAccessException ex ) {
93+ if (!supressError ) {
94+ ex .printStackTrace ();
95+ }
96+ return false ;
97+ }
98+ return true ;
99+
100+ }
101+
102+ public static boolean update (DatabaseModel model ) {
103+
104+ try (Connection conn = Database .connect ();
105+ PreparedStatement pstmt = conn .prepareStatement (model .getSQL (SqlValue .Update ));) {
106+ Field [] fields = model .getClass ().getDeclaredFields ();
107+ int count = 0 ;
108+ Object primaryKey = null ;
109+ for (Field field : fields ) {
110+ field .setAccessible (true );
111+ if (field .isAnnotationPresent (DataType .class )) {
112+ if (field .isAnnotationPresent (Constraints .class )) {
113+ if (field .getAnnotation (Constraints .class ).value ().equalsIgnoreCase ("PRIMARY KEY" )) {
114+ primaryKey = field .get (model );
115+ continue ;
116+ }
117+ }
118+ count ++;
119+ pstmt .setObject (count , field .get (model ));
120+ }
121+ }
122+ count ++;
123+ pstmt .setObject (count , primaryKey );
124+ pstmt .executeUpdate ();
125+ pstmt .closeOnCompletion ();
126+ conn .close ();
127+ } catch (SQLException | IllegalArgumentException | IllegalAccessException ex ) {
128+ ex .printStackTrace ();
129+ return false ;
130+ }
131+ return true ;
132+ }
133+
134+ public static boolean delete (DatabaseModel model ) {
135+
136+ try (Connection conn = Database .connect ();
137+ PreparedStatement pstmt = conn .prepareStatement (model .getSQL (SqlValue .Delete ));) {
138+ Field [] fields = model .getClass ().getDeclaredFields ();
139+ for (Field field : fields ) {
140+ field .setAccessible (true );
141+ if (field .isAnnotationPresent (Constraints .class )) {
142+ if (field .getAnnotation (Constraints .class ).value ().equalsIgnoreCase ("PRIMARY KEY" )) {
143+ pstmt .setObject (1 , field .get (model ));
144+ break ;
145+ }
146+ }
147+ }
148+ pstmt .executeUpdate ();
149+ pstmt .closeOnCompletion ();
150+ conn .close ();
151+ } catch (SQLException | IllegalArgumentException | IllegalAccessException ex ) {
152+ ex .printStackTrace ();
153+ return false ;
154+ }
155+ return true ;
156+
157+ }
158+
159+ public static boolean exists (DatabaseModel model ) {
160+ try (Connection conn = Database .connect ();
161+ PreparedStatement pstmt = conn .prepareStatement (model .getSQL (SqlValue .Get ));) {
162+ Field [] fields = model .getClass ().getDeclaredFields ();
163+ for (Field field : fields ) {
164+ field .setAccessible (true );
165+ if (field .isAnnotationPresent (Constraints .class )) {
166+ if (field .getAnnotation (Constraints .class ).value ().equalsIgnoreCase ("PRIMARY KEY" )) {
167+ pstmt .setObject (1 , field .get (model ));
168+ break ;
169+ }
170+ }
171+ }
172+ ResultSet rs = pstmt .executeQuery ();
173+ rs .close ();
174+ pstmt .closeOnCompletion ();
175+ conn .close ();
176+ } catch (SQLException | IllegalArgumentException | IllegalAccessException ex ) {
177+ ex .printStackTrace ();
178+ return false ;
179+ }
180+ return true ;
181+ }
182+
183+ public static Connection connect () throws SQLException {
184+ return DriverManager .getConnection ("jdbc:sqlite:" + db );
185+ }
186+ }
0 commit comments