All Downloads are FREE. Search and download functionalities are using the official Maven repository.

t.greendao-generator.3.2.2.source-code.dao.ftl Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
<#--

Copyright (C) 2011-2016 Markus Junginger, greenrobot (http://greenrobot.org)
                                                                           
This file is part of greenDAO Generator.                                   
                                                                           
greenDAO Generator 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, either version 3 of the License, or          
(at your option) any later version.                                        
greenDAO Generator 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 greenDAO Generator.  If not, see .

-->
<#-- @ftlvariable name="entity" type="org.greenrobot.greendao.generator.Entity" -->
<#-- @ftlvariable name="schema" type="org.greenrobot.greendao.generator.Schema" -->

<#assign toBindType = {"Boolean":"Long", "Byte":"Long", "Short":"Long", "Int":"Long", "Long":"Long", "Float":"Double", "Double":"Double", "String":"String", "ByteArray":"Blob", "Date": "Long" } />
<#assign toCursorType = {"Boolean":"Short", "Byte":"Short", "Short":"Short", "Int":"Int", "Long":"Long", "Float":"Float", "Double":"Double", "String":"String", "ByteArray":"Blob", "Date": "Long"  } />
package ${entity.javaPackageDao};

<#if entity.toOneRelations?has_content || entity.incomingToManyRelations?has_content>
import java.util.List;

<#if entity.toOneRelations?has_content>
import java.util.ArrayList;

import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
<#if entity.toOneRelations?has_content>
import org.greenrobot.greendao.internal.SqlUtils;

import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
<#if entity.incomingToManyRelations?has_content>
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;


<#if entity.javaPackageDao != schema.defaultJavaPackageDao>
import ${schema.defaultJavaPackageDao}.${schema.prefix}DaoSession;


<#if entity.additionalImportsDao?has_content>
<#list entity.additionalImportsDao as additionalImport>
import ${additionalImport};



<#if entity.javaPackageDao != entity.javaPackage>
import ${entity.javaPackage}.${entity.className};


<#if entity.protobuf>
import ${entity.javaPackage}.${entity.className}.Builder;


// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "${entity.dbName}".
*/
public class ${entity.classNameDao} extends AbstractDao<${entity.className}, ${entity.pkType}> {

    public static final String TABLENAME = "${entity.dbName}";

    /**
     * Properties of entity ${entity.className}.
* Can be used for QueryBuilder and for referencing column names. */ public static class Properties { <#list entity.propertiesColumns as property> public final static Property ${property.propertyName?cap_first} = new Property(${property_index}, ${property.javaType}.class, "${property.propertyName}", ${property.primaryKey?string}, "${property.dbName}"); } <#if entity.active> private ${schema.prefix}DaoSession daoSession; <#list entity.properties as property><#if property.customType?has_content><#-- --> private final ${property.converterClassName} ${property.propertyName}Converter = new ${property.converterClassName}(); <#list entity.incomingToManyRelations as toMany> private Query<${toMany.targetEntity.className}> ${toMany.sourceEntity.className?uncap_first}_${toMany.name?cap_first}Query; public ${entity.classNameDao}(DaoConfig config) { super(config); } public ${entity.classNameDao}(DaoConfig config, ${schema.prefix}DaoSession daoSession) { super(config, daoSession); <#if entity.active> this.daoSession = daoSession; } <#if !entity.skipCreationInDb> /** Creates the underlying database table. */ public static void createTable(Database db, boolean ifNotExists) { String constraint = ifNotExists? "IF NOT EXISTS ": ""; db.execSQL("CREATE TABLE " + constraint + "\"${entity.dbName}\" (" + // <#list entity.propertiesColumns as property> "\"${property.dbName}\" ${property.dbType}<#if property.constraints??> ${property.constraints} <#if property_has_next>," +<#else>);"); // ${property_index}: ${property.propertyName} <#if entity.indexes?has_content > // Add Indexes <#list entity.indexes as index> db.execSQL("CREATE <#if index.unique>UNIQUE INDEX " + constraint + "${index.name} ON \"${entity.dbName}\"" + " (<#list index.properties as property>\"${property.dbName}\"<#if (index.propertiesOrder[property_index])??> ${index.propertiesOrder[property_index]}<#sep>,);"); } /** Drops the underlying database table. */ public static void dropTable(Database db, boolean ifExists) { String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"${entity.dbName}\""; db.execSQL(sql); } <#assign stmtTypes = ["DatabaseStatement", "SQLiteStatement"] /> <#list stmtTypes as stmtType> @Override protected final void bindValues(${stmtType} stmt, ${entity.className} entity) { stmt.clearBindings(); <#list entity.properties as property> <#if property.notNull || entity.protobuf> <#if entity.protobuf> if(entity.has${property.propertyName?cap_first}()) { stmt.bind${toBindType[property.propertyType]}(${property_index + 1}, ${property.databaseValueExpressionNotNull}); <#if entity.protobuf> } <#else> <#-- nullable, non-protobuff --> ${property.javaTypeInEntity} ${property.propertyName} = entity.get${property.propertyName?cap_first}(); if (${property.propertyName} != null) { stmt.bind${toBindType[property.propertyType]}(${property_index + 1}, ${property.databaseValueExpression}); } <#list entity.toOneRelations as toOne> <#if !toOne.fkProperties?has_content> ${toOne.targetEntity.className} ${toOne.name} = entity.peak${toOne.name?cap_first}(); if(${toOne.name} != null) { ${toOne.targetEntity.pkProperty.javaType} ${toOne.name}__targetKey = ${toOne.name}.get${toOne.targetEntity.pkProperty.propertyName?cap_first}(); <#if !toOne.targetEntity.pkProperty.notNull> if(${toOne.name}__targetKey != null) { // TODO bind ${toOne.name}__targetKey } <#else> // TODO bind ${toOne.name}__targetKey } } <#if entity.active && !entity.protobuf> @Override protected final void attachEntity(${entity.className} entity) { super.attachEntity(entity); entity.__setDaoSession(daoSession); } @Override public ${entity.pkType} readKey(Cursor cursor, int offset) { <#if entity.pkProperty??> return <#if !entity.pkProperty.notNull>cursor.isNull(offset + ${entity.pkProperty.ordinal}) ? null : <#if entity.pkProperty.propertyType == "Byte">(byte) <#if entity.pkProperty.propertyType == "Date">new java.util.Date(cursor.get${toCursorType[entity.pkProperty.propertyType]}(offset + ${entity.pkProperty.ordinal})<#if entity.pkProperty.propertyType == "Boolean"> != 0<#if entity.pkProperty.propertyType == "Date">); <#else> return null; } @Override public ${entity.className} readEntity(Cursor cursor, int offset) { <#if entity.protobuf> Builder builder = ${entity.className}.newBuilder(); <#list entity.properties as property> <#if !property.notNull> if (!cursor.isNull(offset + ${property_index})) { builder.set${property.propertyName?cap_first}(cursor.get${toCursorType[property.propertyType]}(offset + ${property_index})); <#if !property.notNull> } return builder.build(); <#elseif entity.constructors> <#-- ############################## readEntity non-protobuff, constructor ############################## --> ${entity.className} entity = new ${entity.className}( // <#list entity.properties as property> <#if !property.notNull>cursor.isNull(offset + ${property_index}) ? null : <#-- -->${property.getEntityValueExpression("cursor.get${toCursorType[property.propertyType]}(offset + ${property_index})")}<#-- --><#if property_has_next>, // ${property.propertyName} ); return entity; <#else> <#-- ############################## readEntity non-protobuff, setters ############################## --> ${entity.className} entity = new ${entity.className}(); readEntity(cursor, entity, offset); return entity; } @Override public void readEntity(Cursor cursor, ${entity.className} entity, int offset) { <#if entity.protobuf> throw new UnsupportedOperationException("Protobuf objects cannot be modified"); <#else> <#list entity.properties as property> entity.set${property.propertyName?cap_first}(<#if !property.notNull>cursor.isNull(offset + ${property_index}) ? null : <#-- -->${property.getEntityValueExpression("cursor.get${toCursorType[property.propertyType]}(offset + ${property_index})")}); } @Override protected final ${entity.pkType} updateKeyAfterInsert(${entity.className} entity, long rowId) { <#if entity.pkProperty??> <#if entity.pkProperty.propertyType == "Long"> <#if !entity.protobuf> entity.set${entity.pkProperty.propertyName?cap_first}(rowId); return rowId; <#else> return entity.get${entity.pkProperty.propertyName?cap_first}(); <#else> // Unsupported or missing PK type return null; } @Override public ${entity.pkType} getKey(${entity.className} entity) { <#if entity.pkProperty??> if(entity != null) { return entity.get${entity.pkProperty.propertyName?cap_first}(); } else { return null; } <#else> return null; } @Override public boolean hasKey(${entity.className} entity) { <#if entity.pkProperty??> <#if entity.pkProperty.notNull> throw new UnsupportedOperationException("Unsupported for entities with a non-null key"); <#else> <#if entity.protobuf> return entity.has${entity.pkProperty.propertyName?cap_first}(); <#else> return entity.get${entity.pkProperty.propertyName?cap_first}() != null; <#else> // TODO return false; } @Override protected final boolean isEntityUpdateable() { return ${(!entity.protobuf)?string}; } <#list entity.incomingToManyRelations as toMany> /** Internal query to resolve the "${toMany.name}" to-many relationship of ${toMany.sourceEntity.className}. */ public List<${toMany.targetEntity.className}> _query${toMany.sourceEntity.className?cap_first}_${toMany.name?cap_first}(<#-- --><#if toMany.targetProperties??><#list toMany.targetProperties as property><#-- -->${property.javaType} ${property.propertyName}<#if property_has_next>, <#else><#-- -->${toMany.sourceProperty.javaType} ${toMany.sourceProperty.propertyName}) { synchronized (this) { if (${toMany.sourceEntity.className?uncap_first}_${toMany.name?cap_first}Query == null) { QueryBuilder<${toMany.targetEntity.className}> queryBuilder = queryBuilder(); <#if toMany.targetProperties??> <#list toMany.targetProperties as property> queryBuilder.where(Properties.${property.propertyName?cap_first}.eq(null)); <#else> queryBuilder.join(${toMany.joinEntity.className}.class, ${toMany.joinEntity.classNameDao}.Properties.${toMany.targetProperty.propertyName?cap_first}) .where(${toMany.joinEntity.classNameDao}.Properties.${toMany.sourceProperty.propertyName?cap_first}.eq(${toMany.sourceProperty.propertyName})); <#if toMany.order?has_content> queryBuilder.orderRaw("${toMany.order}"); ${toMany.sourceEntity.className?uncap_first}_${toMany.name?cap_first}Query = queryBuilder.build(); } } Query<${toMany.targetEntity.className}> query = ${toMany.sourceEntity.className?uncap_first}_${toMany.name?cap_first}Query.forCurrentThread(); <#if toMany.targetProperties??> <#list toMany.targetProperties as property> query.setParameter(${property_index}, ${property.propertyName}); <#else> query.setParameter(0, ${toMany.sourceProperty.propertyName}); return query.list(); } <#if entity.toOneRelations?has_content> <#include "dao-deep.ftl"> }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy