⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 table.java

📁 一个OR Mapping工具
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	        deleteStmt.close();
	        deleteStmt = null;
	    }
	    connectionID = s.connectionID;
        }
    }
	    

    private final int buildFieldsList(Vector buf, Class cls, String prefix)
    {
	Field[] f = cls.getDeclaredFields();

	Class superclass = cls;
	while ((superclass = superclass.getSuperclass()) != null) { 
	    Field[] inheritedFields = superclass.getDeclaredFields();
	    Field[] allFields = new Field[inheritedFields.length + f.length];
	    System.arraycopy(inheritedFields, 0, allFields, 0, 
			     inheritedFields.length);
	    System.arraycopy(f,0, allFields, inheritedFields.length, f.length);
	    f = allFields;
	} 

	try { 
	    for (int i = f.length; --i>= 0;) { 
	        setBypass.invoke(f[i], bypassFlag);
	    }
	} catch(Exception ex) {
	    System.err.println("Failed to set bypass attribute");
	}

	int n = 0;
	for (int i = 0; i < f.length; i++) { 
	    if ((f[i].getModifiers()&(Modifier.TRANSIENT|Modifier.STATIC))==0)
	    {
		String name = f[i].getName();
		Class fieldClass = f[i].getType();
		String fullName = prefix + name;
		FieldDescriptor fd = new FieldDescriptor(f[i], fullName);
		int type;

		buf.addElement(fd);
		n += 1;

		String c = fieldClass.getName();
		if (c.equals("byte")) type = FieldDescriptor.t_byte;
		else if (c.equals("short")) type = FieldDescriptor.t_short;
		else if (c.equals("int")) type = FieldDescriptor.t_int;
		else if (c.equals("long")) type = FieldDescriptor.t_long;
		else if (c.equals("float")) type = FieldDescriptor.t_float;
		else if (c.equals("double")) type = FieldDescriptor.t_double;
		else if (c.equals("boolean")) type = FieldDescriptor.t_boolean;
		else if (c.equals("java.lang.Byte")) 
		    type = FieldDescriptor.tByte;
		else if (c.equals("java.lang.Short")) 
		    type = FieldDescriptor.tShort;
		else if (c.equals("java.lang.Integer")) 
		    type = FieldDescriptor.tInteger;
		else if (c.equals("java.lang.Long")) 
		    type = FieldDescriptor.tLong;
		else if (c.equals("java.lang.Float")) 
		    type = FieldDescriptor.tFloat;
		else if (c.equals("java.lang.Double"))      
		    type = FieldDescriptor.tDouble;
		else if (c.equals("java.lang.Boolean")) 
		    type = FieldDescriptor.tBoolean;
		else if (c.equals("java.math.BigDecimal"))  
		    type = FieldDescriptor.tDecimal;
		else if (c.equals("java.lang.String")) 
		    type = FieldDescriptor.tString;
		else if (c.equals("java.lang.[B"))      
  		    type = FieldDescriptor.tBytes;
		else if (c.equals("java.sql.Date"))        
		    type = FieldDescriptor.tDate;
		else if (c.equals("java.sql.Time"))        
		    type = FieldDescriptor.tTime;
		else if (c.equals("java.sql.Timestamp"))   
		    type = FieldDescriptor.tTimestamp;
		else if (c.equals("java.lang.InputStream")) 
		    type = FieldDescriptor.tStream;
		else if (c.equals("java.sql.BlobLocator")) 
		    type = FieldDescriptor.tBlob;
		else if (c.equals("java.sql.ClobLocator")) 
		    type = FieldDescriptor.tClob;
		else if (serializableClass.isAssignableFrom(fieldClass))
 		    type = FieldDescriptor.tClosure;
                else { 
		    int nComponents = buildFieldsList(buf, fieldClass, 
						      fd.name+fieldSeparator);
		    fd.inType = fd.outType = 
		        FieldDescriptor.tCompound + nComponents;
	
		    try { 
		        fd.constructor = 
			  fieldClass.getDeclaredConstructor(new Class[0]);
			setBypass.invoke(fd.constructor, bypassFlag);
		    } catch(Exception ex) {}

		    n += nComponents;
		    continue;
		}
		if (listOfFields.length() != 0) {
		    listOfFields += ",";
		    listOfAssignments += ",";
		}
		listOfFields += fullName;
		listOfAssignments += fullName + "=?";

		fd.inType = fd.outType = type;
		nColumns += 1;
	    }
	}
	return n;
    }
	    

    protected final Object load(ResultSet result) throws SQLException { 
	Object obj;
        try { 
	    obj = constructor.newInstance(constructorArgs);
	} 
	catch(IllegalAccessException ex) { throw new IllegalAccessError(); }
	catch(InstantiationException ex) { throw new InstantiationError(); }
	catch(Exception ex) { 
	   throw new InstantiationError("Exception was thrown by constructor");
	}
	load(obj, 0, nFields, 0, result);
	return obj;
    }

    private final int load(Object obj, int i, int end, int column,
			    ResultSet result) 
	throws SQLException
    { 
	try { 
	    while (i < end) { 
	        FieldDescriptor fd = fields[i++];
		if (!fd.loadVariable(result, obj, ++column)) { 
		    Object component = 
		        fd.constructor.newInstance(constructorArgs);
		    fd.field.set(obj, component);
		    int nComponents = fd.inType - FieldDescriptor.tCompound;
		    column = load(component, i, i + nComponents, 
				  column-1, result);
		    i += nComponents;
		}
	    }   
	} 
	catch(IllegalAccessException ex) { throw new IllegalAccessError(); }   
	catch(InstantiationException ex) { throw new InstantiationError(); }   
	catch(InvocationTargetException ex) { 
	   throw new InstantiationError("Exception was thrown by constructor");
	}   
	return column;
    }

    protected final void bindUpdateVariables(PreparedStatement pstmt,
					     Object            obj)
      throws SQLException
    {
        bindUpdateVariables(pstmt, obj, 0, nFields, 0); 	
    }

    protected final void bindQueryVariables(PreparedStatement pstmt,
					    Object            obj)
       throws SQLException
    {
	bindQueryVariables(pstmt, obj, 0, nFields, 0); 	
    }


    protected final void updateVariables(ResultSet result, Object obj)
       throws SQLException
    {
        updateVariables(result, obj, 0, nFields, 0);
	result.updateRow();
    }


    protected final String buildQueryList(Object qbe)
    {
        StringBuffer buf = new StringBuffer();
        buildQueryList(buf, qbe, 0, nFields);
	String condition = buf.toString();
	return condition.length() != 0 ? " where " + condition : "";
    }


    private final int bindUpdateVariables(PreparedStatement pstmt, Object obj,
					  int i, int end, int column) 
      throws SQLException
    { 
        try { 
	    while (i < end) { 
		FieldDescriptor fd = fields[i++];
		Object comp = null;
		if (!fd.isBuiltin() && (comp = fd.field.get(obj)) == null) {
		    if (fd.isCompound()) {
 		        int nComponents = fd.outType-FieldDescriptor.tCompound;
			while (--nComponents >= 0) { 
			    fd = fields[i++];
			    if (!fd.isCompound()) { 
			        pstmt.setNull(++column, 
				   FieldDescriptor.sqlTypeMapping[fd.outType]);
			    } 
			}
		    } else { 
		        pstmt.setNull(++column, 
				      FieldDescriptor.sqlTypeMapping[fd.outType]);
		    } 
		} else { 
		    if (!fd.bindVariable(pstmt, obj, ++column)) { 
			int nComponents = fd.outType-FieldDescriptor.tCompound;
			column = bindUpdateVariables(pstmt, comp, 
						     i,i+nComponents,column-1);
			i += nComponents;
		    }
		}
	    }
	} catch(IllegalAccessException ex) { throw new IllegalAccessError(); }
	return column;
    }

    private final int bindQueryVariables(PreparedStatement pstmt, Object obj,
					 int i, int end, int column) 
      throws SQLException
    { 
        try { 
	    while (i < end) { 
		Object comp;
		FieldDescriptor fd = fields[i++];
		if (!fd.field.getDeclaringClass().isInstance(obj)) { 
		    return column;
		}
		int nComponents = 
		    fd.isCompound() ? fd.outType-FieldDescriptor.tCompound : 0;
		if (!fd.isBuiltin() 
		    && fd.outType != FieldDescriptor.tClosure 
		    && (comp = fd.field.get(obj)) != null) 
	        {
		    if (!fd.bindVariable(pstmt, obj, ++column)) { 
		        column = bindQueryVariables(pstmt, comp, 
						    i,i+nComponents,column-1);
		    }
		}
		i += nComponents;
	    }
	} catch(IllegalAccessException ex) { throw new IllegalAccessError(); }
	return column;
    }

    private final void buildQueryList(StringBuffer buf, Object qbe, 
				      int i, int end)
    {
	try { 
	    while (i < end) { 
		Object comp;
		FieldDescriptor fd = fields[i++];
		int nComponents = 
		    fd.isCompound() ? fd.outType-FieldDescriptor.tCompound : 0;
		if (!fd.isBuiltin() 
		    && fd.outType != FieldDescriptor.tClosure 
		    && (comp = fd.field.get(qbe)) != null) 
		{
		    if (nComponents != 0) { 
			buildQueryList(buf, comp, i, i+nComponents);
		    } else { 
			if (buf.length() != 0) { 
			    buf.append(" and ");
			}
			buf.append(fd.name);
			buf.append("=?");
		    }
		} 
		i += nComponents;
	    }
	} catch(IllegalAccessException ex) { throw new IllegalAccessError(); }
    }

    protected final int updateVariables(ResultSet result, Object obj,
					 int i, int end, int column) 
      throws SQLException
    { 
        try { 
	    while (i < end) { 
		FieldDescriptor fd = fields[i++];
		Object comp = null;
		if (!fd.isBuiltin() && (comp = fd.field.get(obj)) == null) {
		    if (fd.isCompound()) {
 		        int nComponents = fd.outType-FieldDescriptor.tCompound;
			while (--nComponents >= 0) { 
			    fd = fields[i++];
			    if (!fd.isCompound()) { 
			        result.updateNull(++column);
			    } 
			}
		    } else { 
		        result.updateNull(++column);
		    } 
		} else { 
		    if (!fd.updateVariable(result, obj, ++column)) { 
			int nComponents = fd.outType-FieldDescriptor.tCompound;
			column = updateVariables(result, comp, 
						 i, i+nComponents, column-1);
			i += nComponents;
		    }
		}
	    }
	} catch(IllegalAccessException ex) { throw new IllegalAccessError(); }
	return column;
    }

}





⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -