1 |
|
-module(mongoose_rdbms_timestamp). |
2 |
|
-export([prepare/0, |
3 |
|
select/0, |
4 |
|
select/2]). |
5 |
|
-ignore_xref([select/2]). |
6 |
|
|
7 |
|
-spec prepare() -> ok. |
8 |
|
prepare() -> |
9 |
16 |
mongoose_rdbms:prepare(mim_timestamp, users, [], select_query()), |
10 |
16 |
ok. |
11 |
|
|
12 |
|
select_query() -> |
13 |
16 |
case {mongoose_rdbms:db_engine(global), mongoose_rdbms:db_type()} of |
14 |
|
{mysql, _} -> |
15 |
:-( |
<<"SELECT UNIX_TIMESTAMP()">>; |
16 |
|
{pgsql, _} -> |
17 |
16 |
<<"SELECT CAST(extract(epoch from now()) AS integer)">>; |
18 |
|
{odbc, mssql} -> |
19 |
:-( |
<<"SELECT DATEDIFF_BIG(second, '1970-01-01 00:00:00', GETUTCDATE())">>; |
20 |
|
Other -> |
21 |
:-( |
error({prepare_timestamp_query_failed, Other}) |
22 |
|
end. |
23 |
|
|
24 |
|
-spec select(mongooseim:host_type_or_global(), mongoose_wpool:tag()) -> integer(). |
25 |
|
select() -> |
26 |
15 |
select(global, default). |
27 |
|
|
28 |
|
select(HostType, PoolTag) -> |
29 |
16 |
Res = mongoose_rdbms:execute_successfully(HostType, PoolTag, mim_timestamp, []), |
30 |
16 |
mongoose_rdbms:selected_to_integer(Res). %% ensure it is an integer |