aaberg / sql2o

sql2o is a small library, which makes it easy to convert the result of your sql-statements into objects. No resultset hacking required. Kind of like an orm, but without the sql-generation capabilities. Supports named parameters.
http://sql2o.org
MIT License
1.15k stars 229 forks source link

Query.withParams upgrade #370

Open vertex-github opened 7 months ago

vertex-github commented 7 months ago

We also had this mod to Query.withParams to allow Lists and arrays to be passed in:

    /**
     * Construct a Query with the provided variable number of parameters.  Accepts all object types, List, and arrays as parameters.
     * @param paramValues
     * @return
     */
    public Query withParams(Object... paramValues){
        int i=0;
        for (Object paramValue : paramValues) {
            if( paramValue == null ) { // null values are valid for inserting in to a DB . We need to check for these first.
                addParameter( "p" + (++i), paramValue );
            }
            else if( paramValue instanceof Collection ) {
                if( paramValue instanceof List ) {
                    List list = (List) paramValue;
                    for( Object val : list ) {
                        addParameter( "p" + (++i), val );
                    }
                }
                else {
                    throw new Sql2oException( "I can only handle ordered Collections - i.e. a List" );
                }
            }
            else if( paramValue.getClass().isArray() ) {
                int len = java.lang.reflect.Array.getLength( paramValue );
                for( int j = 0; j < len; j++ )
                {
                    Object arrVal = java.lang.reflect.Array.get( paramValue, j );
                    addParameter( "p" + (++i), arrVal );
                }
            }
            else {
                addParameter( "p" + (++i), paramValue );
            }
        }
        return this;
    }
vertex-github commented 7 months ago

With the new SequencedCollections this could be improved a little

aaberg commented 7 months ago

Nice. Thanks. I'll take a look 👍🏼