gora-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From e...@apache.org
Subject svn commit: r1006024 [8/8] - in /incubator/gora: branches/ tags/ trunk/ trunk/bin/ trunk/conf/ trunk/docs/ trunk/gora-cassandra/ trunk/gora-cassandra/ivy/ trunk/gora-cassandra/lib-ext/ trunk/gora-cassandra/src/ trunk/gora-cassandra/src/examples/ trunk/...
Date Fri, 08 Oct 2010 21:17:17 GMT
Added: incubator/gora/trunk/gora-sql/src/main/java/org/gora/sql/store/SqlTypeInterface.java
URL: http://svn.apache.org/viewvc/incubator/gora/trunk/gora-sql/src/main/java/org/gora/sql/store/SqlTypeInterface.java?rev=1006024&view=auto
==============================================================================
--- incubator/gora/trunk/gora-sql/src/main/java/org/gora/sql/store/SqlTypeInterface.java (added)
+++ incubator/gora/trunk/gora-sql/src/main/java/org/gora/sql/store/SqlTypeInterface.java Fri
Oct  8 21:17:10 2010
@@ -0,0 +1,227 @@
+
+package org.gora.sql.store;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.sql.Types;
+import java.util.Currency;
+import java.util.HashMap;
+import java.util.Locale;
+
+import org.apache.avro.Schema;
+import org.apache.avro.Schema.Type;
+import org.apache.avro.util.Utf8;
+import org.apache.hadoop.io.DoubleWritable;
+import org.apache.hadoop.io.FloatWritable;
+import org.apache.hadoop.io.IntWritable;
+import org.apache.hadoop.io.LongWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.VIntWritable;
+import org.apache.hadoop.io.VLongWritable;
+import org.apache.hadoop.io.Writable;
+
+/**
+ * Contains utility methods related to type conversion between
+ * java, avro and SQL types.
+ */
+public class SqlTypeInterface {
+
+  /**
+   * Encapsules java.sql.Types as an enum
+   */
+  public static enum JdbcType {
+    ARRAY(Types.ARRAY),
+    BIT(Types.BIT),
+    BIGINT(Types.BIGINT),
+    BINARY(Types.BINARY),
+    BLOB(Types.BLOB),
+    BOOLEAN(Types.BOOLEAN),
+    CHAR(Types.CHAR),
+    CLOB(Types.CLOB),
+    DATALINK(Types.DATALINK),
+    DATE(Types.DATE),
+    DECIMAL(Types.DECIMAL),
+    DISTINCT(Types.DISTINCT),
+    DOUBLE(Types.DOUBLE),
+    FLOAT(Types.FLOAT),
+    INTEGER(Types.INTEGER),
+    LONGNVARCHAR(Types.LONGNVARCHAR),
+    LONGVARBINARY(Types.LONGVARBINARY),
+    LONGVARCHAR(Types.LONGVARCHAR),
+    NCHAR(Types.NCHAR),
+    NCLOB(Types.NCLOB),
+    NULL(Types.NULL),
+    NUMERIC(Types.NUMERIC),
+    NVARCHAR(Types.NVARCHAR),
+    OTHER(Types.OTHER),
+    REAL(Types.REAL),
+    REF(Types.REF),
+    ROWID(Types.ROWID),
+    SMALLINT(Types.SMALLINT),
+    SQLXML(Types.SQLXML, "XML"),
+    STRUCT(Types.STRUCT),
+    TIME(Types.TIME),
+    TIMESTAMP(Types.TIMESTAMP),
+    TINYINT(Types.TINYINT),
+    VARBINARY(Types.VARBINARY),
+    VARCHAR(Types.VARCHAR)
+    ;
+
+    private int order;
+    private String sqlType;
+
+    private JdbcType(int order) {
+      this.order = order;
+    }
+    private JdbcType(int order, String sqlType) {
+      this.order = order;
+      this.sqlType = sqlType;
+    }
+    public String getSqlType() {
+      return sqlType == null ? toString() : sqlType;
+    }
+    public int getOrder() {
+      return order;
+    }
+
+    private static HashMap<Integer, JdbcType> map =
+      new HashMap<Integer, JdbcType>();
+    static {
+      for(JdbcType type : JdbcType.values()) {
+        map.put(type.order, type);
+      }
+    }
+
+    /**
+     * Returns a JdbcType enum from a jdbc type in java.sql.Types
+     * @param order an integer in java.sql.Types
+     */
+    public static final JdbcType get(int order) {
+      return map.get(order);
+    }
+  };
+
+  public static int getSqlType(Class<?> clazz) {
+
+    //jdo default types
+    if (Boolean.class.isAssignableFrom(clazz)) {
+      return Types.BIT;
+    } else if (Character.class.isAssignableFrom(clazz)) {
+      return Types.CHAR;
+    } else if (Byte.class.isAssignableFrom(clazz)) {
+      return Types.TINYINT;
+    } else if (Short.class.isAssignableFrom(clazz)) {
+      return Types.SMALLINT;
+    } else if (Integer.class.isAssignableFrom(clazz)) {
+      return Types.INTEGER;
+    } else if (Long.class.isAssignableFrom(clazz)) {
+      return Types.BIGINT;
+    } else if (Float.class.isAssignableFrom(clazz)) {
+      return Types.FLOAT;
+    } else if (Double.class.isAssignableFrom(clazz)) {
+      return Types.DOUBLE;
+    } else if (java.util.Date.class.isAssignableFrom(clazz)) {
+      return Types.TIMESTAMP;
+    } else if (java.sql.Date.class.isAssignableFrom(clazz)) {
+      return Types.DATE;
+    } else if (java.sql.Time.class.isAssignableFrom(clazz)) {
+      return Types.TIME;
+    } else if (java.sql.Timestamp.class.isAssignableFrom(clazz)) {
+      return Types.TIMESTAMP;
+    } else if (String.class.isAssignableFrom(clazz)) {
+      return Types.VARCHAR;
+    } else if (Locale.class.isAssignableFrom(clazz)) {
+      return Types.VARCHAR;
+    } else if (Currency.class.isAssignableFrom(clazz)) {
+      return Types.VARCHAR;
+    } else if (BigInteger.class.isAssignableFrom(clazz)) {
+      return Types.NUMERIC;
+    } else if (BigDecimal.class.isAssignableFrom(clazz)) {
+      return Types.DECIMAL;
+    } else if (Serializable.class.isAssignableFrom(clazz)) {
+      return Types.LONGVARBINARY;
+    }
+
+    //Hadoop types
+    else if (DoubleWritable.class.isAssignableFrom(clazz)) {
+      return Types.DOUBLE;
+    } else if (FloatWritable.class.isAssignableFrom(clazz)) {
+      return Types.FLOAT;
+    } else if (IntWritable.class.isAssignableFrom(clazz)) {
+      return Types.INTEGER;
+    } else if (LongWritable.class.isAssignableFrom(clazz)) {
+      return Types.BIGINT;
+    } else if (Text.class.isAssignableFrom(clazz)) {
+      return Types.VARCHAR;
+    } else if (VIntWritable.class.isAssignableFrom(clazz)) {
+      return Types.INTEGER;
+    } else if (VLongWritable.class.isAssignableFrom(clazz)) {
+      return Types.BIGINT;
+    } else if (Writable.class.isAssignableFrom(clazz)) {
+      return Types.LONGVARBINARY;
+    }
+
+    //avro types
+    else if (Utf8.class.isAssignableFrom(clazz)) {
+      return Types.VARCHAR;
+    }
+
+    return Types.OTHER;
+  }
+
+  public static JdbcType getJdbcType(Schema schema, int length, int scale) throws IOException
{
+    Type type = schema.getType();
+
+    switch(type) {
+      case MAP    : return JdbcType.BLOB;
+      case ARRAY  : return JdbcType.BLOB;
+      case BOOLEAN: return JdbcType.BIT;
+      case BYTES  : return JdbcType.BLOB;
+      case DOUBLE : return JdbcType.DOUBLE;
+      case ENUM   : return JdbcType.VARCHAR;
+      case FIXED  : return JdbcType.BINARY;
+      case FLOAT  : return JdbcType.FLOAT;
+      case INT    : return JdbcType.INTEGER;
+      case LONG   : return JdbcType.BIGINT;
+      case NULL   : break;
+      case RECORD : return JdbcType.BLOB;
+      case STRING : return JdbcType.VARCHAR;
+      case UNION  : throw new IOException("Union is not supported yet");
+    }
+    return null;
+  }
+
+  public static JdbcType getJdbcType(Class<?> clazz, int length, int scale) throws
IOException {
+    if (clazz.equals(Enum.class)) {
+      return JdbcType.VARCHAR;
+    } else if (clazz.equals(Byte.TYPE) || clazz.equals(Byte.class)) {
+      return JdbcType.BLOB;
+    } else if (clazz.equals(Boolean.TYPE) || clazz.equals(Boolean.class)) {
+      return JdbcType.BIT;
+    } else if (clazz.equals(Short.TYPE) || clazz.equals(Short.class)) {
+      return JdbcType.INTEGER;
+    } else if (clazz.equals(Integer.TYPE) || clazz.equals(Integer.class)) {
+      return JdbcType.INTEGER;
+    } else if (clazz.equals(Long.TYPE) || clazz.equals(Long.class)) {
+      return JdbcType.BIGINT;
+    } else if (clazz.equals(Float.TYPE) || clazz.equals(Float.class)) {
+      return JdbcType.FLOAT;
+    } else if (clazz.equals(Double.TYPE) || clazz.equals(Double.class)) {
+      return JdbcType.FLOAT;
+    } else if (clazz.equals(String.class)) {
+      return JdbcType.VARCHAR;
+    }
+    throw new RuntimeException("Can't parse data as class: " + clazz);
+  }
+
+  public static JdbcType stringToJdbcType(String type) {
+    try {
+      return JdbcType.valueOf(type);
+    }catch (IllegalArgumentException ex) {
+      return JdbcType.OTHER; //db specific type
+    }
+  }
+
+}

Added: incubator/gora/trunk/gora-sql/src/main/java/org/gora/sql/util/SqlUtils.java
URL: http://svn.apache.org/viewvc/incubator/gora/trunk/gora-sql/src/main/java/org/gora/sql/util/SqlUtils.java?rev=1006024&view=auto
==============================================================================
--- incubator/gora/trunk/gora-sql/src/main/java/org/gora/sql/util/SqlUtils.java (added)
+++ incubator/gora/trunk/gora-sql/src/main/java/org/gora/sql/util/SqlUtils.java Fri Oct  8
21:17:10 2010
@@ -0,0 +1,30 @@
+
+package org.gora.sql.util;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+/**
+ * SQL related utilities
+ */
+public class SqlUtils {
+  
+  /** Closes the ResultSet silently */
+  public static void close(ResultSet rs) {
+    if(rs != null) {
+      try {
+        rs.close();
+      } catch (SQLException ignore) { }
+    }
+  }
+  
+  /** Closes the Statement silently */
+  public static void close(Statement statement) {
+    if(statement != null) {
+      try {
+        statement.close();
+      } catch (SQLException ignore) { }
+    }
+  }
+}

Added: incubator/gora/trunk/gora-sql/src/test/conf/.gitignore
URL: http://svn.apache.org/viewvc/incubator/gora/trunk/gora-sql/src/test/conf/.gitignore?rev=1006024&view=auto
==============================================================================
    (empty)

Added: incubator/gora/trunk/gora-sql/src/test/conf/gora-sql-mapping.xml
URL: http://svn.apache.org/viewvc/incubator/gora/trunk/gora-sql/src/test/conf/gora-sql-mapping.xml?rev=1006024&view=auto
==============================================================================
--- incubator/gora/trunk/gora-sql/src/test/conf/gora-sql-mapping.xml (added)
+++ incubator/gora/trunk/gora-sql/src/test/conf/gora-sql-mapping.xml Fri Oct  8 21:17:10 2010
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<gora-orm>
+  <class name="org.gora.examples.generated.Employee" keyClass="java.lang.String" table="Employee">
+    <primarykey column="id" length="16"/>
+    <field name="name" column="name" length="128"/>
+    <field name="dateOfBirth" column="dateOfBirth"/>
+    <field name="ssn" column="ssn" jdbc-type="VARCHAR_IGNORECASE" length="16"/> <!--
jdbc-type is HSQLDB specific for testing -->
+    <field name="salary" column="salary"/>
+  </class>
+
+  <class name="org.gora.examples.generated.WebPage" keyClass="java.lang.String" table="WebPage">
+    <primarykey column="id" length="128"/>
+    <field name="url" column="url" length="128" primarykey="true"/>
+    <field name="content" column="content"/>
+    <field name="parsedContent" column="parsedContent"/>
+    <field name="outlinks" column="outlinks"/>
+    <field name="metadata" column="metadata"/>
+  </class>
+
+<!--
+<table name="TokenDatum" keyClass="java.lang.String" persistentClass="org.gora.examples.generated.TokenDatum">
+  <description>
+    <family name="common"/>
+  </description>
+  <fields>
+    <field name="count" family="common" qualifier="count"/>
+  </fields>
+</table>
+-->
+</gora-orm>
+

Added: incubator/gora/trunk/gora-sql/src/test/java/org/gora/sql/GoraSqlTestDriver.java
URL: http://svn.apache.org/viewvc/incubator/gora/trunk/gora-sql/src/test/java/org/gora/sql/GoraSqlTestDriver.java?rev=1006024&view=auto
==============================================================================
--- incubator/gora/trunk/gora-sql/src/test/java/org/gora/sql/GoraSqlTestDriver.java (added)
+++ incubator/gora/trunk/gora-sql/src/test/java/org/gora/sql/GoraSqlTestDriver.java Fri Oct
 8 21:17:10 2010
@@ -0,0 +1,100 @@
+
+package org.gora.sql;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.util.Properties;
+
+import org.apache.hadoop.util.StringUtils;
+import org.gora.GoraTestDriver;
+import org.gora.sql.store.SqlStore;
+import org.hsqldb.Server;
+
+/**
+ * Helper class for third part tests using gora-sql backend. 
+ * @see GoraTestDriver
+ */
+public class GoraSqlTestDriver extends GoraTestDriver {
+
+  public GoraSqlTestDriver() {
+    super(SqlStore.class);
+  }
+
+  /** The JDBC Driver class name */
+  protected static final String DRIVER_CLASS_PROPERTY = "jdbc.driver";
+  /** JDBC Database access URL */
+  protected static final String URL_PROPERTY = "jdbc.url";
+  /** User name to access the database */
+  protected static final String USERNAME_PROPERTY = "jdbc.user";
+  /** Password to access the database */
+  protected static final String PASSWORD_PROPERTY = "jdbc.password";
+
+
+  private static final String JDBC_URL = "jdbc:hsqldb:hsql://localhost/goratest";
+  private static final String JDBC_DRIVER_CLASS = "org.hsqldb.jdbcDriver";
+
+  private Server server;
+
+  private boolean initialized = false;
+
+  private boolean startHsqldb = true;
+
+  private void startHsqldbServer() {
+    log.info("Starting HSQLDB server");
+    server = new Server();
+    server.setDatabasePath(0,
+        System.getProperty("test.build.data", "/tmp") + "/goratest");
+    server.setDatabaseName(0, "goratest");
+    server.setDaemon(true);
+    server.start();
+  }
+
+  @Override
+  public void setUpClass() throws Exception {
+    super.setUpClass();
+
+    if(!this.initialized && startHsqldb) {
+      startHsqldbServer();
+      this.initialized = true;
+    }
+  }
+
+  @Override
+  public void tearDownClass() throws Exception {
+    super.tearDownClass();
+    try {
+      if(server != null) {
+        server.shutdown();
+      }
+    }catch (Throwable ex) {
+      log.warn("Exception occurred while shutting down HSQLDB :"
+          + StringUtils.stringifyException(ex));
+    }
+  }
+
+  @Override
+  public void tearDown() throws Exception {
+    super.tearDown();
+  }
+
+  @SuppressWarnings("unused")
+  private Connection createConnection(String driverClassName
+      , String url) throws Exception {
+
+    Class.forName(driverClassName);
+    Connection connection = DriverManager.getConnection(url);
+    connection.setAutoCommit(false);
+    return connection;
+  }
+
+
+  @Override
+  protected void setProperties(Properties properties) {
+    super.setProperties(properties);
+    properties.setProperty("gora.sqlstore." + DRIVER_CLASS_PROPERTY, JDBC_DRIVER_CLASS);
+    properties.setProperty("gora.sqlstore." + URL_PROPERTY, JDBC_URL);
+    properties.remove("gora.sqlstore." + USERNAME_PROPERTY);
+    properties.remove("gora.sqlstore." + PASSWORD_PROPERTY);
+  }
+
+}

Added: incubator/gora/trunk/gora-sql/src/test/java/org/gora/sql/store/TestSqlStore.java
URL: http://svn.apache.org/viewvc/incubator/gora/trunk/gora-sql/src/test/java/org/gora/sql/store/TestSqlStore.java?rev=1006024&view=auto
==============================================================================
--- incubator/gora/trunk/gora-sql/src/test/java/org/gora/sql/store/TestSqlStore.java (added)
+++ incubator/gora/trunk/gora-sql/src/test/java/org/gora/sql/store/TestSqlStore.java Fri Oct
 8 21:17:10 2010
@@ -0,0 +1,58 @@
+
+package org.gora.sql.store;
+
+import java.io.IOException;
+
+import org.gora.examples.generated.Employee;
+import org.gora.examples.generated.WebPage;
+import org.gora.sql.GoraSqlTestDriver;
+import org.gora.store.DataStore;
+import org.gora.store.DataStoreFactory;
+import org.gora.store.DataStoreTestBase;
+
+/**
+ * Test case for {@link SqlStore}
+ */
+public class TestSqlStore extends DataStoreTestBase {
+
+  static {
+    setTestDriver(new GoraSqlTestDriver());
+  }
+
+  public TestSqlStore() {
+  }
+
+  @Override
+  protected DataStore<String, Employee> createEmployeeDataStore() throws IOException
{
+    SqlStore<String, Employee> store = new SqlStore<String, Employee>();
+    store.initialize(String.class, Employee.class, DataStoreFactory.properties);
+    return store;
+  }
+
+  @Override
+  protected DataStore<String, WebPage> createWebPageDataStore() throws IOException
{
+    SqlStore<String, WebPage> store = new SqlStore<String, WebPage>();
+    store.initialize(String.class, WebPage.class, DataStoreFactory.properties);
+    return store;
+  }
+
+  //@Override
+  public void testDeleteByQueryFields() {
+    //TODO: implement delete fields in SqlStore
+  }
+
+  //@Override
+  public void testDeleteByQuery() throws IOException {
+    //HSQLDB somehow hangs for this test. we need to solve the issue or switch to
+    //another embedded db.
+  }
+
+  public static void main(String[] args) throws Exception {
+    TestSqlStore test = new TestSqlStore();
+    TestSqlStore.setUpClass();
+    test.setUp();
+    test.testDeleteByQuery();
+    test.tearDown();
+    TestSqlStore.tearDownClass();
+  }
+}

Added: incubator/gora/trunk/ivy/ivy-2.1.0.jar
URL: http://svn.apache.org/viewvc/incubator/gora/trunk/ivy/ivy-2.1.0.jar?rev=1006024&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/gora/trunk/ivy/ivy-2.1.0.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: incubator/gora/trunk/ivy/ivy-configurations.xml
URL: http://svn.apache.org/viewvc/incubator/gora/trunk/ivy/ivy-configurations.xml?rev=1006024&view=auto
==============================================================================
--- incubator/gora/trunk/ivy/ivy-configurations.xml (added)
+++ incubator/gora/trunk/ivy/ivy-configurations.xml Fri Oct  8 21:17:10 2010
@@ -0,0 +1,4 @@
+<configurations>
+  <conf name="compile" visibility="public" />
+  <conf name="test" visibility="public" extends="compile"/>
+</configurations>

Added: incubator/gora/trunk/ivy/ivysettings.xml
URL: http://svn.apache.org/viewvc/incubator/gora/trunk/ivy/ivysettings.xml?rev=1006024&view=auto
==============================================================================
--- incubator/gora/trunk/ivy/ivysettings.xml (added)
+++ incubator/gora/trunk/ivy/ivysettings.xml Fri Oct  8 21:17:10 2010
@@ -0,0 +1,72 @@
+<ivysettings>
+  <!--
+  see http://www.jayasoft.org/ivy/doc/configuration
+  -->
+  <!-- you can override this property to use mirrors
+          http://repo1.maven.org/maven2/
+          http://mirrors.dotsrc.org/maven2
+          http://ftp.ggi-project.org/pub/packages/maven2
+          http://mirrors.sunsite.dk/maven2
+          http://public.planetmirror.com/pub/maven2
+          http://ibiblio.lsu.edu/main/pub/packages/maven2
+          http://www.ibiblio.net/pub/packages/maven2
+  -->
+  <property name="repo.maven.org"
+    value="http://repo1.maven.org/maven2/"
+    override="false"/>
+  <property name="repo.maven.local"
+    value="file://${user.home}/.m2/repository/"
+    override="false"/>
+  <property name="snapshot.apache.org"
+    value="http://people.apache.org/repo/m2-snapshot-repository/"
+    override="false"/>
+  <property name="maven2.pattern"
+    value="[organisation]/[module]/[revision]/[module]-[revision]"/>
+  <property name="maven2.pattern.ext"
+    value="${maven2.pattern}.[ext]"/>
+
+  <!-- pull in the local repository -->
+  <include url="${ivy.default.conf.dir}/ivyconf-local.xml"/>
+  <settings defaultResolver="default"/>
+  <resolvers>
+    <ibiblio name="maven2"
+      root="${repo.maven.org}"
+      pattern="${maven2.pattern.ext}"
+      m2compatible="true"
+      />
+    <ibiblio name="apache-snapshot"
+      root="${snapshot.apache.org}"
+      pattern="${maven2.pattern.ext}"
+      m2compatible="true"
+      />
+    <ibiblio name="maven2-local"
+     root="${repo.maven.local}"
+     pattern="${maven2.pattern.ext}"
+     m2compatible="true"
+     usepoms="true"
+     useMavenMetadata="true">
+    </ibiblio>
+    <chain name="default" dual="true">
+      <resolver ref="local"/>
+      <resolver ref="maven2-local"/>
+      <resolver ref="maven2"/>
+    </chain>
+    <chain name="internal">
+      <resolver ref="local"/>
+    </chain>
+    <chain name="external">
+      <resolver ref="maven2"/>
+    </chain>
+    <chain name="external-and-snapshots">
+      <resolver ref="maven2"/>
+      <resolver ref="apache-snapshot"/>
+    </chain>
+  </resolvers>
+    
+  <modules>
+    <!-- Force gora modules to be resolved locally -->
+    <module organisation="org.gora" name=".*" resolver="local"/>
+  </modules>
+
+</ivysettings>
+



Mime
View raw message