簡體   English   中英

FreeMarker中日期的默認值

[英]Default value for date in FreeMarker

我正在使用Freemarker模板引擎使用來自csv文件的數據在Postgres中生成插入語句。 DB中有一個日期類型的列( 'birthdate' ),如果不存在該值(以使Postgres平和)或實際的出生日期(如果提供),則插入內容應為NULL。 像這樣的東西:

INSERT INTO web_iws.broker (id,create_timestamp,update_timestamp,sfid,deleted,account_name,first_name,last_name,title,ssn,phone,mobile_phone,fax,email,mailing_street,mailing_city,mailing_state,mailing_postal_code,mailing_country,broker_number,npn,ga,ga_number,birthdate,appointed,indor_corp) VALUES(nextval('web_iws.broker_id_seq'::regclass),now(),now(),'003i00234597gnNAAQ',FALSE,'001i045681aYEiMAAW','Mason','Demarcove','Broker','','','','','mrdemarcove@abc.com','','','','','','BRN7J0943','','Savoy Associates','NJGA0306',NULL,'Appointed','');

INSERT INTO web_iws.broker (id,create_timestamp,update_timestamp,sfid,deleted,account_name,first_name,last_name,title,ssn,phone,mobile_phone,fax,email,mailing_street,mailing_city,mailing_state,mailing_postal_code,mailing_country,broker_number,npn,ga,ga_number,birthdate,appointed,indor_corp) VALUES(nextval('web_iws.broker_id_seq'::regclass),now(),now(),'003i07852RjON3sRN',FALSE,'001i0002'43567AAV','Maritza','Galsh','','145-98-1794','','','','marytza@galshben.com','718 River Road','Red Bank','NJ','07701','','BRNJ0877','7420702','Walsh Benefits','NJGA0008','1939-12-03','Appointed','Indv');

Freemarker中是否有比這更優雅的東西來表達上述要求:

"<#if birthdate?has_content>'${birthdate}',<#else>NULL,</#if>"

也許內置的日期可以用可配置的默認值或某種三元表達式替換空值,從而可以刪除if的...

先感謝您。

更新資料

我嘗試了@ddekany發布的建議,這是我看到的內容:

具有空生日值( NJGA0001,,Appointed之間的空白)的隨機原始數據記錄(在csv中)如下所示:

2015-07-28T19:59:09.000Z,2015-07-28T19:59:15.000Z,003i000002oQSRMAA4,FALSE,Centerpoint Consulting,Briant,Hlonan,,,(973) 522-7886,,,briant@centerpointbenefits.com,,,,,,BRNJ0780,,BenefitMall,NJGA0001,,Appointed,Individual

當嘗試使用:

"${birthdate?has_content?then(\"'${birthdate}'\", 'NULL')}"+

在我的Java模板中,出現錯誤:

11:52:37.168 [main] DEBUG util.BrokerInsertGeneratorTest - Error during saml template buildingfreemarker.core.ParseException: Error on line 1, column 672, in template insertProducingTemplate
Found then, expecting one of:  chunk,  is_date,  last,  root,  j_string,  contains,  is_hash,  long,  float,  ends_with,  namespace,  matches,  time,  values,  seq_last_index_of,  uncap_first,  byte,  substring,  is_transform,  web_safe,  groups,  seq_contains,  is_macro,  index_of,  word_list,  int,  is_method,  eval,  parent,  xml,  number,  capitalize,  if_exists,  rtf,  node_type,  double,  is_directive,  url,  size,  default,  is_boolean,  split,  node_name,  is_enumerable,  seq_index_of,  is_sequence,  sort,  is_node,  sort_by,  left_pad,  cap_first,  interpret,  children,  node_namespace,  chop_linebreak,  date,  short,  last_index_of,  is_collection,  ancestors,  length,  trim,  datetime,  is_string,  reverse,  c,  keys,  upper_case,  js_string,  has_content,  right_pad,  replace,  is_hash_ex,  new,  is_number,  is_indexable,  lower_case,  string,  exists,  html,  first,  starts_with

使用此表達式:

"${(\"'${birthdate}',\")!'NULL'}"+

產生以下內容:

INSERT INTO web_iws.broker (id,create_timestamp,update_timestamp,sfid,deleted,account_name,first_name,last_name,title,ssn,phone,mobile_phone,fax,email,mailing_street,mailing_city,mailing_state,mailing_postal_code,mailing_country,broker_number,npn,ga,ga_number,birthdate,appointed,indor_corp) VALUES(nextval('web_iws.broker_id_seq'::regclass),now(),now(),'003i000002oQSRMAA4',FALSE,'Centerpoint Consulting','Briant','Hlonan','','','(973) 522-7886','','','briant@centerpointbenefits.com','','','','','','BRNJ0780','','BenefitMall','NJGA0001','','Appointed','Individual');

這會更好,因為它會創建'NJGA0001','','Appointed' ,但Postgres不會接受-對於空的日期字段,它需要為NULL。

我願意聽到其他建議。

這是可以使用的Java模板字符串,但是在處理其if語句時需要較少的冗長:

public final static String templateStr = "INSERT INTO web_iws.broker (id,create_timestamp,update_timestamp,"
            + "sfid,deleted,account_name,first_name,last_name,title,ssn,phone,mobile_phone,fax,email,mailing_street,mailing_city,mailing_state,mailing_postal_code,mailing_country,broker_number,npn,ga,ga_number,birthdate,appointed,indor_corp) "+
 "VALUES(nextval('web_iws.broker_id_seq'::regclass),now(),now(),"+
"'${sfid}',"+
"${deleted},"+
"'${account_name}',"+
"'${first_name}',"+
"'${last_name}',"+
"'${title}',"+
"'${ssn}',"+
"'${phone}',"+
"'${mobile_phone}',"+
"'${fax}',"+
"'${email}',"+
"'${mailing_street}',"+
"'${mailing_city}',"+
"'${mailing_state}',"+
"'${mailing_postal_code}',"+
"'${mailing_country}',"+
"'${broker_number}',"+
"'${npn}',"+
"'${ga}',"+
"'${ga_number}',"+
"<#if birthdate?has_content>'${birthdate}',<#else>NULL,</#if>"+
"'${appointed}',"+
"'${indor_corp}');";

您可以使用postgres的to_char函數。 例如:

<#if birthdate?has_content>to_char('${birthdate}', 'YYYY-MM-DD'),<#else>NULL,</#if>

http://www.postgresql.org/docs/current/static/functions-formatting.html

沒有三元運算符,但是有一個等效的內置函數:

${birthdate?has_content?then("'${birthdate}'", 'NULL')}

還有一個專門的運算符為缺失值提供默認值,例如someValue!defaultValue 您的情況有些棘手,因為您需要添加這些引號,因此它將類似於:

${("'${birthdate}'")!'NULL'}

另外,請注意,空字符串不算作!缺失! 運算符,與?has_content不同。

最后同樣重要的是,如果您#function執行此操作,則可能需要為其定義一個#function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM