Search in sources :

Example 6 with QueryRunner

use of com.haulmont.bali.db.QueryRunner in project cuba by cuba-platform.

the class DbUpdaterEngine method getExecutedScripts.

protected Set<String> getExecutedScripts() {
    QueryRunner runner = new QueryRunner(getDataSource());
    try {
        // noinspection UnnecessaryLocalVariable
        Set<String> scripts = runner.query("select SCRIPT_NAME from SYS_DB_CHANGELOG", rs -> {
            Set<String> rows = new HashSet<>();
            while (rs.next()) {
                rows.add(rs.getString(1));
            }
            return rows;
        });
        log.trace("Found executed scripts: {}", scripts);
        return scripts;
    } catch (SQLException e) {
        throw new RuntimeException(ERROR + "Error loading executed scripts", e);
    }
}
Also used : QueryRunner(com.haulmont.bali.db.QueryRunner)

Example 7 with QueryRunner

use of com.haulmont.bali.db.QueryRunner in project cuba by cuba-platform.

the class SeveralFetchGroupsTest method tearDown.

@After
public void tearDown() throws SQLException {
    QueryRunner runner = new QueryRunner(cont.persistence().getDataSource());
    runner.update("update TEST_SEVERAL_FETCH_GROUPS_TARIFF set ACTIVE_VERSION_ID = null");
    cont.deleteRecord("TEST_SEVERAL_FETCH_GROUPS_TARIFF_VERSION", tariffVersionId3, tariffVersionId2, tariffVersionId1);
    cont.deleteRecord("TEST_SEVERAL_FETCH_GROUPS_TARIFF", tariffId4_2, tariffId3_1, tariffId2_1, tariffId1);
}
Also used : QueryRunner(com.haulmont.bali.db.QueryRunner)

Example 8 with QueryRunner

use of com.haulmont.bali.db.QueryRunner in project cuba by cuba-platform.

the class SoftDeleteDataManagerTest method testHardDelete.

@Test
public void testHardDelete() throws Exception {
    DataManager dataManager = AppBeans.get(DataManager.class);
    User loadedUser = dataManager.load(LoadContext.create(User.class).setId(user.getId()));
    CommitContext commitContext = new CommitContext().addInstanceToRemove(loadedUser);
    commitContext.setSoftDeletion(false);
    dataManager.commit(commitContext);
    QueryRunner runner = new QueryRunner(persistence.getDataSource());
    Object[] row = runner.query("select count(*) from sec_user where id = ?", user.getId().toString(), new ArrayHandler());
    assertEquals(0, ((Number) row[0]).intValue());
}
Also used : User(com.haulmont.cuba.security.entity.User) ArrayHandler(com.haulmont.bali.db.ArrayHandler) QueryRunner(com.haulmont.bali.db.QueryRunner) Test(org.junit.Test)

Example 9 with QueryRunner

use of com.haulmont.bali.db.QueryRunner in project cuba by cuba-platform.

the class SoftDeleteTest method setUp.

@Before
public void setUp() throws Exception {
    persistence = cont.persistence();
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Group group = new Group();
        groupId = group.getId();
        group.setName("testGroup");
        em.persist(group);
        User user = new User();
        userId = user.getId();
        user.setName("testUser");
        user.setLogin("testLogin");
        user.setGroup(group);
        em.persist(user);
        User user1 = new User();
        user1Id = user1.getId();
        user1.setName("testUser1");
        user1.setLogin("testLogin1");
        user1.setGroup(group);
        em.persist(user1);
        Role role1 = em.find(Role.class, UUID.fromString("0c018061-b26f-4de2-a5be-dff348347f93"));
        UserRole userRole1 = new UserRole();
        userRole1Id = userRole1.getId();
        userRole1.setUser(user);
        userRole1.setRole(role1);
        em.persist(userRole1);
        Role role2 = new Role();
        role2Id = role2.getId();
        role2.setName("roleToBeDeleted");
        em.persist(role2);
        Role role3 = new Role();
        role3Id = role3.getId();
        role3.setName("roleToBeDeleted3");
        em.persist(role3);
        UserRole userRole2 = new UserRole();
        userRole2Id = userRole2.getId();
        userRole2.setUser(user);
        userRole2.setRole(role2);
        em.persist(userRole2);
        UserRole userRole3 = new UserRole();
        userRole3Id = userRole3.getId();
        userRole3.setUser(user1);
        userRole3.setRole(role3);
        em.persist(userRole3);
        SoftDeleteOneToOneB oneToOneB1 = cont.metadata().create(SoftDeleteOneToOneB.class);
        oneToOneB1.setName("oneToOneB1");
        em.persist(oneToOneB1);
        oneToOneB1Id = oneToOneB1.getId();
        SoftDeleteOneToOneB oneToOneB2 = cont.metadata().create(SoftDeleteOneToOneB.class);
        oneToOneB2.setName("oneToOneB2");
        em.persist(oneToOneB2);
        oneToOneB2Id = oneToOneB2.getId();
        SoftDeleteOneToOneA oneToOneA1 = cont.metadata().create(SoftDeleteOneToOneA.class);
        oneToOneA1.setName("oneToOneA1");
        oneToOneA1.setB(oneToOneB1);
        em.persist(oneToOneA1);
        oneToOneA1Id = oneToOneA1.getId();
        SoftDeleteOneToOneA oneToOneA2 = cont.metadata().create(SoftDeleteOneToOneA.class);
        oneToOneA2.setName("oneToOneA2");
        oneToOneA2.setB(oneToOneB2);
        em.persist(oneToOneA2);
        oneToOneA2Id = oneToOneA2.getId();
        Group group1 = new Group();
        group1Id = group1.getId();
        group1.setName("testGroup1");
        em.persist(group1);
        GroupHierarchy groupHierarchy = new GroupHierarchy();
        groupHierarchyId = groupHierarchy.getId();
        groupHierarchy.setGroup(group1);
        groupHierarchy.setParent(group1);
        groupHierarchy.setLevel(1);
        em.persist(groupHierarchy);
        Constraint constraint1 = new Constraint();
        constraint1Id = constraint1.getId();
        constraint1.setCode("constraint1");
        constraint1.setEntityName("sec$Constraint");
        constraint1.setGroup(group1);
        em.persist(constraint1);
        Constraint constraint2 = new Constraint();
        constraint2Id = constraint2.getId();
        constraint2.setCode("constraint2");
        constraint2.setEntityName("sec$Constraint");
        constraint2.setGroup(group1);
        em.persist(constraint2);
        tx.commitRetaining();
        em = cont.persistence().getEntityManager();
        UserRole ur = em.find(UserRole.class, userRole2Id);
        em.remove(ur);
        Role r = em.find(Role.class, role2Id);
        em.remove(r);
        SoftDeleteOneToOneA oneToOneA = em.find(SoftDeleteOneToOneA.class, oneToOneA1Id);
        em.remove(oneToOneA);
        SoftDeleteOneToOneB oneToOneB = em.find(SoftDeleteOneToOneB.class, oneToOneB2Id);
        em.remove(oneToOneB);
        Constraint constraint = em.find(Constraint.class, constraint2Id);
        em.remove(constraint);
        // remove from db to prevent cascade delete user role
        QueryRunner queryRunner = new QueryRunner();
        queryRunner.update(em.getConnection(), "update SEC_ROLE set DELETE_TS = CREATE_TS, DELETED_BY = CREATED_BY where name = 'roleToBeDeleted3'");
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) SoftDeleteOneToOneA(com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA) SoftDeleteOneToOneB(com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneB) QueryRunner(com.haulmont.bali.db.QueryRunner)

Example 10 with QueryRunner

use of com.haulmont.bali.db.QueryRunner in project cuba by cuba-platform.

the class SoftDeleteTest method testManyToOne.

@Test
public void testManyToOne() throws SQLException {
    System.out.println("===================== BEGIN testManyToOne =====================");
    QueryRunner queryRunner = new QueryRunner(persistence.getDataSource());
    queryRunner.update("update SEC_GROUP set DELETE_TS = current_timestamp, DELETED_BY = 'admin' where ID = ?", new Object[] { groupId.toString() });
    // test without view
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        User user = em.find(User.class, userId);
        Group group = user.getGroup();
        tx.commit();
        assertNotNull(group);
        assertTrue(group.isDeleted());
    }
    View view;
    // test fetchMode = AUTO (JOIN is used)
    view = new View(User.class, "testView").addProperty("name").addProperty("login").addProperty("group", new View(Group.class).addProperty("name"));
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        User user = em.find(User.class, userId, view);
        Group group = user.getGroup();
        tx.commit();
        assertNotNull(group);
        assertTrue(group.isDeleted());
    }
    // test fetchMode = UNDEFINED
    view = new View(User.class, "testView").addProperty("name").addProperty("login").addProperty("group", new View(Group.class).addProperty("name"), FetchMode.UNDEFINED);
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        User user = em.find(User.class, userId, view);
        Group group = user.getGroup();
        tx.commit();
        assertNotNull(group);
        assertTrue(group.isDeleted());
    }
    // test fetchMode = BATCH
    view = new View(User.class, "testView").addProperty("name").addProperty("login").addProperty("group", new View(Group.class).addProperty("name"), FetchMode.BATCH);
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        User user = em.find(User.class, userId, view);
        Group group = user.getGroup();
        tx.commit();
        assertNotNull(group);
        assertTrue(group.isDeleted());
    }
    System.out.println("===================== END testManyToOne =====================");
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) View(com.haulmont.cuba.core.global.View) QueryRunner(com.haulmont.bali.db.QueryRunner)

Aggregations

QueryRunner (com.haulmont.bali.db.QueryRunner)38 SQLException (java.sql.SQLException)14 Before (org.junit.Before)10 DbTypeConverter (com.haulmont.cuba.core.sys.persistence.DbTypeConverter)6 User (com.haulmont.cuba.security.entity.User)4 EntityManager (com.haulmont.cuba.core.EntityManager)3 Transaction (com.haulmont.cuba.core.Transaction)3 After (org.junit.After)3 MetaClass (com.haulmont.chile.core.model.MetaClass)2 Entity (com.haulmont.cuba.core.entity.Entity)2 UserSession (com.haulmont.cuba.security.global.UserSession)2 IOException (java.io.IOException)2 ResultSet (java.sql.ResultSet)2 ArrayHandler (com.haulmont.bali.db.ArrayHandler)1 ResultSetHandler (com.haulmont.bali.db.ResultSetHandler)1 EntitySnapshot (com.haulmont.cuba.core.entity.EntitySnapshot)1 Server (com.haulmont.cuba.core.entity.Server)1 View (com.haulmont.cuba.core.global.View)1 EntityListenerManager (com.haulmont.cuba.core.sys.listener.EntityListenerManager)1 Group (com.haulmont.cuba.security.entity.Group)1