diff options
author | Eike Stepper | 2009-06-05 17:01:12 +0000 |
---|---|---|
committer | Eike Stepper | 2009-06-05 17:01:12 +0000 |
commit | deb6e1bad104ad3ec502a6640b42b696bd7ab6ec (patch) | |
tree | dccac4fcb6bc61329c90e882e87713eec6c4f29f /plugins/org.eclipse.net4j.db.h2/src | |
parent | 566ed8478166cb6232acebd181f4862d09f7fd6a (diff) | |
download | cdo-deb6e1bad104ad3ec502a6640b42b696bd7ab6ec.tar.gz cdo-deb6e1bad104ad3ec502a6640b42b696bd7ab6ec.tar.xz cdo-deb6e1bad104ad3ec502a6640b42b696bd7ab6ec.zip |
[277808] [DB] Support for H2 Database
https://bugs.eclipse.org/bugs/show_bug.cgi?id=277808
Diffstat (limited to 'plugins/org.eclipse.net4j.db.h2/src')
-rw-r--r-- | plugins/org.eclipse.net4j.db.h2/src/org/eclipse/net4j/db/h2/H2Adapter.java | 129 | ||||
-rw-r--r-- | plugins/org.eclipse.net4j.db.h2/src/org/eclipse/net4j/db/internal/h2/bundle/OM.java | 46 |
2 files changed, 175 insertions, 0 deletions
diff --git a/plugins/org.eclipse.net4j.db.h2/src/org/eclipse/net4j/db/h2/H2Adapter.java b/plugins/org.eclipse.net4j.db.h2/src/org/eclipse/net4j/db/h2/H2Adapter.java new file mode 100644 index 0000000000..8dbf185741 --- /dev/null +++ b/plugins/org.eclipse.net4j.db.h2/src/org/eclipse/net4j/db/h2/H2Adapter.java @@ -0,0 +1,129 @@ +/** + * Copyright (c) 2004 - 2009 Eike Stepper (Berlin, Germany) and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.db.h2; + +import org.eclipse.net4j.db.DBType; +import org.eclipse.net4j.db.ddl.IDBField; +import org.eclipse.net4j.spi.db.DBAdapter; + +import org.h2.jdbcx.JdbcDataSource; + +import javax.sql.DataSource; + +import java.sql.Driver; +import java.util.StringTokenizer; + +/** + * @author Eike Stepper + * @since 2.0 + */ +public class H2Adapter extends DBAdapter +{ + private static final String NAME = "h2"; //$NON-NLS-1$ + + public static final String VERSION = "1.1.114"; //$NON-NLS-1$ + + public H2Adapter() + { + super(NAME, VERSION); + } + + public Driver getJDBCDriver() + { + return new org.h2.Driver(); + } + + public DataSource createJDBCDataSource() + { + return new JdbcDataSource(); + } + + @Override + protected String getTypeName(IDBField field) + { + DBType type = field.getType(); + switch (type) + { + case BIT: + return "SMALLINT"; //$NON-NLS-1$ + + case FLOAT: + return "REAL"; //$NON-NLS-1$ + + case LONGVARCHAR: + return "VARCHAR"; //$NON-NLS-1$ + + case NUMERIC: + return "INT"; //$NON-NLS-1$ + + case LONGVARBINARY: + case VARBINARY: + return "BLOB"; //$NON-NLS-1$ + } + + return super.getTypeName(field); + } + + public String[] getReservedWords() + { + return getSQL92ReservedWords(); + } + + @Override + public void appendValue(StringBuilder builder, IDBField field, Object value) + { + Object newValue = value; + + if (value instanceof String) + { + // H2 just adds one additional single quote for a single quote + String str = (String)value; + StringTokenizer tokenizer = new StringTokenizer(str, "\'", true); // split on single quote //$NON-NLS-1$ + StringBuilder newValueBuilder = new StringBuilder(); + + while (tokenizer.hasMoreTokens()) + { + String current = tokenizer.nextToken(); + if (current.length() == 0) + { + continue; + } + + if (current.length() > 1) // >1 -> can not be token -> normal string + { + newValueBuilder.append(current); + } + else + { // length == 1 + newValueBuilder.append(processEscape(current.charAt(0))); + } + } + + newValue = newValueBuilder.toString(); + } + else if (value instanceof Character) + { + newValue = processEscape((Character)value); + } + + super.appendValue(builder, field, newValue); + } + + private Object processEscape(char c) + { + if (c == '\'') // one single quote --> + { + return "\'\'"; // results two single quotes //$NON-NLS-1$ + } + + return c; // no escape character --> return as is + } +} diff --git a/plugins/org.eclipse.net4j.db.h2/src/org/eclipse/net4j/db/internal/h2/bundle/OM.java b/plugins/org.eclipse.net4j.db.h2/src/org/eclipse/net4j/db/internal/h2/bundle/OM.java new file mode 100644 index 0000000000..6bf2400f98 --- /dev/null +++ b/plugins/org.eclipse.net4j.db.h2/src/org/eclipse/net4j/db/internal/h2/bundle/OM.java @@ -0,0 +1,46 @@ +/** + * Copyright (c) 2004 - 2009 Eike Stepper (Berlin, Germany) and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.db.internal.h2.bundle; + +import org.eclipse.net4j.util.om.OMBundle; +import org.eclipse.net4j.util.om.OMPlatform; +import org.eclipse.net4j.util.om.OSGiActivator; +import org.eclipse.net4j.util.om.log.OMLogger; +import org.eclipse.net4j.util.om.trace.OMTracer; + +/** + * The <em>Operations & Maintenance</em> class of this bundle. + * + * @author Eike Stepper + */ +public abstract class OM +{ + public static final String BUNDLE_ID = "org.eclipse.net4j.db.h2"; //$NON-NLS-1$ + + public static final OMBundle BUNDLE = OMPlatform.INSTANCE.bundle(BUNDLE_ID, OM.class); + + public static final OMTracer DEBUG = BUNDLE.tracer("debug"); //$NON-NLS-1$ + + public static final OMTracer DEBUG_SQL = DEBUG.tracer("sql"); //$NON-NLS-1$ + + public static final OMLogger LOG = BUNDLE.logger(); + + /** + * @author Eike Stepper + */ + public static final class Activator extends OSGiActivator + { + public Activator() + { + super(BUNDLE); + } + } +} |