Beispiel #1
0
 def testPrimitive(self):
     test1 = self.__jp.Test1()
     intexpectation = 'long'
     # FIXME it is not possible to determine if this is bool/char/byte currently
     #self.assertEqual(intexpectation, test1.testPrimitive(5))
     #self.assertEqual('long', test1.testPrimitive(2**31))
     self.assertEqual('byte', test1.testPrimitive(JByte(5)))
     self.assertEqual('Byte', test1.testPrimitive(java.lang.Byte(5)))
     self.assertEqual('short', test1.testPrimitive(JShort(5)))
     self.assertEqual('Short', test1.testPrimitive(java.lang.Short(5)))
     self.assertEqual('int', test1.testPrimitive(JInt(5)))
     self.assertEqual('Integer', test1.testPrimitive(java.lang.Integer(5)))
     self.assertEqual('long', test1.testPrimitive(JLong(5)))
     self.assertEqual('Long', test1.testPrimitive(java.lang.Long(5)))
     self.assertEqual('float', test1.testPrimitive(JFloat(5)))
     self.assertEqual('Float', test1.testPrimitive(java.lang.Float(5.0)))
     self.assertEqual('double', test1.testPrimitive(JDouble(5)))
     self.assertEqual('Double', test1.testPrimitive(java.lang.Double(5.0)))
     self.assertEqual('boolean', test1.testPrimitive(JBoolean(5)))
     self.assertEqual('Boolean', test1.testPrimitive(java.lang.Boolean(5)))
     self.assertEqual('char', test1.testPrimitive(JChar('5')))
     self.assertEqual('Character',
                      test1.testPrimitive(java.lang.Character('5')))
Beispiel #2
0
 def testPrimitive(self):
     test1 = self.__jp.Test1()
     intexpectation = 'int' if not sys.version_info[
         0] > 2 and sys.maxint == 2**31 - 1 else 'long'
     self.assertEquals(intexpectation, test1.testPrimitive(5))
     self.assertEquals('long', test1.testPrimitive(2**31))
     self.assertEquals('byte', test1.testPrimitive(JByte(5)))
     self.assertEquals('Byte', test1.testPrimitive(java.lang.Byte(5)))
     self.assertEquals('short', test1.testPrimitive(JShort(5)))
     self.assertEquals('Short', test1.testPrimitive(java.lang.Short(5)))
     self.assertEquals('int', test1.testPrimitive(JInt(5)))
     self.assertEquals('Integer', test1.testPrimitive(java.lang.Integer(5)))
     self.assertEquals('long', test1.testPrimitive(JLong(5)))
     self.assertEquals('Long', test1.testPrimitive(java.lang.Long(5)))
     self.assertEquals('float', test1.testPrimitive(JFloat(5)))
     self.assertEquals('Float', test1.testPrimitive(java.lang.Float(5.0)))
     self.assertEquals('double', test1.testPrimitive(JDouble(5)))
     self.assertEquals('Double', test1.testPrimitive(java.lang.Double(5.0)))
     self.assertEquals('boolean', test1.testPrimitive(JBoolean(5)))
     self.assertEquals('Boolean', test1.testPrimitive(java.lang.Boolean(5)))
     self.assertEquals('char', test1.testPrimitive(JChar('5')))
     self.assertEquals('Character',
                       test1.testPrimitive(java.lang.Character('5')))
Beispiel #3
0
    def execute(self, operation, params=None):
        """
        Execute a sql statement with an optional set of parameters

        :param operation: Sql text
        :param params: a sequence or dictionary of parameters
                       Parameters can be positional templates ``%s`` or named templates ``:name``

        :param operation:
        :param params:
        :return:
        """
        if not self._connection_valid():
            raise Error('the connection has been closed')

        self._reset()

        conn = self._connection.jdbc_connection()

        # handle parameters
        if params is not None:
            operation, params = self._parse_params(operation, params)

        # prepare the statement
        self._statement = stmt = conn.prepareStatement(operation)
        stmt.clearParameters()
        for column, value in enumerate(params or [], start=1):
            # note that in JDBC the first column index is 1
            if isinstance(value, str):
                jvalue = JString(value)
                stmt.setString(column, jvalue)
            elif isinstance(value, float):
                jvalue = JDouble(value)
                stmt.setDouble(column, jvalue)
            elif isinstance(value, int):
                try:
                    jvalue = JInt(value)
                    stmt.setInt(column, jvalue)
                except Exception:
                    jvalue = JLong(value)
                    stmt.setLong(column, jvalue)
            elif isinstance(value, bool):
                jvalue = JBoolean(value)
                stmt.setBoolean(column, jvalue)
            elif isinstance(value, bytes):
                ByteArray = JArray(JByte)
                jvalue = ByteArray(value.decode('utf-8'))
                stmt.setBytes(column, jvalue)
            else:
                stmt.setObject(column, value)

        try:
            has_resultset = stmt.execute()
        except JClass('org.apache.hive.service.cli.HiveSQLException') as e:
            raise ProgrammingError('Error executing statement:\n{}\n{}'.format(
                operation, e)) from None

        self._rowcount = -1
        if has_resultset:
            self._resultset = resultset = stmt.getResultSet()
            self._metadata = resultset.getMetaData()

            # get rowcount
            if self._get_rowcounts:
                try:
                    if self._resultset.last(
                    ):  # if the cursor can be moved to the last row.
                        self._rowcount = resultset.getRow()
                    resultset.beforeFirst()
                except JClass('java.sql.SQLException'):
                    # ResultSet.last() is not supported
                    pass

        else:
            try:
                self._rowcount = stmt.getUpdateCount()
            except JClass('java.sql.SQLException'):
                # not supported
                pass