[Corpusreader-svn] SF.net SVN: corpusreader:[222] trunk/corpusreader/src/main
Status: Alpha
Brought to you by:
sylvainloiseau
|
From: <syl...@us...> - 2009-09-12 16:47:02
|
Revision: 222
http://corpusreader.svn.sourceforge.net/corpusreader/?rev=222&view=rev
Author: sylvainloiseau
Date: 2009-09-12 16:46:55 +0000 (Sat, 12 Sep 2009)
Log Message:
-----------
adding a "unique" feature
Modified Paths:
--------------
trunk/corpusreader/src/main/java/tei/cr/filters/UpdateDatabase.java
trunk/corpusreader/src/main/java/tei/cr/querydoc/FilterArguments.java
trunk/corpusreader/src/main/resources/schema/filters/UpdateDatabase.rnc
Modified: trunk/corpusreader/src/main/java/tei/cr/filters/UpdateDatabase.java
===================================================================
--- trunk/corpusreader/src/main/java/tei/cr/filters/UpdateDatabase.java 2009-09-12 14:49:55 UTC (rev 221)
+++ trunk/corpusreader/src/main/java/tei/cr/filters/UpdateDatabase.java 2009-09-12 16:46:55 UTC (rev 222)
@@ -6,6 +6,8 @@
import java.sql.SQLException;
import java.util.logging.Logger;
import java.util.regex.Pattern;
+import java.util.Set;
+import java.util.HashSet;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@@ -37,11 +39,12 @@
private String DbUserName = null;
private String DbUserPwd = null;
- private PreparedStatement preparedStatement = null;
private String parameterizedQuery = null;
private ExtractLocation[] locators;
private String[] fieldTypes;
+ private boolean[] fieldUnique;
+ private Set<String>[] fieldSet;
private Connection connexion;
private int numberOfRecordsInserted;
@@ -69,13 +72,18 @@
}
String fieldType = fA.getText(element, FilterArguments.UPDATE_DATABASE_FIELD_TYPE);
String locatorName = fA.getText(element, FilterArguments.UPDATE_DATABASE_FIELD_FILTERNAME);
+ String uniquestr = fA.getText(element, FilterArguments.UPDATE_DATABASE_FIELD_UNIQUE);
+ boolean unique = false;
+ if (uniquestr != null && !uniquestr.equals("")) {
+ unique = Boolean.getBoolean(uniquestr);
+ }
ExtractLocation locator = (ExtractLocation) nH.get(locatorName);
if (locator == null) {
throw new WrongArgsException("No filter named \"" + locatorName + "\"found in the pipeline");
}
try {
- setField(i,fieldType,locator);
+ setField(i,fieldType,locator, unique);
} catch (FilterException e) {
throw new WrongArgsException("Unable to set a field: " + e.getMessage(), e);
}
@@ -90,6 +98,10 @@
public void initFields(int nbrOfFields) {
locators = new ExtractLocation[nbrOfFields];
fieldTypes = new String[nbrOfFields];
+ fieldUnique = new boolean[nbrOfFields];
+ for (int i = 0; i < nbrOfFields; i++) {
+ fieldSet[i] = new HashSet<String>();
+ }
}
/**
@@ -104,6 +116,10 @@
* for retreive the value for this field.
*/
public void setField(int indexOfField, String fieldType, ExtractLocation locator) throws FilterException {
+ setField(indexOfField, fieldType, locator, false);
+ }
+
+ public void setField(int indexOfField, String fieldType, ExtractLocation locator, boolean unique) throws FilterException {
if (indexOfField >= locators.length) {
throw new FilterException("Field index out of bound.");
}
@@ -118,42 +134,23 @@
}
locators[indexOfField] = locator;
fieldTypes[indexOfField] = fieldType;
+ fieldUnique[indexOfField] = unique;
}
-
+
////////////////////////////////////////////////////////
/**
* Each end document is a query to the database.
*/
public void endDocument() throws SAXException {
-
- PreparedStatement statement;
- try {
- statement = connexion.prepareStatement(parameterizedQuery);
- } catch (SQLException e) {
- throw new FilterException("Unable to create statement. " + e.getMessage(), e);
+ boolean test = shouldUpdate();
+ if (test) {
+ doStatement();
+ numberOfRecordsInserted++;
+ if ((numberOfRecordsInserted % 1000) == 0) {
+ newConnection();
+ }
}
-
- try {
- setParameters(statement);
- } catch (SQLException e) {
- throw new FilterException("Unable to set parameters. " + e.getMessage(), e);
- }
-
- try {
- int updated = statement.executeUpdate();
- log.info(updated + " row(s) updated.");
- statement.close();
- } catch (SQLException e) {
- throw new FilterException("Database or SQL statement error: " + e.getMessage() + ".", e);
- }
-
- numberOfRecordsInserted++;
- if ((numberOfRecordsInserted % 1000) == 0) {
- commitAndCloseConnexion();
- logNumberOfQueries();
- openConnexion();
- }
super.endDocument();
}
@@ -165,19 +162,15 @@
public void startPipeline() throws FilterException {
// check all the arguments.
- if (fieldTypes == null) {
+ if (fieldTypes == null)
throw new FilterException("Field names may not be null.");
- }
- if (locators == null) {
+ if (locators == null)
throw new FilterException("TEILocator filters may not be null.");
- }
for(int i = 0; i < fieldTypes.length; i++) {
- if (fieldTypes[i] == null || fieldTypes[i].equals("")) {
+ if (fieldTypes[i] == null || fieldTypes[i].equals(""))
throw new FilterException("Field "+ i +" has no name.");
- }
- if (locators[i] == null) {
+ if (locators[i] == null)
throw new FilterException("Field " + i + "has no locator.");
- }
}
// don't forget to load the driver.
@@ -193,6 +186,38 @@
super.startPipeline();
}
+ ////////////////////////
+
+ private void doStatement() throws FilterException {
+ PreparedStatement statement;
+ try {
+ statement = connexion.prepareStatement(parameterizedQuery);
+ } catch (SQLException e) {
+ throw new FilterException("Unable to create statement. " + e.getMessage(), e);
+ }
+
+ try {
+ setParameters(statement);
+ } catch (SQLException e) {
+ throw new FilterException("Unable to set parameters. " + e.getMessage(), e);
+ }
+
+ try {
+ int updated = statement.executeUpdate();
+ log.info(updated + " row(s) updated.");
+ statement.close();
+ } catch (SQLException e) {
+ throw new FilterException("Database or SQL statement error: " + e.getMessage() + ".", e);
+ }
+
+ }
+
+ private void newConnection() throws FilterException {
+ commitAndCloseConnexion();
+ logNumberOfQueries();
+ openConnexion();
+ }
+
private void commitAndCloseConnexion() throws FilterException {
try {
connexion.commit();
@@ -227,6 +252,24 @@
}
/**
+ * Test if this set of parameters should be inserted
+ * into a new query.
+ * @return
+ */
+ private boolean shouldUpdate() {
+ for (int i = 0; i < fieldUnique.length; i++) {
+ if (fieldUnique[i]) {
+ if (fieldSet[i].contains(locators[i].getReference())) {
+ return false;
+ } else {
+ fieldSet[i].add(locators[i].getReference());
+ }
+ }
+ }
+ return true;
+ }
+
+ /**
* Escape slash and quote
*/
private String escape(String toBeEscaped) {
@@ -239,6 +282,4 @@
private void logNumberOfQueries() {
log.info(numberOfRecordsInserted + "queries executed.");
}
-
-
}
Modified: trunk/corpusreader/src/main/java/tei/cr/querydoc/FilterArguments.java
===================================================================
--- trunk/corpusreader/src/main/java/tei/cr/querydoc/FilterArguments.java 2009-09-12 14:49:55 UTC (rev 221)
+++ trunk/corpusreader/src/main/java/tei/cr/querydoc/FilterArguments.java 2009-09-12 16:46:55 UTC (rev 222)
@@ -529,6 +529,7 @@
public final static String UPDATE_DATABASE_FIELDS = "fields/field";
public final static String UPDATE_DATABASE_FIELD_TYPE = "@fieldType";
public final static String UPDATE_DATABASE_FIELD_FILTERNAME = "@filterName";
+ public final static String UPDATE_DATABASE_FIELD_UNIQUE = "@unique";
public final static String UPDATE_DATABASE_QUERY = "preparedQuery/@query";
// BUILD_TABLE_FROM_TEI_LOCATOR
Modified: trunk/corpusreader/src/main/resources/schema/filters/UpdateDatabase.rnc
===================================================================
--- trunk/corpusreader/src/main/resources/schema/filters/UpdateDatabase.rnc 2009-09-12 14:49:55 UTC (rev 221)
+++ trunk/corpusreader/src/main/resources/schema/filters/UpdateDatabase.rnc 2009-09-12 16:46:55 UTC (rev 222)
@@ -13,6 +13,7 @@
element field {
attribute fieldType { string },
attribute filterName { xs:IDREF },
+ attribute unique { ("true" | "false") }?,
empty
}+
},
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|