gt: 		catch (SQLException e) {
context: /*
   *  $Id$
   *
   *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
   *  project.
   *
   *  Copyright (C) 1998-2014 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.sesame2.driver;
  
  import info.aduna.iteration.CloseableIteration;
  import info.aduna.iteration.CloseableIteratorIteration;
  import info.aduna.iteration.Iteration;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.Reader;
  import java.net.URL;
  import java.sql.Connection;
  import java.sql.ResultSet;
  import java.sql.ResultSetMetaData;
  import java.sql.PreparedStatement;
  import java.sql.CallableStatement;
  import java.sql.SQLException;
  import java.util.ArrayList;
  import java.util.LinkedList;
  import java.util.HashMap;
  import java.util.Map;
  import java.util.Iterator;
  import java.util.List;
  import java.util.StringTokenizer;
  import java.util.Vector;
  import java.util.NoSuchElementException;
  import java.util.Calendar;
  import java.util.GregorianCalendar;
  import java.util.Set;
  
  import org.openrdf.OpenRDFUtil;
  import org.openrdf.model.BNode;
  import org.openrdf.model.Graph;
  import org.openrdf.model.Literal;
  import org.openrdf.model.Namespace;
  import org.openrdf.model.Resource;
  import org.openrdf.model.Statement;
  import org.openrdf.model.URI;
  import org.openrdf.model.Value;
  import org.openrdf.model.ValueFactory;
  import org.openrdf.model.impl.ContextStatementImpl;
  //import org.openrdf.model.impl.GraphImpl;
  import org.openrdf.model.impl.NamespaceImpl;
  import org.openrdf.model.impl.ValueFactoryImpl;
  import org.openrdf.query.Dataset;
  import org.openrdf.query.BindingSet;
  import org.openrdf.query.BooleanQuery;
  import org.openrdf.query.GraphQuery;
  import org.openrdf.query.GraphQueryResult;
  import org.openrdf.query.MalformedQueryException;
  import org.openrdf.query.Query;
  import org.openrdf.query.Update;
  import org.openrdf.query.QueryEvaluationException;
  import org.openrdf.query.QueryLanguage;
  import org.openrdf.query.TupleQuery;
  import org.openrdf.query.TupleQueryResult;
  import org.openrdf.query.TupleQueryResultHandler;
  import org.openrdf.query.TupleQueryResultHandlerException;
  import org.openrdf.query.algebra.evaluation.QueryBindingSet;
  import org.openrdf.query.impl.GraphQueryResultImpl;
  import org.openrdf.query.impl.TupleQueryResultImpl;
  import org.openrdf.query.UnsupportedQueryLanguageException;
  import org.openrdf.query.UpdateExecutionException;
  import org.openrdf.repository.Repository;
  import org.openrdf.repository.RepositoryConnection;
  import org.openrdf.repository.RepositoryException;
  import org.openrdf.repository.RepositoryResult;
  import org.openrdf.repository.UnknownTransactionStateException;
  import org.openrdf.rio.ParserConfig;
  import org.openrdf.rio.RDFFormat;
  import org.openrdf.rio.RDFHandler;
  import org.openrdf.rio.RDFHandlerException;
  import org.openrdf.rio.RDFParseException;
  import org.openrdf.rio.RDFParser;
  import org.openrdf.rio.RDFParser.DatatypeHandling;
  import org.openrdf.rio.Rio;
  import org.openrdf.rio.helpers.RDFHandlerBase;
  import org.openrdf.rio.helpers.ParseErrorLogger;
  import org.openrdf.rio.n3.N3ParserFactory;
  import org.openrdf.rio.ntriples.NTriplesParserFactory;
  import org.openrdf.rio.rdfxml.RDFXMLParserFactory;
  import org.openrdf.rio.trig.TriGParserFactory;
  import org.openrdf.rio.trix.TriXParserFactory;
  import org.openrdf.rio.turtle.TurtleParserFactory;
  
  import virtuoso.sql.ExtendedString;
  import virtuoso.sql.RdfBox;
  
  
  /**
   * Main interface for updating data in and performing queries on a Sesame
   * repository. By default, a RepositoryConnection is in autoCommit mode, meaning
   * that each operation corresponds to a single transaction on the underlying
   * store. autoCommit can be switched off in which case it is up to the user to
   * handle transaction commit/rollback. Note that care should be taking to always
   * properly close a RepositoryConnection after one is finished with it, to free
   * up resources and avoid unnecessary locks.
   * <p>
   * Several methods take a vararg argument that optionally specifies a (set of)
   * context(s) on which the method should operate. Note that a vararg parameter
   * is optional, it can be completely left out of the method call, in which case
   * a method either operates on a provided statements context (if one of the
   * method parameters is a statement or collection of statements), or operates on
   * the repository as a whole, completely ignoring context. A vararg argument may
   * also be 'null' (cast to Resource) meaning that the method operates on those
   * statements which have no associated context only.
   * <p>
   * Examples:
   * 
   * <pre>
   * // Ex 1: this method retrieves all statements that appear in either context1 or context2, or both.
   * RepositoryConnection.getStatements(null, null, null, true, context1, context2);
   * 
   * // Ex 2: this method retrieves all statements that appear in the repository (regardless of context).
   * RepositoryConnection.getStatements(null, null, null, true);
   * 
   * // Ex 3: this method retrieves all statements that have no associated context in the repository.
   * // Observe that this is not equivalent to the previous method call.
   * RepositoryConnection.getStatements(null, null, null, true, (Resource)null);
   * 
   * // Ex 4: this method adds a statement to the store. If the statement object itself has 
   * // a context (i.e. statement.getContext() != null) the statement is added to that context. Otherwise,
   * // it is added without any associated context.
   * RepositoryConnection.add(statement);
   * 
   * // Ex 5: this method adds a statement to context1 in the store. It completely ignores any
   * // context the statement itself has.
   * RepositoryConnection.add(statement, context1);
   * </pre>
   * 
   */
  public class VirtuosoRepositoryConnection implements RepositoryConnection {
  	private static Resource nilContext;
  	private Connection quadStoreConnection;
  	protected VirtuosoRepository repository;
  	static final String S_INSERT = "sparql insert into graph iri(??) { `iri(??)` `iri(??)` `bif:__rdf_long_from_batch_params(??,??,??)` }";
          static final String S_DELETE = "sparql delete from graph iri(??) {`iri(??)` `iri(??)` `bif:__rdf_long_from_batch_params(??,??,??)`}";
          static final String S_TTLP_INSERT = "DB.DBA.TTLP(?,'',?,255)";
          static final int MAX_CMD_SIZE = 36000;
  
  	private int BATCH_SIZE = 5000;
  	private PreparedStatement psInsert;
  	private HashMap<String, StringBuilder> batchData = new HashMap<String,StringBuilder>();
  	private int psInsertCount = 0;
  	private boolean useLazyAdd = false;
  	private int prefetchSize = 200;
  
  	private volatile ParserConfig parserConfig = new ParserConfig(true, true, false, DatatypeHandling.IGNORE);
  
  
  	public VirtuosoRepositoryConnection(VirtuosoRepository repository, Connection connection) throws RepositoryException {
  		this.quadStoreConnection = connection;
  		this.repository = repository;
  		this.useLazyAdd = repository.useLazyAdd;
  		this.prefetchSize = repository.prefetchSize;
  		this.BATCH_SIZE = repository.batchSize;
  		this.nilContext = new ValueFactoryImpl().createURI(repository.defGraph);
  		this.repository.initialize();
  
  	}
  
  	/**
  	 * Returns the Repository object to which this connection belongs.
  	 */
  	public Repository getRepository() {
  		return repository;
  	}
  
  
  	/**
  	 * Set the parser configuration this connection should use for
  	 * RDFParser-based operations.
  	 * 
  	 * @param config
  	 *        a Rio RDF Parser configuration.
  	 */
  	public void setParserConfig(ParserConfig config)
  	{
  		this.parserConfig = parserConfig;
  	}
  
  	/**
  	 * Returns the parser configuration this connection uses for Rio-based
  	 * operations.
  	 * 
  	 * @return a Rio RDF parser configuration.
  	 */
  	public ParserConfig getParserConfig()
  	{
  		return parserConfig;
  	}
  	
  	/**
  	 * Gets a ValueFactory for this RepositoryConnection.
  	 * 
  	 * @return A repository-specific ValueFactory.
  	 */
  	public ValueFactory getValueFactory() {
  		return repository.getValueFactory();
          }
  
  	/**
  	 * Checks whether this connection is open. A connection is open from the moment it is created until it is closed.
  	 * 
  	 * @see #close()
  	 */
  	public boolean isOpen() throws RepositoryException {
  		try {
  			return !this.getQuadStoreConnection().isClosed();
  		}
  