1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
/*
* $Id$
*
* This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
* project.
*
* Copyright (C) 1998-2012 OpenLink Software
*
* This project is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; only version 2 of the License, dated June 1991.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
package virtuoso.jdbc2;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import virtuoso.sql.RdfBox;
public class VirtuosoRdfBox implements RdfBox
{
// rdf_box_t
public short rb_type;
public short rb_lang;
public boolean rb_is_complete;
public boolean rb_is_outlined;
public boolean rb_chksum_tail;
public boolean rb_is_text_index;
public long rb_ro_id;
public Object rb_box;
// defaults
public static final int RDF_BOX_DEFAULT_TYPE = 257;
public static final int RDF_BOX_DEFAULT_LANG = 257;
// bit position in flags
public static final int RBS_OUTLINED = 0x01;
public static final int RBS_COMPLETE = 0x02;
public static final int RBS_HAS_LANG = 0x04;
public static final int RBS_HAS_TYPE = 0x08;
public static final int RBS_CHKSUM = 0x10;
public static final int RBS_64 = 0x20;
public static final int RBS_SKIP_DTP = 0x40;
private VirtuosoConnection connection = null;
public VirtuosoRdfBox (VirtuosoConnection connection, Object box, boolean is_complete, short type, short lang, long ro_id)
{
this.connection = connection;
this.rb_box = box;
this.rb_type = type;
this.rb_lang = lang;
this.rb_is_complete = is_complete;
this.rb_ro_id = ro_id;
this.rb_is_outlined = false;
this.rb_chksum_tail = false;
}
public VirtuosoRdfBox (Connection connection, Object box, String type, String lang)
{
long ro_id;
this.connection = (VirtuosoConnection) connection;
this.rb_box = box;
ro_id = rdfMakeObj (box, type, lang);
this.rb_type = getTypeKey (type);
this.rb_lang = getLangKey (lang);
this.rb_is_complete = false;
this.rb_ro_id = ro_id;
this.rb_is_outlined = false;
this.rb_chksum_tail = false;
}
private long rdfMakeObj (Object box, String type, String lang)
{
long ro_id = 0;
try
{
VirtuosoPreparedStatement ps = (VirtuosoPreparedStatement) this.connection.prepareStatement
("DB.DBA.RDF_MAKE_OBJ_OF_TYPEDSQLVAL (?, ?, ?)");
ps.setObject (1, box);
ps.setString (2, type);
ps.setString (3, lang);
VirtuosoPreparedStatement ro = (VirtuosoPreparedStatement) this.connection.prepareStatement
("select rdf_box_ro_id (?)");
ro.setObject (1, box);
try
{
ps.executeQuery();
ResultSet rs = ro.executeQuery();
while(rs.next());
{
long rc = rs.getLong (1);
ro_id = rc;
}
ps.close ();
this.connection.rdf_lang_hash.clear ();
this.connection.rdf_type_hash.clear ();
this.connection.rdf_lang_rev.clear ();
this.connection.rdf_type_rev.clear ();
}
catch (SQLException e)
{
}
}
catch (VirtuosoException e)
{
}
return ro_id;
}
public short getLangKey (String lang)
{
Integer k;
if (lang == null)
return (short) RDF_BOX_DEFAULT_LANG;
ensureLangHash ();
k = (Integer) this.connection.rdf_lang_rev.get (lang);
return (k != null ? k.shortValue () : (short)RDF_BOX_DEFAULT_LANG);
}
public short getTypeKey (String type)
{
Integer k;
if (type == null)
return (short) RDF_BOX_DEFAULT_TYPE;
ensureTypeHash ();
k = (Integer) this.connection.rdf_type_rev.get (type);
return (k != null ? k.shortValue () : (short) RDF_BOX_DEFAULT_TYPE);
}
#if JDK_VER >= 16
private void fillHashFromSQL (Hashtable<Integer,String> ht, Hashtable<String,Integer> rev, String sql)
#else
private void fillHashFromSQL (Hashtable ht, Hashtable rev, String sql)
#endif
{
try
{
Statement stmt = this.connection.createStatement ();
try
{
stmt.execute (sql);
stmt.setFetchSize(200);
ResultSet rs = stmt.getResultSet ();
while (rs.next ())
{
Integer k = (Integer) rs.getObject (1);
String v = rs.getString (2);
ht.put (k, v);
rev.put (v, k);
}
stmt.close();
}
catch (SQLException e)
{
}
}
catch (VirtuosoException e)
{
}
}
private void ensureTypeHash ()
{
if (!this.connection.rdf_type_loaded)
{
fillHashFromSQL (this.connection.rdf_type_hash, this.connection.rdf_type_rev, "select RDT_TWOBYTE, RDT_QNAME from DB.DBA.RDF_DATATYPE");
this.connection.rdf_type_loaded = true;
}
}
private void ensureLangHash ()
{
if (!this.connection.rdf_lang_loaded)
{
fillHashFromSQL (this.connection.rdf_lang_hash, this.connection.rdf_lang_rev, "select RL_TWOBYTE, RL_ID from DB.DBA.RDF_LANGUAGE");
this.connection.rdf_lang_loaded = true;
}
}
private String _getType ()
{
ensureTypeHash ();
return (String) this.connection.rdf_type_hash.get (new Integer (this.rb_type));
}
public String getType ()
{
if (this.rb_type == RDF_BOX_DEFAULT_TYPE)
return null;
String r = _getType();
if (r == null) {
synchronized(connection) {
this.connection.rdf_type_loaded = false;
r = _getType();
}
}
return r;
}
private String _getLang ()
{
ensureLangHash ();
return (String) this.connection.rdf_lang_hash.get (new Integer (this.rb_lang));
}
public String getLang ()
{
if (this.rb_lang == RDF_BOX_DEFAULT_LANG)
return null;
String r = _getLang();
if (r == null) {
synchronized(connection) {
this.connection.rdf_lang_loaded = false;
r = _getLang();
}
}
return r;
}
public String toString ()
{
return this.rb_box.toString ();
}
}
|