2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package org
.libreoffice
.report
;
20 import com
.sun
.star
.beans
.UnknownPropertyException
;
21 import com
.sun
.star
.beans
.XPropertySet
;
22 import com
.sun
.star
.container
.XIndexAccess
;
23 import com
.sun
.star
.container
.XNameAccess
;
24 import com
.sun
.star
.lang
.IndexOutOfBoundsException
;
25 import com
.sun
.star
.lang
.WrappedTargetException
;
26 import com
.sun
.star
.sdb
.XParametersSupplier
;
27 import com
.sun
.star
.sdbc
.DataType
;
28 import com
.sun
.star
.sdbc
.SQLException
;
29 import com
.sun
.star
.sdbc
.XResultSetMetaData
;
30 import com
.sun
.star
.sdbc
.XResultSetMetaDataSupplier
;
31 import com
.sun
.star
.sdbc
.XRow
;
32 import com
.sun
.star
.sdbc
.XRowSet
;
33 import com
.sun
.star
.sdbcx
.XColumnsSupplier
;
34 import com
.sun
.star
.uno
.Any
;
35 import com
.sun
.star
.uno
.UnoRuntime
;
36 import com
.sun
.star
.util
.DateTime
;
37 import com
.sun
.star
.util
.Time
;
39 import java
.sql
.Timestamp
;
41 public class SDBCReportData
implements DataSource
44 private final XRowSet rowSet
;
45 private final XRow row
;
47 private XIndexAccess parameters
;
48 private int firstParameterIndex
= -1;
49 private int columnCount
;
50 private final String
[] columnNames
;
51 private final int[] columnTypes
;
53 public SDBCReportData(final XRowSet rowSet
) throws SQLException
55 row
= UnoRuntime
.queryInterface(XRow
.class, rowSet
);
62 columnTypes
= new int[1];
63 columnNames
= new String
[1];
67 final XParametersSupplier xSuppParams
= UnoRuntime
.queryInterface(
68 XParametersSupplier
.class, rowSet
);
69 if (xSuppParams
!= null)
71 parameters
= xSuppParams
.getParameters();
74 final XColumnsSupplier columnsSup
= UnoRuntime
.queryInterface(XColumnsSupplier
.class, rowSet
);
75 final XNameAccess columns
= columnsSup
.getColumns();
76 final String
[] columnNamesList
= columns
.getElementNames();
77 final XResultSetMetaDataSupplier sup
= UnoRuntime
.queryInterface(XResultSetMetaDataSupplier
.class, rowSet
);
78 final XResultSetMetaData resultSetMetaData
= sup
.getMetaData();
80 columnCount
= resultSetMetaData
.getColumnCount();
81 firstParameterIndex
= columnCount
+ 1;
82 if (parameters
!= null)
84 columnCount
+= parameters
.getCount();
87 columnTypes
= new int[columnCount
];
88 columnNames
= new String
[columnCount
];
90 for (int i
= 1; i
<= columnCount
; ++i
)
92 if (i
< firstParameterIndex
)
94 columnNames
[i
- 1] = columnNamesList
[i
- 1];// resultSetMetaData.getColumnName(i);
95 columnTypes
[i
- 1] = resultSetMetaData
.getColumnType(i
);
101 final XPropertySet paramColumn
= UnoRuntime
.queryInterface(
102 XPropertySet
.class, parameters
.getByIndex(i
- firstParameterIndex
));
103 columnNames
[i
- 1] = (String
) paramColumn
.getPropertyValue("Name");
104 columnTypes
[i
- 1] = (Integer
) paramColumn
.getPropertyValue("Type");
108 columnNames
[i
- 1] = "Error";
109 columnTypes
[i
- 1] = DataType
.CHAR
;
116 rowCount
= rowSet
.getRow();
117 rowSet
.beforeFirst();
126 public int getColumnCount() throws DataSourceException
131 public int getRowCount()
136 public String
getColumnName(final int column
) throws DataSourceException
138 return columnNames
[column
- 1];
141 public boolean absolute(final int row
) throws DataSourceException
151 rowSet
.beforeFirst();
154 return rowSet
.absolute(row
);
156 catch (SQLException e
)
158 throw new DataSourceException(e
.getMessage(), e
);
162 public boolean next() throws DataSourceException
170 return rowSet
.next();
172 catch (SQLException e
)
174 throw new DataSourceException(e
.getMessage(), e
);
178 public void close() throws DataSourceException
182 private static java
.sql
.Date
getDate(final Object obj
)
184 final java
.sql
.Date date
;
185 if (obj
instanceof com
.sun
.star
.util
.Date
)
187 final com
.sun
.star
.util
.Date unodate
= (com
.sun
.star
.util
.Date
) obj
;
188 date
= java
.sql
.Date
.valueOf(getDateString(unodate
.Year
, unodate
.Month
, unodate
.Day
).toString());
197 private static StringBuffer
getTimeString(final int hours
, final int minutes
, final int seconds
)
199 final StringBuffer timeString
= new StringBuffer();
202 timeString
.append('0');
204 timeString
.append(hours
);
205 timeString
.append(':');
208 timeString
.append('0');
210 timeString
.append(minutes
);
211 timeString
.append(':');
214 timeString
.append('0');
216 timeString
.append(seconds
);
220 private static StringBuffer
getDateString(final int years
, final int months
, final int days
)
222 final StringBuffer str
= new StringBuffer();
224 final StringBuffer str2
= new StringBuffer("0000");
225 str2
.delete(0, str
.length());
242 private static java
.sql
.Time
getTime(final Object obj
)
244 final java
.sql
.Time time
;
245 if (obj
instanceof Time
)
247 final Time unoTime
= (Time
) obj
;
248 time
= java
.sql
.Time
.valueOf(getTimeString(unoTime
.Hours
, unoTime
.Minutes
, unoTime
.Seconds
).toString());
257 private static Timestamp
getTimestamp(final Object obj
)
260 if (obj
instanceof DateTime
)
262 final DateTime unoTs
= (DateTime
) obj
;
263 final StringBuffer str
= getDateString(unoTs
.Year
, unoTs
.Month
, unoTs
.Day
);
265 str
.append(getTimeString(unoTs
.Hours
, unoTs
.Minutes
, unoTs
.Seconds
));
267 str
.append(String
.format("%09d", unoTs
.NanoSeconds
));
268 ts
= java
.sql
.Timestamp
.valueOf(str
.toString());
277 public Object
getObject(final int column
) throws DataSourceException
285 final boolean isParameterValue
= (parameters
!= null) && (column
>= firstParameterIndex
);
287 final boolean wasNull
;
288 if (isParameterValue
)
290 final XPropertySet paramCol
= UnoRuntime
.queryInterface(
291 XPropertySet
.class, parameters
.getByIndex(column
- firstParameterIndex
));
292 obj
= paramCol
.getPropertyValue("Value");
293 wasNull
= obj
== null;
297 obj
= row
.getObject(column
, null);
298 wasNull
= row
.wasNull();
307 obj
= convertObject(columnTypes
[column
- 1], obj
);
311 catch (SQLException ex
)
313 throw new DataSourceException(ex
.getMessage(), ex
);
315 catch (UnknownPropertyException ex
)
317 throw new DataSourceException(ex
.getMessage(), ex
);
319 catch (IndexOutOfBoundsException ex
)
321 throw new DataSourceException(ex
.getMessage(), ex
);
323 catch (WrappedTargetException ex
)
325 throw new DataSourceException(ex
.getMessage(), ex
);
329 private Object
convertObject(final int type
, final Object obj
)
340 case DataType
.TIMESTAMP
:
341 ret
= getTimestamp(obj
);
343 case DataType
.DECIMAL
:
344 case DataType
.NUMERIC
:
345 if (!(obj
instanceof Any
))
349 ret
= new java
.math
.BigDecimal(String
.valueOf(obj
));
351 catch (NumberFormatException ex
)