Search in sources :

Example 76 with Db

use of org.h2.jaqu.Db in project jdbi by jdbi.

the class TestOnDemandObjectMethodBehavior method setUp.

@Before
public void setUp() throws Exception {
    final JdbcDataSource ds = new JdbcDataSource() {

        private static final long serialVersionUID = 1L;

        @Override
        public Connection getConnection() throws SQLException {
            throw new UnsupportedOperationException();
        }
    };
    db = Jdbi.create(ds);
    db.installPlugin(new SqlObjectPlugin());
    dao = db.onDemand(UselessDao.class);
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Before(org.junit.Before)

Example 77 with Db

use of org.h2.jaqu.Db in project narayana by jbosstm.

the class RecoveryTest method directRecoverableConnection.

@Test
@BMRule(name = "throw rmfail xaexception", targetClass = "org.h2.jdbcx.JdbcXAConnection", targetMethod = "commit", action = "throw new javax.transaction.xa.XAException(javax.transaction.xa.XAException.XAER_RMFAIL)", targetLocation = "AT ENTRY")
public void directRecoverableConnection() throws Exception {
    // jdbc:arjuna: <path to properties file>, path starting at System.getProperty("user.dir")
    String url = TransactionalDriver.arjunaDriver + "ds-direct.properties";
    Properties dbProperties = new Properties();
    dbProperties.put(TransactionalDriver.dynamicClass, PropertyFileDynamicClass.class.getName());
    dbProperties.put(TransactionalDriver.userName, "");
    dbProperties.put(TransactionalDriver.password, "");
    Properties p = System.getProperties();
    p.put("jdbc.drivers", Driver.class.getName());
    System.setProperties(p);
    transactionalDriver = new TransactionalDriver();
    DriverManager.registerDriver(transactionalDriver);
    step0_setupTables(url, dbProperties);
    // rmfail means the db connection was left to be recovered
    step1_leaveXidsInDbTable(url, dbProperties);
    step2_checkDbIsNotCommitted(url, dbProperties);
    // 3. Perform recovery - checking only top down XARecoveryModule functionality
    {
        XARecoveryModule xarm = new XARecoveryModule();
        RecoveryManager manager = RecoveryManager.manager();
        manager.removeAllModules(true);
        manager.addModule(xarm);
        AtomicActionRecoveryModule aarm = new AtomicActionRecoveryModule();
        aarm.periodicWorkFirstPass();
        Transformer.disableTriggers(true);
        aarm.periodicWorkSecondPass();
        Transformer.enableTriggers(true);
    }
    step4_finalDbCommitCheck(url, dbProperties);
}
Also used : RecoveryManager(com.arjuna.ats.arjuna.recovery.RecoveryManager) AtomicActionRecoveryModule(com.arjuna.ats.internal.arjuna.recovery.AtomicActionRecoveryModule) TransactionalDriver(com.arjuna.ats.jdbc.TransactionalDriver) Driver(org.h2.Driver) PropertyFileDynamicClass(com.arjuna.ats.internal.jdbc.drivers.PropertyFileDynamicClass) Properties(java.util.Properties) TransactionalDriver(com.arjuna.ats.jdbc.TransactionalDriver) XARecoveryModule(com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule) BMRule(org.jboss.byteman.contrib.bmunit.BMRule) Test(org.junit.Test)

Example 78 with Db

use of org.h2.jaqu.Db in project syncope by apache.

the class H2StartStopListener method contextInitialized.

@Override
public void contextInitialized(final ServletContextEvent sce) {
    ServletContext context = sce.getServletContext();
    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
    try {
        Server h2TestDb = new Server();
        h2TestDb.runTool("-tcp", "-tcpDaemon", "-web", "-webDaemon", "-webPort", ctx.getBean("testdb.webport", String.class));
        context.setAttribute(H2_TESTDB, h2TestDb);
    } catch (SQLException e) {
        LOG.error("Could not start H2 test db", e);
    }
    DataSource datasource = ctx.getBean(DataSource.class);
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator(ctx.getResource("classpath:/testdb.sql"));
    populator.setSqlScriptEncoding("UTF-8");
    DataSourceInitializer init = new DataSourceInitializer();
    init.setDataSource(datasource);
    init.setEnabled(true);
    init.setDatabasePopulator(populator);
    init.afterPropertiesSet();
    LOG.info("Database successfully initialized");
}
Also used : Server(org.h2.tools.Server) SQLException(java.sql.SQLException) ResourceDatabasePopulator(org.springframework.jdbc.datasource.init.ResourceDatabasePopulator) DataSourceInitializer(org.springframework.jdbc.datasource.init.DataSourceInitializer) ServletContext(javax.servlet.ServletContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) DataSource(javax.sql.DataSource)

Example 79 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class ScriptBase method initStore.

private void initStore() {
    Database db = session.getDatabase();
    byte[] key = null;
    if (cipher != null && password != null) {
        char[] pass = password.optimize(session).getValue(session).getString().toCharArray();
        key = SHA256.getKeyPasswordHash("script", pass);
    }
    String file = getFileName();
    store = FileStore.open(db, file, "rw", cipher, key);
    store.setCheckedWriting(false);
    store.init();
}
Also used : Database(org.h2.engine.Database)

Example 80 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class Query method initOrder.

/**
 * Initialize the order by list. This call may extend the expressions list.
 *
 * @param session the session
 * @param expressions the select list expressions
 * @param expressionSQL the select list SQL snippets
 * @param orderList the order by list
 * @param visible the number of visible columns in the select list
 * @param mustBeInResult all order by expressions must be in the select list
 * @param filters the table filters
 */
static void initOrder(Session session, ArrayList<Expression> expressions, ArrayList<String> expressionSQL, ArrayList<SelectOrderBy> orderList, int visible, boolean mustBeInResult, ArrayList<TableFilter> filters) {
    Database db = session.getDatabase();
    for (SelectOrderBy o : orderList) {
        Expression e = o.expression;
        if (e == null) {
            continue;
        }
        // special case: SELECT 1 AS A FROM DUAL ORDER BY A
        // (oracle supports it, but only in order by, not in group by and
        // not in having):
        // SELECT 1 AS A FROM DUAL ORDER BY -A
        boolean isAlias = false;
        int idx = expressions.size();
        if (e instanceof ExpressionColumn) {
            // order by expression
            ExpressionColumn exprCol = (ExpressionColumn) e;
            String tableAlias = exprCol.getOriginalTableAliasName();
            String col = exprCol.getOriginalColumnName();
            for (int j = 0; j < visible; j++) {
                boolean found = false;
                Expression ec = expressions.get(j);
                if (ec instanceof ExpressionColumn) {
                    // select expression
                    ExpressionColumn c = (ExpressionColumn) ec;
                    found = db.equalsIdentifiers(col, c.getColumnName());
                    if (found && tableAlias != null) {
                        String ca = c.getOriginalTableAliasName();
                        if (ca == null) {
                            found = false;
                            if (filters != null) {
                                // select id from test order by test.id
                                for (TableFilter f : filters) {
                                    if (db.equalsIdentifiers(f.getTableAlias(), tableAlias)) {
                                        found = true;
                                        break;
                                    }
                                }
                            }
                        } else {
                            found = db.equalsIdentifiers(ca, tableAlias);
                        }
                    }
                } else if (!(ec instanceof Alias)) {
                    continue;
                } else if (tableAlias == null && db.equalsIdentifiers(col, ec.getAlias())) {
                    found = true;
                } else {
                    Expression ec2 = ec.getNonAliasExpression();
                    if (ec2 instanceof ExpressionColumn) {
                        ExpressionColumn c2 = (ExpressionColumn) ec2;
                        String ta = exprCol.getSQL();
                        String tb = c2.getSQL();
                        String s2 = c2.getColumnName();
                        found = db.equalsIdentifiers(col, s2);
                        if (!db.equalsIdentifiers(ta, tb)) {
                            found = false;
                        }
                    }
                }
                if (found) {
                    idx = j;
                    isAlias = true;
                    break;
                }
            }
        } else {
            String s = e.getSQL();
            if (expressionSQL != null) {
                for (int j = 0, size = expressionSQL.size(); j < size; j++) {
                    String s2 = expressionSQL.get(j);
                    if (db.equalsIdentifiers(s2, s)) {
                        idx = j;
                        isAlias = true;
                        break;
                    }
                }
            }
        }
        if (!isAlias) {
            if (mustBeInResult) {
                throw DbException.get(ErrorCode.ORDER_BY_NOT_IN_RESULT, e.getSQL());
            }
            expressions.add(e);
            String sql = e.getSQL();
            expressionSQL.add(sql);
        }
        o.columnIndexExpr = ValueExpression.get(ValueInt.get(idx + 1));
        o.expression = expressions.get(idx).getNonAliasExpression();
    }
}
Also used : ValueExpression(org.h2.expression.ValueExpression) Expression(org.h2.expression.Expression) TableFilter(org.h2.table.TableFilter) Alias(org.h2.expression.Alias) Database(org.h2.engine.Database) ExpressionColumn(org.h2.expression.ExpressionColumn)

Aggregations

Database (org.h2.engine.Database)70 Connection (java.sql.Connection)31 Statement (java.sql.Statement)20 Table (org.h2.table.Table)19 PreparedStatement (java.sql.PreparedStatement)18 ResultSet (java.sql.ResultSet)13 SQLException (java.sql.SQLException)13 Column (org.h2.table.Column)12 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)9 StatementBuilder (org.h2.util.StatementBuilder)9 DbObject (org.h2.engine.DbObject)8 File (java.io.File)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 DbException (org.h2.message.DbException)7 Schema (org.h2.schema.Schema)7 Before (org.junit.Before)7 InputStream (java.io.InputStream)6 ExpressionColumn (org.h2.expression.ExpressionColumn)6 JdbcConnection (org.h2.jdbc.JdbcConnection)6