All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src.com.ziclix.python.sql.PyStatement Maven / Gradle / Ivy

Go to download

Jython is an implementation of the high-level, dynamic, object-oriented language Python written in 100% Pure Java, and seamlessly integrated with the Java platform. It thus allows you to run Python on any Java platform.

There is a newer version: 2.7.4
Show newest version
/*
 * Jython Database Specification API 2.0
 *
 *
 * Copyright (c) 2001 brian zimmer 
 *
 */
package com.ziclix.python.sql;

import org.python.core.codecs;
import org.python.core.Py;
import org.python.core.PyException;
import org.python.core.PyList;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.PyUnicode;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Class PyStatement
 *
 * @author brian zimmer
 */
public class PyStatement extends PyObject {

    /** Denotes a simple Statement with no parameters. */
    public static final int STATEMENT_STATIC = 2;

    /** Denotes a PreparedStatement either explicitly created by the user, or from a
     * cursor (due to the presence of bind parameters). */
    public static final int STATEMENT_PREPARED = 4;

    /** Denotes a stored procedure call. */
    public static final int STATEMENT_CALLABLE = 8;

    /** One of the above styles. */
    private int style;

    /** The underlying sql, a String or a Procedure. */
    private Object sql;

    /** Whether this statement is closed. */
    private boolean closed;

    /** The underlying java.sql.Statement. */
    Statement statement;

    /** Field __methods__ */
    protected static PyList __methods__;

    /** Field __members__ */
    protected static PyList __members__;

    static {
        PyObject[] m = new PyObject[1];

        m[0] = new PyString("close");
        __methods__ = new PyList(m);
        m = new PyObject[3];
        m[0] = new PyString("style");
        m[1] = new PyString("closed");
        m[2] = new PyString("__statement__");
        __members__ = new PyList(m);
    }

    /**
     * Constructor PyStatement
     *
     * @param statement
     * @param sql
     * @param style
     */
    public PyStatement(Statement statement, Object sql, int style) {
        this.statement = statement;
        this.sql = sql;
        this.style = style;
        closed = false;
    }

    /**
     * Constructor PyStatement
     *
     * @param statement
     * @param procedure
     */
    public PyStatement(Statement statement, Procedure procedure) {
        this(statement, procedure, STATEMENT_CALLABLE);
    }

    @Override
    public PyUnicode __unicode__() {
        if (sql instanceof String) {
            return Py.newUnicode((String) sql);
        } else if (sql instanceof Procedure) {
            try {
                return Py.newUnicode(((Procedure) sql).toSql());
            } catch (SQLException e) {
                throw zxJDBC.makeException(e);
            }
        }
        return super.__unicode__();
    }

    @Override
    public PyString __str__() {
        return Py.newString(__unicode__().encode(codecs.getDefaultEncoding(), "replace"));
    }

    @Override
    public String toString() {
        return String.format(" 0) {
            column = columns + 1;

            if (procedure != null && !procedure.isInput(column)) {
                continue;
            }

            // working from right to left
            PyObject param = params.__getitem__(--index);
            if (bindings != Py.None) {
                PyObject binding = bindings.__finditem__(Py.newInteger(index));

                if (binding != null) {
                    try {
                        int bindingValue = binding.asInt();
                        datahandler.setJDBCObject(preparedStatement, column, param, bindingValue);
                    } catch (PyException e) {
                        throw zxJDBC.makeException(zxJDBC.ProgrammingError,
                                                   zxJDBC.getString("bindingValue"));
                    }
                    continue;
                }
            }

            datahandler.setJDBCObject(preparedStatement, column, param);
        }
    }

    /**
     * Method close
     */
    public void close() {
        try {
            statement.close();
        } catch (SQLException e) {
            throw zxJDBC.makeException(e);
        } finally {
            closed = true;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy