Example usage for java.sql Array getResultSet

List of usage examples for java.sql Array getResultSet

Introduction

In this page you can find the example usage for java.sql Array getResultSet.

Prototype

ResultSet getResultSet() throws SQLException;

Source Link

Document

Retrieves a result set that contains the elements of the SQL ARRAY value designated by this Array object.

Usage

From source file:org.mayocat.shop.catalog.store.jdbi.mapper.ProductMapper.java

@Override
public Product map(int index, ResultSet resultSet, StatementContext statementContext) throws SQLException {
    try {/*from www .j av a2s.  c om*/
        Product product = new Product((UUID) resultSet.getObject("id"));
        product.setTenantId((UUID) resultSet.getObject("tenant_id"));
        if (resultSet.getObject("parent_id") != null) {
            product.setParentId((UUID) resultSet.getObject("parent_id"));
        }
        product.setSlug(resultSet.getString("slug"));
        product.setTitle(resultSet.getString("title"));
        product.setDescription(resultSet.getString("description"));
        product.setCreationDate(resultSet.getTimestamp("creation_date"));
        if (resultSet.getObject("on_shelf") != null) {
            product.setOnShelf(resultSet.getBoolean("on_shelf"));
        }
        product.setPrice(resultSet.getBigDecimal("price"));

        if (!Strings.isNullOrEmpty(resultSet.getString("taxes"))) {
            ObjectMapper mapper = new ObjectMapper();
            Map<String, String> taxes = mapper.readValue(resultSet.getString("taxes"),
                    new TypeReference<Map<String, String>>() {
                    });
            if (taxes.containsKey("vat")) {
                product.setVatRateId(taxes.get("vat"));
            }
        }

        product.setWeight(resultSet.getBigDecimal("weight"));
        if (resultSet.getObject("stock") != null) {
            product.setStock(resultSet.getInt("stock"));
        }
        product.setVirtual(resultSet.getBoolean("virtual"));
        UUID featuredImageId = (UUID) resultSet.getObject("featured_image_id");
        if (featuredImageId != null) {
            product.setFeaturedImageId(featuredImageId);
        }

        if (MapperUtils.hasColumn("localization_data", resultSet)
                && !Strings.isNullOrEmpty(resultSet.getString("localization_data"))) {
            ObjectMapper mapper = new ObjectMapper();
            Map<Locale, Map<String, Object>> localizedVersions = Maps.newHashMap();
            Map[] data = mapper.readValue(resultSet.getString("localization_data"), Map[].class);
            for (Map map : data) {

                localizedVersions.put(Locale.forLanguageTag((String) map.get("locale")),
                        (Map) map.get("entity"));
            }
            product.setLocalizedVersions(localizedVersions);
        }

        String model = resultSet.getString("model");
        if (!Strings.isNullOrEmpty(model)) {
            product.setModel(model);
        }
        String type = resultSet.getString("product_type");
        if (!Strings.isNullOrEmpty(type)) {
            product.setType(type);
        }

        if (resultSet.getArray("features") != null) {
            // There's no support for getting the pg uuid array as a Java UUID array (or even String array) at the time
            // this is written, we have to iterate over the array own result set and construct the Java array ourselves
            List<UUID> ids = new ArrayList<>();
            Array array = resultSet.getArray("features");
            if (array != null) {
                ResultSet featuresResultSet = array.getResultSet();
                while (featuresResultSet.next()) {
                    ids.add((UUID) featuresResultSet.getObject("value"));
                }
                product.setFeatures(ids);
            }
        }

        return product;
    } catch (IOException e) {
        throw new SQLException("Failed to de-serialize JSON data", e);
    }
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public Table getTable(String dbName, String tableName) throws MetaException, NoSuchObjectException {
    boolean success = false;

    Connection con;//from  w  ww.j a  va  2s.c  om
    Statement ps = null;
    Table tbl = new Table();

    dbName = dbName.toLowerCase();
    tableName = tableName.toLowerCase();

    try {
        con = getSegmentConnectionForRead(dbName);
    } catch (MetaStoreConnectException e1) {
        LOG.error("get table error, db=" + dbName + ", tbl=" + tableName + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("get table error, db=" + dbName + ", tbl=" + tableName + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        ps = con.createStatement();

        String sql = "SELECT tbl_id, create_time"
                + ", is_compressed, retention, tbl_type, db_name, tbl_name, tbl_owner "
                + ", tbl_format, pri_part_type, sub_part_type, pri_part_key, sub_part_key "
                + ", input_format, output_format, serde_name, serde_lib, tbl_location, tbl_comment "
                + " from TBLS where db_name='" + dbName + "' and tbl_name='" + tableName + "'";

        ResultSet tblSet = ps.executeQuery(sql);
        boolean isTblFind = false;
        StorageDescriptor sd = null;
        SerDeInfo sdInfo = null;
        String priPartKey = null;
        String subPartKey = null;
        Partition priPart = null;
        Partition subPart = null;
        long tblID = 0;

        String comment = null;
        String format = null;
        Timestamp createTime = null;
        String tblType = null;

        boolean hasPriPart = false;
        boolean hasSubPart = false;

        while (tblSet.next()) {
            isTblFind = true;
            tblID = tblSet.getLong(1);

            createTime = tblSet.getTimestamp(2);

            if (createTime != null) {
                tbl.setCreateTime((int) (createTime.getTime() / 1000));
            }

            sd = new StorageDescriptor();
            sdInfo = new SerDeInfo();
            sd.setCompressed(tblSet.getBoolean(3));

            tbl.setRetention((int) tblSet.getLong(4));
            tblType = tblSet.getString(5);
            tbl.setTableType(tblType);
            tbl.setDbName(tblSet.getString(6));
            tbl.setTableName(tblSet.getString(7));
            tbl.setOwner(tblSet.getString(8));

            format = tblSet.getString(9);

            priPartKey = tblSet.getString(12);
            subPartKey = tblSet.getString(13);

            if (priPartKey != null && !priPartKey.isEmpty()) {
                hasPriPart = true;
                priPart = new Partition();
                priPart.setLevel(0);
                priPart.setDbName(tblSet.getString(6));
                priPart.setTableName(tblSet.getString(7));
                priPart.setParType(tblSet.getString(10));
            }

            if (subPartKey != null && !subPartKey.isEmpty()) {
                hasSubPart = true;
                subPart = new Partition();
                subPart.setLevel(1);
                subPart.setDbName(tblSet.getString(6));
                subPart.setTableName(tblSet.getString(7));
                subPart.setParType(tblSet.getString(11));
            }

            sd.setInputFormat(tblSet.getString(14));
            sd.setOutputFormat(tblSet.getString(15));
            sdInfo.setName(tblSet.getString(16));
            sdInfo.setSerializationLib(tblSet.getString(17));
            sd.setLocation(tblSet.getString(18));
            comment = tblSet.getString(19);

            break;
        }

        tblSet.close();

        if (!isTblFind) {
            LOG.error(dbName + "." + tableName + " table not found");
            throw new NoSuchObjectException(dbName + "." + tableName + " table not found");
        }

        List<FieldSchema> fieldList = new ArrayList<FieldSchema>();
        Map<String, FieldSchema> fieldMap = new LinkedHashMap<String, FieldSchema>();

        sql = "SELECT column_name, type_name, comment from columns where tbl_id=" + tblID
                + " order by column_index asc";
        ResultSet colSet = ps.executeQuery(sql);
        while (colSet.next()) {
            FieldSchema field = new FieldSchema();
            field.setName(colSet.getString(1));
            field.setType(colSet.getString(2));
            field.setComment(colSet.getString(3));

            fieldList.add(field);
            fieldMap.put(colSet.getString(1), field);
        }
        colSet.close();

        sd.setCols(fieldList);

        sql = "SELECT param_type, param_key, param_value  from table_params where tbl_id=" + tblID;
        ResultSet paramSet = ps.executeQuery(sql);
        Map<String, String> tblParamMap = new HashMap<String, String>();
        Map<String, String> sdParamMap = new HashMap<String, String>();
        Map<String, String> serdeParam = new HashMap<String, String>();

        while (paramSet.next()) {
            String type = paramSet.getString(1);
            if (type == null)
                continue;

            if (type.equalsIgnoreCase("sd")) {
                sdParamMap.put(paramSet.getString(2), paramSet.getString(3));
            } else if (type.equalsIgnoreCase("serde")) {
                serdeParam.put(paramSet.getString(2), paramSet.getString(3));
            } else if (type.equalsIgnoreCase("tbl")) {
                tblParamMap.put(paramSet.getString(2), paramSet.getString(3));
            } else {
                tblParamMap.put(paramSet.getString(2), paramSet.getString(3));
            }
        }
        paramSet.close();

        if (comment != null && !comment.isEmpty()) {
            tblParamMap.put("comment", comment);
        }

        if (format != null && !format.isEmpty()) {
            tblParamMap.put("type", format);
        }

        tbl.setParameters(tblParamMap);
        sd.setParameters(sdParamMap);
        sdInfo.setParameters(serdeParam);

        List<String> bucketCols = new ArrayList<String>();
        sql = "select bucket_col_name from bucket_cols where tbl_id=" + tblID + " order by col_index asc";
        ResultSet bucketSet = ps.executeQuery(sql);
        while (bucketSet.next()) {
            bucketCols.add(bucketSet.getString(1));
        }

        bucketSet.close();
        if (bucketCols.size() > 0) {
            sd.setBucketCols(bucketCols);
            String numBucketStr = sd.getParameters().get("NUM_BUCKETS");
            if (numBucketStr == null) {
                sd.setNumBuckets(-1);
            } else {
                sd.setNumBuckets(Integer.valueOf(numBucketStr));
            }
        } else {
            sd.setBucketCols(bucketCols);
            sd.setNumBuckets(-1);
        }

        sd.getParameters().remove("NUM_BUCKETS");

        List<Order> sortCols = new ArrayList<Order>();
        sql = "select sort_column_name, sort_order from sort_cols where tbl_id=" + tblID
                + " order by col_index asc";
        ResultSet sortSet = ps.executeQuery(sql);
        while (sortSet.next()) {
            Order o = new Order();
            o.setCol(sortSet.getString(1));
            o.setOrder(sortSet.getInt(2));
            sortCols.add(o);
        }

        sortSet.close();
        sd.setSortCols(sortCols);

        sd.setSerdeInfo(sdInfo);
        tbl.setSd(sd);

        if (hasPriPart) {
            sql = "SELECT level, part_name, part_values from  PARTITIONS where tbl_id=" + tblID;
            ResultSet partSet = ps.executeQuery(sql);
            Map<String, List<String>> priPartSpace = new LinkedHashMap<String, List<String>>();
            Map<String, List<String>> subPartSpace = new LinkedHashMap<String, List<String>>();

            while (partSet.next()) {
                int level = partSet.getInt(1);
                switch (level) {
                case 0:
                    String priName = partSet.getString(2);
                    List<String> priValueList = new ArrayList<String>();
                    Array priSpaceArray = partSet.getArray(3);

                    if (priSpaceArray != null) {
                        ResultSet priValueSet = priSpaceArray.getResultSet();

                        while (priValueSet.next()) {
                            priValueList.add(priValueSet.getString(2));
                        }
                    }

                    priPartSpace.put(priName, priValueList);
                    break;

                case 1:
                    String subName = partSet.getString(2);
                    List<String> subValueList = new ArrayList<String>();
                    Array subSpaceArray = partSet.getArray(3);

                    if (subSpaceArray != null) {
                        ResultSet subValueSet = subSpaceArray.getResultSet();
                        while (subValueSet.next()) {
                            subValueList.add(subValueSet.getString(2));
                        }
                    }

                    subPartSpace.put(subName, subValueList);
                    break;

                default:
                    break;
                }
            }

            partSet.close();

            priPart.setParSpaces(priPartSpace);

            priPart.setParKey(fieldMap.get(priPartKey.toLowerCase()));

            if (hasSubPart) {
                subPart.setParSpaces(subPartSpace);
                subPart.setParKey(fieldMap.get(subPartKey.toLowerCase()));
            }
        }

        tbl.setPriPartition(priPart);
        tbl.setSubPartition(subPart);

        if (tblType.equalsIgnoreCase("VIRTUAL_VIEW")) {
            sql = "select view_original_text, view_expanded_text, vtables from " + " tdwview where tbl_id="
                    + tblID;

            ResultSet viewSet = ps.executeQuery(sql);
            while (viewSet.next()) {
                tbl.setViewOriginalText(viewSet.getString(1));
                tbl.setViewExpandedText(viewSet.getString(2));
                tbl.setVtables(viewSet.getString(3));
                break;
            }
        }

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        sqlex.printStackTrace();
        LOG.error("get table error, db=" + dbName + ", tbl=" + tableName + ", msg=" + sqlex.getMessage());
        throw new MetaException(sqlex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }

    if (success)
        return tbl;
    else
        return null;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

@Override
public Partition getPartition(String dbName, String tableName, int level) throws MetaException {
    boolean success = false;

    Connection con = null;/*from   w w  w .  j  a  va 2s.  co m*/
    Statement ps = null;
    Partition part = null;

    dbName = dbName.toLowerCase();
    tableName = tableName.toLowerCase();

    Map<String, List<String>> partNameMap = new LinkedHashMap<String, List<String>>();

    try {
        con = getSegmentConnection(dbName);
    } catch (MetaStoreConnectException e1) {
        LOG.error("get partition error, db=" + dbName + ", tbl=" + tableName + ", level=" + level + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("get partition error, db=" + dbName + ", tbl=" + tableName + ", level=" + level + ", msg="
                + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
        ps = con.createStatement();

        long tblID = 0;
        boolean isTblFind = false;
        String priPartType = null;
        String subPartType = null;
        boolean hasPriPart = false;
        boolean hasSubPart = false;
        String priPartKey = null;
        String subPartKey = null;
        String partKey = null;

        String sql = "SELECT tbl_id,  pri_part_type, pri_part_key, sub_part_type, sub_part_key from TBLS where db_name='"
                + dbName + "' and tbl_name='" + tableName + "'";

        ResultSet tblSet = ps.executeQuery(sql);
        while (tblSet.next()) {
            isTblFind = true;
            tblID = tblSet.getLong(1);
            priPartType = tblSet.getString(2);
            priPartKey = tblSet.getString(3);
            subPartType = tblSet.getString(4);
            subPartKey = tblSet.getString(5);

            if (priPartType != null && !priPartType.isEmpty()) {
                hasPriPart = true;
            }
            if (subPartType != null && !subPartType.isEmpty()) {
                hasSubPart = true;
            }

            if (hasPriPart && level == 0) {
                part = new Partition();
                part.setParType(priPartType);
                partKey = priPartKey;
                break;
            }

            if (hasSubPart && level == 1) {
                part = new Partition();
                part.setParType(subPartType);
                partKey = subPartKey;
                break;
            }

            con.commit();
            return null;
        }

        tblSet.close();

        if (!isTblFind) {
            throw new MetaException("can not find table " + dbName + ":" + tableName);
        }

        FieldSchema field = null;
        sql = "select type_name, comment from columns where tbl_id=" + tblID + " and column_name='" + partKey
                + "'";
        ResultSet colSet = ps.executeQuery(sql);
        while (colSet.next()) {
            field = new FieldSchema();
            field.setType(colSet.getString(1));
            field.setComment(colSet.getString(2));
            field.setName(partKey);

            break;
        }

        colSet.close();

        sql = "select part_name, part_values from partitions where tbl_id=" + tblID + " and level=" + level;
        ResultSet partSet = ps.executeQuery(sql);

        while (partSet.next()) {
            String partName = partSet.getString(1);
            List<String> valueList = new ArrayList<String>();
            Array spaceArray = partSet.getArray(2);

            ResultSet priValueSet = spaceArray.getResultSet();

            if (priValueSet != null) {
                while (priValueSet.next()) {
                    valueList.add(priValueSet.getString(2));
                }
            }

            partNameMap.put(partName, valueList);
        }
        partSet.close();

        part.setParSpaces(partNameMap);
        part.setDbName(dbName);
        part.setTableName(tableName);
        part.setLevel(level);
        part.setParKey(field);

        con.commit();
        success = true;
    } catch (SQLException sqlex) {
        sqlex.printStackTrace();
        LOG.error("get partition error, db=" + dbName + ", tbl=" + tableName + ", level=" + level + ", msg="
                + sqlex.getMessage());
        throw new MetaException(sqlex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }

    if (success)
        return part;
    else
        return null;
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

public void addPartition(String dbName, String tblName, AddPartitionDesc addPartitionDesc)
        throws InvalidObjectException, MetaException {
    boolean success = false;

    Connection con = null;/*from w w  w.j a  v a  2  s .co  m*/
    PreparedStatement ps = null;
    Statement stmt = null;
    dbName = dbName.toLowerCase();
    tblName = tblName.toLowerCase();

    boolean isPathMaked = false;
    ArrayList<Path> pathToMake = new ArrayList<Path>();
    Warehouse wh = new Warehouse(hiveConf);

    long tblID = 0;

    try {
        con = getSegmentConnection(dbName);
    } catch (MetaStoreConnectException e1) {
        LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                + addPartitionDesc.getLevel() + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    } catch (SQLException e1) {
        LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                + addPartitionDesc.getLevel() + ", msg=" + e1.getMessage());
        throw new MetaException(e1.getMessage());
    }

    try {
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        stmt = con.createStatement();

        String tblType = null;
        boolean hasPriPart = false;
        boolean hasSubPart = false;
        String priPartKey = null;
        String subPartKey = null;
        String priPartType = null;
        String subPartType = null;

        String priKeyType = null;
        String subKeyType = null;
        ResultSet tblSet = null;
        boolean isTblFind = false;
        boolean isColFind = false;

        String tblFormat = null;
        String tblLocation = null;

        PrimitiveTypeInfo pti = null;
        ObjectInspector StringIO = null;
        ObjectInspector ValueIO = null;
        ObjectInspectorConverters.Converter converter1 = null;
        ObjectInspectorConverters.Converter converter2 = null;

        ArrayList<String> partToAdd = new ArrayList<String>();
        String sql = null;

        HiveConf hconf = (HiveConf) hiveConf;
        boolean externalPartition = hconf.getBoolVar(HiveConf.ConfVars.HIVESUPPORTEXTERNALPARTITION);

        if (addPartitionDesc.getLevel() == 0) {
            sql = "SELECT tbl_id, tbl_type, pri_part_type, pri_part_key, tbl_format, tbl_location"
                    + " from TBLS where db_name='" + dbName + "' and tbl_name='" + tblName + "'";

            tblSet = stmt.executeQuery(sql);
            isTblFind = false;

            while (tblSet.next()) {
                isTblFind = true;
                tblID = tblSet.getLong(1);
                tblType = tblSet.getString(2);
                priPartKey = tblSet.getString(4);
                priPartType = tblSet.getString(3);
                tblFormat = tblSet.getString(5);
                tblLocation = tblSet.getString(6);

                if (priPartType != null && !priPartType.isEmpty()) {
                    hasPriPart = true;
                }
                break;
            }
            tblSet.close();

            if (!isTblFind) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "can not find table " + dbName + ":"
                        + tblName);

                throw new MetaException("can not find table " + dbName + ":" + tblName);
            }

            if (!tblType.equalsIgnoreCase("MANAGED_TABLE")) {
                if (tblType.equalsIgnoreCase("EXTERNAL_TABLE") && tblFormat != null
                        && tblFormat.equalsIgnoreCase("pgdata")) {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg=" + tblType + ":" + tblFormat
                            + " can not support alter partition");
                    throw new MetaException(tblType + ":" + tblFormat + " can not support alter partition");
                }

                if (externalPartition && tblType.equalsIgnoreCase("EXTERNAL_TABLE")
                        && (tblFormat == null || !tblFormat.equalsIgnoreCase("pgdata"))) {
                } else {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg=" + tblType
                            + " can not support alter partition");

                    throw new MetaException(tblType + " can not support alter partition");
                }
            }

            if (!hasPriPart) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "table " + dbName + ":" + tblName
                        + " is not pri-partitioned");

                throw new MetaException("table " + dbName + ":" + tblName + " is not pri-partitioned");
            }

            sql = "SELECT type_name from COLUMNS where tbl_id=" + tblID + " and column_name='"
                    + priPartKey.toLowerCase() + "'";
            isColFind = false;
            ResultSet colSet = stmt.executeQuery(sql);
            while (colSet.next()) {
                isColFind = true;
                priKeyType = colSet.getString(1);
                break;
            }
            colSet.close();

            if (!isColFind) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "table "
                        + "can not find partition key information " + priPartKey);

                throw new MetaException("can not find partition key information " + priPartKey);
            }

            pti = new PrimitiveTypeInfo();
            pti.setTypeName(priKeyType);
            StringIO = PrimitiveObjectInspectorFactory
                    .getPrimitiveJavaObjectInspector(PrimitiveCategory.STRING);
            ValueIO = PrimitiveObjectInspectorFactory
                    .getPrimitiveWritableObjectInspector(pti.getPrimitiveCategory());
            converter1 = ObjectInspectorConverters.getConverter(StringIO, ValueIO);
            converter2 = ObjectInspectorConverters.getConverter(StringIO, ValueIO);

            if ((addPartitionDesc.getPartType().equalsIgnoreCase("RANGE_PARTITION")
                    && !priPartType.equalsIgnoreCase("range"))
                    || (addPartitionDesc.getPartType().equalsIgnoreCase("LIST_PARTITION")
                            && !priPartType.equalsIgnoreCase("list"))) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "can not add  a "
                        + addPartitionDesc.getPartType() + " partition, but the pri-partition type is "
                        + priPartType);

                throw new MetaException("can not add  a " + addPartitionDesc.getPartType()
                        + " partition, but the pri-partition type is " + priPartType);
            }

            LinkedHashMap<String, List<String>> partSpaces = new LinkedHashMap<String, List<String>>();
            Set<String> subPartNameSet = new TreeSet<String>();

            sql = "SELECT level, part_name, part_values from PARTITIONS where" + " tbl_id=" + tblID;// + " order by level asc";

            ResultSet partSet = stmt.executeQuery(sql);
            int partLevel = 0;

            while (partSet.next()) {
                partLevel = partSet.getInt(1);

                if (partLevel == 0) {
                    String partName = partSet.getString(2);
                    List<String> valueList = new ArrayList<String>();
                    Array spaceArray = partSet.getArray(3);

                    ResultSet priValueSet = spaceArray.getResultSet();

                    while (priValueSet.next()) {
                        valueList.add(priValueSet.getString(2));
                    }

                    partSpaces.put(partName, valueList);
                } else if (partLevel == 1) {
                    String partName = partSet.getString(2);
                    subPartNameSet.add(partName);
                }
            }
            partSet.close();

            partToAdd = new ArrayList<String>();

            LinkedHashMap<String, List<String>> addPartSpaces = (LinkedHashMap<String, List<String>>) addPartitionDesc
                    .getParSpaces();

            Iterator<String> itr = addPartSpaces.keySet().iterator();

            while (itr.hasNext()) {
                String key = itr.next().toLowerCase();
                if (partSpaces.containsKey(key)) {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg=" + "table : " + tblName
                            + " have already contain a pri parititon named: " + key);

                    throw new MetaException(
                            "table : " + tblName + " have already contain a pri parititon named: " + key);
                }
                partToAdd.add(key);
            }

            Iterator<List<String>> listItr = addPartSpaces.values().iterator();

            while (listItr.hasNext()) {
                Iterator<String> valueItr = listItr.next().iterator();
                if (valueItr.hasNext()) {
                    String value = valueItr.next();

                    if (converter1.convert(value) == null) {
                        LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                                + addPartitionDesc.getLevel() + ", msg=" + "value : " + value
                                + " should be type of " + priKeyType);

                        throw new MetaException("value : " + value + " should be type of " + priKeyType);
                    }

                    Iterator<List<String>> PartValuesItr = partSpaces.values().iterator();
                    while (PartValuesItr.hasNext()) {
                        if (PartValuesItr.next().contains(value)) {
                            LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                                    + addPartitionDesc.getLevel() + ", msg=" + "table : " + tblName
                                    + " have already contain a pri partition contain value: " + value);

                            throw new MetaException("table : " + tblName
                                    + " have already contain a pri partition contain value: " + value);
                        }
                    }
                }
            }

            ps = con.prepareStatement(
                    "INSERT INTO partitions(level, tbl_id, " + " part_name, part_values) values(?,?,?,?)");

            for (Map.Entry<String, List<String>> entry : addPartSpaces.entrySet()) {
                ps.setInt(1, 0);
                ps.setLong(2, tblID);

                Array spaceArray = con.createArrayOf("varchar", entry.getValue().toArray());
                ps.setArray(4, spaceArray);
                ps.setString(3, entry.getKey());

                ps.addBatch();
            }
            ps.executeBatch();

            if (!tblType.equalsIgnoreCase("EXTERNAL_TABLE")) {
                for (String partName : partToAdd) {
                    if (tblLocation == null || tblLocation.trim().isEmpty()) {
                        pathToMake.addAll(wh.getPriPartitionPaths(dbName, tblName, partName, subPartNameSet));
                    } else {
                        pathToMake.addAll(Warehouse.getPriPartitionPaths(new Path(tblLocation), partName,
                                subPartNameSet));
                    }
                }
            } else {
                for (String partName : partToAdd) {
                    pathToMake.addAll(
                            Warehouse.getPriPartitionPaths(new Path(tblLocation), partName, subPartNameSet));
                }
            }
        } else if (addPartitionDesc.getLevel() == 1) {
            sql = "SELECT tbl_id, tbl_type, sub_part_type, sub_part_key, tbl_format, tbl_location"
                    + " from TBLS where db_name='" + dbName.toLowerCase() + "' and tbl_name='"
                    + tblName.toLowerCase() + "'";

            tblSet = stmt.executeQuery(sql);
            isTblFind = false;

            while (tblSet.next()) {
                isTblFind = true;
                tblID = tblSet.getLong(1);
                tblType = tblSet.getString(2);
                subPartKey = tblSet.getString(4);
                subPartType = tblSet.getString(3);
                tblFormat = tblSet.getString(5);
                tblLocation = tblSet.getString(6);

                if (subPartType != null && !subPartType.isEmpty()) {
                    hasSubPart = true;
                }

                break;
            }

            tblSet.close();
            if (!isTblFind) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "can not find table " + dbName + ":"
                        + tblName);

                throw new MetaException("can not find table " + dbName + ":" + tblName);
            }

            if (!tblType.equalsIgnoreCase("MANAGED_TABLE")) {
                if (tblType.equalsIgnoreCase("EXTERNAL_TABLE") && tblFormat != null
                        && tblFormat.equalsIgnoreCase("pgdata")) {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg=" + tblType + ":" + tblFormat
                            + " can not support alter partition");
                    throw new MetaException(tblType + ":" + tblFormat + " can not support alter partition");
                }

                if (externalPartition && tblType.equalsIgnoreCase("EXTERNAL_TABLE")
                        && (tblFormat == null || !tblFormat.equalsIgnoreCase("pgdata"))) {
                } else {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg=" + tblType
                            + " can not support alter partition");

                    throw new MetaException(tblType + " can not support alter partition");
                }
            }

            if (!hasSubPart) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "table " + dbName + ":" + tblName
                        + " is not sun-partitioned");

                throw new MetaException("table " + dbName + ":" + tblName + " is not sun-partitioned");
            }

            sql = "SELECT type_name from COLUMNS where tbl_id=" + tblID + " and column_name='"
                    + subPartKey.toLowerCase() + "'";

            isColFind = false;
            ResultSet colSet = stmt.executeQuery(sql);
            while (colSet.next()) {
                isColFind = true;
                subKeyType = colSet.getString(1);
                break;
            }

            colSet.close();

            if (!isColFind) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "can not find partition key information "
                        + priPartKey);

                throw new MetaException("can not find partition key information " + priPartKey);
            }

            pti = new PrimitiveTypeInfo();
            pti.setTypeName(subKeyType);
            StringIO = PrimitiveObjectInspectorFactory
                    .getPrimitiveJavaObjectInspector(PrimitiveCategory.STRING);
            ValueIO = PrimitiveObjectInspectorFactory
                    .getPrimitiveWritableObjectInspector(pti.getPrimitiveCategory());
            converter1 = ObjectInspectorConverters.getConverter(StringIO, ValueIO);
            converter2 = ObjectInspectorConverters.getConverter(StringIO, ValueIO);

            if ((addPartitionDesc.getPartType().equalsIgnoreCase("RANGE_PARTITION")
                    && !subPartType.equalsIgnoreCase("range"))
                    || (addPartitionDesc.getPartType().equalsIgnoreCase("LIST_PARTITION")
                            && !subPartType.equalsIgnoreCase("list"))) {
                LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                        + addPartitionDesc.getLevel() + ", msg=" + "you can not add  a "
                        + addPartitionDesc.getPartType() + " partition, but the sub-partition type is "
                        + subPartType);

                throw new MetaException("you can not add  a " + addPartitionDesc.getPartType()
                        + " partition, but the sub-partition type is " + subPartType);
            }

            LinkedHashMap<String, List<String>> partSpaces = new LinkedHashMap<String, List<String>>();
            Set<String> partNameSet = new TreeSet<String>();

            sql = "SELECT level,  part_name, part_values from PARTITIONS where" + " tbl_id=" + tblID;// + " order by level asc";

            ResultSet partSet = stmt.executeQuery(sql);
            int partLevel = 0;

            while (partSet.next()) {
                partLevel = partSet.getInt(1);

                if (partLevel == 1) {
                    String partName = partSet.getString(2);
                    List<String> valueList = new ArrayList<String>();
                    Array spaceArray = partSet.getArray(3);

                    ResultSet priValueSet = spaceArray.getResultSet();

                    while (priValueSet.next()) {
                        valueList.add(priValueSet.getString(2));
                    }
                    partSpaces.put(partName, valueList);
                } else if (partLevel == 0) {
                    String partName = partSet.getString(2);
                    partNameSet.add(partName);
                }
            }

            partToAdd = new ArrayList<String>();

            LinkedHashMap<String, List<String>> addPartSpaces = (LinkedHashMap<String, List<String>>) addPartitionDesc
                    .getParSpaces();

            Iterator<String> itr = addPartSpaces.keySet().iterator();

            while (itr.hasNext()) {
                String key = itr.next().toLowerCase();
                if (partSpaces.containsKey(key)) {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg=" + "table : " + tblName
                            + " have already contain a sub parititon named: " + key);

                    throw new MetaException(
                            "table : " + tblName + " have already contain a sub parititon named: " + key);
                }

                if (key.equalsIgnoreCase("default")) {
                    LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                            + addPartitionDesc.getLevel() + ", msg="
                            + "use : 'alter table tblname add default subpartition' to add default subpartition!");

                    throw new MetaException(
                            "use : 'alter table tblname add default subpartition' to add default subpartition!");
                }
                partToAdd.add(key);
            }

            Iterator<List<String>> listItr = addPartSpaces.values().iterator();

            while (listItr.hasNext()) {
                Iterator<String> valueItr = listItr.next().iterator();
                if (valueItr.hasNext()) {
                    String value = valueItr.next();

                    if (converter1.convert(value) == null) {
                        LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                                + addPartitionDesc.getLevel() + ", msg=" + "value : " + value
                                + " should be type of " + priKeyType);

                        throw new MetaException("value : " + value + " should be type of " + priKeyType);
                    }

                    Iterator<List<String>> PartValuesItr = partSpaces.values().iterator();
                    while (PartValuesItr.hasNext()) {
                        if (PartValuesItr.next().contains(value)) {
                            LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                                    + addPartitionDesc.getLevel() + ", msg=" + "table : " + tblName
                                    + " have already contain a sub partition contain value: " + value);

                            throw new MetaException("table : " + tblName
                                    + " have already contain a sub partition contain value: " + value);
                        }
                    }
                }
            }

            ps = con.prepareStatement(
                    "INSERT INTO partitions(level, tbl_id, " + " part_name, part_values) values(?,?,?,?)");

            for (Map.Entry<String, List<String>> entry : addPartSpaces.entrySet()) {
                ps.setInt(1, 1);
                ps.setLong(2, tblID);

                Array spaceArray = con.createArrayOf("varchar", entry.getValue().toArray());
                ps.setArray(4, spaceArray);
                ps.setString(3, entry.getKey());

                ps.addBatch();
            }
            ps.executeBatch();

            if (!tblType.equalsIgnoreCase("EXTERNAL_TABLE")) {
                for (String partName : partToAdd) {
                    if (tblLocation == null || tblLocation.trim().isEmpty()) {
                        pathToMake.addAll(wh.getSubPartitionPaths(dbName, tblName, partNameSet, partName));
                    } else {
                        pathToMake.addAll(
                                Warehouse.getSubPartitionPaths(new Path(tblLocation), partNameSet, partName));
                    }
                }
            } else {
                for (String partName : partToAdd) {
                    pathToMake.addAll(
                            Warehouse.getSubPartitionPaths(new Path(tblLocation), partNameSet, partName));
                }
            }
        }

        con.commit();
        success = true;
    } catch (SQLException ex) {
        ex.printStackTrace();
        LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level="
                + addPartitionDesc.getLevel() + ", msg=" + ex.getMessage());

        throw new MetaException(ex.getMessage());
    } finally {
        if (!success) {
            try {
                con.rollback();
            } catch (SQLException e) {
            }

            if (isPathMaked) {
                for (Path path : pathToMake) {
                    wh.deleteDir(path, false);
                }
            }
        }

        closeStatement(ps);
        closeConnection(con);
    }

    if (success) {
        boolean mkDirOK = false;
        List<Path> createdPath = new ArrayList<Path>();
        try {
            for (Path path : pathToMake) {
                mkDirOK = wh.mkdirs(path);
                if (!mkDirOK) {
                    break;
                }

                createdPath.add(path);
            }
        } catch (Exception x) {
            mkDirOK = false;
        }

        if (!mkDirOK) {
            dropPartitionMeta(dbName, tblID, addPartitionDesc);
            if (!createdPath.isEmpty()) {
                for (Path path : createdPath) {
                    wh.deleteDir(path, true);
                }
            }

            throw new MetaException("can not create hdfs path, add partition failed");
        }

    }
}

From source file:org.wso2.carbon.dataservices.core.DBUtils.java

/**
 * Processes a particular SQL Array object and interprets its value as a ParamValue object.
 *
 * @param sqlArray   SQL Array element.//from w ww.j a v  a2s. c  o  m
 * @param paramValue Parameter value object initialized to contain an array of ParamValues.
 * @return ParamValue object representing the SQL Array.
 * @throws SQLException Throws an SQL Exception if the result set is not accessible.
 */
public static ParamValue processSQLArray(Array sqlArray, ParamValue paramValue) throws SQLException {
    ResultSet rs = sqlArray.getResultSet();
    while (rs.next()) {
        Object arrayEl = rs.getObject(2);
        if (arrayEl instanceof Struct) {
            paramValue.getArrayValue().add(new ParamValue((Struct) arrayEl));
        } else if (arrayEl instanceof Array) {
            paramValue.getArrayValue()
                    .add(processSQLArray((Array) arrayEl, new ParamValue(ParamValue.PARAM_VALUE_ARRAY)));
        } else {
            paramValue.getArrayValue().add(new ParamValue(String.valueOf(arrayEl)));
        }
    }
    rs.close();
    return paramValue;
}

From source file:org.wso2.carbon.dataservices.core.description.query.SQLQuery.java

/**
 * Processes a SQL Array instance and transform it into a ParamValue
 * instance/* w  w  w.ja  v  a 2s .  c om*/
 * 
 * @param dataArray
 *            SQLArray instance
 * @param paramValue
 *            Container into which the SQLArray elements should be populated
 * @return ParamValue instance containing all the elements of the
 *         corresponding SQLArray instance
 * @throws SQLException
 *             When it fails to processes the result set produced by the
 *             SQLArray instance
 */
private ParamValue processSQLArray(Array dataArray, ParamValue paramValue) throws SQLException {
    ResultSet rs = null;
    try {
        rs = dataArray.getResultSet();
        while (rs.next()) {
            Object arrayEl = rs.getObject(2);
            if (arrayEl instanceof Struct) {
                paramValue.getArrayValue().add(new ParamValue((Struct) arrayEl));
            } else if (arrayEl instanceof Array) {
                paramValue.getArrayValue()
                        .add(processSQLArray((Array) arrayEl, new ParamValue(ParamValue.PARAM_VALUE_ARRAY)));
            } else {
                paramValue.getArrayValue().add(new ParamValue(String.valueOf(arrayEl)));
            }
        }
        return paramValue;
    } finally {
        this.releaseResources(rs, null);
    }
}