IGNITE-3937 implemented support for unsigned types in MySQL.

Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/0d5ee788
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/0d5ee788
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/0d5ee788

Branch: refs/heads/master
Commit: 0d5ee7887be03295133704227133d619898b6abf
Parents: f4cd72b
Author: Alexey Kuznetsov <akuznet...@apache.org>
Authored: Thu Sep 22 14:36:50 2016 +0700
Committer: Alexey Kuznetsov <akuznet...@apache.org>
Committed: Thu Sep 22 14:36:50 2016 +0700

----------------------------------------------------------------------
 .../apache/ignite/schema/parser/DbColumn.java   | 16 +++++++++++--
 .../parser/dialect/DatabaseMetadataDialect.java |  9 ++++++++
 .../parser/dialect/JdbcMetadataDialect.java     | 23 +++++++++++--------
 .../parser/dialect/MySQLMetadataDialect.java    | 24 ++++++++++++++++++--
 .../parser/dialect/OracleMetadataDialect.java   |  5 ++--
 .../ignite/schema/model/PojoDescriptor.java     | 24 +++++++++++++-------
 6 files changed, 77 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/0d5ee788/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/DbColumn.java
----------------------------------------------------------------------
diff --git 
a/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/DbColumn.java
 
b/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/DbColumn.java
index 8b0c813..10fd50f 100644
--- 
a/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/DbColumn.java
+++ 
b/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/DbColumn.java
@@ -33,17 +33,22 @@ public class DbColumn {
     /** Is {@code NULL} allowed for column in database. */
     private final boolean nullable;
 
+    /** Whether column unsigned. */
+    private final boolean unsigned;
+
     /**
      * @param name Column name.
      * @param type Column JDBC type.
      * @param key {@code true} if this column belongs to primary key.
      * @param nullable {@code true} if {@code NULL } allowed for column in 
database.
+     * @param unsigned {@code true} if column is unsigned.
      */
-    public DbColumn(String name, int type, boolean key, boolean nullable) {
+    public DbColumn(String name, int type, boolean key, boolean nullable, 
boolean unsigned) {
         this.name = name;
         this.type = type;
         this.key = key;
         this.nullable = nullable;
+        this.unsigned = unsigned;
     }
 
     /**
@@ -68,9 +73,16 @@ public class DbColumn {
     }
 
     /**
-     * @return nullable {@code true} if {@code NULL } allowed for column in 
database.
+     * @return {@code true} if {@code NULL } allowed for column in database.
      */
     public boolean nullable() {
         return nullable;
     }
+
+    /**
+     * @return {@code true} if column is unsigned.
+     */
+    public boolean unsigned() {
+        return unsigned;
+    }
 }

http://git-wip-us.apache.org/repos/asf/ignite/blob/0d5ee788/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/dialect/DatabaseMetadataDialect.java
----------------------------------------------------------------------
diff --git 
a/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/dialect/DatabaseMetadataDialect.java
 
b/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/dialect/DatabaseMetadataDialect.java
index 5ef088a..56ee59b 100644
--- 
a/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/dialect/DatabaseMetadataDialect.java
+++ 
b/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/dialect/DatabaseMetadataDialect.java
@@ -18,6 +18,7 @@
 package org.apache.ignite.schema.parser.dialect;
 
 import java.sql.Connection;
+import java.sql.DatabaseMetaData;
 import java.sql.SQLException;
 import java.util.Collection;
 import java.util.Collections;
@@ -63,6 +64,14 @@ public abstract class DatabaseMetadataDialect {
     }
 
     /**
+     * @return Collection of unsigned type names.
+     * @throws SQLException If failed to get unsigned type names.
+     */
+    public Set<String> unsignedTypes(DatabaseMetaData dbMeta) throws 
SQLException {
+        return Collections.emptySet();
+    }
+
+    /**
      * Create table descriptor.
      *
      * @param schema Schema name.

http://git-wip-us.apache.org/repos/asf/ignite/blob/0d5ee788/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/dialect/JdbcMetadataDialect.java
----------------------------------------------------------------------
diff --git 
a/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/dialect/JdbcMetadataDialect.java
 
b/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/dialect/JdbcMetadataDialect.java
index 235c84c..f6c27f2 100644
--- 
a/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/dialect/JdbcMetadataDialect.java
+++ 
b/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/dialect/JdbcMetadataDialect.java
@@ -30,7 +30,6 @@ import java.util.Map;
 import java.util.Set;
 
 import org.apache.ignite.cache.QueryIndex;
-import org.apache.ignite.cache.QueryIndexType;
 import org.apache.ignite.schema.parser.DbColumn;
 import org.apache.ignite.schema.parser.DbTable;
 
@@ -62,6 +61,9 @@ public class JdbcMetadataDialect extends 
DatabaseMetadataDialect {
     /** Column data type index. */
     private static final int COL_DATA_TYPE_IDX = 5;
 
+    /** Column type name index. */
+    private static final int COL_TYPE_NAME_IDX = 6;
+
     /** Column nullable index. */
     private static final int COL_NULLABLE_IDX = 11;
 
@@ -116,11 +118,11 @@ public class JdbcMetadataDialect extends 
DatabaseMetadataDialect {
 
         Set<String> sys = systemSchemas();
 
-        Collection<DbTable> tbls = new ArrayList<>();
-
         if (schemas.isEmpty())
             schemas.add(null);
 
+        Collection<DbTable> tbls = new ArrayList<>();
+
         for (String toSchema: schemas) {
             try (ResultSet tblsRs = dbMeta.getTables(useCatalog() ? toSchema : 
null, useSchema() ? toSchema : null, "%",
                     tblsOnly ? TABLES_ONLY : TABLES_AND_VIEWS)) {
@@ -136,24 +138,27 @@ public class JdbcMetadataDialect extends 
DatabaseMetadataDialect {
                     if (sys.contains(schema))
                         continue;
 
-                    Set<String> pkCols = new HashSet<>();
+                    Collection<String> pkCols = new HashSet<>();
 
                     try (ResultSet pkRs = dbMeta.getPrimaryKeys(tblCatalog, 
tblSchema, tblName)) {
                         while (pkRs.next())
                             pkCols.add(pkRs.getString(PK_COL_NAME_IDX));
                     }
 
-                    List<DbColumn> cols = new ArrayList<>();
+                    Collection<DbColumn> cols = new ArrayList<>();
+
+                    Collection<String> unsignedTypes = unsignedTypes(dbMeta);
 
                     try (ResultSet colsRs = dbMeta.getColumns(tblCatalog, 
tblSchema, tblName, null)) {
                         while (colsRs.next()) {
                             String colName = colsRs.getString(COL_NAME_IDX);
 
                             cols.add(new DbColumn(
-                                    colName,
-                                    colsRs.getInt(COL_DATA_TYPE_IDX),
-                                    pkCols.contains(colName),
-                                    colsRs.getInt(COL_NULLABLE_IDX) == 
DatabaseMetaData.columnNullable));
+                                colName,
+                                colsRs.getInt(COL_DATA_TYPE_IDX),
+                                pkCols.contains(colName),
+                                colsRs.getInt(COL_NULLABLE_IDX) == 
DatabaseMetaData.columnNullable,
+                                
unsignedTypes.contains(colsRs.getString(COL_TYPE_NAME_IDX))));
                         }
                     }
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/0d5ee788/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/dialect/MySQLMetadataDialect.java
----------------------------------------------------------------------
diff --git 
a/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/dialect/MySQLMetadataDialect.java
 
b/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/dialect/MySQLMetadataDialect.java
index 3332046..7bd6f31 100644
--- 
a/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/dialect/MySQLMetadataDialect.java
+++ 
b/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/dialect/MySQLMetadataDialect.java
@@ -18,20 +18,24 @@
 package org.apache.ignite.schema.parser.dialect;
 
 import java.sql.Connection;
+import java.sql.DatabaseMetaData;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.List;
+import java.util.HashSet;
 import java.util.Set;
 
 /**
  * MySQL specific metadata dialect.
  */
 public class MySQLMetadataDialect extends JdbcMetadataDialect {
+    /** Type name index. */
+    private static final int TYPE_NAME_IDX = 1;
+
     /** {@inheritDoc} */
     @Override public Collection<String> schemas(Connection conn) throws 
SQLException {
-        List<String> schemas = new ArrayList<>();
+        Collection<String> schemas = new ArrayList<>();
 
         ResultSet rs = conn.getMetaData().getCatalogs();
 
@@ -59,4 +63,20 @@ public class MySQLMetadataDialect extends 
JdbcMetadataDialect {
     @Override protected boolean useSchema() {
         return false;
     }
+
+    /** {@inheritDoc} */
+    @Override public Set<String> unsignedTypes(DatabaseMetaData dbMeta) throws 
SQLException {
+        Set<String> unsignedTypes = new HashSet<>();
+
+        try (ResultSet typeRs = dbMeta.getTypeInfo()) {
+            while (typeRs.next()) {
+                String typeName = typeRs.getString(TYPE_NAME_IDX);
+
+                if (typeName.contains("UNSIGNED"))
+                    unsignedTypes.add(typeName);
+            }
+        }
+
+        return unsignedTypes;
+    }
 }

http://git-wip-us.apache.org/repos/asf/ignite/blob/0d5ee788/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/dialect/OracleMetadataDialect.java
----------------------------------------------------------------------
diff --git 
a/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/dialect/OracleMetadataDialect.java
 
b/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/dialect/OracleMetadataDialect.java
index 3535655..47fb05c 100644
--- 
a/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/dialect/OracleMetadataDialect.java
+++ 
b/modules/schema-import-db/src/main/java/org/apache/ignite/schema/parser/dialect/OracleMetadataDialect.java
@@ -258,8 +258,7 @@ public class OracleMetadataDialect extends 
DatabaseMetadataDialect {
      * @return Indexes.
      * @throws SQLException If failed to retrieve indexes columns.
      */
-    private Collection<QueryIndex> indexes(PreparedStatement stmt, String 
owner, String tbl)
-        throws SQLException {
+    private Collection<QueryIndex> indexes(PreparedStatement stmt, String 
owner, String tbl) throws SQLException {
         Map<String, QueryIndex> idxs = new LinkedHashMap<>();
 
         stmt.setString(1, owner);
@@ -347,7 +346,7 @@ public class OracleMetadataDialect extends 
DatabaseMetadataDialect {
                         String colName = colsRs.getString(COL_NAME_IDX);
 
                         cols.add(new DbColumn(colName, decodeType(colsRs), 
pkCols.contains(colName),
-                                !"N".equals(colsRs.getString(NULLABLE_IDX))));
+                            !"N".equals(colsRs.getString(NULLABLE_IDX)), 
false));
                     }
 
                     if (!cols.isEmpty())

http://git-wip-us.apache.org/repos/asf/ignite/blob/0d5ee788/modules/schema-import/src/main/java/org/apache/ignite/schema/model/PojoDescriptor.java
----------------------------------------------------------------------
diff --git 
a/modules/schema-import/src/main/java/org/apache/ignite/schema/model/PojoDescriptor.java
 
b/modules/schema-import/src/main/java/org/apache/ignite/schema/model/PojoDescriptor.java
index 5053b07..4f696d6 100644
--- 
a/modules/schema-import/src/main/java/org/apache/ignite/schema/model/PojoDescriptor.java
+++ 
b/modules/schema-import/src/main/java/org/apache/ignite/schema/model/PojoDescriptor.java
@@ -123,7 +123,7 @@ public class PojoDescriptor {
             String colName = col.name();
 
             PojoField fld = new PojoField(colName, col.type(),
-                toJavaFieldName(colName), toJavaType(col.type(), 
col.nullable()).getName(),
+                toJavaFieldName(colName), toJavaType(col).getName(),
                 col.key(), col.nullable());
 
             fld.owner(this);
@@ -427,24 +427,32 @@ public class PojoDescriptor {
     /**
      * Convert JDBC data type to java type.
      *
-     * @param type JDBC SQL data type.
-     * @param nullable {@code true} if {@code NULL} is allowed for this field 
in database.
+     * @param col Database column descriptor.
      * @return Java data type.
      */
-    private static Class<?> toJavaType(int type, boolean nullable) {
-        switch (type) {
+    private static Class<?> toJavaType(DbColumn col) {
+        boolean nullable = col.nullable();
+        boolean unsigned = col.unsigned();
+
+        switch (col.type()) {
             case BIT:
             case BOOLEAN:
                 return nullable ? Boolean.class : boolean.class;
 
             case TINYINT:
-                return nullable ? Byte.class : byte.class;
+                return unsigned
+                    ? (nullable ? Short.class : short.class)
+                    : (nullable ? Byte.class : byte.class);
 
             case SMALLINT:
-                return nullable ? Short.class : short.class;
+                return unsigned
+                    ? (nullable ? Integer.class : int.class)
+                    : (nullable ? Short.class : short.class);
 
             case INTEGER:
-                return nullable ? Integer.class : int.class;
+                return unsigned
+                    ? (nullable ? Long.class : long.class)
+                    : (nullable ? Integer.class : int.class);
 
             case BIGINT:
                 return nullable ? Long.class : long.class;

Reply via email to