List MySQL enum in Java - java

Is there a way to get all possible values from a MySQL enum column?
The MySQL documentation says the MySQL enum type is returned as a Java String, so I basically would like a way to get all possible strings I can pass when querying a table with such an enum.
I couldn't immediately find anything when I was looking through the metadata returned for such a column, but since enum isn't standard SQL, I'm not sure it's even possible... any suggestions?

SHOW COLUMNS FROM Table LIKE field returns something like:
enum('value1','value2','value3','value4'). Parse out the enum values from the string with a regular expression ("/'(.*?)'/").

Related

Should I just disable UInteger type on JOOQ?

I've been trying to write some queries (for MySQL) and I'm not sure how to write this particular one that involves Integer and UInteger types:
DSL.position(T1.FIELD, ":", -1).eq(T2.UINTFIELD)
position returns a Field<Integer>, so I can't simply compare this result to my Field<UInteger>. Is there a simple way to achieve it? I'd like to keep the correct field types for further validation.
You can use DSL.cast() or DSL.coerce() for this. I recommend coerce.
DSL.cast() produces a SQL cast expression and a jOOQ field reference of the desired type
DSL.coerce() does not affect the generated SQL but still produces a jOOQ filed reference of the desired type.
For example:
position(T1.FIELD, ":", -1).eq(coerce(T2.UINTFIELD, SQLDataType.INTEGER))

ActiveJDBC: integer[] on PostgreSQL with multiple schemas in one DB

I am trying to use ActiveJDBC to properly convert an integer[] from my PostgreSQL database into the java equivalent int[]. I get the fetch done properly, but the object that is returned is weblogic.jdbc.wrapper.Array_org_postgresql_jdbc_PgArray. I have yet to find a way to convert that.
I have tried 2 different ways of accessing the data:
First, using the standard record.findFirst("id = ?", id) format. Because I have multiple schemas in my database, I added the #Table notation to my Model.
Second, I tried doing a record.findBySQL("select array from record where id = ?", id). I also tried array::integer[].
Each time I get back the PgArray type. I have searched for a way to convert this type into something else for use, but nothing has worked.
Is there a way to do this? Do I need to use a different way of data retrieval other than ActiveJDBC?
JDBC defines java.sql.Array to handle array typed columns. PgArray is just the Postgres implementation of java.sql.Array.
Given the array object a you can call (Integer[])a.getArray() to get an Integer array from the JDBC array object (if the JDBC driver has decided that it will return you Integer objects and not some other Numbers). A helper method to extract the value and convert to int[] would probably be a good idea.
Don't know if activejdbc has support to do this conversion behind the scenes.

What is fieldIndex about in JOOQ

I read JOOQ API documentation about following APIs, but still couldn't understand what is fieldIndex about.
fetchAny(int fieldIndex, java.lang.Class<? extends T> type)
For example, in the following code I already knows which column to select, why do we need filedIndex of 0? What does 0 mean?
String name = getDslContext().select(TESTB.STU_NAME)
.from(TESTB)
.where(TESTB.ID.eq(studentId))
.fetchAny(0, String.class);
jOOQ mostly operates on Record types. For instance, when you create the following query:
.select(TESTB.STU_NAME)
.from(TESTB)
.where(TESTB.ID.eq(studentId));
You're really operating on a ResultQuery<Record1<String>>. By calling fetchAny(0, String.class) on that type, you're telling jOOQ that you want to fetch any record, and from that record, you want to get only the value at index 0, converted to String.
This may feel like repeating the same information twice (using TESTB.STU_NAME, and fetching the column at index 0). This is because fetchign a single value is quite a special case in the jOOQ API. Unfortunately, the ResultQuery API doesn't really "know" that you're selecting only one column even if that type information is present via generics.
One alternative would be to use DSLContext.fetchValue(ResultQuery):
String name = getDslContext().fetchValue(
DSL.select(TESTB.STU_NAME)
.from(TESTB)
.where(TESTB.ID.eq(studentId))
);

How to find out the variable types in sql result set in java?

I wonder if there is way to know the types of results I retrieved from the database in java. Ultimately I want to be able to use appropriate get method for each column of the resultSet (or be able to convert getObject() to the proper type) without me knowing anything about the database/data explicitly.
You will need the ResultSetMetadata. Call getMetadata() on the ResultSest, then call getColumnType on the metadata object.
The returned value is from java.sql.Types.
The number, types and properties of a ResultSet object's columns are provided by the ResulSetMetaData object returned by the ResultSet.getMetaData method.
Use getMetaData(). It contains the type of each column, among other things.

Why execSQL accepts Object[] and raw Query String[]

I would like to have an option to call 'rawQuery' with a list of Integers passed into it, but it looks I can't: all 'rawQuery' methods require array of Strings...
Why? I can call 'toString' for each passed int object, but I don't see any logic here. Why Google don't provide rawQuery that accept list of Objects?
Are there any limitation or constrains for that?
Probably I miss something?
Thank you.
Probably because rawQuery isn't the prefered method of querying. And since sqlite columns basically don't have types, they probably don't want to assume calling toString on whatever it is you're passing.
SQLite supports the concept of "type affinity" on columns. The type
affinity of a column is the recommended type for data stored in that
column. The important idea here is that the type is recommended, not
required. Any column can still store any type of data. It is just that
some columns, given the choice, will prefer to use one storage class
over another. The preferred storage class for a column is called its
"affinity".
Is there a reason you want to use rawQuery instead of execSQL?
No complex objects are accepted in the list of parameters, as stated on the documentation for the parameter bindArgs:
Parameters
sql the SQL statement to be executed. Multiple statements separated by semicolons are not supported.
bindArgs only byte[], String, Long and Double are supported in bindArgs.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#execSQL(java.lang.String, java.lang.Object[]).

Categories

Resources