小编uld*_*all的帖子

使用jOOQ在PostgreSQL中进行UPSERT

我正在尝试使用jOOQ库在PostgreSQL中执行UPSERT.

为此,我目前正在尝试在jOOQ中实现以下SQL语句:https://stackoverflow.com/a/6527838

到目前为止我的代码看起来像这样:

public class UpsertExecutor {

    private static final Logger logger = LoggerFactory.getLogger(UpsertExecutor.class);

    private final JOOQContextProvider jooqProvider;

    @Inject
    public UpsertExecutor(JOOQContextProvider jooqProvider) {
        Preconditions.checkNotNull(jooqProvider);

        this.jooqProvider = jooqProvider;
    }

    @Transactional
    public <T extends Record> void executeUpsert(Table<T> table, Condition condition, Map<? extends Field<?>, ?> recordValues) {
        /*
         * All of this is for trying to do an UPSERT on PostgreSQL. See:
         * https://stackoverflow.com/a/6527838
         */

        SelectConditionStep<Record1<Integer>> notExistsSelect = jooqProvider.getDSLContext().selectOne().from(table).where(condition);
        SelectConditionStep<Record> insertIntoSelect = jooqProvider.getDSLContext().select(recordValues).whereNotExists(notExistsSelect);

        try {
            int[] result = jooqProvider.getDSLContext().batch(
                jooqProvider.getDSLContext().update(table).set(recordValues).where(condition),
                jooqProvider.getDSLContext().insertInto(table).select(insertIntoSelect) …
Run Code Online (Sandbox Code Playgroud)

java sql postgresql upsert jooq

36
推荐指数
3
解决办法
7002
查看次数

在SQL中跨时间轴汇总值

问题

我有一个PostgreSQL数据库,我试图总结一下收银机的收入.收银机可以具有状态ACTIVE或INACTIVE,但我只想总结在给定时间段内处于ACTIVE状态时创建的收益.

我有两张桌子; 一个标志着收入,另一个标志着收银机状态:

CREATE TABLE counters
(
  id bigserial NOT NULL,
  "timestamp" timestamp with time zone,
  total_revenue bigint,
  id_of_machine character varying(50),
  CONSTRAINT counters_pkey PRIMARY KEY (id)
)

CREATE TABLE machine_lifecycle_events
(
  id bigserial NOT NULL,
  event_type character varying(50),
  "timestamp" timestamp with time zone,
  id_of_affected_machine character varying(50),
  CONSTRAINT machine_lifecycle_events_pkey PRIMARY KEY (id)
)
Run Code Online (Sandbox Code Playgroud)

每1分钟添加一个计数器条目,而total_revenue仅增加.每次机器状态发生变化时,都会添加machine_lifecycle_events条目.

我添加了一个说明问题的图像.应该总结蓝色时期的收入.

时间线显示问题.

到目前为止我尝试过的

我创建了一个查询,它可以在给定的瞬间为我提供总收入:

SELECT total_revenue 
  FROM counters 
 WHERE timestamp < '2014-03-05 11:00:00' 
       AND id_of_machine='1' 
ORDER BY 
       timestamp desc 
 LIMIT 1
Run Code Online (Sandbox Code Playgroud)

问题

  1. 如何计算两个时间戳之间的收入?
  2. 当我必须将machine_lifecycle_events中的时间戳与输入周期进行比较时,如何确定蓝色时段的开始和结束时间戳?

关于如何解决这个问题的任何想法?

更新

示例数据: …

sql postgresql aggregate-functions date-arithmetic window-functions

7
推荐指数
1
解决办法
994
查看次数

@Transactional注释未启动事务(Guice Persist)

我正在尝试使用Guice Persist创建一个带有@Transactional注释方法的简单服务类:

public class TestServiceImpl implements TestService {

    @Inject
    private Provider<EntityManager> entityManager;

    @Override
    @Transactional
    public Long createEvent(String name) {
        Event event = new Event(name);

        System.out.println("Is transaction active? " + entityManager.get().getTransaction().isActive() );

        entityManager.get().persist(event);

        return event.getId();
    }

}
Run Code Online (Sandbox Code Playgroud)

遗憾的是,Guice Persist没有创建任何交易.日志输出可以在这里看到:http://pastebin.com/Mh5BkqSC

请注意第450行的打印:

交易是否有效?假

结果是数据库中没有存储任何内容:

mydb=# \d
                 List of relations
 Schema |        Name        |   Type   |  Owner   
--------+--------------------+----------+----------
 public | events             | table    | postgres
 public | hibernate_sequence | sequence | postgres
(2 rows)

mydb=# select * from events;
 id | …
Run Code Online (Sandbox Code Playgroud)

java hibernate jpa transactions guice-persist

5
推荐指数
1
解决办法
1614
查看次数

在Dart JS interop中提到"this"

我想在Dart中实现以下代码:

var HelloWorldScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
    }
});
Run Code Online (Sandbox Code Playgroud)

我的Dart实现如下所示:

class HelloWorldScene {
  HelloWorldScene() {
    var sceneCollectionJS = new JsObject.jsify({ "onEnter": _onEnter});

    context["HelloWorldScene"] = context["cc"]["Scene"].callMethod("extend", [sceneCollectionJS]);
  }

  void _onEnter() {
    context["this"].callMethod("_super");
  }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,运行代码时出现以下错误:

null对象没有方法'callMethod'

在以下行:

context ["this"].callMethod("_ super",[]);

context ["this"]似乎是null,所以我的问题是:如何从Dart引用"this"变量?

更新1:完整的示例代码可以在github上找到:https: //github.com/uldall/DartCocos2dTest

javascript dart dart-js-interop

5
推荐指数
1
解决办法
95
查看次数

Swagger YAML声明中的子路径

我试图通过在Swagger YAML中描述它来创建REST服务.

该服务有三条路径:

  • /版本
  • /合作伙伴/ {} PARTNERID /用户/ {} userId的/会话
  • /合作伙伴/ {} PARTNERID /书籍/ {} BOOKID /

我当前描述这些路径的YAML文件如下所示:

swagger: '2.0'
info:
  version: '0.0.1'
  title: Test API
host: api.test.com
basePath: /
schemes:
  - https
consumes:
  - application/json
produces:
  - application/json
paths:
  /versions:
    post:
      responses:
        '201':
          description: Returns all versions.
        default:
          description: unexpected error
  /partners/{partnerId}/users/{userId}/sessions:
    parameters:
      - name: partnerId
        in: path
        type: integer
      - name: userId
        in: path
        type: string
    post:
      responses:
        '201':
          description: Returns a UserSession object with info about the …
Run Code Online (Sandbox Code Playgroud)

rest yaml swagger swagger-2.0

5
推荐指数
1
解决办法
2943
查看次数