@@ -30,6 +30,7 @@ public Sync(Event event, String... args){
3030 synchronized (Sync .class ){
3131 if (Initializer .stop && event !=Event .SHUTDOWN ){ return ; }
3232 try {
33+ final boolean debug = "true" .equalsIgnoreCase (settings .get ("debug" ));
3334 final String url = "jdbc:postgresql://" +Config .connectionURL ;
3435 final String username = Config .username ;
3536 final String password = Config .password ;
@@ -59,6 +60,49 @@ public Sync(Event event, String... args){
5960 }
6061 break ;
6162 }
63+ case SELECT_TABLE :{
64+ if (ID ==-1 || !started || args .length !=1 ){
65+ return ;
66+ }
67+ args [0 ] = args [0 ].replace ("$ID" , String .valueOf (ID ));
68+ try (
69+ Connection con = DriverManager .getConnection (url , connectionParams );
70+ ){
71+ syncLog (con ,ID );
72+ try (
73+ Statement s = con .createStatement ();
74+ ){
75+ fillCache (args [0 ],s );
76+ }
77+ }
78+ break ;
79+ }
80+ case UPDATE_TABLE :{
81+ if (ID ==-1 || !started || args .length <=1 ){
82+ return ;
83+ }
84+ try (
85+ Connection con = DriverManager .getConnection (url , connectionParams );
86+ ){
87+ con .setAutoCommit (false );
88+ syncLog (con ,ID );
89+ try (
90+ Statement s = con .createStatement ();
91+ ){
92+ for (int i =1 ;i <args .length ;++i ){
93+ if (Initializer .stop ){ return ; }
94+ if (debug ){
95+ Initializer .log (args [i ]);
96+ }
97+ s .executeUpdate (args [i ]);
98+ }
99+ con .commit ();
100+ fillCache (args [0 ],s );
101+ }
102+ con .commit ();
103+ }
104+ break ;
105+ }
62106 case SAVE_OPERATOR :{
63107 if (ID ==-1 || args .length !=2 || !versionCompatible ){
64108 return ;
@@ -160,7 +204,7 @@ public void execute(SystemAccess sys){
160204 while (r .next ()){
161205 k = r .getString (1 );
162206 v = r .getString (2 );
163- if (k !=null && v !=null ){
207+ if (k !=null && v !=null && ! k . isEmpty () && ! v . isEmpty () ){
164208 map .put (k ,v );
165209 }
166210 }
@@ -226,7 +270,6 @@ public void execute(SystemAccess sys){
226270 }
227271 // Update webctrl.log
228272 syncLog (con ,ID );
229- con .commit ();
230273 if (Initializer .stop ){ return ; }
231274 // Delete operators in webctrl.operator_blacklist
232275 // Create operators in webctrl.operator_whitelist
@@ -254,7 +297,7 @@ public void execute(SystemAccess sys){
254297 data .password = r .getString (3 );
255298 data .lvl5_auto_logout = r .getInt (4 );
256299 data .lvl5_auto_collapse = r .getBoolean (5 );
257- if (data .password .isEmpty ()){
300+ if (data .password == null || data . password .isEmpty ()){
258301 data .password = defaultPassword ;
259302 }
260303 whitelist .put (data .username ,data );
@@ -331,7 +374,7 @@ public void execute(SystemAccess sys){
331374 while (r .next ()){
332375 min = r .getString (2 );
333376 max = r .getString (3 );
334- if ((min ==null || Utility .compareVersions (Initializer .simpleVersion ,min )>=0 ) && (max ==null || Utility .compareVersions (Initializer .simpleVersion ,max )<=0 )){
377+ if ((min ==null || min . isEmpty () || Utility .compareVersions (Initializer .simpleVersion ,min )>=0 ) && (max ==null || max . isEmpty () || Utility .compareVersions (Initializer .simpleVersion ,max )<=0 )){
335378 addon = r .getString (1 );
336379 if (addon !=null && !Initializer .getName ().equalsIgnoreCase (addon )){
337380 blacklist .add (addon .toLowerCase ());
@@ -356,7 +399,7 @@ public void execute(SystemAccess sys){
356399 while (r .next ()){
357400 min = r .getString (5 );
358401 max = r .getString (6 );
359- if ((min ==null || Utility .compareVersions (Initializer .simpleVersion ,min )>=0 ) && (max ==null || Utility .compareVersions (Initializer .simpleVersion ,max )<=0 )){
402+ if ((min ==null || min . isEmpty () || Utility .compareVersions (Initializer .simpleVersion ,min )>=0 ) && (max ==null || max . isEmpty () || Utility .compareVersions (Initializer .simpleVersion ,max )<=0 )){
360403 addon = r .getString (1 );
361404 if (addon !=null && !Initializer .getName ().equalsIgnoreCase (addon )){
362405 d = new AddonDownload ();
@@ -418,7 +461,6 @@ public void execute(SystemAccess sys){
418461 s .executeUpdate ("INSERT INTO webctrl.events VALUES(" +ID +",'SYNCED',CURRENT_TIMESTAMP);" );
419462 }
420463 syncLog (con ,ID );
421- con .commit ();
422464 }finally {
423465 con .rollback ();
424466 }
@@ -431,8 +473,9 @@ public void execute(SystemAccess sys){
431473 success = true ;
432474 }catch (Throwable t ){
433475 if (!(t instanceof InterruptedException )){
434- Initializer .status = "Sync Error" ;
435- Initializer .log ("Sync error." );
476+ final String s = "Sync error during: " +event .name ();
477+ Initializer .status = s ;
478+ Initializer .log (s );
436479 Initializer .log (t );
437480 }
438481 }finally {
@@ -485,10 +528,47 @@ private void syncLog(Connection con, int ID) throws Throwable {
485528 s .addBatch ();
486529 }
487530 s .executeBatch ();
531+ if (!con .getAutoCommit ()){
532+ con .commit ();
533+ }
488534 }catch (Throwable t ){
489535 Initializer .logCache .addAll (cache );
490536 throw t ;
491537 }
492538 }
493539 }
540+ private void fillCache (String query , Statement s ) throws Throwable {
541+ try (
542+ ResultSet r = s .executeQuery (query );
543+ ){
544+ final TableCache cache = new TableCache ();
545+ final ResultSetMetaData meta = r .getMetaData ();
546+ cache .columns = meta .getColumnCount ();
547+ final int [] types = new int [cache .columns ];
548+ int i ;
549+ for (i =0 ;i <types .length ;++i ){
550+ types [i ] = meta .getColumnType (i +1 );
551+ }
552+ cache .data = new ArrayList <String >(cache .columns <<6 );
553+ cache .rows = 0 ;
554+ String ss ;
555+ while (r .next ()){
556+ if (Initializer .stop ){ return ; }
557+ for (i =0 ;i <types .length ;++i ){
558+ switch (types [i ]){
559+ case java .sql .Types .BOOLEAN : case java .sql .Types .BIT : {
560+ ss = String .valueOf (r .getBoolean (i +1 ));
561+ break ;
562+ }
563+ default :{
564+ ss = r .getString (i +1 );
565+ }
566+ }
567+ cache .data .add (ss );
568+ }
569+ ++cache .rows ;
570+ }
571+ TableCache .instance = cache ;
572+ }
573+ }
494574}
0 commit comments