Skip to content

Commit

Permalink
add skeleton for groovy-sql module
Browse files Browse the repository at this point in the history
  • Loading branch information
paulk-asert committed Jan 3, 2012
1 parent cd61c71 commit 69639ad
Show file tree
Hide file tree
Showing 42 changed files with 1,653 additions and 1,633 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ dependencies {
testCompile "jmock:jmock:1.2.0"
testCompile "jmock:jmock-cglib:1.2.0"
testCompile "xmlunit:xmlunit:1.3"
testCompile "hsqldb:hsqldb:1.8.0.10"
testCompile "ch.qos.logback:logback-classic:0.9.21"
testCompile "log4j:log4j:1.2.16"
testCompile "org.slf4j:jcl-over-slf4j:1.6.0"
Expand Down Expand Up @@ -112,10 +111,11 @@ dependencies {

antlr "org.apache.ant:ant-antlr:1.8.2"

testCompile project(':subprojects:groovy-test')
testCompile project(':subprojects:groovy-jsr223')
testCompile project(':subprojects:groovy-bsf')
testCompile project(':subprojects:groovy-jsr223')
testCompile project(':subprojects:groovy-jmx')
testCompile project(':subprojects:groovy-sql')
testCompile project(':subprojects:groovy-test')
}

sourceSets {
Expand Down
4 changes: 2 additions & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
include 'subprojects:groovy-bsf', 'subprojects:groovy-console', 'subprojects:groovy-jmx',
'subprojects:groovy-jsr223', 'subprojects:groovy-shell', 'subprojects:groovy-swing',
'subprojects:groovy-test'
'subprojects:groovy-jsr223', 'subprojects:groovy-shell', 'subprojects:groovy-sql',
'subprojects:groovy-swing', 'subprojects:groovy-test'
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.codehaus.groovy.runtime.dgmimpl.NumberNumberMultiply;
import org.codehaus.groovy.runtime.dgmimpl.NumberNumberPlus;
import org.codehaus.groovy.runtime.dgmimpl.arrays.*;
import org.codehaus.groovy.runtime.metaclass.ClosureMetaClass;
import org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl;
import org.codehaus.groovy.runtime.metaclass.MissingPropertyExceptionNoStack;
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
Expand Down Expand Up @@ -133,9 +132,9 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {

public static final Class[] DGM_LIKE_CLASSES = new Class[]{
DefaultGroovyMethods.class,
// TODO provide alternative way for this to be registered
// TODO provide alternative way for these to be registered
//SwingGroovyMethods.class,
SqlGroovyMethods.class,
// SqlGroovyMethods.class,
XmlGroovyMethods.class,
EncodingGroovyMethods.class,
DateGroovyMethods.class,
Expand Down
3 changes: 3 additions & 0 deletions subprojects/groovy-console/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ dependencies {
compile project(':subprojects:groovy-swing')
testCompile project(':subprojects:groovy-test')
}

// Required for MessageSourceTest
sourceSets.test.runtimeClasspath += files(sourceSets.test.groovy.srcDirs)
20 changes: 20 additions & 0 deletions subprojects/groovy-sql/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apply plugin: 'groovy'
apply plugin: 'code-quality'

// TODO set these using subprojects?
checkstyleConfigFileName = "../../config/checkstyle/checkstyle.xml"
codeNarcConfigFileName = "../../config/codenarc/codenarc.groovy"

repositories {
mavenCentral()
}

dependencies {
compile project(':')
groovy project(':')
testCompile "hsqldb:hsqldb:1.8.0.10"
testCompile project(':subprojects:groovy-test')
}

// required for DataSet tests
sourceSets.test.runtimeClasspath += files('src/test/groovy')
Original file line number Diff line number Diff line change
@@ -1,104 +1,104 @@
/*
* Copyright 2003-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package groovy.sql;

import groovy.lang.GroovyObjectSupport;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.codehaus.groovy.runtime.InvokerHelper;

import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
* Class which delegates to a Statement but keeps track of a batch count size.
* If the batch count reaches the predefined number, this Statement does an executeBatch()
* automatically. If batchSize is zero, then no batching is performed.
*/
public class BatchingStatementWrapper extends GroovyObjectSupport {
private Statement delegate;
protected int batchSize;
protected int batchCount;
protected Logger log;
protected List<Integer> results;

public BatchingStatementWrapper(Statement delegate, int batchSize, Logger log) {
this.delegate = delegate;
this.batchSize = batchSize;
this.log = log;
reset();
}

protected void reset() {
batchCount = 0;
results = new ArrayList<Integer>();
}

@Override
public Object invokeMethod(String name, Object args) {
return InvokerHelper.invokeMethod(delegate, name, args);
}

public void addBatch(String sql) throws SQLException {
delegate.addBatch(sql);
batchCount++;
if (batchCount == batchSize /* never true for batchSize of 0 */) {
int[] result = delegate.executeBatch();
processResult(result);
batchCount = 0;
}
}

public void clearBatch() throws SQLException {
if (batchSize != 0) {
reset();
}
delegate.clearBatch();
}

public int[] executeBatch() throws SQLException {
int[] lastResult = delegate.executeBatch();
processResult(lastResult);
int[] result = new int[results.size()];
for (int i = 0; i < results.size(); i++) {
result[i] = results.get(i);
}
reset();
return result;
}

protected void processResult(int[] lastResult) {
boolean foundError = false;
for (int i : lastResult) {
if (i == Statement.EXECUTE_FAILED) foundError = true;
results.add(i);
}
// A little bit of paranoid checking here? Most drivers will throw BatchUpdateException perhaps?
if (batchCount != lastResult.length) {
log.warning("Problem executing batch - expected result length of " + batchCount + " but got " + lastResult.length);
} else if (foundError) {
log.warning("Problem executing batch - at least one result failed in: " + DefaultGroovyMethods.toList(lastResult));
} else {
log.fine("Successfully executed batch with " + lastResult.length + " command(s)");
}
}

public void close() throws SQLException {
delegate.close();
}
}
/*
* Copyright 2003-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package groovy.sql;

import groovy.lang.GroovyObjectSupport;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.codehaus.groovy.runtime.InvokerHelper;

import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
* Class which delegates to a Statement but keeps track of a batch count size.
* If the batch count reaches the predefined number, this Statement does an executeBatch()
* automatically. If batchSize is zero, then no batching is performed.
*/
public class BatchingStatementWrapper extends GroovyObjectSupport {
private Statement delegate;
protected int batchSize;
protected int batchCount;
protected Logger log;
protected List<Integer> results;

public BatchingStatementWrapper(Statement delegate, int batchSize, Logger log) {
this.delegate = delegate;
this.batchSize = batchSize;
this.log = log;
reset();
}

protected void reset() {
batchCount = 0;
results = new ArrayList<Integer>();
}

@Override
public Object invokeMethod(String name, Object args) {
return InvokerHelper.invokeMethod(delegate, name, args);
}

public void addBatch(String sql) throws SQLException {
delegate.addBatch(sql);
batchCount++;
if (batchCount == batchSize /* never true for batchSize of 0 */) {
int[] result = delegate.executeBatch();
processResult(result);
batchCount = 0;
}
}

public void clearBatch() throws SQLException {
if (batchSize != 0) {
reset();
}
delegate.clearBatch();
}

public int[] executeBatch() throws SQLException {
int[] lastResult = delegate.executeBatch();
processResult(lastResult);
int[] result = new int[results.size()];
for (int i = 0; i < results.size(); i++) {
result[i] = results.get(i);
}
reset();
return result;
}

protected void processResult(int[] lastResult) {
boolean foundError = false;
for (int i : lastResult) {
if (i == Statement.EXECUTE_FAILED) foundError = true;
results.add(i);
}
// A little bit of paranoid checking here? Most drivers will throw BatchUpdateException perhaps?
if (batchCount != lastResult.length) {
log.warning("Problem executing batch - expected result length of " + batchCount + " but got " + lastResult.length);
} else if (foundError) {
log.warning("Problem executing batch - at least one result failed in: " + DefaultGroovyMethods.toList(lastResult));
} else {
log.fine("Successfully executed batch with " + lastResult.length + " command(s)");
}
}

public void close() throws SQLException {
delegate.close();
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 69639ad

Please sign in to comment.