List of usage examples for java.sql PreparedStatement setArray
void setArray(int parameterIndex, Array x) throws SQLException;
java.sql.Array
object. From source file:org.apache.cocoon.util.JDBCTypeConversions.java
/** * Set the Statement column so that the results are mapped correctly. * * @param statement the prepared statement * @param position the position of the column * @param value the value of the column// w ww . j a v a 2s . c o m */ public static void setColumn(PreparedStatement statement, int position, Object value, Integer typeObject) throws Exception { if (value instanceof String) { value = ((String) value).trim(); } if (typeObject == null) { throw new SQLException("Can't set column because the type is unrecognized"); } if (value == null) { /** If the value is null, set the column value null and return **/ statement.setNull(position, typeObject.intValue()); return; } if ("".equals(value)) { switch (typeObject.intValue()) { case Types.CHAR: case Types.CLOB: case Types.VARCHAR: /** If the value is an empty string and the column is a string type, we can continue **/ break; default: /** If the value is an empty string and the column is something else, we treat it as a null value **/ statement.setNull(position, typeObject.intValue()); return; } } File file = null; int length = -1; InputStream asciiStream = null; //System.out.println("========================================================================"); //System.out.println("JDBCTypeConversions: setting type "+typeObject.intValue()); switch (typeObject.intValue()) { case Types.CLOB: //System.out.println("CLOB"); Clob clob = null; if (value instanceof Clob) { clob = (Clob) value; } else if (value instanceof File) { File asciiFile = (File) value; asciiStream = new BufferedInputStream(new FileInputStream(asciiFile)); length = (int) asciiFile.length(); clob = new ClobHelper(asciiStream, length); } else if (value instanceof Part) { Part anyFile = (Part) value; asciiStream = new BufferedInputStream(anyFile.getInputStream()); length = anyFile.getSize(); clob = new ClobHelper(asciiStream, length); } else if (value instanceof JDBCxlobHelper) { asciiStream = ((JDBCxlobHelper) value).inputStream; length = ((JDBCxlobHelper) value).length; clob = new ClobHelper(asciiStream, length); } else if (value instanceof Source) { asciiStream = ((Source) value).getInputStream(); length = (int) ((Source) value).getContentLength(); clob = new ClobHelper(asciiStream, length); } else { String asciiText = value.toString(); asciiStream = new ByteArrayInputStream(asciiText.getBytes()); length = asciiText.length(); clob = new ClobHelper(asciiStream, length); } statement.setClob(position, clob); break; case Types.CHAR: // simple large object, e.g. Informix's TEXT //System.out.println("CHAR"); if (value instanceof File) { File asciiFile = (File) value; asciiStream = new BufferedInputStream(new FileInputStream(asciiFile)); length = (int) asciiFile.length(); } else if (value instanceof JDBCxlobHelper) { asciiStream = ((JDBCxlobHelper) value).inputStream; length = ((JDBCxlobHelper) value).length; } else if (value instanceof Source) { asciiStream = ((Source) value).getInputStream(); length = (int) ((Source) value).getContentLength(); } else if (value instanceof Part) { Part anyFile = (Part) value; asciiStream = new BufferedInputStream(anyFile.getInputStream()); length = anyFile.getSize(); clob = new ClobHelper(asciiStream, length); } else { String asciiText = value.toString(); asciiStream = new BufferedInputStream(new ByteArrayInputStream(asciiText.getBytes())); length = asciiText.length(); } statement.setAsciiStream(position, asciiStream, length); break; case Types.BIGINT: //System.out.println("BIGINT"); BigDecimal bd = null; if (value instanceof BigDecimal) { bd = (BigDecimal) value; } else if (value instanceof Number) { bd = BigDecimal.valueOf(((Number) value).longValue()); } else { bd = new BigDecimal(value.toString()); } statement.setBigDecimal(position, bd); break; case Types.TINYINT: //System.out.println("TINYINT"); Byte b = null; if (value instanceof Byte) { b = (Byte) value; } else if (value instanceof Number) { b = new Byte(((Number) value).byteValue()); } else { b = new Byte(value.toString()); } statement.setByte(position, b.byteValue()); break; case Types.DATE: //System.out.println("DATE"); Date d = null; if (value instanceof Date) { d = (Date) value; } else if (value instanceof java.util.Date) { d = new Date(((java.util.Date) value).getTime()); } else if (value instanceof Calendar) { d = new Date(((Calendar) value).getTime().getTime()); } else { d = Date.valueOf(value.toString()); } statement.setDate(position, d); break; case Types.DOUBLE: //System.out.println("DOUBLE"); double db; if (value instanceof Number) { db = (((Number) value).doubleValue()); } else { db = Double.parseDouble(value.toString()); } statement.setDouble(position, db); break; case Types.FLOAT: //System.out.println("FLOAT"); float f; if (value instanceof Number) { f = (((Number) value).floatValue()); } else { f = Float.parseFloat(value.toString()); } statement.setFloat(position, f); break; case Types.NUMERIC: //System.out.println("NUMERIC"); long l; if (value instanceof Number) { l = (((Number) value).longValue()); } else { l = Long.parseLong(value.toString()); } statement.setLong(position, l); break; case Types.SMALLINT: //System.out.println("SMALLINT"); Short s = null; if (value instanceof Short) { s = (Short) value; } else if (value instanceof Number) { s = new Short(((Number) value).shortValue()); } else { s = new Short(value.toString()); } statement.setShort(position, s.shortValue()); break; case Types.TIME: //System.out.println("TIME"); Time t = null; if (value instanceof Time) { t = (Time) value; } else if (value instanceof java.util.Date) { t = new Time(((java.util.Date) value).getTime()); } else { t = Time.valueOf(value.toString()); } statement.setTime(position, t); break; case Types.TIMESTAMP: //System.out.println("TIMESTAMP"); Timestamp ts = null; if (value instanceof Time) { ts = (Timestamp) value; } else if (value instanceof java.util.Date) { ts = new Timestamp(((java.util.Date) value).getTime()); } else { ts = Timestamp.valueOf(value.toString()); } statement.setTimestamp(position, ts); break; case Types.ARRAY: //System.out.println("ARRAY"); statement.setArray(position, (Array) value); // no way to convert string to array break; case Types.STRUCT: //System.out.println("STRUCT"); case Types.OTHER: //System.out.println("OTHER"); statement.setObject(position, value); break; case Types.LONGVARBINARY: //System.out.println("LONGVARBINARY"); statement.setTimestamp(position, new Timestamp((new java.util.Date()).getTime())); break; case Types.VARCHAR: //System.out.println("VARCHAR"); statement.setString(position, value.toString()); break; case Types.BLOB: //System.out.println("BLOB"); if (value instanceof JDBCxlobHelper) { statement.setBinaryStream(position, ((JDBCxlobHelper) value).inputStream, ((JDBCxlobHelper) value).length); } else if (value instanceof Source) { statement.setBinaryStream(position, ((Source) value).getInputStream(), (int) ((Source) value).getContentLength()); } else { Blob blob = null; if (value instanceof Blob) { blob = (Blob) value; } else if (value instanceof File) { file = (File) value; blob = new BlobHelper(new FileInputStream(file), (int) file.length()); } else if (value instanceof String) { file = new File((String) value); blob = new BlobHelper(new FileInputStream(file), (int) file.length()); } else if (value instanceof Part) { Part anyFile = (Part) value; blob = new BlobHelper(new BufferedInputStream(anyFile.getInputStream()), anyFile.getSize()); } else { throw new SQLException("Invalid type for blob: " + value.getClass().getName()); } //InputStream input = new BufferedInputStream(new FileInputStream(file)); statement.setBlob(position, blob); } break; case Types.VARBINARY: //System.out.println("VARBINARY"); if (value instanceof JDBCxlobHelper) { statement.setBinaryStream(position, ((JDBCxlobHelper) value).inputStream, ((JDBCxlobHelper) value).length); } else if (value instanceof Source) { statement.setBinaryStream(position, ((Source) value).getInputStream(), (int) ((Source) value).getContentLength()); } else if (value instanceof Part) { statement.setBinaryStream(position, ((Part) value).getInputStream(), ((Part) value).getSize()); } else { if (value instanceof File) { file = (File) value; } else if (value instanceof String) { file = new File((String) value); } else { throw new SQLException("Invalid type for blob: " + value.getClass().getName()); } //InputStream input = new BufferedInputStream(new FileInputStream(file)); FileInputStream input = new FileInputStream(file); statement.setBinaryStream(position, input, (int) file.length()); } break; case Types.INTEGER: //System.out.println("INTEGER"); Integer i = null; if (value instanceof Integer) { i = (Integer) value; } else if (value instanceof Number) { i = new Integer(((Number) value).intValue()); } else { i = new Integer(value.toString()); } statement.setInt(position, i.intValue()); break; case Types.BIT: //System.out.println("BIT"); Boolean bo = null; if (value instanceof Boolean) { bo = (Boolean) value; } else if (value instanceof Number) { bo = BooleanUtils.toBooleanObject(((Number) value).intValue() == 1); } else { bo = BooleanUtils.toBooleanObject(value.toString()); } statement.setBoolean(position, bo.booleanValue()); break; default: //System.out.println("default"); throw new SQLException("Impossible exception - invalid type "); } //System.out.println("========================================================================"); }
From source file:org.apache.hadoop.hive.metastore.MyXid.java
@Override public void createTable(Table tbl) throws InvalidObjectException, MetaException, AlreadyExistsException { if (tbl == null) { throw new InvalidObjectException("unvalid parameters, tbl is null"); }//from w ww . j a va2 s . c o m if (tbl.getTableType() == null) { tbl.setTableType("MANAGED_TABLE"); } if (tbl.getTableType().equalsIgnoreCase("VIRTUAL_VIEW")) { jdbcCreateView(tbl); return; } tbl.setDbName(tbl.getDbName().toLowerCase()); tbl.setTableName(tbl.getTableName().toLowerCase()); LOG.debug("first, check the name is valid or not"); if (!MetaStoreUtils.validateName(tbl.getTableName()) || !MetaStoreUtils.validateColNames(tbl.getSd().getCols()) || (tbl.getPriPartition() != null && !MetaStoreUtils.validateName(tbl.getPriPartition().getParKey().getName())) || (tbl.getSubPartition() != null && !MetaStoreUtils.validateName(tbl.getSubPartition().getParKey().getName()))) { throw new InvalidObjectException(tbl.getTableName() + " is not a valid object name"); } long tblID = genTblID(tbl.getDbName(), tbl.getTableName()); boolean success = false; Connection con; PreparedStatement ps = null; Statement stmt = null; Path tblPath = null; Warehouse wh = new Warehouse(hiveConf); boolean madeDir = false; LOG.debug("2, generate table path "); if (tbl.getSd().getLocation() == null || tbl.getSd().getLocation().isEmpty()) { tblPath = wh.getDefaultTablePath(tbl.getDbName(), tbl.getTableName()); } else { if (tbl.getTableType().equalsIgnoreCase("EXTERNAL_TABLE")) { LOG.warn("Location: " + tbl.getSd().getLocation() + "specified for non-external table:" + tbl.getTableName()); } tblPath = wh.getDnsPath(new Path(tbl.getSd().getLocation())); } tbl.getSd().setLocation(tblPath.toString()); try { con = getSegmentConnection(tbl.getDbName()); } catch (MetaStoreConnectException e1) { LOG.error("create table error, db=" + tbl.getDbName() + ", table=" + tbl.getTableName() + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } catch (SQLException e1) { LOG.error("create table error, db=" + tbl.getDbName() + ", table=" + tbl.getTableName() + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } try { con.setAutoCommit(false); con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); stmt = con.createStatement(); LOG.debug("1 check the table is exist or not"); String sql = "select tbl_id from tbls where db_name='" + tbl.getDbName().toLowerCase() + "' and tbl_name='" + tbl.getTableName().toLowerCase() + "'"; boolean isTblFind = false; ResultSet checkTblSet = stmt.executeQuery(sql); while (checkTblSet.next()) { isTblFind = true; break; } checkTblSet.close(); if (isTblFind) { throw new AlreadyExistsException( "table " + tbl.getDbName() + ":" + tbl.getTableName() + " has exist"); } LOG.debug("2 insert into tbls"); ps = con.prepareStatement("INSERT INTO TBLS(tbl_id, 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)" + " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); StorageDescriptor sd = tbl.getSd(); if (sd == null || sd.getSerdeInfo() == null) { throw new MetaException("storage descriptor of table " + tbl.getTableName() + " is null"); } SerDeInfo sdInfo = sd.getSerdeInfo(); ps.setLong(1, tblID); ps.setBoolean(2, sd.isCompressed()); ps.setLong(3, tbl.getRetention()); if (tbl.getParameters() != null && tbl.getParameters().get("EXTERNAL") != null && tbl.getParameters().get("EXTERNAL").equalsIgnoreCase("TRUE")) { ps.setString(4, "EXTERNAL_TABLE"); } else { ps.setString(4, tbl.getTableType()); } ps.setString(5, tbl.getDbName()); ps.setString(6, tbl.getTableName()); ps.setString(7, tbl.getOwner()); if (tbl.getParameters() == null) { ps.setString(8, null); } else { ps.setString(8, tbl.getParameters().get("type")); } Partition priPart = tbl.getPriPartition(); Partition subPart = tbl.getSubPartition(); if (priPart != null) { ps.setString(11, priPart.getParKey().getName()); ps.setString(9, priPart.getParType()); } else { ps.setString(11, null); ps.setString(9, null); } if (subPart != null) { ps.setString(12, subPart.getParKey().getName()); ps.setString(10, subPart.getParType()); } else { ps.setString(12, null); ps.setString(10, null); } ps.setString(13, sd.getInputFormat()); ps.setString(14, sd.getOutputFormat()); ps.setString(15, sdInfo.getName()); ps.setString(16, sdInfo.getSerializationLib()); ps.setString(17, sd.getLocation()); if (tbl.getParameters() == null) { ps.setString(18, null); } else { ps.setString(18, tbl.getParameters().get("comment")); } ps.executeUpdate(); ps.close(); LOG.debug("3 insert into partitions"); if (priPart != null) { ps = con.prepareStatement( "INSERT INTO PARTITIONS(level, tbl_id," + "part_name, part_values) values(?,?,?,?)"); Map<String, List<String>> partSpaceMap = priPart.getParSpaces(); for (Map.Entry<String, List<String>> entry : partSpaceMap.entrySet()) { ps.setInt(1, 0); ps.setLong(2, tblID); ps.setString(3, entry.getKey()); if (entry.getValue() != null) { Array spaceArray = con.createArrayOf("varchar", entry.getValue().toArray()); ps.setArray(4, spaceArray); } else { ps.setArray(4, null); } ps.addBatch(); } ps.executeBatch(); ps.close(); } if (subPart != null) { ps = con.prepareStatement( "INSERT INTO PARTITIONS(level, tbl_id," + "part_name, part_values) values(?,?,?,?)"); Map<String, List<String>> partSpaceMap = subPart.getParSpaces(); for (Map.Entry<String, List<String>> entry : partSpaceMap.entrySet()) { ps.setInt(1, 1); ps.setLong(2, tblID); ps.setString(3, entry.getKey()); if (entry.getValue() != null) { Array spaceArray = con.createArrayOf("varchar", entry.getValue().toArray()); ps.setArray(4, spaceArray); } else { ps.setArray(4, null); } ps.addBatch(); } ps.executeBatch(); ps.close(); } LOG.debug("4 insert into columns"); ps = con.prepareStatement("INSERT INTO COLUMNS(column_index, tbl_id, column_name, type_name, comment) " + " values(?,?,?,?,?)"); List<FieldSchema> fieldList = sd.getCols(); int fieldSize = fieldList.size(); for (int i = 0; i < fieldSize; i++) { FieldSchema field = fieldList.get(i); ps.setInt(1, i); ps.setLong(2, tblID); ps.setString(3, field.getName().toLowerCase()); ps.setString(4, field.getType()); ps.setString(5, field.getComment()); ps.addBatch(); } ps.executeBatch(); ps.close(); LOG.debug("5 insert into parameters"); boolean createExtDirIfNotExist = true; if (tbl.getParametersSize() > 0) { String createExtDirIfNotExistStr = tbl.getParameters().get("hive.exttable.createdir.ifnotexist"); LOG.info("XXcreateExtDirIfNotExistStr=" + createExtDirIfNotExistStr); if (createExtDirIfNotExistStr != null && createExtDirIfNotExistStr.equalsIgnoreCase("false")) { createExtDirIfNotExist = false; } tbl.getParameters().remove("hive.exttable.createdir.ifnotexist"); } if (tbl.getParametersSize() > 0 || sd.getParametersSize() > 0 || sd.getSerdeInfo().getParametersSize() > 0 || sd.getNumBuckets() > -1) { ps = con.prepareStatement("insert into table_params(tbl_id, param_type, param_key, param_value) " + " values(?,?,?,?)"); if (tbl.getParametersSize() > 0) { for (Map.Entry<String, String> entry : tbl.getParameters().entrySet()) { if (entry.getKey().equalsIgnoreCase("type") || entry.getKey().equalsIgnoreCase("comment")) continue; ps.setLong(1, tblID); ps.setString(2, "TBL"); ps.setString(3, entry.getKey()); ps.setString(4, entry.getValue()); ps.addBatch(); } } if (sd.getParametersSize() > 0) { for (Map.Entry<String, String> entry : sd.getParameters().entrySet()) { ps.setLong(1, tblID); ps.setString(2, "SD"); ps.setString(3, entry.getKey()); ps.setString(4, entry.getValue()); ps.addBatch(); } } if (sd.getSerdeInfo().getParametersSize() > 0) { for (Map.Entry<String, String> entry : sd.getSerdeInfo().getParameters().entrySet()) { ps.setLong(1, tblID); ps.setString(2, "SERDE"); ps.setString(3, entry.getKey()); ps.setString(4, entry.getValue()); ps.addBatch(); } } if (sd.getNumBuckets() > -1) { ps.setLong(1, tblID); ps.setString(2, "SD"); ps.setString(3, "NUM_BUCKETS"); ps.setString(4, String.valueOf(sd.getNumBuckets())); ps.addBatch(); } ps.executeBatch(); ps.close(); } if (tbl.getSd().getBucketCols() != null && !tbl.getSd().getBucketCols().isEmpty()) { ps = con.prepareStatement( "insert into bucket_cols(tbl_id, bucket_col_name, col_index) values(?,?,?)"); int index = 0; for (String col : tbl.getSd().getBucketCols()) { ps.setLong(1, tblID); ps.setString(2, col.toLowerCase()); ps.setInt(3, index); index++; ps.addBatch(); } ps.executeBatch(); ps.close(); } if (tbl.getSd().getSortCols() != null && !tbl.getSd().getSortCols().isEmpty()) { ps = con.prepareStatement( "insert into sort_cols(tbl_id, sort_column_name, sort_order, col_index) values(?,?,?,?)"); int index = 0; for (Order o : tbl.getSd().getSortCols()) { ps.setLong(1, tblID); ps.setString(2, o.getCol()); ps.setInt(3, o.getOrder()); ps.setInt(4, index); index++; ps.addBatch(); } ps.executeBatch(); ps.close(); } LOG.debug("make hdfs directory for table"); if (createExtDirIfNotExist && tblPath != null) { if (!wh.isDir(tblPath)) { if (!wh.mkdirs(tblPath)) { throw new MetaException(tblPath + " is not a directory or unable to create one"); } madeDir = true; } if (tbl.getPriPartition() != null) { Set<String> priPartNames = tbl.getPriPartition().getParSpaces().keySet(); Set<String> subPartNames = null; if (tbl.getSubPartition() != null) { subPartNames = tbl.getSubPartition().getParSpaces().keySet(); } List<Path> partPaths = Warehouse.getPartitionPaths(tblPath, priPartNames, subPartNames); for (Path partPath : partPaths) { if (!wh.mkdirs(partPath)) { throw new MetaException( "Partition path " + partPath + " is not a directory or unable to create one."); } } } } con.commit(); success = true; } catch (SQLException sqlex) { LOG.error("create table error db=" + tbl.getDbName() + ", table=" + tbl.getTableName() + ",msg=" + sqlex.getMessage()); sqlex.printStackTrace(); throw new MetaException(sqlex.getMessage()); } finally { if (!success) { try { con.rollback(); } catch (SQLException e) { } if (madeDir) { wh.deleteDir(tblPath, true); } } closeStatement(stmt); closeStatement(ps); closeConnection(con); } return; }
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 ww w. j a v a 2 s .c o 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.apache.hadoop.hive.metastore.MyXid.java
@Override public void addDefaultPartition(String dbName, String tblName, int level) throws InvalidObjectException, MetaException { boolean success = false; Connection con = null;//from w ww . ja va 2 s . com Statement ps = null; PreparedStatement pss = 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 default partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } catch (SQLException e1) { LOG.error("add default partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level + ", msg=" + e1.getMessage()); throw new MetaException(e1.getMessage()); } try { con.setAutoCommit(false); con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); ps = con.createStatement(); String tblType = null; boolean hasPriPart = false; boolean hasSubPart = false; String priPartKey = null; String subPartKey = null; String priPartType = null; String subPartType = null; String tblFormat = null; String tblLocation = null; String priKeyType = null; String subKeyType = null; ResultSet tblSet = null; boolean isTblFind = false; ArrayList<String> partToAdd = new ArrayList<String>(); String sql = null; HiveConf hconf = (HiveConf) hiveConf; boolean externalPartition = hconf.getBoolVar(HiveConf.ConfVars.HIVESUPPORTEXTERNALPARTITION); if (level == 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 = ps.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=" + level + ", 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 default partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level + ", 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 default partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level + ", 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=" + level + ", msg=" + "table " + dbName + ":" + tblName + " is not pri-partitioned"); throw new MetaException("table " + dbName + ":" + tblName + " is not pri-partitioned"); } List<String> partNames = new ArrayList<String>(); Set<String> subPartNameSet = new TreeSet<String>(); sql = "SELECT level, part_name from PARTITIONS where" + " tbl_id=" + tblID + " order by level asc"; ResultSet partSet = ps.executeQuery(sql); int partLevel = 0; while (partSet.next()) { partLevel = partSet.getInt(1); if (partLevel == 0) { String partName = partSet.getString(2); partNames.add(partName); } else if (partLevel == 1) { String partName = partSet.getString(2); subPartNameSet.add(partName); } } partSet.close(); if (partNames.contains("default")) { LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level + ", msg=" + "table : " + tblName + " have already contain a pri parititon named: default"); throw new MetaException( "table : " + tblName + " have already contain a pri parititon named: default"); } pss = con.prepareStatement( "INSERT INTO partitions(level, tbl_id, " + " part_name, part_values) values(?,?,?,?)"); pss.setInt(1, 0); pss.setLong(2, tblID); pss.setString(3, "default"); Array spaceArray = con.createArrayOf("varchar", new ArrayList<String>().toArray()); pss.setArray(4, spaceArray); pss.executeUpdate(); if (!tblType.equalsIgnoreCase("EXTERNAL_TABLE")) { if (tblLocation == null || tblLocation.trim().isEmpty()) { pathToMake.addAll(wh.getPriPartitionPaths(dbName, tblName, "default", subPartNameSet)); } else { pathToMake.addAll( Warehouse.getPriPartitionPaths(new Path(tblLocation), "default", subPartNameSet)); } } else { pathToMake.addAll( Warehouse.getPriPartitionPaths(new Path(tblLocation), "default", subPartNameSet)); } } else if (level == 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 = ps.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 default partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level + ", 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 default partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level + ", 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 default partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level + ", msg=" + tblType + " can not support alter partition"); throw new MetaException(tblType + " can not support alter partition"); } } if (!hasSubPart) { LOG.error("add default partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level + ", msg=" + "table " + dbName + ":" + tblName + " is not sun-partitioned"); throw new MetaException("table " + dbName + ":" + tblName + " is not sun-partitioned"); } List<String> partNames = new ArrayList<String>(); Set<String> partNameSet = new TreeSet<String>(); sql = "SELECT level, part_name from PARTITIONS where" + " tbl_id=" + tblID + " order by level asc"; ResultSet partSet = ps.executeQuery(sql); int partLevel = 0; while (partSet.next()) { partLevel = partSet.getInt(1); if (partLevel == 1) { String partName = partSet.getString(2); partNames.add(partName); } else if (partLevel == 0) { String partName = partSet.getString(2); partNameSet.add(partName); } } partSet.close(); if (partNames.contains("default")) { LOG.error("add default partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level + ", msg=" + "table : " + tblName + " have already contain a sub parititon named: default"); throw new MetaException( "table : " + tblName + " have already contain a sub parititon named: default"); } pss = con.prepareStatement( "INSERT INTO partitions(level, tbl_id, " + " part_name, part_values) values(?,?,?,?)"); pss.setInt(1, 1); pss.setLong(2, tblID); pss.setString(3, "default"); Array spaceArray = con.createArrayOf("varchar", new ArrayList<String>().toArray()); pss.setArray(4, spaceArray); pss.executeUpdate(); if (!tblType.equalsIgnoreCase("EXTERNAL_TABLE")) { if (tblLocation == null || tblLocation.trim().isEmpty()) { pathToMake.addAll(wh.getSubPartitionPaths(dbName, tblName, partNameSet, "default")); } else { pathToMake.addAll( Warehouse.getSubPartitionPaths(new Path(tblLocation), partNameSet, "default")); } } else { pathToMake .addAll(Warehouse.getSubPartitionPaths(new Path(tblLocation), partNameSet, "default")); } } con.commit(); success = true; } catch (SQLException ex) { LOG.error("add partition error, db=" + dbName + ", tbl=" + tblName + ", level=" + level + ", 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); closeStatement(pss); 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, "default", level); if (!createdPath.isEmpty()) { for (Path path : createdPath) { wh.deleteDir(path, true); } } } } }
From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java
/** * Set the given value as a parameter to the statement. *//*ww w.j av a 2 s. c om*/ public void setArray(PreparedStatement stmnt, int idx, Array val, Column col) throws SQLException { stmnt.setArray(idx, val); }
From source file:org.apache.phoenix.schema.stats.BaseStatsCollectorIT.java
@Test public void testUpdateStats() throws Exception { Connection conn;//from w ww . j a va2 s .c o m PreparedStatement stmt; ResultSet rs; Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); conn = getConnection(); conn.createStatement().execute("CREATE TABLE " + fullTableName + " ( k VARCHAR, a_string_array VARCHAR(100) ARRAY[4], b_string_array VARCHAR(100) ARRAY[4] \n" + " CONSTRAINT pk PRIMARY KEY (k, b_string_array DESC))" + tableDDLOptions); String[] s; Array array; conn = upsertValues(props, fullTableName); collectStatistics(conn, fullTableName); rs = conn.createStatement().executeQuery("EXPLAIN SELECT k FROM " + fullTableName); rs.next(); long rows1 = (Long) rs.getObject(PhoenixRuntime.EXPLAIN_PLAN_ESTIMATED_ROWS_READ_COLUMN); stmt = upsertStmt(conn, fullTableName); stmt.setString(1, "z"); s = new String[] { "xyz", "def", "ghi", "jkll", null, null, "xxx" }; array = conn.createArrayOf("VARCHAR", s); stmt.setArray(2, array); s = new String[] { "zya", "def", "ghi", "jkll", null, null, null, "xxx" }; array = conn.createArrayOf("VARCHAR", s); stmt.setArray(3, array); stmt.execute(); conn.commit(); collectStatistics(conn, fullTableName); rs = conn.createStatement().executeQuery("EXPLAIN SELECT k FROM " + fullTableName); rs.next(); long rows2 = (Long) rs.getObject(PhoenixRuntime.EXPLAIN_PLAN_ESTIMATED_ROWS_READ_COLUMN); assertNotEquals(rows1, rows2); conn.close(); }
From source file:org.apache.phoenix.schema.stats.BaseStatsCollectorIT.java
private Connection upsertValues(Properties props, String tableName) throws SQLException, IOException, InterruptedException { Connection conn;/* w w w . ja v a2 s.c o m*/ PreparedStatement stmt; conn = getConnection(); stmt = upsertStmt(conn, tableName); stmt.setString(1, "a"); String[] s = new String[] { "abc", "def", "ghi", "jkll", null, null, "xxx" }; Array array = conn.createArrayOf("VARCHAR", s); stmt.setArray(2, array); s = new String[] { "abc", "def", "ghi", "jkll", null, null, null, "xxx" }; array = conn.createArrayOf("VARCHAR", s); stmt.setArray(3, array); stmt.execute(); conn.commit(); stmt = upsertStmt(conn, tableName); stmt.setString(1, "b"); s = new String[] { "xyz", "def", "ghi", "jkll", null, null, "xxx" }; array = conn.createArrayOf("VARCHAR", s); stmt.setArray(2, array); s = new String[] { "zya", "def", "ghi", "jkll", null, null, null, "xxx" }; array = conn.createArrayOf("VARCHAR", s); stmt.setArray(3, array); stmt.execute(); conn.commit(); stmt = upsertStmt(conn, tableName); stmt.setString(1, "c"); s = new String[] { "xyz", "def", "ghi", "jkll", null, null, "xxx" }; array = conn.createArrayOf("VARCHAR", s); stmt.setArray(2, array); s = new String[] { "zya", "def", "ghi", "jkll", null, null, null, "xxx" }; array = conn.createArrayOf("VARCHAR", s); stmt.setArray(3, array); stmt.execute(); conn.commit(); stmt = upsertStmt(conn, tableName); stmt.setString(1, "d"); s = new String[] { "xyz", "def", "ghi", "jkll", null, null, "xxx" }; array = conn.createArrayOf("VARCHAR", s); stmt.setArray(2, array); s = new String[] { "zya", "def", "ghi", "jkll", null, null, null, "xxx" }; array = conn.createArrayOf("VARCHAR", s); stmt.setArray(3, array); stmt.execute(); conn.commit(); stmt = upsertStmt(conn, tableName); stmt.setString(1, "b"); s = new String[] { "xyz", "def", "ghi", "jkll", null, null, "xxx" }; array = conn.createArrayOf("VARCHAR", s); stmt.setArray(2, array); s = new String[] { "zya", "def", "ghi", "jkll", null, null, null, "xxx" }; array = conn.createArrayOf("VARCHAR", s); stmt.setArray(3, array); stmt.execute(); conn.commit(); stmt = upsertStmt(conn, tableName); stmt.setString(1, "e"); s = new String[] { "xyz", "def", "ghi", "jkll", null, null, "xxx" }; array = conn.createArrayOf("VARCHAR", s); stmt.setArray(2, array); s = new String[] { "zya", "def", "ghi", "jkll", null, null, null, "xxx" }; array = conn.createArrayOf("VARCHAR", s); stmt.setArray(3, array); stmt.execute(); conn.commit(); return conn; }
From source file:org.apache.phoenix.schema.stats.NoOpStatsCollectorIT.java
private void upsertValues(Connection conn, String tableName) throws SQLException { PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + tableName + " VALUES(?,?,?)"); stmt.setString(1, "a"); String[] s = new String[] { "abc", "def", "ghi", "jkll", null, null, "xxx" }; Array array = conn.createArrayOf("VARCHAR", s); stmt.setArray(2, array); s = new String[] { "abc", "def", "ghi", "jkll", null, null, null, "xxx" }; array = conn.createArrayOf("VARCHAR", s); stmt.setArray(3, array);//from w w w . j ava2s. c o m stmt.execute(); conn.commit(); }
From source file:org.eclipse.ecr.core.storage.sql.jdbc.dialect.DialectPostgreSQL.java
@Override public void setToPreparedStatement(PreparedStatement ps, int index, Serializable value, Column column) throws SQLException { switch (column.getJdbcType()) { case Types.VARCHAR: case Types.CLOB: setToPreparedStatementString(ps, index, value, column); return;//from ww w. j a v a 2s .co m case Types.BIT: ps.setBoolean(index, ((Boolean) value).booleanValue()); return; case Types.SMALLINT: ps.setInt(index, ((Long) value).intValue()); return; case Types.INTEGER: case Types.BIGINT: ps.setLong(index, ((Long) value).longValue()); return; case Types.DOUBLE: ps.setDouble(index, ((Double) value).doubleValue()); return; case Types.TIMESTAMP: setToPreparedStatementTimestamp(ps, index, value, column); return; case Types.ARRAY: Array array = createArrayOf(Types.VARCHAR, (Object[]) value, ps.getConnection()); ps.setArray(index, array); return; case Types.OTHER: if (column.getType() == ColumnType.FTSTORED) { ps.setString(index, (String) value); return; } throw new SQLException("Unhandled type: " + column.getType()); default: throw new SQLException("Unhandled JDBC type: " + column.getJdbcType()); } }
From source file:org.eclipse.ecr.core.storage.sql.jdbc.JDBCMapper.java
@Override public PartialList<Serializable> query(String query, QueryFilter queryFilter, boolean countTotal) throws StorageException { QueryMaker queryMaker = findQueryMaker(query); if (queryMaker == null) { throw new StorageException("No QueryMaker accepts query: " + query); }/*from ww w.j a va2 s .c o m*/ QueryMaker.Query q = queryMaker.buildQuery(sqlInfo, model, pathResolver, query, queryFilter); if (q == null) { logger.log("Query cannot return anything due to conflicting clauses"); return new PartialList<Serializable>(Collections.<Serializable>emptyList(), 0); } long limit = queryFilter.getLimit(); long offset = queryFilter.getOffset(); if (logger.isLogEnabled()) { String sql = q.selectInfo.sql; if (limit != 0) { sql += " -- LIMIT " + limit + " OFFSET " + offset; } if (countTotal) { sql += " -- COUNT TOTAL"; } logger.logSQL(sql, q.selectParams); } String sql = q.selectInfo.sql; if (!countTotal && limit > 0 && sqlInfo.dialect.supportsPaging()) { // full result set not needed for counting sql += " " + sqlInfo.dialect.getPagingClause(limit, offset); limit = 0; offset = 0; } PreparedStatement ps = null; try { ps = connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); int i = 1; for (Object object : q.selectParams) { if (object instanceof Calendar) { Calendar cal = (Calendar) object; Timestamp ts = new Timestamp(cal.getTimeInMillis()); ps.setTimestamp(i++, ts, cal); // cal passed for timezone } else if (object instanceof String[]) { Array array = sqlInfo.dialect.createArrayOf(Types.VARCHAR, (Object[]) object, connection); ps.setArray(i++, array); } else { ps.setObject(i++, object); } } ResultSet rs = ps.executeQuery(); // limit/offset long totalSize = -1; boolean available; if ((limit == 0) || (offset == 0)) { available = rs.first(); if (!available) { totalSize = 0; } if (limit == 0) { limit = -1; // infinite } } else { available = rs.absolute((int) offset + 1); } Column column = q.selectInfo.whatColumns.get(0); List<Serializable> ids = new LinkedList<Serializable>(); int rowNum = 0; while (available && (limit != 0)) { Serializable id = column.getFromResultSet(rs, 1); ids.add(id); rowNum = rs.getRow(); available = rs.next(); limit--; } // total size if (countTotal && (totalSize == -1)) { if (!available && (rowNum != 0)) { // last row read was the actual last totalSize = rowNum; } else { // available if limit reached with some left // rowNum == 0 if skipped too far rs.last(); totalSize = rs.getRow(); } } if (logger.isLogEnabled()) { logger.logIds(ids, countTotal, totalSize); } return new PartialList<Serializable>(ids, totalSize); } catch (Exception e) { checkConnectionReset(e); throw new StorageException("Invalid query: " + query, e); } finally { if (ps != null) { try { closeStatement(ps); } catch (SQLException e) { log.error("Cannot close connection", e); } } } }