JAVA   55

interface Vendor

Guest on 11th May 2022 04:52:28 PM

  1. package de.fuberlin.wiwiss.d2rq.sql.vendor;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.SQLException;
  5. import java.util.Properties;
  6.  
  7. import de.fuberlin.wiwiss.d2rq.algebra.Attribute;
  8. import de.fuberlin.wiwiss.d2rq.algebra.RelationName;
  9. import de.fuberlin.wiwiss.d2rq.expr.Expression;
  10. import de.fuberlin.wiwiss.d2rq.map.Database;
  11. import de.fuberlin.wiwiss.d2rq.sql.types.DataType;
  12.  
  13. /**
  14.  * Encapsulates differences in SQL syntax between database engines.
  15.  * Methods only exists for SQL features where at least one engine
  16.  * requires custom syntax differing from SQL-92.
  17.  *
  18.  * @author Richard Cyganiak
  19.  */
  20. public interface Vendor {
  21.  
  22.         public final static Vendor SQL92 = new SQL92(true);
  23.         public final static Vendor MySQL = new MySQL();
  24.         public final static Vendor PostgreSQL = new PostgreSQL();
  25.         public final static Vendor InterbaseOrFirebird = new SQL92(false);
  26.         public final static Vendor Oracle = new Oracle();
  27.         public final static Vendor SQLServer = new SQLServer();
  28.         public final static Vendor MSAccess = new SQLServer(); // TODO
  29.         public final static Vendor HSQLDB = new HSQLDB();
  30.  
  31.         /**
  32.          * Concatenation of <code>a</code> and <code>b</code> is
  33.          * "<code>a || b</code>" in standard SQL, but <code>CONCAT(a, b)</code>
  34.          * in MySQL.
  35.          *
  36.          * @param sqlFragments An array of SQL expressions to be concatenated
  37.          * @return A SQL expression that concatenates the arguments
  38.          */
  39.         String getConcatenationExpression(String[] sqlFragments);
  40.        
  41.         /**
  42.          * A relation name with an alias name for use in <code>FROM</code>
  43.          * clauses. Would return <code>relation AS alias</code> for
  44.          * SQL 92 (the AS is optional in SQL 92, but some engines require
  45.          * it, while others don't understand it).
  46.          *  
  47.          * @param relationName The original table name
  48.          * @param aliasName The alias for the table name
  49.          * @return An expression that assigns the alias to the table name
  50.          */
  51.         String getRelationNameAliasExpression(RelationName relationName, RelationName aliasName);
  52.        
  53.         /**
  54.          * Handles special characters in attribute names.
  55.          *
  56.          * @param attribute An attribute name (column name)
  57.          * @return Quoted form for use in SQL statements
  58.          */
  59.         String quoteAttribute(Attribute attribute);
  60.        
  61.         /**
  62.          * Handles special characters in relation names.
  63.          *
  64.          * @param relationName A relation name (table name)
  65.          * @return Quoted form for use in SQL statements
  66.          */
  67.         String quoteRelationName(RelationName relationName);
  68.  
  69.         /**
  70.          * Handles special characters in identifiers. SQL 92 puts identifiers
  71.          * in double quotes, but MySQL uses backticks.
  72.          *
  73.          * @param identifier An identifier, such as a table or column name
  74.          * @return Quoted form of the identifier for use in SQL statements
  75.          */
  76.         String quoteIdentifier(String identifier);
  77.        
  78.         /**
  79.          * Handles special characters in strings. Most databases wrap the string
  80.          * in single quotes, and escape single quotes by doubling them. Some
  81.          * databases also require doubling of backslashes.
  82.          *
  83.          * @param s An arbitrary character string
  84.          * @return A quoted and escaped version safe for use in SQL statements
  85.          */
  86.         String quoteStringLiteral(String s);
  87.        
  88.         String quoteBinaryLiteral(String hexString);
  89.        
  90.         String quoteDateLiteral(String date);
  91.        
  92.         String quoteTimeLiteral(String time);
  93.        
  94.         String quoteTimestampLiteral(String timestamp);
  95.        
  96.         /**
  97.          * Returns an expression for limiting the number of returned rows
  98.          * for engines that support this (<code>ROWNUM &lt;= n</code>)
  99.          *
  100.          * @param limit A maximum number of rows, or {@link Database#NO_LIMIT}
  101.          * @return An expression that limits the number of rows, or {@link Expression#TRUE}
  102.          * if not supported by the engine
  103.          */
  104.         Expression getRowNumLimitAsExpression(int limit);
  105.  
  106.         /**
  107.          * Returns a modifier for the SELECT keyword that adds a limit
  108.          * to the number of returned rows for engines that support this (<code>TOP n</code>)
  109.          *
  110.          * @param limit A maximum number of rows, or {@link Database#NO_LIMIT}
  111.          * @return A SELECT keyword modifier, or the empty string if unsupported/unnecessary
  112.          */
  113.         String getRowNumLimitAsSelectModifier(int limit);
  114.        
  115.         /**
  116.          * Returns a fragment to be appended to a SQL query in order to add a limit
  117.          * to the number of returned rows for engines that support this (<code>LIMIT n</code>)
  118.          *
  119.          * @param limit A maximum number of rows, or {@link Database#NO_LIMIT}
  120.          * @return A SQL fragment, or the empty string if unsupported/unnecessary
  121.          */
  122.         String getRowNumLimitAsQueryAppendage(int limit);
  123.        
  124.         /**
  125.          * Returns a set of default connection properties to be used
  126.          * when connecting to this database engine type
  127.          *
  128.          * @return A collection of properties
  129.          */
  130.         Properties getDefaultConnectionProperties();
  131.        
  132.         /**
  133.          * Returns a {@link DataType} corresponding to a JDBC type. This may be
  134.          * an unsupported datatype; in this case, its {@link DataType#isUnsupported()}
  135.          * method will return true. <code>null</code> will be returned if the vendor
  136.          * code doesn't handle this datatype at all; that should generally be
  137.          * considered a bug.
  138.          *
  139.          * @param jdbcType A <code>java.sql.Types</code> constant
  140.          * @param name The type name, as reported by <code>java.sql</code> metadata methods, normalized to uppercase
  141.          * @param size Character size of the type, or 0 if not applicable
  142.          * @return A compatible D2RQ DataType instance, or <code>null</code> if the vendor code is broken
  143.          */
  144.         DataType getDataType(int jdbcType, String name, int size);
  145.  
  146.         /**
  147.          * Turns a BOOLEAN expression into an expression that is guaranteed to
  148.          * be usable in any place where an expression is allowed.
  149.          * @param expression A boolean expression
  150.          * @return A simple expression returning an equivalent value, e.g., INT 0 and 1
  151.          */
  152.         Expression booleanExpressionToSimpleExpression(Expression expression);
  153.        
  154.         /**
  155.          * TODO Use the Filter interface for this
  156.          * @param schema A schema name, or <code>null</code> for the connection's default schema
  157.          * @param table A table name
  158.          * @return <code>true</code> if this is a system table that doesn't contain user/application data
  159.          */
  160.         boolean isIgnoredTable(String schema, String table);
  161.        
  162.         /**
  163.          * Vendor-specific initialization for a database connection.
  164.          *
  165.          * @param connection
  166.          */
  167.         void initializeConnection(Connection connection) throws SQLException;
  168. }

Raw Paste


Login or Register to edit or fork this paste. It's free.