use of lucee.runtime.type.query.QueryArray in project Lucee by lucee.
the class QueryImpl method fillResult.
private static boolean fillResult(QueryImpl qry, QueryResult qr, Collection.Key keyName, DatasourceConnection dc, ResultSet result, int maxrow, boolean closeResult, boolean createGeneratedKeys, TimeZone tz) throws SQLException, IOException, PageException {
if (result == null)
return false;
int recordcount = 0, columncount = 0;
Collection.Key[] columnNames = null;
QueryColumnImpl[] columns = null;
try {
ResultSetMetaData meta = result.getMetaData();
columncount = meta.getColumnCount();
// set header arrays
Collection.Key[] tmpColumnNames = new Collection.Key[columncount];
int count = 0;
Collection.Key key;
String columnName;
for (int i = 0; i < columncount; i++) {
columnName = QueryUtil.getColumnName(meta, i + 1);
if (StringUtil.isEmpty(columnName))
columnName = "column_" + i;
key = KeyImpl.init(columnName);
int index = getIndexFrom(tmpColumnNames, key, 0, i);
if (index == -1) {
tmpColumnNames[i] = key;
count++;
}
}
columncount = count;
columnNames = new Collection.Key[columncount];
if (qry != null)
columns = new QueryColumnImpl[columncount];
Cast[] casts = new Cast[columncount];
// get all used ints
int[] usedColumns = new int[columncount];
count = 0;
for (int i = 0; i < tmpColumnNames.length; i++) {
if (tmpColumnNames[i] != null) {
usedColumns[count++] = i;
}
}
// set used column names
int type;
for (int i = 0; i < usedColumns.length; i++) {
columnNames[i] = tmpColumnNames[usedColumns[i]];
type = meta.getColumnType(usedColumns[i] + 1);
if (qry != null)
columns[i] = new QueryColumnImpl(qry, columnNames[i], type);
casts[i] = QueryUtil.toCast(result, type);
}
if (createGeneratedKeys && columncount == 1 && columnNames[0].equals(GENERATED_KEYS) && dc != null && DataSourceUtil.isMSSQLDriver(dc)) {
columncount = 0;
columnNames = null;
if (qry != null) {
columns = null;
qry.setGeneratedKeys(dc, result, tz);
}
return false;
}
// fill QUERY
if (qry != null) {
while (result.next()) {
if (maxrow > -1 && recordcount >= maxrow) {
break;
}
for (int i = 0; i < usedColumns.length; i++) {
columns[i].add(casts[i].toCFType(tz, result, usedColumns[i] + 1));
}
++recordcount;
}
} else // fill QueryResult
{
Struct sct;
QueryArray qa = qr instanceof QueryArray ? (QueryArray) qr : null;
QueryStruct qs = qa == null ? (QueryStruct) qr : null;
Object k;
while (result.next()) {
if (maxrow > -1 && recordcount >= maxrow) {
break;
}
sct = new StructImpl();
for (int i = 0; i < usedColumns.length; i++) {
sct.set(columnNames[i], casts[i].toCFType(tz, result, usedColumns[i] + 1));
}
if (qa != null)
qa.appendEL(sct);
else {
k = sct.get(keyName, NULL);
if (k == NULL) {
Struct keys = new StructImpl();
for (Collection.Key tmp : columnNames) {
keys.set(tmp, "");
}
throw StructSupport.invalidKey(null, keys, keyName, "resultset");
}
if (k == null)
k = "";
qs.set(KeyImpl.toKey(k), sct);
}
++recordcount;
}
}
} finally {
if (qry != null) {
qry.columncount = columncount;
qry.recordcount = recordcount;
qry.columnNames = columnNames;
qry.columns = columns;
} else {
qr.setColumnNames(columnNames);
}
if (closeResult)
IOUtil.closeEL(result);
}
return true;
}
use of lucee.runtime.type.query.QueryArray in project Lucee by lucee.
the class QueryImpl method toArray.
public static QueryArray toArray(PageContext pc, DatasourceConnection dc, SQL sql, int maxrow, int fetchsize, TimeSpan timeout, String name, String template, boolean createUpdateData, boolean allowToCachePreperadeStatement) throws PageException {
QueryArray arr = new QueryArray(name, sql, template);
execute(pc, dc, sql, maxrow, fetchsize, timeout, createUpdateData, allowToCachePreperadeStatement, null, arr, null);
return arr;
}
Aggregations