@ -52,6 +52,7 @@ public class MariaDB implements Database {
@@ -52,6 +52,7 @@ public class MariaDB implements Database {
if ( rs . next ( ) ) {
version = rs . getInt ( VALUE ) ;
}
rs . getStatement ( ) . close ( ) ;
rs . close ( ) ;
switch ( version ) {
case 0 :
@ -66,8 +67,10 @@ public class MariaDB implements Database {
@@ -66,8 +67,10 @@ public class MariaDB implements Database {
}
private void createTagSearch ( ) throws SQLException {
connection . prepareStatement ( "CREATE VIEW tag_search AS SELECT aid, GROUP_CONCAT(keyword) AS tags FROM appointment_tags LEFT JOIN tags ON appointment_tags.tid = tags.tid GROUP BY aid" ) . execute ( ) ;
Query . update ( CONFIG ) . set ( VALUE ) . where ( KEYNAME , equal ( DB_VERSION ) ) . prepare ( connection ) . apply ( 2 ) ;
var stmt = connection . prepareStatement ( "CREATE VIEW tag_search AS SELECT aid, GROUP_CONCAT(keyword) AS tags FROM appointment_tags LEFT JOIN tags ON appointment_tags.tid = tags.tid GROUP BY aid" ) ;
stmt . execute ( ) ;
stmt . close ( ) ;
Query . update ( CONFIG ) . set ( VALUE ) . where ( KEYNAME , equal ( DB_VERSION ) ) . prepare ( connection ) . apply ( 2 ) . close ( ) ;
}
@Override
@ -83,6 +86,7 @@ public class MariaDB implements Database {
@@ -83,6 +86,7 @@ public class MariaDB implements Database {
. getGeneratedKeys ( ) ;
Appointment saved = null ;
if ( keys . next ( ) ) saved = appointment . clone ( keys . getLong ( 1 ) ) ;
keys . getStatement ( ) . close ( ) ;
keys . close ( ) ;
if ( saved = = null ) return error ( "Insert query did not return appointment id!" ) ;
@ -105,7 +109,7 @@ public class MariaDB implements Database {
@@ -105,7 +109,7 @@ public class MariaDB implements Database {
if ( assignQuery = = null ) assignQuery = insertInto ( APPOINTMENT_ATTACHMENTS , AID , UID , MIME ) ;
if ( urlId . isPresent ( ) ) assignQuery . values ( saved . id ( ) , urlId . get ( ) , attachment . mime ( ) ) ;
}
if ( assignQuery ! = null ) assignQuery . ignoreDuplicates ( ) . execute ( connection ) ;
if ( assignQuery ! = null ) assignQuery . ignoreDuplicates ( ) . execute ( connection ) . close ( ) ;
}
private void writeLinks ( Appointment saved ) throws SQLException { // link to links
@ -116,7 +120,7 @@ public class MariaDB implements Database {
@@ -116,7 +120,7 @@ public class MariaDB implements Database {
if ( assignQuery = = null ) assignQuery = insertInto ( APPOINTMENT_URLS , AID , UID , DESCRIPTION ) ;
if ( urlId . isPresent ( ) ) assignQuery . values ( saved . id ( ) , urlId . get ( ) , link . desciption ( ) ) ;
}
if ( assignQuery ! = null ) assignQuery . ignoreDuplicates ( ) . execute ( connection ) ;
if ( assignQuery ! = null ) assignQuery . ignoreDuplicates ( ) . execute ( connection ) . close ( ) ;
}
private void writeTags ( Appointment saved ) throws SQLException {
@ -127,17 +131,19 @@ public class MariaDB implements Database {
@@ -127,17 +131,19 @@ public class MariaDB implements Database {
if ( assignQuery = = null ) assignQuery = insertInto ( APPOINTMENT_TAGS , AID , TID ) ;
if ( tagId . isPresent ( ) ) assignQuery . values ( saved . id ( ) , tagId . get ( ) ) ;
}
if ( assignQuery ! = null ) assignQuery . ignoreDuplicates ( ) . execute ( connection ) ;
if ( assignQuery ! = null ) assignQuery . ignoreDuplicates ( ) . execute ( connection ) . close ( ) ;
}
private Optional < Long > getOrCreateUrl ( URL url ) throws SQLException {
var rs = select ( UID ) . from ( URLS ) . where ( URL , equal ( url . toString ( ) ) ) . exec ( connection ) ;
Long uid = null ;
if ( rs . next ( ) ) uid = rs . getLong ( 1 ) ;
rs . getStatement ( ) . close ( ) ;
rs . close ( ) ;
if ( uid = = null ) {
rs = insertInto ( URLS , URL ) . values ( url . toString ( ) ) . execute ( connection ) . getGeneratedKeys ( ) ;
if ( rs . next ( ) ) uid = rs . getLong ( 1 ) ;
rs . getStatement ( ) . close ( ) ;
rs . close ( ) ;
}
return nullable ( uid ) ;
@ -147,10 +153,12 @@ public class MariaDB implements Database {
@@ -147,10 +153,12 @@ public class MariaDB implements Database {
var rs = select ( TID ) . from ( TAGS ) . where ( KEYWORD , equal ( tag ) ) . exec ( connection ) ;
Long tid = null ;
if ( rs . next ( ) ) tid = rs . getLong ( 1 ) ;
rs . getStatement ( ) . close ( ) ;
rs . close ( ) ;
if ( tid = = null ) {
rs = insertInto ( TAGS , KEYWORD ) . values ( tag ) . execute ( connection ) . getGeneratedKeys ( ) ;
if ( rs . next ( ) ) tid = rs . getLong ( 1 ) ;
rs . getStatement ( ) . close ( ) ;
rs . close ( ) ;
}
return nullable ( tid ) ;
@ -166,6 +174,7 @@ public class MariaDB implements Database {
@@ -166,6 +174,7 @@ public class MariaDB implements Database {
List < String > results = new ArrayList < > ( ) ;
var rs = select ( KEYWORD ) . from ( TAGS ) . where ( KEYWORD , like ( "%%%s%%" . formatted ( infix ) ) ) . sort ( KEYWORD ) . exec ( connection ) ;
while ( rs . next ( ) ) results . add ( rs . getString ( KEYWORD ) ) ;
rs . getStatement ( ) . close ( ) ;
rs . close ( ) ;
return Payload . of ( results ) ;
} catch ( SQLException e ) {
@ -184,6 +193,7 @@ public class MariaDB implements Database {
@@ -184,6 +193,7 @@ public class MariaDB implements Database {
var rs = query . exec ( connection ) ;
aids = new ArrayList < > ( ) ;
while ( rs . next ( ) ) aids . add ( rs . getLong ( AID ) ) ;
rs . getStatement ( ) . close ( ) ;
rs . close ( ) ;
} catch ( SQLException e ) {
return SqlError . of ( e , "Failed to read appointment ids for tags %s" , tags ) ;
@ -204,6 +214,7 @@ public class MariaDB implements Database {
@@ -204,6 +214,7 @@ public class MariaDB implements Database {
var results = query . exec ( connection ) ;
var list = new ArrayList < Appointment > ( ) ;
while ( results . next ( ) ) createAppointmentOf ( results ) . optional ( ) . ifPresent ( list : : add ) ;
results . getStatement ( ) . close ( ) ;
results . close ( ) ;
addAttachments ( list ) ;
return Payload . of ( list ) ;
@ -235,6 +246,7 @@ public class MariaDB implements Database {
@@ -235,6 +246,7 @@ public class MariaDB implements Database {
LOG . log ( WARNING , "Failed to create URL object from %s" , uri ) ;
}
}
rs . getStatement ( ) . close ( ) ;
rs . close ( ) ;
} catch ( Exception e ) {
LOG . log ( WARNING , "Failed to load attachments." , e ) ;
@ -246,6 +258,7 @@ public class MariaDB implements Database {
@@ -246,6 +258,7 @@ public class MariaDB implements Database {
try {
var rs = select ( ALL ) . from ( APPOINTMENTS ) . where ( AID , equal ( id ) ) . exec ( connection ) ;
Result < Appointment > result = rs . next ( ) ? createAppointmentOf ( rs ) . map ( this : : loadExtra ) : NotFound . of ( "Failed to find appointment with id %s" , id ) ;
rs . getStatement ( ) . close ( ) ;
rs . close ( ) ;
return result ;
} catch ( SQLException e ) {
@ -258,6 +271,7 @@ public class MariaDB implements Database {
@@ -258,6 +271,7 @@ public class MariaDB implements Database {
try {
var rs = select ( ALL ) . from ( APPOINTMENTS ) . where ( LOCATION , equal ( location ) ) . where ( START , equal ( Timestamp . valueOf ( start ) ) ) . exec ( connection ) ;
Result < Appointment > result = rs . next ( ) ? createAppointmentOf ( rs ) . map ( this : : loadExtra ) : error ( "Failed to find appointment starting %s @ %s" , start , location ) ;
rs . getStatement ( ) . close ( ) ;
rs . close ( ) ;
return result ;
} catch ( SQLException e ) {
@ -276,6 +290,7 @@ public class MariaDB implements Database {
@@ -276,6 +290,7 @@ public class MariaDB implements Database {
try {
var rs = select ( KEYWORD ) . from ( APPOINTMENT_TAGS ) . leftJoin ( TID , "tags" , TID ) . where ( AID , equal ( id ) ) . exec ( connection ) ;
while ( rs . next ( ) ) event . tags ( rs . getString ( 1 ) ) ;
rs . getStatement ( ) . close ( ) ;
rs . close ( ) ;
return Payload . of ( event ) ;
} catch ( SQLException e ) {
@ -299,6 +314,7 @@ public class MariaDB implements Database {
@@ -299,6 +314,7 @@ public class MariaDB implements Database {
LOG . log ( WARNING , ( ) - > "Failed to convert %s to URI!" . formatted ( u ) ) ;
}
}
rs . getStatement ( ) . close ( ) ;
rs . close ( ) ;
return Payload . of ( event ) ;
} catch ( SQLException e ) {
@ -322,6 +338,7 @@ public class MariaDB implements Database {
@@ -322,6 +338,7 @@ public class MariaDB implements Database {
LOG . log ( WARNING , ( ) - > "Failed to convert %s to URI!" . formatted ( u ) ) ;
}
}
rs . getStatement ( ) . close ( ) ;
rs . close ( ) ;
return Payload . of ( event ) ;
} catch ( SQLException e ) {
@ -384,7 +401,8 @@ public class MariaDB implements Database {
@@ -384,7 +401,8 @@ public class MariaDB implements Database {
. set ( TITLE , DESCRIPTION , START , END , LOCATION , COORDS )
. where ( AID , equal ( id ) )
. prepare ( connection )
. apply ( event . title ( ) , event . description ( ) , start , end , location , coords ) ;
. apply ( event . title ( ) , event . description ( ) , start , end , location , coords )
. close ( ) ;
delete ( ) . from ( APPOINTMENT_TAGS ) . where ( AID , equal ( id ) ) . execute ( connection ) ;
writeTags ( event ) ;