mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-30 21:08:30 +08:00
This commit is contained in:
parent
fb0620792f
commit
8a7f476e00
@ -51,13 +51,6 @@ class ScalaEntitySerializer(val namePrefix: String, val nameSuffix: String) exte
|
||||
|
||||
var modelName = writer.getRawName(model);
|
||||
|
||||
// entity property fields
|
||||
model.getProperties filter (_.getType.getCategory == ENTITY) foreach { property =>
|
||||
var queryType = typeMappings.getPathType(property.getType, model, false);
|
||||
var typeName = writer.getRawName(queryType);
|
||||
scalaWriter.line("private var _", property.getEscapedName, ": ", typeName, " = _;\n");
|
||||
}
|
||||
|
||||
writeAdditionalFields(model, scalaWriter);
|
||||
|
||||
// additional constructors
|
||||
@ -101,7 +94,7 @@ class ScalaEntitySerializer(val namePrefix: String, val nameSuffix: String) exte
|
||||
var queryType = typeMappings.getPathType(property.getType, model, false);
|
||||
var typeName = writer.getRawName(queryType);
|
||||
var name = property.getEscapedName;
|
||||
val value = String.format("def %1$s: %2$s = { if (_%1$s == null){_%1$s = new %2$s(this, \"%1$s\"); }; _%1$s; }", name, typeName)
|
||||
val value = String.format("lazy val %1$s = new %2$s(this, \"%1$s\");", name, typeName)
|
||||
writer.line(value, "\n");
|
||||
}
|
||||
|
||||
@ -159,7 +152,7 @@ class ScalaEntitySerializer(val namePrefix: String, val nameSuffix: String) exte
|
||||
}
|
||||
|
||||
// writer.publicFinal(ptype, property.getEscapedName, value);
|
||||
writer.line("val ", property.getEscapedName, " = ", value, ";\n");
|
||||
writer.line("lazy val ", property.getEscapedName, " = ", value, ";\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -10,6 +10,8 @@ import com.mysema.query.codegen._
|
||||
import com.mysema.query.sql._
|
||||
import com.mysema.query.sql.support._
|
||||
|
||||
import com.mysema.query.scala.ScalaEntitySerializer
|
||||
|
||||
import java.util._
|
||||
import java.io.IOException
|
||||
|
||||
@ -17,59 +19,19 @@ import scala.reflect.BeanProperty
|
||||
import scala.collection.JavaConversions._
|
||||
import scala.collection.mutable.Set
|
||||
|
||||
class ScalaMetaDataSerializer(val namePrefix: String, val nameSuffix: String, val namingStrategy: NamingStrategy) extends Serializer {
|
||||
class ScalaMetaDataSerializer(namePrefix: String, nameSuffix: String, val namingStrategy: NamingStrategy)
|
||||
extends ScalaEntitySerializer(namePrefix, nameSuffix) {
|
||||
|
||||
val typeMappings = new TypeMappings();
|
||||
|
||||
val classHeaderFormat = "%1$s(path: String) extends RelationalPathBase[%2$s](classOf[%2$s], path)";
|
||||
|
||||
//val javadocSuffix = " is a Querydsl query type";
|
||||
override val classHeaderFormat = "%1$s(path: String) extends RelationalPathImpl[%2$s](classOf[%2$s], path)";
|
||||
|
||||
def this(namePrefix: String, namingStrategy: NamingStrategy) = this(namePrefix, "", namingStrategy);
|
||||
|
||||
def serialize(model: EntityType, serializerConfig: SerializerConfig, writer: CodeWriter) {
|
||||
val scalaWriter = writer.asInstanceOf[ScalaWriter];
|
||||
val simpleName: String = model.getSimpleName;
|
||||
|
||||
// package
|
||||
if (!model.getPackageName.isEmpty()) {
|
||||
writer.packageDecl(model.getPackageName);
|
||||
}
|
||||
|
||||
// imports
|
||||
writer.importPackages("com.mysema.query.sql", "com.mysema.query.types.path");
|
||||
|
||||
var importedClasses = getAnnotationTypes(model);
|
||||
importedClasses.add("java.util.Arrays");
|
||||
if (model.hasLists()) {
|
||||
importedClasses.add(classOf[List[_]].getName);
|
||||
}
|
||||
if (model.hasMaps()) {
|
||||
importedClasses.add(classOf[Map[_, _]].getName);
|
||||
}
|
||||
|
||||
writer.importClasses(importedClasses.toArray: _*);
|
||||
|
||||
// javadoc
|
||||
//writer.javadoc(simpleName + javadocSuffix);
|
||||
override def writeHeader(model: EntityType, writer: ScalaWriter) {
|
||||
writer.imports(classOf[RelationalPathImpl[_]]);
|
||||
super.writeHeader(model, writer);
|
||||
}
|
||||
|
||||
val queryType = typeMappings.getPathType(model, model, true);
|
||||
var modelName = writer.getRawName(model);
|
||||
var queryTypeName = writer.getRawName(queryType);
|
||||
var classHeader = String.format(classHeaderFormat, queryTypeName, modelName);
|
||||
|
||||
scalaWriter.beginObject(queryTypeName);
|
||||
scalaWriter.line("def as(variable: String) = new ", queryTypeName, "(variable)");
|
||||
scalaWriter.end();
|
||||
|
||||
// header
|
||||
model.getAnnotations.foreach(writer.annotation(_));
|
||||
|
||||
scalaWriter.beginClass(classHeader);
|
||||
|
||||
// properties
|
||||
serializeProperties(model, writer, model.getProperties);
|
||||
|
||||
override def writeAdditionalProperties(model: EntityType, writer: ScalaWriter) {
|
||||
// primary keys
|
||||
val primaryKeys: Collection[PrimaryKeyData] =
|
||||
model.getData.get(classOf[PrimaryKeyData]).asInstanceOf[Collection[PrimaryKeyData]];
|
||||
@ -90,32 +52,6 @@ class ScalaMetaDataSerializer(val namePrefix: String, val nameSuffix: String, va
|
||||
if (inverseForeignKeys != null) {
|
||||
serializeForeignKeys(model, writer, inverseForeignKeys, true);
|
||||
}
|
||||
|
||||
writer.end();
|
||||
}
|
||||
|
||||
def serializeProperties(model: EntityType, writer: CodeWriter, properties: Collection[Property]) {
|
||||
properties foreach { property =>
|
||||
val methodName: String = property.getType.getCategory match {
|
||||
case COMPARABLE => "createComparable";
|
||||
case BOOLEAN => "createBoolean";
|
||||
case DATE => "createDate";
|
||||
case DATETIME => "createDateTime";
|
||||
case ENUM => "createEnum";
|
||||
case NUMERIC => "createNumber";
|
||||
case STRING => "createString";
|
||||
case SIMPLE => "createSimple";
|
||||
case TIME => "createTime";
|
||||
}
|
||||
var ptype = typeMappings.getPathType(property.getType, model, false);
|
||||
var value: String = null;
|
||||
if (property.getType.getCategory == BOOLEAN || property.getType.getCategory == STRING) {
|
||||
value = methodName + "(\"" + property.getName + "\")";
|
||||
} else {
|
||||
value = methodName + "(\"" + property.getName + "\", classOf[" + writer.getRawName(property.getType) + "])";
|
||||
}
|
||||
writer.publicFinal(ptype, property.getEscapedName, value);
|
||||
}
|
||||
}
|
||||
|
||||
def serializePrimaryKeys(model: EntityType, writer: CodeWriter, primaryKeys: Collection[PrimaryKeyData]) {
|
||||
@ -174,9 +110,4 @@ class ScalaMetaDataSerializer(val namePrefix: String, val nameSuffix: String, va
|
||||
}
|
||||
}
|
||||
|
||||
def getAnnotationTypes(model: EntityType): Set[String] = {
|
||||
val imports = Set();
|
||||
imports ++ (model.getAnnotations.map(_.annotationType.getName))
|
||||
}
|
||||
|
||||
}
|
||||
@ -45,39 +45,38 @@ object QPerson {
|
||||
}
|
||||
|
||||
class QPerson(cl: Class[_ <: Person], md: PathMetadata[_]) extends EntityPathImpl[Person](cl, md) {
|
||||
private var _other: QPerson = _;
|
||||
|
||||
def this(variable: String) = this(classOf[Person], forVariable(variable));
|
||||
|
||||
def this(parent: Path[_], variable: String) = this(classOf[Person], forProperty(parent, variable));
|
||||
|
||||
def other: QPerson = { if (_other == null){_other = new QPerson(this, "other"); }; _other; }
|
||||
lazy val other = new QPerson(this, "other");
|
||||
|
||||
val firstName = createString("firstName");
|
||||
lazy val firstName = createString("firstName");
|
||||
|
||||
val scalaMap = createMap("scalaMap", classOf[String], classOf[String], classOf[StringPath]);
|
||||
lazy val scalaMap = createMap("scalaMap", classOf[String], classOf[String], classOf[StringPath]);
|
||||
|
||||
val scalaInt = createNumber("scalaInt", classOf[Integer]);
|
||||
lazy val scalaInt = createNumber("scalaInt", classOf[Integer]);
|
||||
|
||||
val javaCollection = createCollection("javaCollection", classOf[String], classOf[StringPath]);
|
||||
lazy val javaCollection = createCollection("javaCollection", classOf[String], classOf[StringPath]);
|
||||
|
||||
val javaInt = createNumber("javaInt", classOf[Integer]);
|
||||
lazy val javaInt = createNumber("javaInt", classOf[Integer]);
|
||||
|
||||
val scalaList = createList("scalaList", classOf[String], classOf[StringPath]);
|
||||
lazy val scalaList = createList("scalaList", classOf[String], classOf[StringPath]);
|
||||
|
||||
val javaMap = createMap("javaMap", classOf[String], classOf[String], classOf[StringPath]);
|
||||
lazy val javaMap = createMap("javaMap", classOf[String], classOf[String], classOf[StringPath]);
|
||||
|
||||
val javaList = createList("javaList", classOf[String], classOf[StringPath]);
|
||||
lazy val javaList = createList("javaList", classOf[String], classOf[StringPath]);
|
||||
|
||||
val javaDouble = createNumber("javaDouble", classOf[java.lang.Double]);
|
||||
lazy val javaDouble = createNumber("javaDouble", classOf[java.lang.Double]);
|
||||
|
||||
val javaSet = createSet("javaSet", classOf[String], classOf[StringPath]);
|
||||
lazy val javaSet = createSet("javaSet", classOf[String], classOf[StringPath]);
|
||||
|
||||
val lastName = createString("lastName");
|
||||
lazy val lastName = createString("lastName");
|
||||
|
||||
val array = createArray("array", classOf[Array[String]]);
|
||||
lazy val array = createArray("array", classOf[Array[String]]);
|
||||
|
||||
val listOfPersons = createList("listOfPersons", classOf[Person], classOf[QPerson]);
|
||||
lazy val listOfPersons = createList("listOfPersons", classOf[Person], classOf[QPerson]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -49,7 +49,7 @@ class ScalaBeanSerializerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
def Print() {
|
||||
def Print {
|
||||
val serializer = new com.mysema.query.scala.sql.ScalaBeanSerializer();
|
||||
serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new ScalaWriter(writer));
|
||||
val str = writer.toString().replaceAll("\\s+", " ");
|
||||
|
||||
@ -36,15 +36,14 @@ class ScalaMetaDataSerializerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
def Print() {
|
||||
def Print {
|
||||
val namingStrategy = new DefaultNamingStrategy();
|
||||
val serializer = new ScalaMetaDataSerializer("Q", "", namingStrategy);
|
||||
serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new ScalaWriter(writer));
|
||||
val str = writer.toString();
|
||||
assertTrue("companion object isn't before class", str.indexOf("object") < str.indexOf("class"));
|
||||
assertTrue("companion object isn't before annotations", str.indexOf("object") < str.indexOf("@Table"));
|
||||
System.err.println(str);
|
||||
|
||||
System.err.println(str);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user