00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "PropertyObject.h"
00035
00036 #include "Transaction.h"
00037
00038 namespace Dataquay
00039 {
00040
00041 QString
00042 PropertyObject::m_defaultPrefix = "property";
00043
00044 PropertyObject::PropertyObject(Store *s, QUrl uri) :
00045 m_store(s), m_pfx(m_defaultPrefix), m_uri(uri)
00046 {
00047 }
00048
00049 PropertyObject::PropertyObject(Store *s, QString uri) :
00050 m_store(s), m_pfx(m_defaultPrefix), m_uri(s->expand(uri))
00051 {
00052 }
00053
00054 PropertyObject::PropertyObject(Store *s, QString pfx, QUrl uri) :
00055 m_store(s), m_pfx(pfx), m_uri(uri)
00056 {
00057 }
00058
00059 PropertyObject::PropertyObject(Store *s, QString pfx, QString uri) :
00060 m_store(s), m_pfx(pfx), m_uri(s->expand(uri))
00061 {
00062 }
00063
00064 bool
00065 PropertyObject::hasProperty(QString name) const
00066 {
00067 QUrl property = getPropertyUri(name);
00068 Triple r = m_store->matchFirst(Triple(m_uri, property, Node()));
00069 return (r != Triple());
00070 }
00071
00072 bool
00073 PropertyObject::hasProperty(Transaction *tx, QString name) const
00074 {
00075 Store *s = getStore(tx);
00076 QUrl property = getPropertyUri(name);
00077 Triple r = s->matchFirst(Triple(m_uri, property, Node()));
00078 return (r != Triple());
00079 }
00080
00081 QVariant
00082 PropertyObject::getProperty(QString name) const
00083 {
00084 QUrl property = getPropertyUri(name);
00085 Triple r = m_store->matchFirst(Triple(m_uri, property, Node()));
00086 if (r == Triple()) return QVariant();
00087 return r.c.toVariant();
00088 }
00089
00090 QVariant
00091 PropertyObject::getProperty(Transaction *tx, QString name) const
00092 {
00093 Store *s = getStore(tx);
00094 QUrl property = getPropertyUri(name);
00095 Triple r = s->matchFirst(Triple(m_uri, property, Node()));
00096 if (r == Triple()) return QVariant();
00097 return r.c.toVariant();
00098 }
00099
00100 void
00101 PropertyObject::setProperty(Transaction *tx, QString name, QVariant value)
00102 {
00103 Store *s = getStore(tx);
00104 QUrl property = getPropertyUri(name);
00105 Triple t(m_uri, property, Node());
00106 s->remove(t);
00107 t.c = Node::fromVariant(value);
00108 s->add(t);
00109 }
00110
00111 void
00112 PropertyObject::removeProperty(Transaction *tx, QString name)
00113 {
00114 Store *s = getStore(tx);
00115 QUrl property = getPropertyUri(name);
00116 Triple t(m_uri, property, Node());
00117 s->remove(t);
00118 }
00119
00120 Store *
00121 PropertyObject::getStore(Transaction *tx) const
00122 {
00123 if (tx == NoTransaction) return m_store;
00124 else return tx;
00125 }
00126
00127 QUrl
00128 PropertyObject::getPropertyUri(QString name) const
00129 {
00130 if (name == "a") return m_store->expand(name);
00131 if (name.contains(':')) return m_store->expand(name);
00132 return m_store->expand(m_pfx + ":" + name);
00133 }
00134
00135 void
00136 PropertyObject::setDefaultPropertyPrefix(QString prefix)
00137 {
00138 m_defaultPrefix = prefix;
00139 }
00140
00141 CacheingPropertyObject::CacheingPropertyObject(Store *s, QUrl uri) :
00142 m_po(s, uri)
00143 {
00144 }
00145
00146 CacheingPropertyObject::CacheingPropertyObject(Store *s, QString uri) :
00147 m_po(s, uri)
00148 {
00149 }
00150
00151 CacheingPropertyObject::CacheingPropertyObject(Store *s, QString pfx, QUrl uri) :
00152 m_po(s, pfx, uri)
00153 {
00154 }
00155
00156 CacheingPropertyObject::CacheingPropertyObject(Store *s, QString pfx, QString uri) :
00157 m_po(s, pfx, uri)
00158 {
00159 }
00160
00161 bool
00162 CacheingPropertyObject::hasProperty(QString name) const
00163 {
00164 return m_po.hasProperty(name);
00165 }
00166
00167 bool
00168 CacheingPropertyObject::hasProperty(Transaction *tx, QString name) const
00169 {
00170 return m_po.hasProperty(tx, name);
00171 }
00172
00173 QVariant
00174 CacheingPropertyObject::getProperty(QString name) const
00175 {
00176 StringVariantMap::iterator i = m_cache.find(name);
00177 if (i == m_cache.end()) {
00178 QVariant value = m_po.getProperty(name);
00179 m_cache[name] = value;
00180 return value;
00181 }
00182 return i->second;
00183 }
00184
00185 QVariant
00186 CacheingPropertyObject::getProperty(Transaction *tx, QString name) const
00187 {
00188 StringVariantMap::iterator i = m_cache.find(name);
00189 if (i == m_cache.end()) {
00190 QVariant value = m_po.getProperty(tx, name);
00191 m_cache[name] = value;
00192 return value;
00193 }
00194 return i->second;
00195 }
00196
00197 void
00198 CacheingPropertyObject::setProperty(Transaction *tx, QString name, QVariant value)
00199 {
00200 m_po.setProperty(tx, name, value);
00201 m_cache[name] = value;
00202 }
00203
00204 void
00205 CacheingPropertyObject::removeProperty(Transaction *tx, QString name)
00206 {
00207 m_po.removeProperty(tx, name);
00208 m_cache.erase(name);
00209 }
00210
00211 Store *
00212 CacheingPropertyObject::getStore(Transaction *tx) const
00213 {
00214 return m_po.getStore(tx);
00215 }
00216
00217 QUrl
00218 CacheingPropertyObject::getPropertyUri(QString name) const
00219 {
00220 return m_po.getPropertyUri(name);
00221 }
00222
00223 }
00224