Search in sources :

Example 91 with User

use of org.h2.engine.User in project ignite by apache.

the class UpdatePlanBuilder method planForUpdate.

/**
 * Prepare update plan for UPDATE or DELETE.
 *
 * @param planKey Plan key.
 * @param stmt UPDATE or DELETE statement.
 * @param idx Indexing.
 * @param mvccEnabled MVCC flag.
 * @return Update plan.
 * @throws IgniteCheckedException if failed.
 */
private static UpdatePlan planForUpdate(QueryDescriptor planKey, GridSqlStatement stmt, IgniteH2Indexing idx, boolean mvccEnabled, IgniteLogger log) throws IgniteCheckedException {
    GridSqlElement target;
    FastUpdate fastUpdate;
    UpdateMode mode;
    if (stmt instanceof GridSqlUpdate) {
        // Let's verify that user is not trying to mess with key's columns directly
        verifyUpdateColumns(stmt);
        GridSqlUpdate update = (GridSqlUpdate) stmt;
        target = update.target();
        fastUpdate = DmlAstUtils.getFastUpdateArgs(update);
        mode = UpdateMode.UPDATE;
    } else if (stmt instanceof GridSqlDelete) {
        GridSqlDelete del = (GridSqlDelete) stmt;
        target = del.from();
        fastUpdate = DmlAstUtils.getFastDeleteArgs(del);
        mode = UpdateMode.DELETE;
    } else
        throw new IgniteSQLException("Unexpected DML operation [cls=" + stmt.getClass().getName() + ']', IgniteQueryErrorCode.UNEXPECTED_OPERATION);
    GridSqlTable tbl = DmlAstUtils.gridTableForElement(target);
    GridH2Table h2Tbl = tbl.dataTable();
    assert h2Tbl != null;
    GridH2RowDescriptor desc = h2Tbl.rowDescriptor();
    if (desc == null)
        throw new IgniteSQLException("Row descriptor undefined for table '" + h2Tbl.getName() + "'", IgniteQueryErrorCode.NULL_TABLE_DESCRIPTOR);
    if (fastUpdate != null) {
        return new UpdatePlan(mode, h2Tbl, null, fastUpdate, null);
    } else {
        GridSqlSelect sel;
        if (stmt instanceof GridSqlUpdate) {
            List<GridSqlColumn> updatedCols = ((GridSqlUpdate) stmt).cols();
            int valColIdx = -1;
            String[] colNames = new String[updatedCols.size()];
            int[] colTypes = new int[updatedCols.size()];
            for (int i = 0; i < updatedCols.size(); i++) {
                colNames[i] = updatedCols.get(i).columnName();
                colTypes[i] = updatedCols.get(i).resultType().type();
                Column col = updatedCols.get(i).column();
                if (desc.isValueColumn(col.getColumnId()))
                    valColIdx = i;
            }
            boolean hasNewVal = (valColIdx != -1);
            // Statement updates distinct properties if it does not have _val in updated columns list
            // or if its list of updated columns includes only _val, i.e. is single element.
            boolean hasProps = !hasNewVal || updatedCols.size() > 1;
            // Index of new _val in results of SELECT
            if (hasNewVal)
                valColIdx += 2;
            int newValColIdx = (hasNewVal ? valColIdx : 1);
            KeyValueSupplier valSupplier = createSupplier(desc.context(), desc.type(), newValColIdx, hasProps, false, true);
            sel = DmlAstUtils.selectForUpdate((GridSqlUpdate) stmt);
            String selectSql = sel.getSQL();
            DmlDistributedPlanInfo distributed = null;
            if (!F.isEmpty(selectSql)) {
                distributed = checkPlanCanBeDistributed(idx, mvccEnabled, planKey, selectSql, tbl.dataTable().cacheName(), log);
            }
            return new UpdatePlan(UpdateMode.UPDATE, h2Tbl, colNames, colTypes, null, valSupplier, -1, valColIdx, selectSql, false, null, 0, null, distributed, sel.canBeLazy(), false);
        } else {
            sel = DmlAstUtils.selectForDelete((GridSqlDelete) stmt);
            String selectSql = sel.getSQL();
            DmlDistributedPlanInfo distributed = null;
            if (!F.isEmpty(selectSql)) {
                distributed = checkPlanCanBeDistributed(idx, mvccEnabled, planKey, selectSql, tbl.dataTable().cacheName(), log);
            }
            return new UpdatePlan(UpdateMode.DELETE, h2Tbl, selectSql, null, distributed);
        }
    }
}
Also used : GridSqlDelete(org.apache.ignite.internal.processors.query.h2.sql.GridSqlDelete) GridSqlSelect(org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect) GridH2RowDescriptor(org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) Column(org.h2.table.Column) GridSqlTable(org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable) GridH2Table(org.apache.ignite.internal.processors.query.h2.opt.GridH2Table) GridSqlColumn(org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) GridSqlElement(org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement) GridSqlUpdate(org.apache.ignite.internal.processors.query.h2.sql.GridSqlUpdate)

Example 92 with User

use of org.h2.engine.User in project ma-core-public by infiniteautomation.

the class H2InMemoryDatabaseProxy method initialize.

/* (non-Javadoc)
     * @see com.serotonin.m2m2.db.DatabaseProxy#initialize(java.lang.ClassLoader)
     */
@Override
public void initialize(ClassLoader classLoader) {
    JdbcDataSource jds = new JdbcDataSource();
    String url = "jdbc:h2:mem:" + databaseName + ";DB_CLOSE_DELAY=-1";
    jds.setUrl(url);
    dataSource = JdbcConnectionPool.create(jds);
    transactionManager = new DataSourceTransactionManager(dataSource);
    if (initWebConsole) {
        String[] webArgs = new String[4];
        webArgs[0] = "-webPort";
        webArgs[1] = webPort.toString();
        webArgs[2] = "-ifExists";
        webArgs[3] = "-webAllowOthers";
        try {
            this.web = Server.createWebServer(webArgs);
            this.web.start();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    ExtendedJdbcTemplate ejt = new ExtendedJdbcTemplate();
    ejt.setDataSource(getDataSource());
    // Create the empty database
    if (!tableExists(ejt, SchemaDefinition.USERS_TABLE)) {
        // The users table wasn't found, so assume that this is a new instance.
        // Create the tables
        runScript(this.getClass().getResourceAsStream("/createTables-" + getType().name() + ".sql"), null);
        for (DatabaseSchemaDefinition def : ModuleRegistry.getDefinitions(DatabaseSchemaDefinition.class)) def.newInstallationCheck(ejt);
        SystemSettingsDao.instance.setValue(SystemSettingsDao.DATABASE_SCHEMA_VERSION, Integer.toString(Common.getDatabaseSchemaVersion()));
        SystemSettingsDao.instance.setValue(SystemSettingsDao.BACKUP_ENABLED, "false");
        SystemSettingsDao.instance.setValue(SystemSettingsDao.DATABASE_BACKUP_ENABLED, "false");
        // Add the settings flag that this is a new instance. This flag is removed when an administrator
        // logs in.
        SystemSettingsDao.instance.setBooleanValue(SystemSettingsDao.NEW_INSTANCE, true);
        Providers.get(IMangoLifecycle.class).addStartupTask(new Runnable() {

            @Override
            public void run() {
                // New database. Create a default user.
                User user = new User();
                user.setId(Common.NEW_ID);
                user.setName("Administrator");
                user.setUsername("admin");
                user.setPassword(Common.encrypt("admin"));
                user.setEmail("admin@yourMangoDomain.com");
                user.setPhone("");
                user.setPermissions(SuperadminPermissionDefinition.GROUP_NAME);
                user.setDisabled(false);
                UserDao.instance.saveUser(user);
                DefaultDataPointPropertiesTemplateFactory factory = new DefaultDataPointPropertiesTemplateFactory();
                factory.saveDefaultTemplates();
            }
        });
    }
    // Check if we are using NoSQL
    if (NoSQLProxyFactory.instance.getProxy() != null) {
        noSQLProxy = NoSQLProxyFactory.instance.getProxy();
        noSQLProxy.initialize();
    }
    initialized = true;
}
Also used : User(com.serotonin.m2m2.vo.User) SQLException(java.sql.SQLException) DatabaseSchemaDefinition(com.serotonin.m2m2.module.DatabaseSchemaDefinition) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) DefaultDataPointPropertiesTemplateFactory(com.serotonin.m2m2.vo.template.DefaultDataPointPropertiesTemplateFactory) ExtendedJdbcTemplate(com.serotonin.db.spring.ExtendedJdbcTemplate) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager)

Example 93 with User

use of org.h2.engine.User in project symmetric-ds by JumpMind.

the class DbSqlCommand method executeWithOptions.

@Override
protected boolean executeWithOptions(CommandLine line) throws Exception {
    BasicDataSource basicDataSource = getDatabasePlatform(false).getDataSource();
    String url = basicDataSource.getUrl();
    String user = basicDataSource.getUsername();
    String password = basicDataSource.getPassword();
    String driver = basicDataSource.getDriverClassName();
    Shell shell = new Shell();
    if (line.hasOption(OPTION_SQL)) {
        String sql = line.getOptionValue(OPTION_SQL);
        shell.runTool("-url", url, "-user", user, "-password", password, "-driver", driver, "-sql", sql);
    } else {
        shell.runTool("-url", url, "-user", user, "-password", password, "-driver", driver);
    }
    return true;
}
Also used : Shell(org.h2.tools.Shell) BasicDataSource(org.apache.commons.dbcp.BasicDataSource)

Aggregations

Connection (java.sql.Connection)36 SQLException (java.sql.SQLException)21 PreparedStatement (java.sql.PreparedStatement)17 Statement (java.sql.Statement)17 ResultSet (java.sql.ResultSet)16 Server (org.h2.tools.Server)15 DbException (org.h2.message.DbException)14 Column (org.h2.table.Column)12 ValueString (org.h2.value.ValueString)12 Properties (java.util.Properties)10 Database (org.h2.engine.Database)10 Schema (org.h2.schema.Schema)8 IOException (java.io.IOException)7 User (org.h2.engine.User)7 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)7 SimpleResultSet (org.h2.tools.SimpleResultSet)7 Value (org.h2.value.Value)7 PrintStream (java.io.PrintStream)6 Timestamp (java.sql.Timestamp)6 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)6