Skip to content

Commit

Permalink
solves LDEV-1980 and LDEV-1026
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Sep 21, 2018
1 parent 14acd68 commit 8ea79e1
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 187 deletions.
65 changes: 39 additions & 26 deletions core/src/main/java/lucee/runtime/tag/DBInfo.java
Expand Up @@ -102,14 +102,9 @@ public final class DBInfo extends TagImpl {
private static final int TYPE_TERMS = 10;
private static final Collection.Key CARDINALITY = KeyImpl.init("CARDINALITY");


//private static final String[] ALL_TABLE_TYPES = {"TABLE", "VIEW", "SYSTEM TABLE", "SYNONYM"};

private String datasource;
private DataSource datasource;
private String name;
private int type;


private String dbname;
private String password;
private String pattern;
Expand All @@ -118,7 +113,6 @@ public final class DBInfo extends TagImpl {
private String username;
private String strType;


@Override
public void release() {
super.release();
Expand Down Expand Up @@ -146,8 +140,11 @@ public void setProcedure(String procedure) {
/**
* @param datasource the datasource to set
*/
public void setDatasource(String datasource) {
this.datasource = datasource;
public void setDatasource(String datasource) throws PageException { // exist for old bytecode in archives
this.datasource = lucee.runtime.tag.Query.toDatasource(pageContext, datasource);
}
public void setDatasource(Object datasource) throws PageException {
this.datasource = lucee.runtime.tag.Query.toDatasource(pageContext, datasource);
}

/**
Expand Down Expand Up @@ -252,12 +249,14 @@ public void setUsername(String username) {
@Override
public int doStartTag() throws PageException {
Object ds=getDatasource(pageContext, datasource);

DataSourceManager manager = pageContext.getDataSourceManager();
DatasourceConnection dc=ds instanceof DataSource?
manager.getConnection(pageContext,(DataSource)ds,username,password):
manager.getConnection(pageContext,Caster.toString(ds),username,password);
try {



try {
if(type==TYPE_TABLE_COLUMNS) typeColumns(dc.getConnection().getMetaData());
else if(type==TYPE_DBNAMES) typeDBNames(dc.getConnection().getMetaData());
else if(type==TYPE_FOREIGNKEYS) typeForeignKeys(dc.getConnection().getMetaData());
Expand Down Expand Up @@ -290,25 +289,26 @@ private void typeColumns(DatabaseMetaData metaData) throws PageException, SQLExc

Stopwatch stopwatch=new Stopwatch(Stopwatch.UNIT_NANO);
stopwatch.start();

table=setCase(metaData, table);
pattern=setCase(metaData, pattern);
if(StringUtil.isEmpty(pattern,true)) pattern=null;


String schema=null;
int index=table.indexOf('.');
if(index>0) {
schema=table.substring(0,index);
table=table.substring(index+1);
}
checkTable(metaData);

checkTable(metaData);
Query qry = new QueryImpl(
metaData.getColumns(dbname, schema, table, pattern),
"query",
pageContext.getTimeZone());

int len=qry.getRecordcount();

if(qry.getColumn(COLUMN_DEF,null) != null)
qry.rename(COLUMN_DEF,COLUMN_DEFAULT_VALUE);
else if(qry.getColumn(COLUMN_DEFAULT,null) != null)
Expand All @@ -326,23 +326,31 @@ else if(qry.getColumn(COLUMN_DEFAULT,null) != null)


// add is primary
Map primaries = new HashMap();
String tblName;
Map<String,Set<String>> primaries = new HashMap<>();
Array isPrimary=new ArrayImpl();
Set set;
Set<String> set;
Object o;
String tblCat,tblScheme,tblName;
for(int i=1;i<=len;i++) {

// decimal digits
o=qry.getAt(DECIMAL_DIGITS, i,null);
if(o==null)qry.setAtEL(DECIMAL_DIGITS, i,lucee.runtime.op.Constants.DOUBLE_ZERO);

set=(Set) primaries.get(tblName=(String) qry.getAt(TABLE_NAME, i));
tblCat = StringUtil.emptyAsNull(Caster.toString(qry.getAt(TABLE_CAT, i),null),true);
tblScheme = StringUtil.emptyAsNull(Caster.toString(qry.getAt(TABLE_SCHEM, i),null),true);
tblName = StringUtil.emptyAsNull(Caster.toString(qry.getAt(TABLE_NAME, i),null),true);


set=primaries.get(tblName);
if(set==null) {
set=toSet(metaData.getPrimaryKeys(dbname, null, tblName),true,"COLUMN_NAME");
primaries.put(tblName,set);
try {
set=toSet(metaData.getPrimaryKeys(tblCat, tblScheme, tblName),true,"COLUMN_NAME");
primaries.put(tblName,set);
}
catch(Exception e) {}
}
isPrimary.append(set.contains(qry.getAt(COLUMN_NAME, i))?"YES":"NO");
isPrimary.append(set!=null && set.contains(qry.getAt(COLUMN_NAME, i))?"YES":"NO");
}
qry.addColumn(IS_PRIMARYKEY, isPrimary);

Expand All @@ -355,10 +363,16 @@ else if(qry.getColumn(COLUMN_DEFAULT,null) != null)
Map<String, Map<String, SVArray>> map;
Map<String, SVArray> inner;
for(int i=1;i<=len;i++) {
map=(Map) foreigns.get(tblName=(String) qry.getAt(TABLE_NAME, i));

tblCat = StringUtil.emptyAsNull(Caster.toString(qry.getAt(TABLE_CAT, i),null),true);
tblScheme = StringUtil.emptyAsNull(Caster.toString(qry.getAt(TABLE_SCHEM, i),null),true);
tblName = StringUtil.emptyAsNull(Caster.toString(qry.getAt(TABLE_NAME, i),null),true);


map=(Map) foreigns.get(tblName);
if(map==null) {
map=toMap(
metaData.getImportedKeys(dbname, schema, table),
metaData.getImportedKeys(tblCat, tblScheme, tblName),
true,
"FKCOLUMN_NAME",
new String[]{"PKCOLUMN_NAME","PKTABLE_NAME"});
Expand Down Expand Up @@ -508,7 +522,6 @@ private void typeForeignKeys(DatabaseMetaData metaData) throws PageException, SQ

private void checkTable(DatabaseMetaData metaData) throws SQLException, ApplicationException {
ResultSet tables =null;

try {
tables = metaData.getTables(null, null, setCase(metaData,table), null);
if(!tables.next()) throw new ApplicationException("there is no table that match the following pattern ["+table+"]");
Expand Down Expand Up @@ -714,8 +727,8 @@ public int doEndTag() {
}


public static Object getDatasource(PageContext pageContext, String datasource) throws ApplicationException {
if(StringUtil.isEmpty(datasource)){
public static Object getDatasource(PageContext pageContext, DataSource datasource) throws ApplicationException {
if(datasource==null){
Object ds=pageContext.getApplicationContext().getDefDataSource();

if(StringUtil.isEmpty(ds)) {
Expand Down
9 changes: 6 additions & 3 deletions core/src/main/java/lucee/runtime/tag/Insert.java
Expand Up @@ -60,7 +60,7 @@ public final class Insert extends TagImpl {
private String password;

/** Name of the data source that contains your table. */
private String datasource;
private DataSource datasource;

/** If specified, username overrides the username value specified in the ODBC setup. */
private String username;
Expand Down Expand Up @@ -105,8 +105,11 @@ public void setPassword(String password) {
* Name of the data source that contains your table.
* @param datasource value to set
**/
public void setDatasource(String datasource) {
this.datasource=datasource;
public void setDatasource(String datasource) throws PageException { // exist for old bytecode in archives
this.datasource = lucee.runtime.tag.Query.toDatasource(pageContext, datasource);
}
public void setDatasource(Object datasource) throws PageException {
this.datasource = lucee.runtime.tag.Query.toDatasource(pageContext, datasource);
}

/** set the value username
Expand Down
4 changes: 1 addition & 3 deletions core/src/main/java/lucee/runtime/tag/Query.java
Expand Up @@ -26,7 +26,6 @@
import java.util.Map.Entry;
import java.util.TimeZone;

import lucee.print;
import lucee.commons.io.SystemUtil;
import lucee.commons.io.SystemUtil.TemplateLine;
import lucee.commons.io.log.Log;
Expand Down Expand Up @@ -242,7 +241,7 @@ public void setDatasource(Object datasource) throws PageException, ClassExceptio
data.datasource = toDatasource(pageContext,datasource);
}

private static DataSource toDatasource(PageContext pageContext, Object datasource) throws PageException {
public static DataSource toDatasource(PageContext pageContext, Object datasource) throws PageException {
if(Decision.isStruct(datasource)) {
return AppListenerUtil.toDataSource(pageContext.getConfig(), "__temp__", Caster.toStruct(datasource),
pageContext.getConfig().getLog("application"));
Expand Down Expand Up @@ -497,7 +496,6 @@ public int doStartTag() throws PageException {
}
data.datasource = obj instanceof DataSource ? (DataSource)obj : pageContext.getDataSource(Caster.toString(obj));
}

// timeout
if(data.datasource instanceof DataSourceImpl && ((DataSourceImpl)data.datasource).getAlwaysSetTimeout()) {
TimeSpan remaining = PageContextUtil.remainingTime(pageContext, true);
Expand Down
9 changes: 6 additions & 3 deletions core/src/main/java/lucee/runtime/tag/Update.java
Expand Up @@ -61,7 +61,7 @@ public final class Update extends TagImpl {
private String password;

/** Name of the data source that contains a table. */
private String datasource;
private DataSource datasource;

/** If specified, username overrides the username value specified in the ODBC setup. */
private String username;
Expand Down Expand Up @@ -106,8 +106,11 @@ public void setPassword(String password) {
* Name of the data source that contains a table.
* @param datasource value to set
**/
public void setDatasource(String datasource) {
this.datasource=datasource;
public void setDatasource(String datasource) throws PageException { // exist for old bytecode in archives
this.datasource = lucee.runtime.tag.Query.toDatasource(pageContext, datasource);
}
public void setDatasource(Object datasource) throws PageException {
this.datasource = lucee.runtime.tag.Query.toDatasource(pageContext, datasource);
}

/** set the value username
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/lucee/runtime/type/QueryImpl.java
Expand Up @@ -1427,7 +1427,7 @@ else if(o instanceof Number)
else if(o instanceof Clob)
sb.append(getToStringField(Caster.toString(o)));
else
sb.append(getToStringField(o.toString()));
sb.append(getToStringField(o==null?"[null]":o.toString()));
}
catch (PageException e) {
sb.append(getToStringField("[empty]"));
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/resource/tld/core-base.tld
Expand Up @@ -956,7 +956,7 @@ A return value from the CreateTimeSpan function, for example, "#CreateTimeSpan(0
<description>Lets you retrieve information about a data source, including details about the database, tables, queries, procedures, foreign keys, indexes, and version information about the database, driver, and JDBC. This tag supports only JDBC drivers, and does not support ODBC-based drivers, including the Microsoft Access driver.</description>
<attribute-type>fixed</attribute-type>
<attribute>
<type>string</type>
<type>object</type>
<name>datasource</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
Expand Down Expand Up @@ -4276,7 +4276,7 @@ Permits searching collections by title or displaying a separate title from the k
<description>Inserts records in data sources.</description>
<attribute-type>fixed</attribute-type>
<attribute>
<type>string</type>
<type>object</type>
<name>dataSource</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
Expand Down Expand Up @@ -7909,7 +7909,7 @@ If you terminate a thread, the thread scope includes an ERROR metadata structure
<description>Updates existing records in data sources.</description>
<attribute-type>fixed</attribute-type>
<attribute>
<type>string</type>
<type>object</type>
<name>dataSource</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
Expand Down
2 changes: 1 addition & 1 deletion loader/build.xml
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project default="core" basedir="." name="Lucee" xmlns:artifact="antlib:org.apache.maven.artifact.ant">

<property name="version" value="5.3.1.71-SNAPSHOT"/>
<property name="version" value="5.3.1.72-SNAPSHOT"/>

<path id="maven-ant-tasks.classpath" path="../ant/lib/maven-ant-tasks-2.1.3.jar" />
<typedef resource="org/apache/maven/artifact/ant/antlib.xml"
Expand Down
2 changes: 1 addition & 1 deletion loader/pom.xml
Expand Up @@ -3,7 +3,7 @@

<groupId>org.lucee</groupId>
<artifactId>lucee</artifactId>
<version>5.3.1.71-SNAPSHOT</version>
<version>5.3.1.72-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down

0 comments on commit 8ea79e1

Please sign in to comment.