private IRubyObject callEachType(
     MethodType type, IRubyObject rubyReceiver, String methodName, Block block, Object... args) {
   Ruby runtime = rubyReceiver.getRuntime();
   IRubyObject[] rubyArgs = null;
   if (args != null && args.length > 0) {
     rubyArgs = JavaUtil.convertJavaArrayToRuby(runtime, args);
     for (int i = 0; i < rubyArgs.length; i++) {
       IRubyObject obj = rubyArgs[i];
       if (obj instanceof JavaObject) {
         rubyArgs[i] = Java.wrap(runtime, obj);
       }
     }
   }
   ThreadContext context = runtime.getCurrentContext();
   switch (type) {
     case CALLMETHOD_NOARG:
       return RuntimeHelpers.invoke(context, rubyReceiver, methodName);
     case CALLMETHOD:
       return RuntimeHelpers.invoke(context, rubyReceiver, methodName, rubyArgs);
     case CALLMETHOD_WITHBLOCK:
       return RuntimeHelpers.invoke(context, rubyReceiver, methodName, rubyArgs, block);
     case CALLSUPER:
       return RuntimeHelpers.invokeSuper(context, rubyReceiver, rubyArgs, Block.NULL_BLOCK);
     case CALLSUPER_WITHBLOCK:
       return RuntimeHelpers.invokeSuper(context, rubyReceiver, rubyArgs, block);
     default:
       break;
   }
   return null;
 }
示例#2
0
 @JRubyMethod
 public static IRubyObject to_java(ThreadContext context, IRubyObject fromObject) {
   if (fromObject instanceof RubyArray) {
     return context
         .runtime
         .getJavaSupport()
         .getObjectJavaClass()
         .javaArrayFromRubyArray(context, fromObject);
   } else {
     return Java.getInstance(context.runtime, fromObject.toJava(Object.class));
   }
 }
示例#3
0
 @JRubyMethod(name = "[]", meta = true, rest = true)
 public static IRubyObject op_aref(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
   IRubyObject javaClass = RuntimeHelpers.invoke(context, recv, "java_class");
   if (args.length > 0) {
     // construct new array proxy (ArrayJavaProxy)
     ArrayJavaProxyCreator ajpc = new ArrayJavaProxyCreator(context.runtime);
     ajpc.setup(context, javaClass, args);
     return ajpc;
   } else {
     return Java.get_proxy_class(
         javaClass, RuntimeHelpers.invoke(context, javaClass, "array_class"));
   }
 }
示例#4
0
  @JRubyMethod
  public static IRubyObject to_java(
      ThreadContext context, IRubyObject fromObject, IRubyObject type) {
    if (type.isNil()) {
      return to_java(context, fromObject);
    }

    Ruby runtime = context.runtime;
    JavaClass targetType = getTargetType(context, runtime, type);

    if (fromObject instanceof RubyArray) {
      return targetType.javaArrayFromRubyArray(context, fromObject);
    } else {
      return Java.getInstance(runtime, fromObject.toJava(targetType.javaClass()));
    }
  }
示例#5
0
 @JRubyMethod(name = "hadoop")
 public IRubyObject hadoop(final ThreadContext ctx) {
   return Java.getInstance(ctx.runtime, NullWritable.get());
 }
示例#6
0
文件: Command.java 项目: gryn/do
  @JRubyMethod(optional = 1, rest = true)
  public static IRubyObject execute_reader(IRubyObject recv, IRubyObject[] args) {
    Ruby runtime = recv.getRuntime();
    IRubyObject connection_instance = api.getInstanceVariable(recv, "@connection");
    IRubyObject wrapped_jdbc_connection =
        api.getInstanceVariable(connection_instance, "@connection");
    if (wrapped_jdbc_connection.isNil()) {
      throw DataObjectsUtils.newDriverError(
          runtime, errorName, "This connection has already been closed.");
    }
    java.sql.Connection conn = getConnection(wrapped_jdbc_connection);

    RubyClass readerClass = Reader.createReaderClass(runtime, moduleName, errorName, driver);
    boolean inferTypes = false;
    int columnCount = 0;
    PreparedStatement sqlStatement = null;
    ResultSet resultSet = null;
    ResultSetMetaData metaData = null;

    // instantiate a new reader
    IRubyObject reader =
        readerClass.newInstance(
            runtime.getCurrentContext(), new IRubyObject[] {}, Block.NULL_BLOCK);

    // execute the query
    try {
      String sqlText =
          prepareSqlTextForPs(api.getInstanceVariable(recv, "@text").asJavaString(), recv, args);

      sqlStatement =
          conn.prepareStatement(
              sqlText,
              driver.supportsJdbcScrollableResultSets()
                  ? ResultSet.TYPE_SCROLL_INSENSITIVE
                  : ResultSet.TYPE_FORWARD_ONLY,
              ResultSet.CONCUR_READ_ONLY);

      // sqlStatement.setMaxRows();
      prepareStatementFromArgs(sqlStatement, recv, args);

      long startTime = System.currentTimeMillis();
      resultSet = sqlStatement.executeQuery();
      long endTime = System.currentTimeMillis();

      debug(recv.getRuntime(), driver.toString(sqlStatement), Long.valueOf(endTime - startTime));

      metaData = resultSet.getMetaData();
      columnCount = metaData.getColumnCount();

      // pass the response to the reader
      IRubyObject wrappedResultSet =
          Java.java_to_ruby(recv, JavaObject.wrap(recv.getRuntime(), resultSet), Block.NULL_BLOCK);
      reader.getInstanceVariables().setInstanceVariable("@reader", wrappedResultSet);

      wrappedResultSet.dataWrapStruct(resultSet);

      // handle each result

      // mark the reader as opened
      api.setInstanceVariable(reader, "@opened", runtime.newBoolean(true));
      // TODO: if no response return nil

      api.setInstanceVariable(reader, "@position", runtime.newFixnum(0));

      // save the field_count in reader
      api.setInstanceVariable(reader, "@field_count", runtime.newFixnum(columnCount));

      // get the field types
      RubyArray field_names = runtime.newArray();
      IRubyObject field_types = api.getInstanceVariable(recv, "@field_types");

      // If no types are passed in, infer them
      if (field_types == null) {
        field_types = runtime.newArray();
        inferTypes = true;
      } else {
        int fieldTypesCount = field_types.convertToArray().getLength();
        if (field_types.isNil() || fieldTypesCount == 0) {
          field_types = runtime.newArray();
          inferTypes = true;
        } else if (fieldTypesCount != columnCount) {
          // Wrong number of fields passed to set_types. Close the reader
          // and raise an error.
          api.callMethod(reader, "close");
          throw runtime.newArgumentError(
              String.format(
                  "Field-count mismatch. Expected %1$d fields, but the query yielded %2$d",
                  fieldTypesCount, columnCount));
        }
      }

      // for each field
      for (int i = 0; i < columnCount; i++) {
        RubyString field_name = runtime.newString(metaData.getColumnName(i + 1));
        // infer the type if no types passed
        field_names.push_m(new IRubyObject[] {field_name});

        if (inferTypes) {
          // TODO: do something
        }
      }

      // set the reader @field_names and @types (guessed or otherwise)
      api.setInstanceVariable(reader, "@fields", field_names);
      api.setInstanceVariable(reader, "@field_types", field_types);

      // keep the statement open

      // TODO why keep it open ???

      // sqlStatement.close();
      // sqlStatement = null;
    } catch (SQLException sqle) {
      // TODO: log sqle.printStackTrace();
      throw newQueryError(runtime, sqle, sqlStatement);
    } finally {
      // if (sqlStatement != null) {
      //    try {
      //        sqlStatement.close();
      //    } catch (SQLException stsqlex) {
      //    }
      // }
    }

    // return the reader
    return reader;
  }