use of org.h2.dev.util.BinaryArithmeticStream.In in project spf4j by zolyfarkas.
the class JdbcSemaphoreTest method testSingleProcessLock.
@Test
public void testSingleProcessLock() throws SQLException, IOException, InterruptedException, TimeoutException {
JdbcDataSource ds = new JdbcDataSource();
ds.setURL("jdbc:h2:mem:test");
ds.setUser("sa");
ds.setPassword("sa");
try (Connection conn = ds.getConnection()) {
// only to keep the schema arround in thsi section
createSchemaObjects(ds);
JdbcLock lock = new JdbcLock(ds, SemaphoreTablesDesc.DEFAULT, "testLock", 10);
lock.lock();
Assert.assertFalse(lock.tryLock());
lock.unlock();
}
}
use of org.h2.dev.util.BinaryArithmeticStream.In in project sqlg by pietermartin.
the class H2Dialect method sqlArrayTypeNameToPropertyType.
/**
* All this is because H2 does not return the TYPE_NAME for column meta data.
* The strategy is to actualy query the table get the column's value and interrogate it to get its type.
* If the column has no data then we are stuffed and an exception is thrown.
* @param typeName
* @param sqlgGraph
* @param schema
* @param table
* @param columnName
* @param metaDataIter
* @return
*/
@Override
public PropertyType sqlArrayTypeNameToPropertyType(String typeName, SqlgGraph sqlgGraph, String schema, String table, String columnName, ListIterator<Triple<String, Integer, String>> metaDataIter) {
Connection connection = sqlgGraph.tx().getConnection();
try (Statement statement = connection.createStatement()) {
String sql = "SELECT \"" + columnName + "\" FROM \"" + schema + "\".\"" + table + "\" WHERE \"" + columnName + "\" IS NOT NULL LIMIT 1";
ResultSet rs = statement.executeQuery(sql);
if (!rs.next()) {
throw new IllegalStateException("The sqlg_schema can not be created because the column " + schema + "." + table + "." + columnName + " has no data in it. For arrays on H2 there must be data in the column to determine the type.");
} else {
java.sql.Array o = rs.getArray(1);
JdbcArray jdbcArray = (JdbcArray) o;
Object[] array = (Object[]) jdbcArray.getArray();
for (Object o1 : array) {
if (o1 instanceof Byte) {
return PropertyType.BYTE_ARRAY;
} else if (o1 instanceof Short) {
return PropertyType.SHORT_ARRAY;
} else if (o1 instanceof Integer) {
return PropertyType.INTEGER_ARRAY;
} else if (o1 instanceof Long) {
return PropertyType.LONG_ARRAY;
} else if (o1 instanceof Float) {
return PropertyType.FLOAT_ARRAY;
} else if (o1 instanceof Double) {
return PropertyType.DOUBLE_ARRAY;
} else if (o1 instanceof String) {
return PropertyType.STRING_ARRAY;
} else if (o1 instanceof Timestamp) {
// ja well this sucks but I know of no other way to distinguish between LocalDateTime and LocalDate
Timestamp timestamp = (Timestamp) o1;
LocalDateTime localDateTime = timestamp.toLocalDateTime();
if (localDateTime.getHour() == 0 && localDateTime.getMinute() == 0 && localDateTime.getSecond() == 0 && localDateTime.getNano() == 0) {
return PropertyType.LOCALDATE_ARRAY;
} else {
return PropertyType.LOCALDATETIME_ARRAY;
}
} else if (o1 instanceof Time) {
return PropertyType.LOCALTIME_ARRAY;
} else {
throw new UnsupportedOperationException("H2 does not support typeName on arrays");
}
}
}
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
throw new UnsupportedOperationException("H2 does not support typeName on arrays");
}
use of org.h2.dev.util.BinaryArithmeticStream.In in project jdbi by jdbi.
the class TestTransactional 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 {
final Connection real = super.getConnection();
return (Connection) Proxy.newProxyInstance(real.getClass().getClassLoader(), new Class<?>[] { Connection.class }, new TxnIsolationCheckingInvocationHandler(real));
}
};
// in MVCC mode h2 doesn't shut down immediately on all connections closed, so need random db name
ds.setURL(String.format("jdbc:h2:mem:%s;MVCC=TRUE", UUID.randomUUID()));
db = Jdbi.create(ds);
db.installPlugin(new SqlObjectPlugin());
db.registerRowMapper(new SomethingMapper());
handle = db.open();
handle.execute("create table something (id int primary key, name varchar(100))");
}
use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.
the class Parser method parseAlterTable.
private Prepared parseAlterTable() {
boolean ifTableExists = readIfExists(false);
String tableName = readIdentifierWithSchema();
Schema schema = getSchema();
if (readIf("ADD")) {
Prepared command = parseAlterTableAddConstraintIf(tableName, schema, ifTableExists);
if (command != null) {
return command;
}
return parseAlterTableAddColumn(tableName, schema, ifTableExists);
} else if (readIf("SET")) {
read("REFERENTIAL_INTEGRITY");
int type = CommandInterface.ALTER_TABLE_SET_REFERENTIAL_INTEGRITY;
boolean value = readBooleanSetting();
AlterTableSet command = new AlterTableSet(session, schema, type, value);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
if (readIf("CHECK")) {
command.setCheckExisting(true);
} else if (readIf("NOCHECK")) {
command.setCheckExisting(false);
}
return command;
} else if (readIf("RENAME")) {
if (readIf("COLUMN")) {
// PostgreSQL syntax
String columnName = readColumnIdentifier();
read("TO");
AlterTableRenameColumn command = new AlterTableRenameColumn(session, schema);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setOldColumnName(columnName);
String newName = readColumnIdentifier();
command.setNewColumnName(newName);
return command;
} else if (readIf("CONSTRAINT")) {
String constraintName = readIdentifierWithSchema(schema.getName());
checkSchema(schema);
read("TO");
AlterTableRenameConstraint command = new AlterTableRenameConstraint(session, schema);
command.setConstraintName(constraintName);
String newName = readColumnIdentifier();
command.setNewConstraintName(newName);
return commandIfTableExists(schema, tableName, ifTableExists, command);
} else {
read("TO");
String newName = readIdentifierWithSchema(schema.getName());
checkSchema(schema);
AlterTableRename command = new AlterTableRename(session, getSchema());
command.setOldTableName(tableName);
command.setNewTableName(newName);
command.setIfTableExists(ifTableExists);
command.setHidden(readIf("HIDDEN"));
return command;
}
} else if (readIf("DROP")) {
if (readIf("CONSTRAINT")) {
boolean ifExists = readIfExists(false);
String constraintName = readIdentifierWithSchema(schema.getName());
ifExists = readIfExists(ifExists);
checkSchema(schema);
AlterTableDropConstraint command = new AlterTableDropConstraint(session, getSchema(), ifExists);
command.setConstraintName(constraintName);
return commandIfTableExists(schema, tableName, ifTableExists, command);
} else if (readIf("FOREIGN")) {
// MySQL compatibility
read("KEY");
String constraintName = readIdentifierWithSchema(schema.getName());
checkSchema(schema);
AlterTableDropConstraint command = new AlterTableDropConstraint(session, getSchema(), false);
command.setConstraintName(constraintName);
return commandIfTableExists(schema, tableName, ifTableExists, command);
} else if (readIf("INDEX")) {
// MySQL compatibility
String indexOrConstraintName = readIdentifierWithSchema();
final SchemaCommand command;
if (schema.findIndex(session, indexOrConstraintName) != null) {
DropIndex dropIndexCommand = new DropIndex(session, getSchema());
dropIndexCommand.setIndexName(indexOrConstraintName);
command = dropIndexCommand;
} else {
AlterTableDropConstraint dropCommand = new AlterTableDropConstraint(session, getSchema(), false);
dropCommand.setConstraintName(indexOrConstraintName);
command = dropCommand;
}
return commandIfTableExists(schema, tableName, ifTableExists, command);
} else if (readIf("PRIMARY")) {
read("KEY");
Table table = tableIfTableExists(schema, tableName, ifTableExists);
if (table == null) {
return new NoOperation(session);
}
Index idx = table.getPrimaryKey();
DropIndex command = new DropIndex(session, schema);
command.setIndexName(idx.getName());
return command;
} else {
readIf("COLUMN");
boolean ifExists = readIfExists(false);
ArrayList<Column> columnsToRemove = New.arrayList();
Table table = tableIfTableExists(schema, tableName, ifTableExists);
// For Oracle compatibility - open bracket required
boolean openingBracketDetected = readIf("(");
do {
String columnName = readColumnIdentifier();
if (table != null) {
if (!ifExists || table.doesColumnExist(columnName)) {
Column column = table.getColumn(columnName);
columnsToRemove.add(column);
}
}
} while (readIf(","));
if (openingBracketDetected) {
// For Oracle compatibility - close bracket
read(")");
}
if (table == null || columnsToRemove.isEmpty()) {
return new NoOperation(session);
}
AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema);
command.setType(CommandInterface.ALTER_TABLE_DROP_COLUMN);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setColumnsToRemove(columnsToRemove);
return command;
}
} else if (readIf("CHANGE")) {
// MySQL compatibility
readIf("COLUMN");
String columnName = readColumnIdentifier();
String newColumnName = readColumnIdentifier();
Column column = columnIfTableExists(schema, tableName, columnName, ifTableExists);
boolean nullable = column == null ? true : column.isNullable();
// new column type ignored. RENAME and MODIFY are
// a single command in MySQL but two different commands in H2.
parseColumnForTable(newColumnName, nullable);
AlterTableRenameColumn command = new AlterTableRenameColumn(session, schema);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setOldColumnName(columnName);
command.setNewColumnName(newColumnName);
return command;
} else if (readIf("MODIFY")) {
// MySQL compatibility (optional)
readIf("COLUMN");
// Oracle specifies (but will not require) an opening parenthesis
boolean hasOpeningBracket = readIf("(");
String columnName = readColumnIdentifier();
AlterTableAlterColumn command = null;
NullConstraintType nullConstraint = parseNotNullConstraint();
switch(nullConstraint) {
case NULL_IS_ALLOWED:
case NULL_IS_NOT_ALLOWED:
command = new AlterTableAlterColumn(session, schema);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
Column column = columnIfTableExists(schema, tableName, columnName, ifTableExists);
command.setOldColumn(column);
if (nullConstraint == NullConstraintType.NULL_IS_ALLOWED) {
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL);
} else {
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NOT_NULL);
}
break;
case NO_NULL_CONSTRAINT_FOUND:
command = parseAlterTableAlterColumnType(schema, tableName, columnName, ifTableExists);
break;
default:
throw DbException.get(ErrorCode.UNKNOWN_MODE_1, "Internal Error - unhandled case: " + nullConstraint.name());
}
if (hasOpeningBracket) {
read(")");
}
return command;
} else if (readIf("ALTER")) {
readIf("COLUMN");
String columnName = readColumnIdentifier();
Column column = columnIfTableExists(schema, tableName, columnName, ifTableExists);
if (readIf("RENAME")) {
read("TO");
AlterTableRenameColumn command = new AlterTableRenameColumn(session, schema);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setOldColumnName(columnName);
String newName = readColumnIdentifier();
command.setNewColumnName(newName);
return command;
} else if (readIf("DROP")) {
// PostgreSQL compatibility
if (readIf("DEFAULT")) {
AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setOldColumn(column);
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT);
command.setDefaultExpression(null);
return command;
}
if (readIf("ON")) {
read("UPDATE");
AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setOldColumn(column);
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_ON_UPDATE);
command.setDefaultExpression(null);
return command;
}
read("NOT");
read("NULL");
AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setOldColumn(column);
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL);
return command;
} else if (readIf("TYPE")) {
// PostgreSQL compatibility
return parseAlterTableAlterColumnType(schema, tableName, columnName, ifTableExists);
} else if (readIf("SET")) {
if (readIf("DATA")) {
// Derby compatibility
read("TYPE");
return parseAlterTableAlterColumnType(schema, tableName, columnName, ifTableExists);
}
AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setOldColumn(column);
NullConstraintType nullConstraint = parseNotNullConstraint();
switch(nullConstraint) {
case NULL_IS_ALLOWED:
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL);
break;
case NULL_IS_NOT_ALLOWED:
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_NOT_NULL);
break;
case NO_NULL_CONSTRAINT_FOUND:
if (readIf("DEFAULT")) {
Expression defaultExpression = readExpression();
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT);
command.setDefaultExpression(defaultExpression);
} else if (readIf("ON")) {
read("UPDATE");
Expression onUpdateExpression = readExpression();
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_ON_UPDATE);
command.setDefaultExpression(onUpdateExpression);
} else if (readIf("INVISIBLE")) {
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_VISIBILITY);
command.setVisible(false);
} else if (readIf("VISIBLE")) {
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_VISIBILITY);
command.setVisible(true);
}
break;
default:
throw DbException.get(ErrorCode.UNKNOWN_MODE_1, "Internal Error - unhandled case: " + nullConstraint.name());
}
return command;
} else if (readIf("RESTART")) {
readIf("WITH");
Expression start = readExpression();
AlterSequence command = new AlterSequence(session, schema);
command.setColumn(column);
command.setStartWith(start);
return commandIfTableExists(schema, tableName, ifTableExists, command);
} else if (readIf("SELECTIVITY")) {
AlterTableAlterColumn command = new AlterTableAlterColumn(session, schema);
command.setTableName(tableName);
command.setIfTableExists(ifTableExists);
command.setType(CommandInterface.ALTER_TABLE_ALTER_COLUMN_SELECTIVITY);
command.setOldColumn(column);
command.setSelectivity(readExpression());
return command;
} else {
return parseAlterTableAlterColumnType(schema, tableName, columnName, ifTableExists);
}
}
throw getSyntaxError();
}
use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.
the class Parser method readIfAll.
/*
* Reads every token in list, in order - returns true if all are found.
* If any are not found, returns false - AND resets parsing back to state when called.
*/
private boolean readIfAll(String... tokens) {
// save parse location in case we have to fail this test
int start = lastParseIndex;
for (String token : tokens) {
if (!currentTokenQuoted && equalsToken(token, currentToken)) {
read();
} else {
// read failed - revert parse location to before when called
parseIndex = start;
read();
return false;
}
}
return true;
}
Aggregations