001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.internal.query.processor;
022:
023: import com.db4o.*;
024: import com.db4o.foundation.*;
025: import com.db4o.internal.*;
026: import com.db4o.internal.handlers.*;
027: import com.db4o.reflect.*;
028: import com.db4o.types.*;
029:
030: /**
031: * @exclude
032: */
033: public class QField implements Visitor4, Unversioned {
034:
035: transient Transaction i_trans;
036: public String i_name;
037: transient FieldMetadata i_yapField;
038: public int i_yapClassID;
039: public int i_index;
040:
041: public QField() {
042: // C/S only
043: }
044:
045: public QField(Transaction a_trans, String name,
046: FieldMetadata a_yapField, int a_yapClassID, int a_index) {
047: i_trans = a_trans;
048: i_name = name;
049: i_yapField = a_yapField;
050: i_yapClassID = a_yapClassID;
051: i_index = a_index;
052: if (i_yapField != null) {
053: if (!i_yapField.alive()) {
054: i_yapField = null;
055: }
056: }
057: }
058:
059: boolean canHold(ReflectClass claxx) {
060: return i_yapField == null || i_yapField.canHold(claxx);
061: }
062:
063: Object coerce(Object a_object) {
064: ReflectClass claxx = null;
065: Reflector reflector = i_trans.reflector();
066: if (a_object != null) {
067: if (a_object instanceof ReflectClass) {
068: claxx = (ReflectClass) a_object;
069: } else {
070: claxx = reflector.forObject(a_object);
071: }
072: } else {
073: if (Deploy.csharp) {
074: return a_object;
075: }
076: }
077: if (i_yapField == null) {
078: return a_object;
079: }
080: return i_yapField.coerce(claxx, a_object);
081: }
082:
083: ClassMetadata getYapClass() {
084: if (i_yapField != null) {
085: return i_yapField.handlerClassMetadata(i_trans.container());
086: }
087: return null;
088: }
089:
090: FieldMetadata getYapField(ClassMetadata yc) {
091: if (i_yapField != null) {
092: return i_yapField;
093: }
094: FieldMetadata yf = yc.fieldMetadataForName(i_name);
095: if (yf != null) {
096: yf.alive();
097: }
098: return yf;
099: }
100:
101: public FieldMetadata getYapField() {
102: return i_yapField;
103: }
104:
105: boolean isArray() {
106: return i_yapField != null
107: && i_yapField.getHandler() instanceof ArrayHandler;
108: }
109:
110: boolean isClass() {
111: return i_yapField == null
112: || Handlers4.handlesClass(i_yapField.getHandler());
113: }
114:
115: boolean isSimple() {
116: return i_yapField != null
117: && Handlers4.handlesSimple(i_yapField.getHandler());
118: }
119:
120: Comparable4 prepareComparison(Object obj) {
121: if (i_yapField != null) {
122: return i_yapField.prepareComparison(obj);
123: }
124: if (obj == null) {
125: return Null.INSTANCE;
126: }
127: ClassMetadata yc = i_trans.container().produceClassMetadata(
128: i_trans.reflector().forObject(obj));
129: FieldMetadata yf = yc.fieldMetadataForName(i_name);
130: if (yf != null) {
131: return yf.prepareComparison(obj);
132: }
133: return null;
134: }
135:
136: void unmarshall(Transaction a_trans) {
137: if (i_yapClassID != 0) {
138: ClassMetadata yc = a_trans.container().classMetadataForId(
139: i_yapClassID);
140: i_yapField = yc.i_fields[i_index];
141: }
142: }
143:
144: public void visit(Object obj) {
145: ((QCandidate) obj).useField(this );
146: }
147:
148: public String toString() {
149: if (i_yapField != null) {
150: return "QField " + i_yapField.toString();
151: }
152: return super.toString();
153: }
154: }
|