Interface IDBManager

All Superinterfaces:
com.smartfoxserver.bitswarm.service.IService
All Known Implementing Classes:
SFSDBManager

public interface IDBManager extends com.smartfoxserver.bitswarm.service.IService
  • Method Summary

    Modifier and Type
    Method
    Description
    executeInsert(String sql, Object[] params)
    Executes a SQL INSERT command returning the key of the inserted row
    This is a small variation on executeQuery(String, Object[]) where no additional SQL parameter is used.
    executeQuery(String sql, Object[] params)
    Perform a SQL query and return a structured object based on SFSArray and SFSObject.
    void
    Executes a non-query SQL command such as INSERT, UPDATE, DELETE etc...
    void
    executeUpdate(String sql, Object[] params)
    Executes a non-query SQL command such as INSERT, UPDATE, DELETE etc...
    Get the configuration details of the JDBC connection and connection pool
    Get a direct reference to the JDBC connection object.
    boolean
    True if the Service is active

    Methods inherited from interface com.smartfoxserver.bitswarm.service.IService

    destroy, getName, handleMessage, init, setName
  • Method Details

    • isActive

      boolean isActive()
      True if the Service is active
      Returns:
      true if the Service is active
    • getConfig

      DBConfig getConfig()
      Get the configuration details of the JDBC connection and connection pool
      Returns:
      the configuration details of the JDBC connection and connection pool
      See Also:
    • getConnection

      Connection getConnection() throws SQLException
      Get a direct reference to the JDBC connection object. This way you can access the underlying pooled connection and perform any JDBC API call directly. The connection object must be returned to the connection pool once you are finished using it. This is done by calling the connection's close() method.

      An example of a code template would be:

         try
              {
                      // An example query ... it could be anything
                      sql = "SELECT * FROM table";
                      conn = getParentZone().getDBManager().getConnection();
                      stmt = conn.prepareStatement(sql);
                      
                      ResultSet resultSet = stmt.executeQuery();
                      
                      // More code here...
              }
              
              // Not mandatory
              catch (SQLException)
              {
                      // do something about it
              }
              
              // Mandatory! Close connection before leaving this method
              finally
              {
                      if (stmt != null)
                              stmt.close();
                      
                      if (conn != null)
                              conn.close();
              }
       
      Returns:
      the pooled JDBC connection
      Throws:
      SQLException
    • executeQuery

      ISFSArray executeQuery(String sql, Object[] params) throws SQLException
      Perform a SQL query and return a structured object based on SFSArray and SFSObject. This is a simplified technique that provides the query result(s) in a convenient format, ready to be sent to the client(s).

      The SQL code can include placeholders (using a question mark) and an array of parameters that will be used to populate them, just like when using prepared statements via JDBC. Example:

              executeQuery("SELECT * FROM Users WHERE age > ? AND country=?", new Object[] {35, "Sweden"}); 
       

      The structure of the returned object is as follows:

      SFSArray: represents the result set. It contains all the selected records in form of SFSObject(s)

      • Index 0: SFSObject (record)
        • key (field name): value
        • key (field name): value
        • etc...
      • ...
        ...
        ...
      • Index N: SFSObject (record)
        • key (field name): value
        • key (field name): value
        • etc...

      Data types from the database are translated to SFSObject types according to this table:

      SQL TypeSFSObject Type
      NULLNULL
      BOOLEANBOOLEAN
      DATELONG (Unix timestamp)
      FLOAT, DECIMAL, DOUBLE, REALDOUBLE
      TINYINT, SMALLINT, INTEGERINTEGER
      CHAR, VARCHAR, LONGVARCHARUTF_STRING
      NCHAR, NVARCHAR, LONGNVARCHARUTF_STRING
      TIMESTAMPLONG
      BIGINT (up to 2^63)LONG
      LONGVARBINARY, BLOBBYTE_ARRAY
      Parameters:
      sql - the SQL code. Placeholders for parameters can be used such as: SELECT * FROM Users WHERE name=?
      params - An array of objects that will be used to populate the placeholders in the SQL code
      Returns:
      the SFSArray representing the result set
      Throws:
      SQLException - reports any errors related with the execution of the SQL query
    • executeQuery

      ISFSArray executeQuery(String sql) throws SQLException
      This is a small variation on executeQuery(String, Object[]) where no additional SQL parameter is used. Please see executeQuery(String, Object[])
      Parameters:
      sql - the SQL code
      Returns:
      the SFSArray representing the result set
      Throws:
      SQLException - reports any errors related with the execution of the SQL query
    • executeUpdate

      void executeUpdate(String sql, Object[] params) throws SQLException
      Executes a non-query SQL command such as INSERT, UPDATE, DELETE etc...
      Parameters:
      sql - the SQL code. Placeholders for parameters can be used such as: SELECT * FROM Users WHERE name=?
      params - An array of objects that will be used to populate the placeholders in the SQL code
      Throws:
      SQLException - reports any errors related with the execution of the SQL update
    • executeInsert

      Object executeInsert(String sql, Object[] params) throws SQLException
      Executes a SQL INSERT command returning the key of the inserted row
      Parameters:
      sql - the SQL code. Placeholders for parameters can be used such as: INSERT INTO users (name, email) VALUES(?, ?)
      params - An array of objects that will be used to populate the placeholders in the SQL code
      Throws:
      SQLException - reports any errors related with the execution of the SQL update
      Since:
      2.7
    • executeUpdate

      void executeUpdate(String sql) throws SQLException
      Executes a non-query SQL command such as INSERT, UPDATE, DELETE etc...
      Parameters:
      sql - the SQL code.
      Throws:
      SQLException - reports any errors related with the execution of the SQL update