1313import java .util .List ;
1414import java .util .Locale ;
1515import java .util .Map ;
16- import java .util .regex .Matcher ;
17- import java .util .regex .Pattern ;
1816
1917public class LibsqlResultSetMetaData implements ResultSetMetaData {
2018
21- private static final Pattern COLUMN_TYPENAME = Pattern .compile ("([^\\ (]*)" );
22- private static final Pattern COLUMN_TYPECAST =
23- Pattern .compile ("cast\\ (.*?\\ s+as\\ s+(.*?)\\ s*\\ )" );
24- private static final Pattern COLUMN_PRECISION = Pattern .compile (".*?\\ ((.*?)\\ )" );
25-
2619 private final List <Col > cols ;
2720 private final List <Row > rows ;
2821 private final String sql ;
@@ -104,8 +97,9 @@ public int isNullable(int column) throws SQLException {
10497
10598 @ Override
10699 public boolean isSigned (int column ) throws SQLException {
107- if (declType (column ) != null ) {
108- return switch (getColumnTypeName (column )) {
100+ DeclaredType declared = DeclaredType .of (getCol (column ));
101+ if (declared .isDeclared ()) {
102+ return switch (declared .typeName ()) {
109103 case "NUMERIC" , "INTEGER" , "REAL" -> true ;
110104 default -> false ;
111105 };
@@ -143,28 +137,12 @@ public String getSchemaName(int column) throws SQLException {
143137
144138 @ Override
145139 public int getPrecision (int column ) throws SQLException {
146- String declType = declType (column );
147- if (declType == null ) {
148- return 0 ;
149- }
150- Matcher matcher = COLUMN_PRECISION .matcher (declType );
151- return matcher .find () ? Integer .parseInt (matcher .group (1 ).split ("," )[0 ].trim ()) : 0 ;
140+ return DeclaredType .of (getCol (column )).precision ();
152141 }
153142
154143 @ Override
155144 public int getScale (int column ) throws SQLException {
156- String declType = declType (column );
157- if (declType == null ) {
158- return 0 ;
159- }
160- Matcher matcher = COLUMN_PRECISION .matcher (declType );
161- if (matcher .find ()) {
162- String [] parts = matcher .group (1 ).split ("," );
163- if (parts .length == 2 ) {
164- return Integer .parseInt (parts [1 ].trim ());
165- }
166- }
167- return 0 ;
145+ return DeclaredType .of (getCol (column )).scale ();
168146 }
169147
170148 @ Override
@@ -189,14 +167,7 @@ public int getColumnType(int column) throws SQLException {
189167
190168 @ Override
191169 public String getColumnTypeName (int column ) throws SQLException {
192- getCol (column );
193- String declType = declType (column );
194- if (declType != null ) {
195- Matcher matcher = COLUMN_TYPENAME .matcher (declType );
196- matcher .find ();
197- return matcher .group (1 ).toUpperCase (Locale .ENGLISH );
198- }
199- return "NUMERIC" ;
170+ return DeclaredType .of (getCol (column )).typeName ();
200171 }
201172
202173 @ Override
@@ -223,19 +194,6 @@ public String getColumnClassName(int column) throws SQLException {
223194 return XerialColumnType .className (runtimeValue (column ));
224195 }
225196
226- private String declType (int column ) throws SQLException {
227- Col col = getCol (column );
228- String declType = col .decltype ();
229- if (declType == null ) {
230- String name = col .name ();
231- if (name != null ) {
232- Matcher matcher = COLUMN_TYPECAST .matcher (name );
233- declType = matcher .find () ? matcher .group (1 ) : null ;
234- }
235- }
236- return declType ;
237- }
238-
239197 private String columnName (int column ) throws SQLException {
240198 String name = getCol (column ).name ();
241199 return name != null ? name .toLowerCase (Locale .ROOT ) : "" ;
0 commit comments