sql中時間巨集替換

2021-09-01 09:28:39 字數 1553 閱讀 4608

之前做了乙個sql任務排程平台,sql根據配置的cron表示式執行;排程平台需要靈活的根據當前時間去計算並替換sql中特殊的時間巨集;舉個例子

select * from order a join order order_item b on a.id=b.order_id and a.day = $ 

and b.day=$ and a.signed_time>$

如果今天是20181119,那排程平台執行的sql就需要替換$$,替換後如下

select * from order a join order order_item b on a.id=b.order_id and a.day = 20181118 and b.day=20181118 

and a.signed_time>1542463989123

**也很簡單

public class test " +

"and b.day=$ and a.signed_time>$";

string pattern = "\\$\\";

pattern r = pattern.compile(pattern);

matcher m = r.matcher(sql);

setdates = new hashset();

while(m.find())

system.out.println("需要格式化的時間串:"+dates);

for(string s:dates)";

sql = sql.replaceall(st,date_str(s));

// system.out.println(line.replaceall("\\$\\","20181109"));

}system.out.println(sql);

system.out.println(date_str("date-1,yyyymmdd"));

system.out.println(date_str("date-1"));

system.out.println(date_str("date-1,yyyymmddmmss"));

}public static string date_str(string str)else if(arr.length==2)else

return df.format(cal.gettime());

}}

輸出如下

需要格式化的時間串:[date-2, date- 1,yyyymmdd, date-1,yyyymmdd]

select * from order a join order order_item b on a.id=b.order_id and a.day = 20181118 and b.day=20181118 and a.signed_time>1542463989123

20181118

1542550389166

2018111813166

end

python 巨集替換 和 在巨集替換中的作用

include define f a,b a b define g a a define h a g a int main printf s n h f 1,2 printf s n g f 1,2 return 0 首先需要了解 和 的意義。將右邊的引數做整體的字串替換。define g a a ...

C 中的巨集替換

1.引子 define cat x,y x y 那麼cat a,b 和cat cat a,b c 的結果是啥.define str impl x x define str x str impl x 的意圖何在.2.規則 巨集替換是c c 的預處理中的一部分,在c 標準中有4條規則來定義替換.規則1 ...

python巨集替換 簡單的巨集替換

簡單的巨集替換 1.巨集定義必須寫在第一次使用該巨集定義的 之前 2.巨集定義不是以分號結束的 3.define string1 string2 之間至少要有乙個空格 4.string 1稱為巨集,string2 稱為巨集擴充套件 5.巨集名用大寫的字母表示是乙個習慣 6.使用巨集的好處 a 簡化程...