List of usage examples for java.sql Types OTHER
int OTHER
To view the source code for java.sql Types OTHER.
Click Source Link
getObject
and setObject
. From source file:hoot.services.db.DbUtils.java
public static void batchRecordsDirectRelations(final long mapId, final List<?> records, final RecordBatchType recordBatchType, Connection conn, int maxRecordBatchSize) throws Exception { PreparedStatement ps = null;//from ww w . j ava 2 s . co m try { String sql = null; long execResult = -1; //conn.setAutoCommit(false); int count = 0; switch (recordBatchType) { case INSERT: sql = "insert into current_relations_" + mapId + " (id, changeset_id, \"timestamp\", visible, version, tags) " + "values (?, ?, ?, ?, ?, ?)"; ps = conn.prepareStatement(sql); for (Object o : records) { CurrentRelations rel = (CurrentRelations) o; ps.setLong(1, rel.getId()); ps.setLong(2, rel.getChangesetId()); ps.setTimestamp(3, rel.getTimestamp()); ps.setBoolean(4, rel.getVisible()); ps.setLong(5, rel.getVersion()); Map<String, String> tags = (Map<String, String>) rel.getTags(); String hstoreStr = ""; Iterator it = tags.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry) it.next(); if (hstoreStr.length() > 0) { hstoreStr += ","; } hstoreStr += "\"" + pairs.getKey() + "\"=>\"" + pairs.getValue() + "\""; } ps.setObject(6, hstoreStr, Types.OTHER); ps.addBatch(); if (maxRecordBatchSize > -1) { if (++count % maxRecordBatchSize == 0) { ps.executeBatch(); //conn.commit(); } } } break; case UPDATE: sql = "update current_relations_" + mapId + " set changeset_id=?, visible=?, \"timestamp\"=?, version=?, tags=? " + "where id=?"; ps = conn.prepareStatement(sql); for (Object o : records) { CurrentRelations rel = (CurrentRelations) o; ps.setLong(1, rel.getChangesetId()); ps.setBoolean(2, rel.getVisible()); ps.setTimestamp(3, rel.getTimestamp()); ps.setLong(4, rel.getVersion()); Map<String, String> tags = (Map<String, String>) rel.getTags(); String hstoreStr = ""; Iterator it = tags.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry) it.next(); if (hstoreStr.length() > 0) { hstoreStr += ","; } hstoreStr += "\"" + pairs.getKey() + "\"=>\"" + pairs.getValue() + "\""; } ps.setObject(5, hstoreStr, Types.OTHER); ps.setLong(6, rel.getId()); ps.addBatch(); if (maxRecordBatchSize > -1) { if (++count % maxRecordBatchSize == 0) { ps.executeBatch(); //conn.commit(); } } } break; case DELETE: sql = "delete from current_relations_" + mapId + " where id=?"; ps = conn.prepareStatement(sql); for (Object o : records) { CurrentRelations rel = (CurrentRelations) o; ps.setLong(1, rel.getId()); ps.addBatch(); if (maxRecordBatchSize > -1) { if (++count % maxRecordBatchSize == 0) { ps.executeBatch(); //conn.commit(); } } } break; default: throw new Exception(""); } ps.executeBatch(); //conn.commit(); } catch (Exception e) { conn.rollback(); String msg = "Error executing batch query."; msg += " " + e.getMessage(); msg += " Cause:" + e.getCause().toString(); throw new Exception(msg); } finally { if (ps != null) { ps.close(); } //conn.setAutoCommit(true); } }
From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java
/** * Set a column value into a prepared statement. * * @param stmnt the prepared statement to parameterize * @param idx the index of the parameter in the prepared statement * @param val the value of the column/*from w ww. j ava 2s . c o m*/ * @param col the column being set * @param type the field mapping type code for the value * @param store the store manager for the current context */ public void setTyped(PreparedStatement stmnt, int idx, Object val, Column col, int type, JDBCStore store) throws SQLException { if (val == null) { setNull(stmnt, idx, (col == null) ? Types.OTHER : col.getType(), col); return; } Sized s; Calendard c; switch (type) { case JavaTypes.BOOLEAN: case JavaTypes.BOOLEAN_OBJ: setBoolean(stmnt, idx, ((Boolean) val).booleanValue(), col); break; case JavaTypes.BYTE: case JavaTypes.BYTE_OBJ: setByte(stmnt, idx, ((Number) val).byteValue(), col); break; case JavaTypes.CHAR: case JavaTypes.CHAR_OBJ: setChar(stmnt, idx, ((Character) val).charValue(), col); break; case JavaTypes.DOUBLE: case JavaTypes.DOUBLE_OBJ: setDouble(stmnt, idx, ((Number) val).doubleValue(), col); break; case JavaTypes.FLOAT: case JavaTypes.FLOAT_OBJ: setFloat(stmnt, idx, ((Number) val).floatValue(), col); break; case JavaTypes.INT: case JavaTypes.INT_OBJ: setInt(stmnt, idx, ((Number) val).intValue(), col); break; case JavaTypes.LONG: case JavaTypes.LONG_OBJ: setLong(stmnt, idx, ((Number) val).longValue(), col); break; case JavaTypes.SHORT: case JavaTypes.SHORT_OBJ: setShort(stmnt, idx, ((Number) val).shortValue(), col); break; case JavaTypes.STRING: if (col != null && (col.getType() == Types.CLOB || col.getType() == Types.LONGVARCHAR)) setClobString(stmnt, idx, (String) val, col); else { if (val instanceof String) setString(stmnt, idx, (String) val, col); else setString(stmnt, idx, val.toString(), col); } break; case JavaTypes.OBJECT: setBlobObject(stmnt, idx, val, col, store); break; case JavaTypes.DATE: setDate(stmnt, idx, (Date) val, col); break; case JavaTypes.CALENDAR: setCalendar(stmnt, idx, (Calendar) val, col); break; case JavaTypes.BIGDECIMAL: setBigDecimal(stmnt, idx, (BigDecimal) val, col); break; case JavaTypes.BIGINTEGER: setBigInteger(stmnt, idx, (BigInteger) val, col); break; case JavaTypes.NUMBER: setNumber(stmnt, idx, (Number) val, col); break; case JavaTypes.LOCALE: setLocale(stmnt, idx, (Locale) val, col); break; case JavaSQLTypes.SQL_ARRAY: setArray(stmnt, idx, (Array) val, col); break; case JavaSQLTypes.ASCII_STREAM: s = (Sized) val; setAsciiStream(stmnt, idx, (InputStream) s.value, s.size, col); break; case JavaSQLTypes.BINARY_STREAM: s = (Sized) val; setBinaryStream(stmnt, idx, (InputStream) s.value, s.size, col); break; case JavaSQLTypes.BLOB: setBlob(stmnt, idx, (Blob) val, col); break; case JavaSQLTypes.BYTES: setBytes(stmnt, idx, (byte[]) val, col); break; case JavaSQLTypes.CHAR_STREAM: s = (Sized) val; setCharacterStream(stmnt, idx, (Reader) s.value, s.size, col); break; case JavaSQLTypes.CLOB: setClob(stmnt, idx, (Clob) val, col); break; case JavaSQLTypes.SQL_DATE: if (val instanceof Calendard) { c = (Calendard) val; setDate(stmnt, idx, (java.sql.Date) c.value, c.calendar, col); } else setDate(stmnt, idx, (java.sql.Date) val, null, col); break; case JavaSQLTypes.REF: setRef(stmnt, idx, (Ref) val, col); break; case JavaSQLTypes.TIME: if (val instanceof Calendard) { c = (Calendard) val; setTime(stmnt, idx, (Time) c.value, c.calendar, col); } else setTime(stmnt, idx, (Time) val, null, col); break; case JavaSQLTypes.TIMESTAMP: if (val instanceof Calendard) { c = (Calendard) val; setTimestamp(stmnt, idx, (Timestamp) c.value, c.calendar, col); } else setTimestamp(stmnt, idx, (Timestamp) val, null, col); break; default: if (col != null && (col.getType() == Types.BLOB || col.getType() == Types.VARBINARY)) setBlobObject(stmnt, idx, val, col, store); else setObject(stmnt, idx, val, col.getType(), col); } }
From source file:org.apache.sqoop.mapreduce.hcat.SqoopHCatUtilities.java
public static String sqlTypeString(int sqlType) { switch (sqlType) { case Types.BIT: return "BIT"; case Types.TINYINT: return "TINYINT"; case Types.SMALLINT: return "SMALLINT"; case Types.INTEGER: return "INTEGER"; case Types.BIGINT: return "BIGINT"; case Types.FLOAT: return "FLOAT"; case Types.REAL: return "REAL"; case Types.DOUBLE: return "DOUBLE"; case Types.NUMERIC: return "NUMERIC"; case Types.DECIMAL: return "DECIMAL"; case Types.CHAR: return "CHAR"; case Types.VARCHAR: return "VARCHAR"; case Types.LONGVARCHAR: return "LONGVARCHAR"; case Types.DATE: return "DATE"; case Types.TIME: return "TIME"; case Types.TIMESTAMP: return "TIMESTAMP"; case Types.BINARY: return "BINARY"; case Types.VARBINARY: return "VARBINARY"; case Types.LONGVARBINARY: return "LONGVARBINARY"; case Types.NULL: return "NULL"; case Types.OTHER: return "OTHER"; case Types.JAVA_OBJECT: return "JAVA_OBJECT"; case Types.DISTINCT: return "DISTINCT"; case Types.STRUCT: return "STRUCT"; case Types.ARRAY: return "ARRAY"; case Types.BLOB: return "BLOB"; case Types.CLOB: return "CLOB"; case Types.REF: return "REF"; case Types.DATALINK: return "DATALINK"; case Types.BOOLEAN: return "BOOLEAN"; case Types.ROWID: return "ROWID"; case Types.NCHAR: return "NCHAR"; case Types.NVARCHAR: return "NVARCHAR"; case Types.LONGNVARCHAR: return "LONGNVARCHAR"; case Types.NCLOB: return "NCLOB"; case Types.SQLXML: return "SQLXML"; default:/*from w w w .ja v a 2s . c om*/ return "<UNKNOWN>"; } }
From source file:com.feedzai.commons.sql.abstraction.engine.AbstractDatabaseEngine.java
/** * Maps the database type to {@link DbColumnType}. If there's no mapping a {@link DbColumnType#UNMAPPED} is returned. * * @param type The SQL type from {@link java.sql.Types}. * @return The {@link DbColumnType}./*from w ww . j av a 2 s . c om*/ */ protected DbColumnType toPdbType(final int type) { switch (type) { case Types.ARRAY: return DbColumnType.UNMAPPED; case Types.BIGINT: return DbColumnType.LONG; case Types.BINARY: return DbColumnType.BLOB; case Types.BIT: return DbColumnType.BOOLEAN; case Types.BLOB: return DbColumnType.BLOB; case Types.BOOLEAN: return DbColumnType.BOOLEAN; case Types.CHAR: return DbColumnType.STRING; case Types.CLOB: return DbColumnType.STRING; case Types.DATALINK: return DbColumnType.UNMAPPED; case Types.DATE: return DbColumnType.UNMAPPED; case Types.DECIMAL: return DbColumnType.DOUBLE; case Types.DISTINCT: return DbColumnType.UNMAPPED; case Types.DOUBLE: return DbColumnType.DOUBLE; case Types.FLOAT: return DbColumnType.DOUBLE; case Types.INTEGER: return DbColumnType.INT; case Types.JAVA_OBJECT: return DbColumnType.BLOB; case Types.LONGNVARCHAR: return DbColumnType.STRING; case Types.LONGVARBINARY: return DbColumnType.BLOB; case Types.LONGVARCHAR: return DbColumnType.STRING; case Types.NCHAR: return DbColumnType.STRING; case Types.NCLOB: return DbColumnType.STRING; case Types.NULL: return DbColumnType.UNMAPPED; case Types.NUMERIC: return DbColumnType.DOUBLE; case Types.NVARCHAR: return DbColumnType.STRING; case Types.OTHER: return DbColumnType.UNMAPPED; case Types.REAL: return DbColumnType.DOUBLE; case Types.REF: return DbColumnType.UNMAPPED; case Types.ROWID: return DbColumnType.STRING; case Types.SMALLINT: return DbColumnType.INT; case Types.SQLXML: return DbColumnType.STRING; case Types.STRUCT: return DbColumnType.UNMAPPED; case Types.TIME: return DbColumnType.UNMAPPED; case Types.TIMESTAMP: return DbColumnType.LONG; case Types.TINYINT: return DbColumnType.INT; case Types.VARBINARY: return DbColumnType.BLOB; case Types.VARCHAR: return DbColumnType.STRING; default: return DbColumnType.UNMAPPED; } }
From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java
/** * Set a completely unknown parameter into a prepared statement. *//*from ww w . ja va 2s . c om*/ public void setUnknown(PreparedStatement stmnt, int idx, Object val, Column col) throws SQLException { Sized sized = null; Calendard cald = null; if (val instanceof Sized) { sized = (Sized) val; val = sized.value; } else if (val instanceof Calendard) { cald = (Calendard) val; val = cald.value; } if (val == null) setNull(stmnt, idx, (col == null) ? Types.OTHER : col.getType(), col); else if (val instanceof String) setString(stmnt, idx, val.toString(), col); else if (val instanceof Integer) setInt(stmnt, idx, ((Integer) val).intValue(), col); else if (val instanceof Boolean) setBoolean(stmnt, idx, ((Boolean) val).booleanValue(), col); else if (val instanceof Long) setLong(stmnt, idx, ((Long) val).longValue(), col); else if (val instanceof Float) setFloat(stmnt, idx, ((Float) val).floatValue(), col); else if (val instanceof Double) setDouble(stmnt, idx, ((Double) val).doubleValue(), col); else if (val instanceof Byte) setByte(stmnt, idx, ((Byte) val).byteValue(), col); else if (val instanceof Character) setChar(stmnt, idx, ((Character) val).charValue(), col); else if (val instanceof Short) setShort(stmnt, idx, ((Short) val).shortValue(), col); else if (val instanceof Locale) setLocale(stmnt, idx, (Locale) val, col); else if (val instanceof BigDecimal) setBigDecimal(stmnt, idx, (BigDecimal) val, col); else if (val instanceof BigInteger) setBigInteger(stmnt, idx, (BigInteger) val, col); else if (val instanceof Array) setArray(stmnt, idx, (Array) val, col); else if (val instanceof Blob) setBlob(stmnt, idx, (Blob) val, col); else if (val instanceof byte[]) setBytes(stmnt, idx, (byte[]) val, col); else if (val instanceof Clob) setClob(stmnt, idx, (Clob) val, col); else if (val instanceof Ref) setRef(stmnt, idx, (Ref) val, col); else if (val instanceof java.sql.Date) setDate(stmnt, idx, (java.sql.Date) val, (cald == null) ? null : cald.calendar, col); else if (val instanceof Timestamp) setTimestamp(stmnt, idx, (Timestamp) val, (cald == null) ? null : cald.calendar, col); else if (val instanceof Time) setTime(stmnt, idx, (Time) val, (cald == null) ? null : cald.calendar, col); else if (val instanceof Date) setDate(stmnt, idx, (Date) val, col); else if (val instanceof Calendar) setDate(stmnt, idx, ((Calendar) val).getTime(), col); else if (val instanceof Reader) setCharacterStream(stmnt, idx, (Reader) val, (sized == null) ? 0 : sized.size, col); else throw new UserException(_loc.get("bad-param", val.getClass())); }
From source file:helma.objectmodel.db.NodeManager.java
/** * Create a new Node from a ResultSet.//from w w w . ja v a 2 s .co m */ public Node createNode(DbMapping dbm, ResultSet rs, DbColumn[] columns, int offset) throws SQLException, IOException, ClassNotFoundException { HashMap propBuffer = new HashMap(); String id = null; String name = null; String protoName = dbm.getTypeName(); DbMapping dbmap = dbm; Node node = new Node(safe); for (int i = 0; i < columns.length; i++) { int columnNumber = i + 1 + offset; // set prototype? if (columns[i].isPrototypeField()) { String protoId = rs.getString(columnNumber); protoName = dbm.getPrototypeName(protoId); if (protoName != null) { dbmap = getDbMapping(protoName); if (dbmap == null) { // invalid prototype name! app.logError("No prototype defined for prototype mapping \"" + protoName + "\" - Using default prototype \"" + dbm.getTypeName() + "\"."); dbmap = dbm; protoName = dbmap.getTypeName(); } } } // set id? if (columns[i].isIdField()) { id = rs.getString(columnNumber); // if id == null, the object doesn't actually exist - return null if (id == null) { return null; } } // set name? if (columns[i].isNameField()) { name = rs.getString(columnNumber); } Property newprop = new Property(node); switch (columns[i].getType()) { case Types.BIT: case Types.BOOLEAN: newprop.setBooleanValue(rs.getBoolean(columnNumber)); break; case Types.TINYINT: case Types.BIGINT: case Types.SMALLINT: case Types.INTEGER: newprop.setIntegerValue(rs.getLong(columnNumber)); break; case Types.REAL: case Types.FLOAT: case Types.DOUBLE: newprop.setFloatValue(rs.getDouble(columnNumber)); break; case Types.DECIMAL: case Types.NUMERIC: BigDecimal num = rs.getBigDecimal(columnNumber); if (num == null) { break; } if (num.scale() > 0) { newprop.setFloatValue(num.doubleValue()); } else { newprop.setIntegerValue(num.longValue()); } break; case Types.VARBINARY: case Types.BINARY: newprop.setJavaObjectValue(rs.getBytes(columnNumber)); break; case Types.BLOB: case Types.LONGVARBINARY: { InputStream in = rs.getBinaryStream(columnNumber); if (in == null) { break; } ByteArrayOutputStream bout = new ByteArrayOutputStream(); byte[] buffer = new byte[2048]; int read; while ((read = in.read(buffer)) > -1) { bout.write(buffer, 0, read); } newprop.setJavaObjectValue(bout.toByteArray()); } break; case Types.LONGVARCHAR: try { newprop.setStringValue(rs.getString(columnNumber)); } catch (SQLException x) { Reader in = rs.getCharacterStream(columnNumber); if (in == null) { newprop.setStringValue(null); break; } StringBuffer out = new StringBuffer(); char[] buffer = new char[2048]; int read; while ((read = in.read(buffer)) > -1) { out.append(buffer, 0, read); } newprop.setStringValue(out.toString()); } break; case Types.CHAR: case Types.VARCHAR: case Types.OTHER: newprop.setStringValue(rs.getString(columnNumber)); break; case Types.DATE: case Types.TIME: case Types.TIMESTAMP: newprop.setDateValue(rs.getTimestamp(columnNumber)); break; case Types.NULL: newprop.setStringValue(null); break; case Types.CLOB: Clob cl = rs.getClob(columnNumber); if (cl == null) { newprop.setStringValue(null); break; } char[] c = new char[(int) cl.length()]; Reader isr = cl.getCharacterStream(); isr.read(c); newprop.setStringValue(String.copyValueOf(c)); break; default: newprop.setStringValue(rs.getString(columnNumber)); break; } if (rs.wasNull()) { newprop.setStringValue(null); } propBuffer.put(columns[i].getName(), newprop); // mark property as clean, since it's fresh from the db newprop.dirty = false; } if (id == null) { return null; } else { Transactor tx = Transactor.getInstance(); if (tx != null) { // Check if the node is already registered with the transactor - // it may be in the process of being DELETED, but do return the // new node if the old one has been marked as INVALID. DbKey key = new DbKey(dbmap, id); Node dirtyNode = tx.getDirtyNode(key); if (dirtyNode != null && dirtyNode.getState() != Node.INVALID) { return dirtyNode; } } } Hashtable propMap = new Hashtable(); DbColumn[] columns2 = dbmap.getColumns(); for (int i = 0; i < columns2.length; i++) { Relation rel = columns2[i].getRelation(); if (rel != null && rel.isPrimitiveOrReference()) { Property prop = (Property) propBuffer.get(columns2[i].getName()); if (prop == null) { continue; } prop.setName(rel.propName); // if the property is a pointer to another node, change the property type to NODE if (rel.isReference() && rel.usesPrimaryKey()) { // FIXME: References to anything other than the primary key are not supported prop.convertToNodeReference(rel); } propMap.put(rel.propName, prop); } } node.init(dbmap, id, name, protoName, propMap); return node; }
From source file:helma.objectmodel.db.NodeManager.java
private void setStatementValue(PreparedStatement stmt, int stmtNumber, Property p, int columnType) throws SQLException { if (p.getValue() == null) { stmt.setNull(stmtNumber, columnType); } else {/*from ww w . j a va2 s . c o m*/ switch (columnType) { case Types.BIT: case Types.BOOLEAN: stmt.setBoolean(stmtNumber, p.getBooleanValue()); break; case Types.TINYINT: case Types.BIGINT: case Types.SMALLINT: case Types.INTEGER: stmt.setLong(stmtNumber, p.getIntegerValue()); break; case Types.REAL: case Types.FLOAT: case Types.DOUBLE: case Types.NUMERIC: case Types.DECIMAL: stmt.setDouble(stmtNumber, p.getFloatValue()); break; case Types.LONGVARBINARY: case Types.VARBINARY: case Types.BINARY: case Types.BLOB: Object b = p.getJavaObjectValue(); if (b instanceof byte[]) { byte[] buf = (byte[]) b; try { stmt.setBytes(stmtNumber, buf); } catch (SQLException x) { ByteArrayInputStream bout = new ByteArrayInputStream(buf); stmt.setBinaryStream(stmtNumber, bout, buf.length); } } else { throw new SQLException( "expected byte[] for binary column '" + p.getName() + "', found " + b.getClass()); } break; case Types.LONGVARCHAR: try { stmt.setString(stmtNumber, p.getStringValue()); } catch (SQLException x) { String str = p.getStringValue(); Reader r = new StringReader(str); stmt.setCharacterStream(stmtNumber, r, str.length()); } break; case Types.CLOB: String val = p.getStringValue(); Reader isr = new StringReader(val); stmt.setCharacterStream(stmtNumber, isr, val.length()); break; case Types.CHAR: case Types.VARCHAR: case Types.OTHER: stmt.setString(stmtNumber, p.getStringValue()); break; case Types.DATE: case Types.TIME: case Types.TIMESTAMP: stmt.setTimestamp(stmtNumber, p.getTimestampValue()); break; case Types.NULL: stmt.setNull(stmtNumber, 0); break; default: stmt.setString(stmtNumber, p.getStringValue()); break; } } }
From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java
/** * Returns the type name for the specific constant as defined * by {@link java.sql.Types}.//from ww w .j a v a2 s .c o m * * @param type the type * @return the name for the type */ public String getTypeName(int type) { switch (type) { case Types.ARRAY: return arrayTypeName; case Types.BIGINT: return bigintTypeName; case Types.BINARY: return binaryTypeName; case Types.BIT: return bitTypeName; case Types.BLOB: return blobTypeName; case Types.BOOLEAN: return booleanTypeName; case Types.CHAR: return charTypeName; case Types.CLOB: return clobTypeName; case Types.DATE: return dateTypeName; case Types.DECIMAL: return decimalTypeName; case Types.DISTINCT: return distinctTypeName; case Types.DOUBLE: return doubleTypeName; case Types.FLOAT: return floatTypeName; case Types.INTEGER: return integerTypeName; case Types.JAVA_OBJECT: return javaObjectTypeName; case Types.LONGVARBINARY: return longVarbinaryTypeName; case Types.LONGVARCHAR: return longVarcharTypeName; case Types.NULL: return nullTypeName; case Types.NUMERIC: return numericTypeName; case Types.OTHER: return otherTypeName; case Types.REAL: return realTypeName; case Types.REF: return refTypeName; case Types.SMALLINT: return smallintTypeName; case Types.STRUCT: return structTypeName; case Types.TIME: return timeTypeName; case Types.TIMESTAMP: return timestampTypeName; case Types.TINYINT: return tinyintTypeName; case Types.VARBINARY: return varbinaryTypeName; case Types.VARCHAR: return varcharTypeName; default: return otherTypeName; } }
From source file:org.apache.openjpa.jdbc.meta.MappingInfo.java
/** * Create a copy of the given column with the raw mapping information * set correctly, and without settings that match defaults. * * @param num the number of columns for this mapping * @param forceJDBCType whether the jdbc-type of the created column * should be set, even if it matches the default * for the given column's java type//from w w w. j av a 2 s . c o m * @param colTable expected table for the column * @param targetTable expected target table for join column * @param target target column or object for join column; for a * constant null target, use {@link #NULL} * @param inverse whether join column is for inverse join */ protected static Column syncColumn(MetaDataContext context, Column col, int num, boolean forceJDBCType, Table colTable, Table targetTable, Object target, boolean inverse) { // use full name for cols that aren't in the expected table, or that // are inverse joins DBDictionary dict = ((MappingRepository) context.getRepository()).getDBDictionary(); Column copy = new Column(); if (col.getTable() != colTable || inverse) copy.setIdentifier(QualifiedDBIdentifier.newPath(dict.getFullIdentifier(col.getTable(), true), col.getIdentifier())); else copy.setIdentifier(col.getIdentifier()); // set target if not default if (target != null) { if (target == NULL) copy.setTargetIdentifier(DBIdentifier.newColumn("null")); else if (target instanceof Column) { Column tcol = (Column) target; if ((!inverse && tcol.getTable() != targetTable) || (inverse && tcol.getTable() != colTable)) copy.setTargetIdentifier(QualifiedDBIdentifier .newPath(dict.getFullIdentifier(tcol.getTable(), true), tcol.getIdentifier())); else if (!defaultTarget(col, tcol, num)) copy.setTargetIdentifier(tcol.getIdentifier()); } else if (target instanceof Number) copy.setTargetIdentifier(DBIdentifier.newConstant(target.toString())); else copy.setTargetIdentifier(DBIdentifier.newConstant("'" + target + "'")); } else if (num > 1) copy.setTargetField(col.getTargetField()); if (col.getSize() != 0 && col.getSize() != dict.characterColumnSize && (col.getSize() != -1 || !col.isLob())) copy.setSize(col.getSize()); if (col.getDecimalDigits() != 0) copy.setDecimalDigits(col.getDecimalDigits()); if (col.getDefaultString() != null) copy.setDefaultString(col.getDefaultString()); if (col.isNotNull() && !col.isPrimaryKey() && (!isPrimitive(col.getJavaType()) || isForeignKey(col))) copy.setNotNull(true); // set type name if not default String typeName = col.getTypeName(); if (typeName != null || copy.getSize() != 0 || copy.getDecimalDigits() != 0) { // is this the dict default type? have to ensure jdbc-type set // prior to finding dict default copy.setType(col.getType()); String defName = dict.getTypeName(copy); copy.setType(Types.OTHER); // copy should not have size info set if it isn't used in type name boolean defSized = defName.indexOf('(') != -1; if (!defSized) { if (copy.getSize() > 0) copy.setSize(0); copy.setDecimalDigits(0); } if (typeName != null) { // make sure to strip size for comparison if (typeName.indexOf('(') == -1 && defSized) defName = defName.substring(0, defName.indexOf('(')); if (!typeName.equalsIgnoreCase(defName)) copy.setTypeName(typeName); } } // set jdbc-type if not default or if forced if (forceJDBCType || (target != null && !(target instanceof Column) && col.getType() != Types.VARCHAR) || dict.getJDBCType(col.getJavaType(), false) != col.getType()) copy.setType(col.getType()); return copy; }
From source file:axiom.objectmodel.db.NodeManager.java
/** * Create a new Node from a ResultSet./*from w ww . j a va2 s . c om*/ */ public Node createNode(DbMapping dbm, ResultSet rs, DbColumn[] columns, int offset) throws SQLException, IOException, ClassNotFoundException { HashMap propBuffer = new HashMap(); String id = null; String name = null; String protoName = dbm.getTypeName(); DbMapping dbmap = dbm; Node node = new Node(); for (int i = 0; i < columns.length; i++) { // set prototype? if (columns[i].isPrototypeField()) { protoName = rs.getString(i + 1 + offset); if (protoName != null) { dbmap = getDbMapping(protoName); if (dbmap == null) { // invalid prototype name! app.logError(ErrorReporter.errorMsg(this.getClass(), "createNode") + "Invalid prototype name: " + protoName + " - using default"); dbmap = dbm; protoName = dbmap.getTypeName(); } } } // set id? if (columns[i].isIdField()) { id = rs.getString(i + 1 + offset); // if id == null, the object doesn't actually exist - return null if (id == null) { return null; } } // set name? if (columns[i].isNameField()) { name = rs.getString(i + 1 + offset); } Property newprop = new Property(node); switch (columns[i].getType()) { case Types.BIT: newprop.setBooleanValue(rs.getBoolean(i + 1 + offset)); break; case Types.TINYINT: case Types.BIGINT: case Types.SMALLINT: case Types.INTEGER: newprop.setIntegerValue(rs.getLong(i + 1 + offset)); break; case Types.REAL: case Types.FLOAT: case Types.DOUBLE: newprop.setFloatValue(rs.getDouble(i + 1 + offset)); break; case Types.DECIMAL: case Types.NUMERIC: BigDecimal num = rs.getBigDecimal(i + 1 + offset); if (num == null) { break; } if (num.scale() > 0) { newprop.setFloatValue(num.doubleValue()); } else { newprop.setIntegerValue(num.longValue()); } break; case Types.VARBINARY: case Types.BINARY: // newprop.setStringValue(rs.getString(i+1+offset)); newprop.setJavaObjectValue(rs.getBytes(i + 1 + offset)); break; case Types.LONGVARBINARY: { InputStream in = rs.getBinaryStream(i + 1 + offset); if (in == null) { break; } ByteArrayOutputStream bout = new ByteArrayOutputStream(); byte[] buffer = new byte[2048]; int read; while ((read = in.read(buffer)) > -1) { bout.write(buffer, 0, read); } newprop.setJavaObjectValue(bout.toByteArray()); } break; case Types.LONGVARCHAR: try { newprop.setStringValue(rs.getString(i + 1 + offset)); } catch (SQLException x) { Reader in = rs.getCharacterStream(i + 1 + offset); char[] buffer = new char[2048]; int read = 0; int r; while ((r = in.read(buffer, read, buffer.length - read)) > -1) { read += r; if (read == buffer.length) { // grow input buffer char[] newBuffer = new char[buffer.length * 2]; System.arraycopy(buffer, 0, newBuffer, 0, buffer.length); buffer = newBuffer; } } newprop.setStringValue(new String(buffer, 0, read)); } break; case Types.CHAR: case Types.VARCHAR: case Types.OTHER: newprop.setStringValue(rs.getString(i + 1 + offset)); break; case Types.DATE: case Types.TIME: case Types.TIMESTAMP: newprop.setDateValue(rs.getTimestamp(i + 1 + offset)); break; case Types.NULL: newprop.setStringValue(null); break; case Types.CLOB: Clob cl = rs.getClob(i + 1 + offset); if (cl == null) { newprop.setStringValue(null); break; } char[] c = new char[(int) cl.length()]; Reader isr = cl.getCharacterStream(); isr.read(c); newprop.setStringValue(String.copyValueOf(c)); break; default: newprop.setStringValue(rs.getString(i + 1 + offset)); break; } if (rs.wasNull()) { newprop.setStringValue(null); } propBuffer.put(columns[i].getName(), newprop); // mark property as clean, since it's fresh from the db newprop.dirty = false; } if (id == null) { return null; } Hashtable propMap = new Hashtable(); DbColumn[] columns2 = dbmap.getColumns(); for (int i = 0; i < columns2.length; i++) { Relation rel = columns2[i].getRelation(); if (rel != null && (rel.reftype == Relation.PRIMITIVE || rel.reftype == Relation.REFERENCE)) { Property prop = (Property) propBuffer.get(columns2[i].getName()); if (prop == null) { continue; } prop.setName(rel.propName); // if the property is a pointer to another node, change the property type to NODE if ((rel.reftype == Relation.REFERENCE) && rel.usesPrimaryKey()) { // FIXME: References to anything other than the primary key are not supported prop.convertToNodeReference(rel.otherType, this.app.getCurrentRequestEvaluator().getLayer()); } propMap.put(rel.propName.toLowerCase(), prop); } } node.init(dbmap, id, name, protoName, propMap, safe); return node; }