forked from Limework/skript-db
		
	Add synchronous support on current thread
This commit is contained in:
		
							parent
							
								
									4d795edf01
								
							
						
					
					
						commit
						b435696385
					
				@ -43,6 +43,7 @@ task buildReadme(type: Javadoc) {
 | 
				
			|||||||
  options.addStringOption('file', 'README.md')
 | 
					  options.addStringOption('file', 'README.md')
 | 
				
			||||||
  options.addStringOption('markdown', '-quiet')
 | 
					  options.addStringOption('markdown', '-quiet')
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
task fatJar(type: Jar) {
 | 
					task fatJar(type: Jar) {
 | 
				
			||||||
  manifest {
 | 
					  manifest {
 | 
				
			||||||
    attributes 'Implementation-Title': 'Gradle Jar File Example',
 | 
					    attributes 'Implementation-Title': 'Gradle Jar File Example',
 | 
				
			||||||
 | 
				
			|||||||
@ -34,7 +34,7 @@ import java.util.concurrent.Executors;
 | 
				
			|||||||
 * variable in the form `{test::<column name>::<row number>}`
 | 
					 * variable in the form `{test::<column name>::<row number>}`
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * @name Execute Statement
 | 
					 * @name Execute Statement
 | 
				
			||||||
 * @pattern execute %string% (in|on) %datasource% [and store [[the] (output|result)[s]] (to|in)
 | 
					 * @pattern [synchronously] execute %string% (in|on) %datasource% [and store [[the] (output|result)[s]] (to|in)
 | 
				
			||||||
 * [the] [var[iable]] %-objects%]
 | 
					 * [the] [var[iable]] %-objects%]
 | 
				
			||||||
 * @example execute "select * from table" in {sql} and store the result in {output::*}
 | 
					 * @example execute "select * from table" in {sql} and store the result in {output::*}
 | 
				
			||||||
 * @example execute "select * from %{table variable}%" in {sql} and store the result in {output::*}
 | 
					 * @example execute "select * from %{table variable}%" in {sql} and store the result in {output::*}
 | 
				
			||||||
@ -44,6 +44,7 @@ public class EffExecuteStatement extends Delay {
 | 
				
			|||||||
  static {
 | 
					  static {
 | 
				
			||||||
    Skript.registerEffect(EffExecuteStatement.class,
 | 
					    Skript.registerEffect(EffExecuteStatement.class,
 | 
				
			||||||
        "execute %string% (in|on) %datasource% " +
 | 
					        "execute %string% (in|on) %datasource% " +
 | 
				
			||||||
 | 
					            "[and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]", "synchronously execute %string% (in|on) %datasource% " +
 | 
				
			||||||
            "[and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]");
 | 
					            "[and store [[the] (output|result)[s]] (to|in) [the] [var[iable]] %-objects%]");
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -58,7 +59,17 @@ public class EffExecuteStatement extends Delay {
 | 
				
			|||||||
  private boolean isLocal;
 | 
					  private boolean isLocal;
 | 
				
			||||||
  private boolean isList;
 | 
					  private boolean isList;
 | 
				
			||||||
  private Map<String, Object> doLater = new HashMap<>();
 | 
					  private Map<String, Object> doLater = new HashMap<>();
 | 
				
			||||||
 | 
					  private boolean isSync;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private void continueScriptExecution(Event e, String res) {
 | 
				
			||||||
 | 
					    lastError = res;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (getNext() != null) {
 | 
				
			||||||
 | 
					      doLater.forEach((name, value) -> setVariable(e, name, value));
 | 
				
			||||||
 | 
					      doLater.clear();
 | 
				
			||||||
 | 
					      TriggerItem.walk(getNext(), e);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
  @Override
 | 
					  @Override
 | 
				
			||||||
  protected void execute(Event e) {
 | 
					  protected void execute(Event e) {
 | 
				
			||||||
    DataSource ds = dataSource.getSingle(e);
 | 
					    DataSource ds = dataSource.getSingle(e);
 | 
				
			||||||
@ -67,7 +78,10 @@ public class EffExecuteStatement extends Delay {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    if (ds == null)
 | 
					    if (ds == null)
 | 
				
			||||||
      return;
 | 
					      return;
 | 
				
			||||||
 | 
					    if (isSync) {
 | 
				
			||||||
 | 
					      String result = executeStatement(ds, baseVariable, query);
 | 
				
			||||||
 | 
					      continueScriptExecution(e, result);
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
      Object locals = Variables.removeLocals(e);
 | 
					      Object locals = Variables.removeLocals(e);
 | 
				
			||||||
      CompletableFuture<String> sql =
 | 
					      CompletableFuture<String> sql =
 | 
				
			||||||
          CompletableFuture.supplyAsync(() -> executeStatement(ds, baseVariable, query), threadPool);
 | 
					          CompletableFuture.supplyAsync(() -> executeStatement(ds, baseVariable, query), threadPool);
 | 
				
			||||||
@ -91,11 +105,14 @@ public class EffExecuteStatement extends Delay {
 | 
				
			|||||||
        });
 | 
					        });
 | 
				
			||||||
      });
 | 
					      });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @Override
 | 
					  @Override
 | 
				
			||||||
  protected TriggerItem walk(Event e) {
 | 
					  protected TriggerItem walk(Event e) {
 | 
				
			||||||
    debug(e, true);
 | 
					    debug(e, true);
 | 
				
			||||||
 | 
					    if (!isSync) {
 | 
				
			||||||
      Delay.addDelayedEvent(e);
 | 
					      Delay.addDelayedEvent(e);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    execute(e);
 | 
					    execute(e);
 | 
				
			||||||
    return null;
 | 
					    return null;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@ -260,6 +277,8 @@ public class EffExecuteStatement extends Delay {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
    dataSource = (Expression<HikariDataSource>) exprs[1];
 | 
					    dataSource = (Expression<HikariDataSource>) exprs[1];
 | 
				
			||||||
    Expression<?> expr = exprs[2];
 | 
					    Expression<?> expr = exprs[2];
 | 
				
			||||||
 | 
					    System.out.println(matchedPattern);
 | 
				
			||||||
 | 
					    isSync = matchedPattern == 1;
 | 
				
			||||||
    if (expr instanceof Variable) {
 | 
					    if (expr instanceof Variable) {
 | 
				
			||||||
      Variable<?> varExpr = (Variable<?>) expr;
 | 
					      Variable<?> varExpr = (Variable<?>) expr;
 | 
				
			||||||
      var = varExpr.getName();
 | 
					      var = varExpr.getName();
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user