Class Command

Direct Known Subclasses:
ACreate, AlterBackup, AlterPassword, AlterUser, AQuery, Check, Close, Copy, CreateBackup, CreateUser, DropBackup, DropUser, Execute, Exit, Export, Flush, Get, Grant, Help, Info, InfoDB, InfoIndex, InfoStorage, Inspect, JobsList, JobsResult, JobsStop, Kill, List, Open, Password, RepoDelete, RepoInstall, RepoList, Restore, Set, ShowBackups, ShowSessions, ShowUsers, Test

public abstract class Command extends Job
This class provides the architecture for all internal command implementations. It evaluates queries that are sent by the GUI, the client or the standalone version.
Author:
BaseX Team 2005-21, BSD License, Christian Gruen
  • Field Details

    • args

      public final String[] args
      Command arguments.
    • openDB

      public final boolean openDB
      Indicates if the command requires an opened database.
    • context

      protected Context context
      Database context.
    • options

      protected MainOptions options
      Convenience access to database options.
    • soptions

      protected StaticOptions soptions
      Convenience access to static options.
    • out

      protected PrintOutput out
      Output stream.
    • in

      protected InputSource in
      Optional input source.
    • uri

      protected String uri
      Base URI.
    • exception

      protected Exception exception
      Exception, resulting from command execution.
    • maxResults

      protected int maxResults
      Maximum number of results (ignored if negative).
  • Constructor Details

    • Command

      protected Command(Perm perm, String... args)
      Constructor for commands requiring no opened database.
      Parameters:
      perm - required permission
      args - arguments
    • Command

      protected Command(Perm perm, boolean openDB, String... args)
      Constructor.
      Parameters:
      perm - required permission
      openDB - requires an opened database
      args - arguments
  • Method Details

    • execute

      public final void execute(Context ctx, OutputStream os) throws BaseXException
      Executes the command and prints the result to the specified output stream. If an exception occurs, a BaseXException is thrown.
      Parameters:
      ctx - database context
      os - output stream reference
      Throws:
      BaseXException - command exception
    • execute

      public final String execute(Context ctx) throws BaseXException
      Executes the command and returns the result as string. If an exception occurs, a BaseXException is thrown.
      Parameters:
      ctx - database context
      Returns:
      string result
      Throws:
      BaseXException - command exception
    • setInput

      public final void setInput(InputStream input)
      Attaches an input stream.
      Parameters:
      input - input stream
    • setInput

      public final void setInput(InputSource input)
      Attaches an input source.
      Parameters:
      input - input source
    • run

      public final boolean run(Context ctx)
      Runs the command without permission, data and concurrency checks. Should only be called by other database commands.
      Parameters:
      ctx - database context
      Returns:
      result of check
    • info

      public final String info()
      Returns command information.
      Returns:
      info string
    • exception

      public final Exception exception()
      Returns the cause of an error.
      Returns:
      error
    • result

      public Value result()
      Returns a cached result set generated by AQuery. Can only be called once.
      Returns:
      result set or null
    • updating

      public boolean updating(Context ctx)
      Checks if the command performs updates/write operations.
      Parameters:
      ctx - database context
      Returns:
      result of check
    • updated

      public boolean updated(Context ctx)
      Checks if the command has updated any data.
      Parameters:
      ctx - database context
      Returns:
      result of check
    • newData

      public boolean newData(Context ctx)
      Closes an open data reference and returns true if this command will change the Context.data() reference. This method is only required by the GUI.
      Parameters:
      ctx - database context
      Returns:
      result of check
    • maxResults

      public final void maxResults(int max)
      Enforces a maximum number of query results. This method is only required by the GUI.
      Parameters:
      max - maximum number of results (ignored if negative)
    • supportsProg

      public boolean supportsProg()
      Returns true if this command returns a progress value. This method is only required by the GUI.
      Returns:
      result of check
    • stoppable

      public boolean stoppable()
      Returns true if this command can be stopped. This method is only required by the GUI.
      Returns:
      result of check
    • init

      public final void init(Context ctx, OutputStream os)
      Initializes the command execution.
      Parameters:
      ctx - database context
      os - output stream
    • run

      public final boolean run(Context ctx, OutputStream os)
      Runs the command without permission, data and concurrency checks.
      Parameters:
      ctx - database context
      os - output stream
      Returns:
      result of check
    • toString

      public final String toString(boolean conf)
      Returns a string representation of the command.
      Parameters:
      conf - hide confidential information
      Returns:
      result of check
    • baseURI

      public Command baseURI(String base)
      Sets a base URI.
      Parameters:
      base - base URI
      Returns:
      self reference
    • baseURI

      public String baseURI()
      Returns the base URI.
      Returns:
      base URI
    • toString

      public final String toString()
      Overrides:
      toString in class Object
    • run

      protected abstract boolean run() throws IOException
      Executes the command and serializes the result (internal call).
      Returns:
      success of operation
      Throws:
      IOException - I/O exception
    • build

      protected void build(CmdBuilder cb)
      Builds a string representation from the command. This string must be correctly built, as commands are sent to the server as strings.
      Parameters:
      cb - command builder
    • error

      protected final boolean error(String msg, Object... ext)
      Adds the error message to the message buffer info.
      Parameters:
      msg - error message
      ext - error extension
      Returns:
      false
    • info

      protected final boolean info(String str, Object... ext)
      Adds information on command execution.
      Parameters:
      str - information to be added
      ext - extended info
      Returns:
      true
    • getOption

      protected final <E extends Enum<E>> E getOption(Class<E> typ)
      Returns the specified command option.
      Type Parameters:
      E - token type
      Parameters:
      typ - options enumeration
      Returns:
      option
    • addLocks

      protected final boolean addLocks(LockList list, int index)
      Adds the name of the database that has been addressed by the argument index. No databases will be added if the argument uses glob syntax.
      Parameters:
      list - lock list
      index - argument index
      Returns:
      false if database cannot be determined due to glob syntax
    • getOption

      protected static <E extends Enum<E>> E getOption(String string, Class<E> type)
      Returns the specified command option.
      Type Parameters:
      E - token type
      Parameters:
      string - string to be found
      type - options enumeration
      Returns:
      option, or null if the option is not found
    • close

      protected static boolean close(Context ctx, String db)
      Closes the specified database if it is currently opened and only pinned once.
      Parameters:
      ctx - database context
      db - database to be closed
      Returns:
      true if opened database was closed