Class DefaultDBTranslator

java.lang.Object
com.inductiveautomation.ignition.gateway.db.DefaultDBTranslator
All Implemented Interfaces:
DBTranslator

public class DefaultDBTranslator extends Object implements DBTranslator
  • Field Details

    • createTable

      protected String createTable
    • createAISequence

      protected String createAISequence
    • createAITrigger

      protected String createAITrigger
    • createIndex

      protected String createIndex
    • autoIncTypeDef

      protected String autoIncTypeDef
    • limitDef

      protected String limitDef
    • limitPos

      protected LimitPosition limitPos
    • alterTable

      protected String alterTable
    • alterColumnDef

      protected String alterColumnDef
    • primaryKeyDef

      protected String primaryKeyDef
    • currentTimeQuery

      protected String currentTimeQuery
    • columnQuoteChar

      protected String columnQuoteChar
    • supportsRgk

      protected boolean supportsRgk
    • readGKQuery

      protected String readGKQuery
    • tableListFilter

      protected String tableListFilter
    • maxIdentifierLength

      protected int maxIdentifierLength
    • typeMap

      protected Map<DataType,String> typeMap
  • Constructor Details

  • Method Details

    • createTypeMap

      protected EnumMap<DataType,String> createTypeMap()
    • getCurrentTimeQuery

      public String getCurrentTimeQuery()
      Description copied from interface: DBTranslator
      Returns a query that can be used to get the current database time.
      Specified by:
      getCurrentTimeQuery in interface DBTranslator
    • isNativeBooleanType

      public static boolean isNativeBooleanType(String columnType)
      Returns whether the given SQL column-type keyword denotes a native boolean column -- one that requires a real boolean bind rather than an integer 0/1. The keyword may come from a translator's type map (the keyword that lands in generated DDL) or be read back from the database via column metadata, so callers can classify a column's actual type the same way it is declared. Any spelling beginning with bool (e.g. PostgreSQL's bool/ BOOLEAN, including a value carrying a modifier such as "BOOLEAN NOT NULL") is native. SQL Server's bit is intentionally excluded because it accepts an integer 0/1 bind.
    • usesNativeBooleanType

      public static boolean usesNativeBooleanType(DBTranslator translator)
      Null-safe access to DBTranslator.usesNativeBooleanType() (see that method for the fallback semantics); false for a null translator. Must go through the interface method rather than an instanceof DefaultDBTranslator check because a datasource hands out a swap-able wrapper around the real translator, not the DefaultDBTranslator itself.
    • usesNativeBooleanType

      public boolean usesNativeBooleanType()
      Description copied from interface: DBTranslator
      Whether this translator maps DataType.Boolean to a native boolean SQL type -- one that requires a real boolean bind rather than an integer 0/1. This is the best available signal for the type a boolean column was most likely created with when the column's actual type cannot be read back from the database; it is consulted only as a fallback, since the mapping is mutable and can diverge from an already-created column. Defaults to false; DefaultDBTranslator overrides it to classify its configured boolean keyword, and any translator that wraps another (e.g. a datasource's swap-able delegate) must forward this call so the wrapped mapping is not lost.
      Specified by:
      usesNativeBooleanType in interface DBTranslator
    • probeNativeBooleanColumns

      public static Map<String,Boolean> probeNativeBooleanColumns(Connection con, String probeSql, LoggerEx log)
      Reads the column types of the table selected by probeSql (a SELECT ... WHERE 1 = 0 statement) and classifies each as a native boolean SQL type (isNativeBooleanType(String)), keyed by lower-cased column name. The probe is isolated with a savepoint when the connection is not in auto-commit, so on PostgreSQL a probe failure -- which aborts the current transaction -- does not poison the write that follows. Returns an empty map if the column types cannot be read, in which case the caller falls back to usesNativeBooleanType(DBTranslator).
    • isColumnTypeMismatch

      public static boolean isColumnTypeMismatch(SQLException e)
      Returns whether e -- or any exception chained via SQLException.getNextException() (e.g. inside a BatchUpdateException) -- reports a column/parameter datatype mismatch, PostgreSQL SQLState 42804. This is the signal that a cached native-boolean classification has gone stale (for instance the column was altered on a live gateway) and must be re-resolved; an unrelated failure should leave the cached classification intact rather than force a fresh metadata probe on every retry.
    • quoteColumn

      public String quoteColumn(Column col)
    • quoteColumn

      public String quoteColumn(String col)
    • getCreateCommand

      public List<String> getCreateCommand(String tableName, ColumnList columns)
      Specified by:
      getCreateCommand in interface DBTranslator
    • applyLimit

      public String applyLimit(String query, Integer limit)
      Description copied from interface: DBTranslator
      Takes a SELECT query and applies the given limit to it, resulting in a select query with a limiting clause. If the incoming query already has a limit clause, the existing text will be used (that is, nothing further will be done).
      Specified by:
      applyLimit in interface DBTranslator
    • getAlterCommand

      public List<String> getAlterCommand(String tableName, ColumnList columns)
      Specified by:
      getAlterCommand in interface DBTranslator
    • getCreateIndexCommand

      public String getCreateIndexCommand(String tableName, Column column)
      Specified by:
      getCreateIndexCommand in interface DBTranslator
    • getShortendName

      protected String getShortendName(String name)
    • getCreationDef

      protected String getCreationDef(ColumnList columns)
    • getSpecialtyDef

      protected String getSpecialtyDef(String tokenVal, String definition, String token)
    • replaceToken

      protected String replaceToken(String source, String token, String value)
    • replaceTokens

      protected String replaceTokens(String source, List<String> tokens, List<String> value)
    • getDataTypeKeyword

      protected String getDataTypeKeyword(DataType type)
    • getDatabaseTypeName

      public String getDatabaseTypeName(DataType type)
      Description copied from interface: DBTranslator
      The database-specific column type keyword this translator emits for the given platform DataType in a CREATE/ALTER statement — e.g. Postgres maps DataType.DateTime to "timestamp" while SQLite maps it to "text". Returns null if this translator declares no mapping for the type.

      Lets schema validation compare an existing column against the type auto-create would actually produce, rather than against an abstract ideal: an audit event_timestamp is DataType.DateTime, but on a SQLite datasource auto-create declares it text (timestamps are stored as epoch-millis in TEXT), so validating it as a temporal column would fault the very table auto-create just wrote. The default returns null; DefaultDBTranslator overrides it to expose its configured type map.

      Specified by:
      getDatabaseTypeName in interface DBTranslator
    • getColumnQuoteChar

      public String getColumnQuoteChar()
      Description copied from interface: DBTranslator
      Returns the character that this driver uses to quote names in queries
      Specified by:
      getColumnQuoteChar in interface DBTranslator
    • getFetchKeyQuery

      public String getFetchKeyQuery(String tableName, String columnName)
      Description copied from interface: DBTranslator
      A query to be executed instead of using JDBC's built-in facility for obtaining auto-generated keys. Only used if supportsRGK is false. Takes the table name and column name in case the query needs to use them- it will depend on the database as to whether or not they are.
      Specified by:
      getFetchKeyQuery in interface DBTranslator
    • isSupportsRGK

      public boolean isSupportsRGK()
      Specified by:
      isSupportsRGK in interface DBTranslator
      Returns:
      whether or not this driver supports returning generated keys on insert queries
    • getTableListFilter

      public String getTableListFilter()
      Description copied from interface: DBTranslator
      Returns a filter that will be used to narrow the list of tables returned from the database. A semi-colon separated list of simple patterns, like "apex_*;sys_info*"
      Specified by:
      getTableListFilter in interface DBTranslator