use of org.h2.engine.Mode in project h2database by h2database.
the class Table method compareTypeSafe.
/**
* Compare two values with the current comparison mode. The values may be of
* different type.
*
* @param a the first value
* @param b the second value
* @return 0 if both values are equal, -1 if the first value is smaller, and
* 1 otherwise
*/
public int compareTypeSafe(Value a, Value b) {
if (a == b) {
return 0;
}
int dataType = Value.getHigherOrder(a.getType(), b.getType());
a = a.convertTo(dataType);
b = b.convertTo(dataType);
return a.compareTypeSafe(b, compareMode);
}
use of org.h2.engine.Mode in project h2database by h2database.
the class PgServerThread method process.
private void process() throws IOException {
int x;
if (initDone) {
x = dataInRaw.read();
if (x < 0) {
stop = true;
return;
}
} else {
x = 0;
}
int len = dataInRaw.readInt();
len -= 4;
byte[] data = Utils.newBytes(len);
dataInRaw.readFully(data, 0, len);
dataIn = new DataInputStream(new ByteArrayInputStream(data, 0, len));
switch(x) {
case 0:
server.trace("Init");
int version = readInt();
if (version == 80877102) {
server.trace("CancelRequest");
int pid = readInt();
int key = readInt();
PgServerThread c = server.getThread(pid);
if (c != null && key == c.secret) {
c.cancelRequest();
} else {
// According to the PostgreSQL documentation, when canceling
// a request, if an invalid secret is provided then no
// exception should be sent back to the client.
server.trace("Invalid CancelRequest: pid=" + pid + ", key=" + key);
}
close();
} else if (version == 80877103) {
server.trace("SSLRequest");
out.write('N');
} else {
server.trace("StartupMessage");
server.trace(" version " + version + " (" + (version >> 16) + "." + (version & 0xff) + ")");
while (true) {
String param = readString();
if (param.length() == 0) {
break;
}
String value = readString();
if ("user".equals(param)) {
this.userName = value;
} else if ("database".equals(param)) {
this.databaseName = server.checkKeyAndGetDatabaseName(value);
} else if ("client_encoding".equals(param)) {
// UTF8
clientEncoding = value;
} else if ("DateStyle".equals(param)) {
if (value.indexOf(',') < 0) {
value += ", MDY";
}
dateStyle = value;
}
// extra_float_digits 2
// geqo on (Genetic Query Optimization)
server.trace(" param " + param + "=" + value);
}
sendAuthenticationCleartextPassword();
initDone = true;
}
break;
case 'p':
{
server.trace("PasswordMessage");
String password = readString();
try {
Properties info = new Properties();
info.put("MODE", "PostgreSQL");
info.put("USER", userName);
info.put("PASSWORD", password);
String url = "jdbc:h2:" + databaseName;
ConnectionInfo ci = new ConnectionInfo(url, info);
String baseDir = server.getBaseDir();
if (baseDir == null) {
baseDir = SysProperties.getBaseDir();
}
if (baseDir != null) {
ci.setBaseDir(baseDir);
}
if (server.getIfExists()) {
ci.setProperty("IFEXISTS", "TRUE");
}
conn = new JdbcConnection(ci, false);
// can not do this because when called inside
// DriverManager.getConnection, a deadlock occurs
// conn = DriverManager.getConnection(url, userName, password);
initDb();
sendAuthenticationOk();
} catch (Exception e) {
e.printStackTrace();
stop = true;
}
break;
}
case 'P':
{
server.trace("Parse");
Prepared p = new Prepared();
p.name = readString();
p.sql = getSQL(readString());
int paramTypesCount = readShort();
int[] paramTypes = null;
if (paramTypesCount > 0) {
paramTypes = new int[paramTypesCount];
for (int i = 0; i < paramTypesCount; i++) {
paramTypes[i] = readInt();
}
}
try {
p.prep = (JdbcPreparedStatement) conn.prepareStatement(p.sql);
ParameterMetaData meta = p.prep.getParameterMetaData();
p.paramType = new int[meta.getParameterCount()];
for (int i = 0; i < p.paramType.length; i++) {
int type;
if (i < paramTypesCount && paramTypes[i] != 0) {
type = paramTypes[i];
server.checkType(type);
} else {
type = PgServer.convertType(meta.getParameterType(i + 1));
}
p.paramType[i] = type;
}
prepared.put(p.name, p);
sendParseComplete();
} catch (Exception e) {
sendErrorResponse(e);
}
break;
}
case 'B':
{
server.trace("Bind");
Portal portal = new Portal();
portal.name = readString();
String prepName = readString();
Prepared prep = prepared.get(prepName);
if (prep == null) {
sendErrorResponse("Prepared not found");
break;
}
portal.prep = prep;
portals.put(portal.name, portal);
int formatCodeCount = readShort();
int[] formatCodes = new int[formatCodeCount];
for (int i = 0; i < formatCodeCount; i++) {
formatCodes[i] = readShort();
}
int paramCount = readShort();
try {
for (int i = 0; i < paramCount; i++) {
setParameter(prep.prep, prep.paramType[i], i, formatCodes);
}
} catch (Exception e) {
sendErrorResponse(e);
break;
}
int resultCodeCount = readShort();
portal.resultColumnFormat = new int[resultCodeCount];
for (int i = 0; i < resultCodeCount; i++) {
portal.resultColumnFormat[i] = readShort();
}
sendBindComplete();
break;
}
case 'C':
{
char type = (char) readByte();
String name = readString();
server.trace("Close");
if (type == 'S') {
Prepared p = prepared.remove(name);
if (p != null) {
JdbcUtils.closeSilently(p.prep);
}
} else if (type == 'P') {
portals.remove(name);
} else {
server.trace("expected S or P, got " + type);
sendErrorResponse("expected S or P");
break;
}
sendCloseComplete();
break;
}
case 'D':
{
char type = (char) readByte();
String name = readString();
server.trace("Describe");
if (type == 'S') {
Prepared p = prepared.get(name);
if (p == null) {
sendErrorResponse("Prepared not found: " + name);
} else {
try {
sendParameterDescription(p.prep.getParameterMetaData(), p.paramType);
sendRowDescription(p.prep.getMetaData());
} catch (Exception e) {
sendErrorResponse(e);
}
}
} else if (type == 'P') {
Portal p = portals.get(name);
if (p == null) {
sendErrorResponse("Portal not found: " + name);
} else {
PreparedStatement prep = p.prep.prep;
try {
ResultSetMetaData meta = prep.getMetaData();
sendRowDescription(meta);
} catch (Exception e) {
sendErrorResponse(e);
}
}
} else {
server.trace("expected S or P, got " + type);
sendErrorResponse("expected S or P");
}
break;
}
case 'E':
{
String name = readString();
server.trace("Execute");
Portal p = portals.get(name);
if (p == null) {
sendErrorResponse("Portal not found: " + name);
break;
}
int maxRows = readShort();
Prepared prepared = p.prep;
JdbcPreparedStatement prep = prepared.prep;
server.trace(prepared.sql);
try {
prep.setMaxRows(maxRows);
setActiveRequest(prep);
boolean result = prep.execute();
if (result) {
try {
ResultSet rs = prep.getResultSet();
// the meta-data is sent in the prior 'Describe'
while (rs.next()) {
sendDataRow(rs, p.resultColumnFormat);
}
sendCommandComplete(prep, 0);
} catch (Exception e) {
sendErrorResponse(e);
}
} else {
sendCommandComplete(prep, prep.getUpdateCount());
}
} catch (Exception e) {
if (prep.isCancelled()) {
sendCancelQueryResponse();
} else {
sendErrorResponse(e);
}
} finally {
setActiveRequest(null);
}
break;
}
case 'S':
{
server.trace("Sync");
sendReadyForQuery();
break;
}
case 'Q':
{
server.trace("Query");
String query = readString();
ScriptReader reader = new ScriptReader(new StringReader(query));
while (true) {
JdbcStatement stat = null;
try {
String s = reader.readStatement();
if (s == null) {
break;
}
s = getSQL(s);
stat = (JdbcStatement) conn.createStatement();
setActiveRequest(stat);
boolean result = stat.execute(s);
if (result) {
ResultSet rs = stat.getResultSet();
ResultSetMetaData meta = rs.getMetaData();
try {
sendRowDescription(meta);
while (rs.next()) {
sendDataRow(rs, null);
}
sendCommandComplete(stat, 0);
} catch (Exception e) {
sendErrorResponse(e);
break;
}
} else {
sendCommandComplete(stat, stat.getUpdateCount());
}
} catch (SQLException e) {
if (stat != null && stat.isCancelled()) {
sendCancelQueryResponse();
} else {
sendErrorResponse(e);
}
break;
} finally {
JdbcUtils.closeSilently(stat);
setActiveRequest(null);
}
}
sendReadyForQuery();
break;
}
case 'X':
{
server.trace("Terminate");
close();
break;
}
default:
server.trace("Unsupported: " + x + " (" + (char) x + ")");
break;
}
}
use of org.h2.engine.Mode in project h2database by h2database.
the class CommandRemote method executeUpdate.
@Override
public ResultWithGeneratedKeys executeUpdate(Object generatedKeysRequest) {
checkParameters();
boolean supportsGeneratedKeys = session.isSupportsGeneratedKeys();
boolean readGeneratedKeys = supportsGeneratedKeys && !Boolean.FALSE.equals(generatedKeysRequest);
int objectId = readGeneratedKeys ? session.getNextId() : 0;
synchronized (session) {
int updateCount = 0;
ResultRemote generatedKeys = null;
boolean autoCommit = false;
for (int i = 0, count = 0; i < transferList.size(); i++) {
prepareIfRequired();
Transfer transfer = transferList.get(i);
try {
session.traceOperation("COMMAND_EXECUTE_UPDATE", id);
transfer.writeInt(SessionRemote.COMMAND_EXECUTE_UPDATE).writeInt(id);
sendParameters(transfer);
if (supportsGeneratedKeys) {
int mode = GeneratedKeysMode.valueOf(generatedKeysRequest);
transfer.writeInt(mode);
switch(mode) {
case GeneratedKeysMode.COLUMN_NUMBERS:
{
int[] keys = (int[]) generatedKeysRequest;
transfer.writeInt(keys.length);
for (int key : keys) {
transfer.writeInt(key);
}
break;
}
case GeneratedKeysMode.COLUMN_NAMES:
{
String[] keys = (String[]) generatedKeysRequest;
transfer.writeInt(keys.length);
for (String key : keys) {
transfer.writeString(key);
}
break;
}
}
}
session.done(transfer);
updateCount = transfer.readInt();
autoCommit = transfer.readBoolean();
if (readGeneratedKeys) {
int columnCount = transfer.readInt();
if (generatedKeys != null) {
generatedKeys.close();
generatedKeys = null;
}
generatedKeys = new ResultRemote(session, transfer, objectId, columnCount, Integer.MAX_VALUE);
}
} catch (IOException e) {
session.removeServer(e, i--, ++count);
}
}
session.setAutoCommitFromServer(autoCommit);
session.autoCommitIfCluster();
session.readSessionState();
if (generatedKeys != null) {
return new ResultWithGeneratedKeys.WithKeys(updateCount, generatedKeys);
}
return ResultWithGeneratedKeys.of(updateCount);
}
}
use of org.h2.engine.Mode in project h2database by h2database.
the class Parser method parseSet.
private Prepared parseSet() {
if (readIf("@")) {
Set command = new Set(session, SetTypes.VARIABLE);
command.setString(readAliasIdentifier());
readIfEqualOrTo();
command.setExpression(readExpression());
return command;
} else if (readIf("AUTOCOMMIT")) {
readIfEqualOrTo();
boolean value = readBooleanSetting();
int setting = value ? CommandInterface.SET_AUTOCOMMIT_TRUE : CommandInterface.SET_AUTOCOMMIT_FALSE;
return new TransactionCommand(session, setting);
} else if (readIf("MVCC")) {
readIfEqualOrTo();
boolean value = readBooleanSetting();
Set command = new Set(session, SetTypes.MVCC);
command.setInt(value ? 1 : 0);
return command;
} else if (readIf("EXCLUSIVE")) {
readIfEqualOrTo();
Set command = new Set(session, SetTypes.EXCLUSIVE);
command.setExpression(readExpression());
return command;
} else if (readIf("IGNORECASE")) {
readIfEqualOrTo();
boolean value = readBooleanSetting();
Set command = new Set(session, SetTypes.IGNORECASE);
command.setInt(value ? 1 : 0);
return command;
} else if (readIf("PASSWORD")) {
readIfEqualOrTo();
AlterUser command = new AlterUser(session);
command.setType(CommandInterface.ALTER_USER_SET_PASSWORD);
command.setUser(session.getUser());
command.setPassword(readExpression());
return command;
} else if (readIf("SALT")) {
readIfEqualOrTo();
AlterUser command = new AlterUser(session);
command.setType(CommandInterface.ALTER_USER_SET_PASSWORD);
command.setUser(session.getUser());
command.setSalt(readExpression());
read("HASH");
command.setHash(readExpression());
return command;
} else if (readIf("MODE")) {
readIfEqualOrTo();
Set command = new Set(session, SetTypes.MODE);
command.setString(readAliasIdentifier());
return command;
} else if (readIf("COMPRESS_LOB")) {
readIfEqualOrTo();
Set command = new Set(session, SetTypes.COMPRESS_LOB);
if (currentTokenType == VALUE) {
command.setString(readString());
} else {
command.setString(readUniqueIdentifier());
}
return command;
} else if (readIf("DATABASE")) {
readIfEqualOrTo();
read("COLLATION");
return parseSetCollation();
} else if (readIf("COLLATION")) {
readIfEqualOrTo();
return parseSetCollation();
} else if (readIf("BINARY_COLLATION")) {
readIfEqualOrTo();
return parseSetBinaryCollation();
} else if (readIf("CLUSTER")) {
readIfEqualOrTo();
Set command = new Set(session, SetTypes.CLUSTER);
command.setString(readString());
return command;
} else if (readIf("DATABASE_EVENT_LISTENER")) {
readIfEqualOrTo();
Set command = new Set(session, SetTypes.DATABASE_EVENT_LISTENER);
command.setString(readString());
return command;
} else if (readIf("ALLOW_LITERALS")) {
readIfEqualOrTo();
Set command = new Set(session, SetTypes.ALLOW_LITERALS);
if (readIf("NONE")) {
command.setInt(Constants.ALLOW_LITERALS_NONE);
} else if (readIf("ALL")) {
command.setInt(Constants.ALLOW_LITERALS_ALL);
} else if (readIf("NUMBERS")) {
command.setInt(Constants.ALLOW_LITERALS_NUMBERS);
} else {
command.setInt(readPositiveInt());
}
return command;
} else if (readIf("DEFAULT_TABLE_TYPE")) {
readIfEqualOrTo();
Set command = new Set(session, SetTypes.DEFAULT_TABLE_TYPE);
if (readIf("MEMORY")) {
command.setInt(Table.TYPE_MEMORY);
} else if (readIf("CACHED")) {
command.setInt(Table.TYPE_CACHED);
} else {
command.setInt(readPositiveInt());
}
return command;
} else if (readIf("CREATE")) {
readIfEqualOrTo();
// Derby compatibility (CREATE=TRUE in the database URL)
read();
return new NoOperation(session);
} else if (readIf("HSQLDB.DEFAULT_TABLE_TYPE")) {
readIfEqualOrTo();
read();
return new NoOperation(session);
} else if (readIf("PAGE_STORE")) {
readIfEqualOrTo();
read();
return new NoOperation(session);
} else if (readIf("CACHE_TYPE")) {
readIfEqualOrTo();
read();
return new NoOperation(session);
} else if (readIf("FILE_LOCK")) {
readIfEqualOrTo();
read();
return new NoOperation(session);
} else if (readIf("DB_CLOSE_ON_EXIT")) {
readIfEqualOrTo();
read();
return new NoOperation(session);
} else if (readIf("AUTO_SERVER")) {
readIfEqualOrTo();
read();
return new NoOperation(session);
} else if (readIf("AUTO_SERVER_PORT")) {
readIfEqualOrTo();
read();
return new NoOperation(session);
} else if (readIf("AUTO_RECONNECT")) {
readIfEqualOrTo();
read();
return new NoOperation(session);
} else if (readIf("ASSERT")) {
readIfEqualOrTo();
read();
return new NoOperation(session);
} else if (readIf("ACCESS_MODE_DATA")) {
readIfEqualOrTo();
read();
return new NoOperation(session);
} else if (readIf("OPEN_NEW")) {
readIfEqualOrTo();
read();
return new NoOperation(session);
} else if (readIf("JMX")) {
readIfEqualOrTo();
read();
return new NoOperation(session);
} else if (readIf("PAGE_SIZE")) {
readIfEqualOrTo();
read();
return new NoOperation(session);
} else if (readIf("RECOVER")) {
readIfEqualOrTo();
read();
return new NoOperation(session);
} else if (readIf("NAMES")) {
// Quercus PHP MySQL driver compatibility
readIfEqualOrTo();
read();
return new NoOperation(session);
} else if (readIf("SCOPE_GENERATED_KEYS")) {
readIfEqualOrTo();
read();
return new NoOperation(session);
} else if (readIf("SCHEMA")) {
readIfEqualOrTo();
Set command = new Set(session, SetTypes.SCHEMA);
command.setString(readAliasIdentifier());
return command;
} else if (readIf("DATESTYLE")) {
// PostgreSQL compatibility
readIfEqualOrTo();
if (!readIf("ISO")) {
String s = readString();
if (!equalsToken(s, "ISO")) {
throw getSyntaxError();
}
}
return new NoOperation(session);
} else if (readIf("SEARCH_PATH") || readIf(SetTypes.getTypeName(SetTypes.SCHEMA_SEARCH_PATH))) {
readIfEqualOrTo();
Set command = new Set(session, SetTypes.SCHEMA_SEARCH_PATH);
ArrayList<String> list = New.arrayList();
list.add(readAliasIdentifier());
while (readIf(",")) {
list.add(readAliasIdentifier());
}
command.setStringArray(list.toArray(new String[0]));
return command;
} else if (readIf("JAVA_OBJECT_SERIALIZER")) {
readIfEqualOrTo();
return parseSetJavaObjectSerializer();
} else {
if (isToken("LOGSIZE")) {
// HSQLDB compatibility
currentToken = SetTypes.getTypeName(SetTypes.MAX_LOG_SIZE);
}
if (isToken("FOREIGN_KEY_CHECKS")) {
// MySQL compatibility
currentToken = SetTypes.getTypeName(SetTypes.REFERENTIAL_INTEGRITY);
}
int type = SetTypes.getType(currentToken);
if (type < 0) {
throw getSyntaxError();
}
read();
readIfEqualOrTo();
Set command = new Set(session, type);
command.setExpression(readExpression());
return command;
}
}
use of org.h2.engine.Mode in project h2database by h2database.
the class Parser method parseShow.
private Prepared parseShow() {
ArrayList<Value> paramValues = New.arrayList();
StringBuilder buff = new StringBuilder("SELECT ");
if (readIf("CLIENT_ENCODING")) {
// for PostgreSQL compatibility
buff.append("'UNICODE' AS CLIENT_ENCODING FROM DUAL");
} else if (readIf("DEFAULT_TRANSACTION_ISOLATION")) {
// for PostgreSQL compatibility
buff.append("'read committed' AS DEFAULT_TRANSACTION_ISOLATION " + "FROM DUAL");
} else if (readIf("TRANSACTION")) {
// for PostgreSQL compatibility
read("ISOLATION");
read("LEVEL");
buff.append("'read committed' AS TRANSACTION_ISOLATION " + "FROM DUAL");
} else if (readIf("DATESTYLE")) {
// for PostgreSQL compatibility
buff.append("'ISO' AS DATESTYLE FROM DUAL");
} else if (readIf("SERVER_VERSION")) {
// for PostgreSQL compatibility
buff.append("'" + Constants.PG_VERSION + "' AS SERVER_VERSION FROM DUAL");
} else if (readIf("SERVER_ENCODING")) {
// for PostgreSQL compatibility
buff.append("'UTF8' AS SERVER_ENCODING FROM DUAL");
} else if (readIf("TABLES")) {
// for MySQL compatibility
String schema = Constants.SCHEMA_MAIN;
if (readIf("FROM")) {
schema = readUniqueIdentifier();
}
buff.append("TABLE_NAME, TABLE_SCHEMA FROM " + "INFORMATION_SCHEMA.TABLES " + "WHERE TABLE_SCHEMA=? ORDER BY TABLE_NAME");
paramValues.add(ValueString.get(schema));
} else if (readIf("COLUMNS")) {
// for MySQL compatibility
read("FROM");
String tableName = readIdentifierWithSchema();
String schemaName = getSchema().getName();
paramValues.add(ValueString.get(tableName));
if (readIf("FROM")) {
schemaName = readUniqueIdentifier();
}
buff.append("C.COLUMN_NAME FIELD, " + "C.TYPE_NAME || '(' || C.NUMERIC_PRECISION || ')' TYPE, " + "C.IS_NULLABLE \"NULL\", " + "CASE (SELECT MAX(I.INDEX_TYPE_NAME) FROM " + "INFORMATION_SCHEMA.INDEXES I " + "WHERE I.TABLE_SCHEMA=C.TABLE_SCHEMA " + "AND I.TABLE_NAME=C.TABLE_NAME " + "AND I.COLUMN_NAME=C.COLUMN_NAME)" + "WHEN 'PRIMARY KEY' THEN 'PRI' " + "WHEN 'UNIQUE INDEX' THEN 'UNI' ELSE '' END KEY, " + "IFNULL(COLUMN_DEFAULT, 'NULL') DEFAULT " + "FROM INFORMATION_SCHEMA.COLUMNS C " + "WHERE C.TABLE_NAME=? AND C.TABLE_SCHEMA=? " + "ORDER BY C.ORDINAL_POSITION");
paramValues.add(ValueString.get(schemaName));
} else if (readIf("DATABASES") || readIf("SCHEMAS")) {
// for MySQL compatibility
buff.append("SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA");
}
boolean b = session.getAllowLiterals();
try {
// need to temporarily enable it, in case we are in
// ALLOW_LITERALS_NUMBERS mode
session.setAllowLiterals(true);
return prepare(session, buff.toString(), paramValues);
} finally {
session.setAllowLiterals(b);
}
}
Aggregations