package leap.db.platform.dm;

import leap.db.DbLimitQuery;
import leap.db.platform.GenericDbDialect;
import leap.lang.value.Limit;

import java.sql.Types;

public class DM7Dialect extends GenericDbDialect {

    @Override
    protected String getTestDriverSupportsGetParameterTypeSQL() {
        return "select 1";
    }

    @Override
    protected String getOpenQuoteString() {
        return "\"";
    }

    @Override
    protected String getCloseQuoteString() {
        return "\"";
    }

    @Override
    protected void registerColumnTypes() {
        columnTypes.add(Types.BOOLEAN,       "BIT");
        columnTypes.add(Types.BIT,           "BIT");

        columnTypes.add(Types.TINYINT,       "TINYINT");
        columnTypes.add(Types.SMALLINT,      "SMALLINT");
        columnTypes.add(Types.INTEGER,       "INTEGER");
        columnTypes.add(Types.BIGINT,        "BIGINT"  );

        //JDBC's real type mapping to java's float, JDBC's float type mapping to java's double
        columnTypes.add(Types.REAL,          "REAL");
        columnTypes.add(Types.FLOAT,         "FLOAT");
        columnTypes.add(Types.DOUBLE,        "DOUBLE");

        columnTypes.add(Types.DECIMAL,       "DECIMAL($p,$s)");
        columnTypes.add(Types.NUMERIC,       "NUMERIC($p,$s)");

        columnTypes.add(Types.CHAR,          "CHAR($l)",0,32767);
        columnTypes.add(Types.VARCHAR,       "VARCHAR($l)",0,8188);
        columnTypes.add(Types.VARCHAR,       "CLOB");
        columnTypes.add(Types.LONGVARCHAR,   "LONGVARCHAR");

        columnTypes.add(Types.BINARY,        "BINARY($l)",1,32767);
        columnTypes.add(Types.BINARY,        "BLOB");
        columnTypes.add(Types.VARBINARY,     "VARBINARY($l)",1,8188);
        columnTypes.add(Types.VARBINARY,     "BLOB");
        columnTypes.add(Types.LONGVARBINARY, "BLOB");

        columnTypes.add(Types.DATE,          "DATE");
        columnTypes.add(Types.TIME,          "TIME");
        columnTypes.add(Types.TIMESTAMP,     "TIMESTAMP");

        columnTypes.add(Types.BLOB,          "BLOB");
        columnTypes.add(Types.CLOB,          "CLOB");
    }

    @Override
    public String getLimitQuerySql(DbLimitQuery query) {
        Limit limit = query.getLimit();

        int offset = limit.getStart() - 1;
        int rows   = limit.getEnd()   - offset;

        String sql = query.getSql(db) + " limit ?,?";
        query.getArgs().add(offset);
        query.getArgs().add(rows);

        return sql;
    }
}

@@ -124,17 +124,16 @@
 .Limit;%0A
-%0A
 import j
@@ -146,16 +146,41 @@
 l.Types;
+%0Aimport java.util.Arrays;
 %0A%0Apublic
@@ -225,16 +225,93 @@
 lect %7B%0A%0A
+    private static final String%5B%5D SQL_KEY_WORDS = new String%5B%5D%7B%22VERSIONS%22%7D;%0A%0A
     @Ove
@@ -553,32 +553,32 @@
 QuoteString() %7B%0A
-
         return %22
@@ -581,32 +581,191 @@
 rn %22%5C%22%22;%0A    %7D%0A%0A
+    @Override%0A    protected void registerSQLKeyWords() %7B%0A        super.registerSQLKeyWords();%0A        sqlKeyWords.addAll(Arrays.asList(SQL_KEY_WORDS));%0A    %7D%0A%0A
     @Override%0A  

add sql key words of dm