Use default values for the values section of an insert statement for hsqldb as well as postgres

It blows up expecting a column name in the values parenthesis, but the default values string that
postgres was working seems to work. It's not in http://hsqldb.org/doc/guide/ch09.html, but so it
goes.
This commit is contained in:
Charlie Groves
2011-06-24 19:25:57 +00:00
parent a28990116c
commit 7ba475e61c
5 changed files with 75 additions and 22 deletions
@@ -919,6 +919,12 @@ public abstract class BuildVisitor implements FragmentVisitor<Void>
} }
} }
// output the column name and value portion of an insert when all values are defaulted
protected void appendEmptyInsertValues ()
{
_builder.append("default values");
}
// output the column names and values for an insert // output the column names and values for an insert
protected void appendInsertColumns (InsertClause insertClause) protected void appendInsertColumns (InsertClause insertClause)
{ {
@@ -928,6 +934,18 @@ public abstract class BuildVisitor implements FragmentVisitor<Void>
Set<String> idFields = insertClause.getIdentityFields(); Set<String> idFields = insertClause.getIdentityFields();
ColumnExp<?>[] fields = marsh.getColumnFieldNames(); ColumnExp<?>[] fields = marsh.getColumnFieldNames();
boolean insertingSomething = false;
for (ColumnExp<?> field : fields) {
if (!idFields.contains(field.name)) {
insertingSomething = true;
break;
}
}
if (!insertingSomething) {
appendEmptyInsertValues();
return;
}
_builder.append("("); _builder.append("(");
boolean comma = false; boolean comma = false;
for (ColumnExp<?> field : fields) { for (ColumnExp<?> field : fields) {
@@ -155,6 +155,11 @@ public class MySQLBuilder
bindValue(fullText.getQuery()); bindValue(fullText.getQuery());
_builder.append(" in boolean mode)"); _builder.append(" in boolean mode)");
} }
@Override protected void appendEmptyInsertValues ()
{
_builder.append("() values ()");
}
} }
public MySQLBuilder (DepotTypes types) public MySQLBuilder (DepotTypes types)
@@ -22,8 +22,6 @@ import com.samskivert.depot.Exps;
import com.samskivert.depot.PersistentRecord; import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.annotation.FullTextIndex.Configuration; import com.samskivert.depot.annotation.FullTextIndex.Configuration;
import com.samskivert.depot.annotation.FullTextIndex; import com.samskivert.depot.annotation.FullTextIndex;
import com.samskivert.depot.clause.InsertClause;
import com.samskivert.depot.expression.ColumnExp;
import com.samskivert.depot.impl.expression.IntervalExp; import com.samskivert.depot.impl.expression.IntervalExp;
import com.samskivert.depot.impl.expression.DateFun.DatePart; import com.samskivert.depot.impl.expression.DateFun.DatePart;
import com.samskivert.depot.impl.expression.DateFun.DateTruncate; import com.samskivert.depot.impl.expression.DateFun.DateTruncate;
@@ -121,22 +119,6 @@ public class PostgreSQLBuilder
_builder.append("\"").append(field).append("\""); _builder.append("\"").append(field).append("\"");
} }
@Override protected void appendInsertColumns (InsertClause insertClause)
{
// see if we will be inserting any columns whatsoever
Class<? extends PersistentRecord> pClass = insertClause.getPersistentClass();
Set<String> idFields = insertClause.getIdentityFields();
for (ColumnExp<?> field : _types.getMarshaller(pClass).getColumnFieldNames()) {
if (!idFields.contains(field.name)) {
// we found a field we're inserting, so call super and finish
super.appendInsertColumns(insertClause);
return;
}
}
// we never found anything we'll actually be inserting
_builder.append("default values");
}
protected PGBuildVisitor (DepotTypes types) protected PGBuildVisitor (DepotTypes types)
{ {
super(types, true); super(types, true);
@@ -0,0 +1,31 @@
package com.samskivert.depot;
import com.samskivert.depot.annotation.GeneratedValue;
import com.samskivert.depot.annotation.Id;
import com.samskivert.depot.expression.ColumnExp;
public class AllGeneratedRecord extends PersistentRecord
{
// AUTO-GENERATED: FIELDS START
public static final Class<AllGeneratedRecord> _R = AllGeneratedRecord.class;
public static final ColumnExp<Integer> RECORD_ID = colexp(_R, "recordId");
// AUTO-GENERATED: FIELDS END
public static final int SCHEMA_VERSION = 1;
@Id @GeneratedValue public int recordId;
// AUTO-GENERATED: METHODS START
/**
* Create and return a primary {@link Key} to identify a {@link AllGeneratedRecord}
* with the supplied key values.
*/
public static Key<AllGeneratedRecord> getKey (int recordId)
{
return newKey(_R, recordId);
}
/** Register the key fields in an order matching the getKey() factory. */
static { registerKeyFields(RECORD_ID); }
// AUTO-GENERATED: METHODS END
}
@@ -7,6 +7,7 @@ package com.samskivert.depot;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
@@ -15,22 +16,38 @@ import static org.junit.Assert.assertEquals;
public class GeneratedValueTest public class GeneratedValueTest
{ {
@Test public void insertAutoGeneratedValue () @Before public void createRepository ()
{ {
PersistenceContext pc = TestBase.createPersistenceContext(); PersistenceContext pc = TestBase.createPersistenceContext();
DepotRepository dr = new DepotRepository(pc) { _dr = new DepotRepository(pc) {
@Override @Override
protected void getManagedRecords (Set<Class<? extends PersistentRecord>> classes) { protected void getManagedRecords (Set<Class<? extends PersistentRecord>> classes) {
classes.add(GeneratedValueRecord.class); classes.add(GeneratedValueRecord.class);
classes.add(AllGeneratedRecord.class);
} }
}; };
}
@Test public void insertAutoGeneratedValue ()
{
GeneratedValueRecord rec = new GeneratedValueRecord(); GeneratedValueRecord rec = new GeneratedValueRecord();
rec.value = 2; rec.value = 2;
assertEquals(0, rec.recordId); assertEquals(0, rec.recordId);
assertEquals(1, dr.insert(rec)); assertEquals(1, _dr.insert(rec));
assertEquals(1, rec.recordId); assertEquals(1, rec.recordId);
List<GeneratedValueRecord> recs = dr.from(GeneratedValueRecord.class). List<GeneratedValueRecord> recs = _dr.from(GeneratedValueRecord.class).
where(GeneratedValueRecord.VALUE.eq(2)).select(); where(GeneratedValueRecord.VALUE.eq(2)).select();
assertEquals(1, Iterables.getOnlyElement(recs).recordId); assertEquals(1, Iterables.getOnlyElement(recs).recordId);
} }
@Test public void insertAllGeneratedValue ()
{
AllGeneratedRecord rec = new AllGeneratedRecord();
assertEquals(0, rec.recordId);
assertEquals(1, _dr.insert(rec));
assertEquals(1, rec.recordId);
}
protected DepotRepository _dr;
} }