forked from fanruan/demo-tabledata-redis
abel
6 years ago
1 changed files with 154 additions and 153 deletions
@ -1,154 +1,155 @@ |
|||||||
package com.fr.plugin.db.redis.core; |
package com.fr.plugin.db.redis.core; |
||||||
|
|
||||||
import com.fr.base.Parameter; |
import com.fr.base.Parameter; |
||||||
import com.fr.base.TableData; |
import com.fr.base.TableData; |
||||||
import com.fr.base.TemplateUtils; |
import com.fr.base.TemplateUtils; |
||||||
import com.fr.config.holder.Conf; |
import com.fr.config.holder.Conf; |
||||||
import com.fr.config.holder.factory.Holders; |
import com.fr.config.holder.factory.Holders; |
||||||
import com.fr.config.holder.factory.XmlHolders; |
import com.fr.config.holder.factory.XmlHolders; |
||||||
import com.fr.data.AbstractParameterTableData; |
import com.fr.data.AbstractParameterTableData; |
||||||
import com.fr.data.core.DataCoreXmlUtils; |
import com.fr.data.core.DataCoreXmlUtils; |
||||||
import com.fr.data.impl.Connection; |
import com.fr.data.impl.Connection; |
||||||
import com.fr.data.impl.NameDatabaseConnection; |
import com.fr.data.impl.NameDatabaseConnection; |
||||||
import com.fr.file.DatasourceManager; |
import com.fr.file.DatasourceManager; |
||||||
import com.fr.general.data.DataModel; |
import com.fr.general.data.DataModel; |
||||||
import com.fr.general.xml.GeneralXMLTools; |
import com.fr.general.xml.GeneralXMLTools; |
||||||
import com.fr.intelli.record.Focus; |
import com.fr.intelli.record.Focus; |
||||||
import com.fr.intelli.record.Original; |
import com.fr.intelli.record.Original; |
||||||
import com.fr.plugin.db.redis.core.order.OrderValue; |
import com.fr.plugin.db.redis.core.order.OrderValue; |
||||||
import com.fr.plugin.db.redis.core.order.impl.NumberOrderValue; |
import com.fr.plugin.db.redis.core.order.impl.NumberOrderValue; |
||||||
import com.fr.record.analyzer.EnableMetrics; |
import com.fr.record.analyzer.EnableMetrics; |
||||||
import com.fr.script.Calculator; |
import com.fr.script.Calculator; |
||||||
import com.fr.stable.ArrayUtils; |
import com.fr.stable.ArrayUtils; |
||||||
import com.fr.stable.ParameterProvider; |
import com.fr.stable.ParameterProvider; |
||||||
import com.fr.stable.StringUtils; |
import com.fr.stable.StringUtils; |
||||||
import com.fr.stable.xml.XMLPrintWriter; |
import com.fr.stable.xml.XMLPrintWriter; |
||||||
import com.fr.stable.xml.XMLableReader; |
import com.fr.stable.xml.XMLableReader; |
||||||
|
|
||||||
import java.util.HashMap; |
import java.util.HashMap; |
||||||
import java.util.Map; |
import java.util.Map; |
||||||
|
|
||||||
|
|
||||||
@EnableMetrics |
@EnableMetrics |
||||||
public class RedisTableData extends AbstractParameterTableData { |
public class RedisTableData extends AbstractParameterTableData { |
||||||
|
|
||||||
private Conf<Connection> database = Holders.obj(null, Connection.class); |
private static final long serialVersionUID = 7017455818551800001L; |
||||||
private Conf<OrderValue> dbIndex = XmlHolders.obj(new NumberOrderValue(0), OrderValue.class, OrderValue.XML_TAG); |
private Conf<Connection> database = Holders.obj(null, Connection.class); |
||||||
private Conf<String> query = Holders.simple(StringUtils.EMPTY); |
private Conf<OrderValue> dbIndex = XmlHolders.obj(new NumberOrderValue(0), OrderValue.class, OrderValue.XML_TAG); |
||||||
|
private Conf<String> query = Holders.simple(StringUtils.EMPTY); |
||||||
public void setDatabase(Connection c) { |
|
||||||
this.database.set(c); |
public void setDatabase(Connection c) { |
||||||
} |
this.database.set(c); |
||||||
|
} |
||||||
public Connection getDatabase() { |
|
||||||
return database.get(); |
public Connection getDatabase() { |
||||||
} |
return database.get(); |
||||||
|
} |
||||||
public OrderValue getOrderValue() { |
|
||||||
return dbIndex.get(); |
public OrderValue getOrderValue() { |
||||||
} |
return dbIndex.get(); |
||||||
|
} |
||||||
public void setOrderValue(OrderValue dbIndex) { |
|
||||||
this.dbIndex.set(dbIndex); |
public void setOrderValue(OrderValue dbIndex) { |
||||||
} |
this.dbIndex.set(dbIndex); |
||||||
|
} |
||||||
public String getQuery() { |
|
||||||
return query.get(); |
public String getQuery() { |
||||||
} |
return query.get(); |
||||||
|
} |
||||||
public void setQuery(String query) { |
|
||||||
this.query.set(query); |
public void setQuery(String query) { |
||||||
} |
this.query.set(query); |
||||||
|
} |
||||||
public void setParameters(ParameterProvider[] providers) { |
|
||||||
super.setDefaultParameters(providers); |
public void setParameters(ParameterProvider[] providers) { |
||||||
} |
super.setDefaultParameters(providers); |
||||||
|
} |
||||||
@Override |
|
||||||
public DataModel createDataModel(Calculator calculator) { |
@Override |
||||||
return createDataModel(calculator, TableData.RESULT_ALL); |
public DataModel createDataModel(Calculator calculator) { |
||||||
} |
return createDataModel(calculator, TableData.RESULT_ALL); |
||||||
|
} |
||||||
@Override |
|
||||||
@Focus(id = RedisConstants.PLUGIN_ID, text = "Plugin-Redis_DB", source = Original.PLUGIN) |
@Override |
||||||
public DataModel createDataModel(Calculator calculator, int rowCount) { |
@Focus(id = RedisConstants.PLUGIN_ID, text = "Plugin-Redis_DB", source = Original.PLUGIN) |
||||||
Parameter[] ps = Parameter.providers2Parameter(getParameters(calculator)); |
public DataModel createDataModel(Calculator calculator, int rowCount) { |
||||||
Connection connection = database.get(); |
Parameter[] ps = Parameter.providers2Parameter(getParameters(calculator)); |
||||||
if (connection instanceof NameDatabaseConnection) { |
Connection connection = database.get(); |
||||||
String name = ((NameDatabaseConnection) connection).getName(); |
if (connection instanceof NameDatabaseConnection) { |
||||||
RedisDatabaseConnection rc = DatasourceManager.getProviderInstance().getConnection(name, RedisDatabaseConnection.class); |
String name = ((NameDatabaseConnection) connection).getName(); |
||||||
if (rc != null) { |
RedisDatabaseConnection rc = DatasourceManager.getProviderInstance().getConnection(name, RedisDatabaseConnection.class); |
||||||
OrderValue orderValue = dbIndex.get(); |
if (rc != null) { |
||||||
return new RedisTableDataModel(calculator, ps, rc, |
OrderValue orderValue = dbIndex.get(); |
||||||
orderValue == null ? 0 : orderValue.toIndex(calculator, ps), |
return new RedisTableDataModel(calculator, ps, rc, |
||||||
calculateQuery(query.get(), ps), |
orderValue == null ? 0 : orderValue.toIndex(calculator, ps), |
||||||
rowCount); |
calculateQuery(query.get(), ps), |
||||||
} |
rowCount); |
||||||
} |
} |
||||||
return null; |
} |
||||||
} |
return null; |
||||||
|
} |
||||||
private String calculateQuery(String query, Parameter[] ps) { |
|
||||||
if (ArrayUtils.isEmpty(ps)) { |
private String calculateQuery(String query, Parameter[] ps) { |
||||||
return query; |
if (ArrayUtils.isEmpty(ps)) { |
||||||
} |
return query; |
||||||
Map<String, Object> map = new HashMap<String, Object>(); |
} |
||||||
for (Parameter p : ps) { |
Map<String, Object> map = new HashMap<String, Object>(); |
||||||
map.put(p.getName(), p.getValue()); |
for (Parameter p : ps) { |
||||||
} |
map.put(p.getName(), p.getValue()); |
||||||
try { |
} |
||||||
return TemplateUtils.renderParameter4Tpl(query, map); |
try { |
||||||
} catch (Exception e) { |
return TemplateUtils.renderParameter4Tpl(query, map); |
||||||
return query; |
} catch (Exception e) { |
||||||
} |
return query; |
||||||
} |
} |
||||||
|
} |
||||||
public void readXML(XMLableReader reader) { |
|
||||||
super.readXML(reader); |
public void readXML(XMLableReader reader) { |
||||||
|
super.readXML(reader); |
||||||
if (reader.isChildNode()) { |
|
||||||
String tmpName = reader.getTagName(); |
if (reader.isChildNode()) { |
||||||
String tmpVal; |
String tmpName = reader.getTagName(); |
||||||
|
String tmpVal; |
||||||
if (OrderValue.XML_TAG.equals(tmpName)) { |
|
||||||
OrderValue orderValue = (OrderValue) GeneralXMLTools.readXMLable(reader); |
if (OrderValue.XML_TAG.equals(tmpName)) { |
||||||
if (orderValue != null) { |
OrderValue orderValue = (OrderValue) GeneralXMLTools.readXMLable(reader); |
||||||
setOrderValue(orderValue); |
if (orderValue != null) { |
||||||
} |
setOrderValue(orderValue); |
||||||
} else if (com.fr.data.impl.Connection.XML_TAG.equals(tmpName)) { |
} |
||||||
if (reader.getAttrAsString("class", null) != null) { |
} else if (com.fr.data.impl.Connection.XML_TAG.equals(tmpName)) { |
||||||
com.fr.data.impl.Connection con = DataCoreXmlUtils.readXMLConnection(reader); |
if (reader.getAttrAsString("class", null) != null) { |
||||||
this.setDatabase(con); |
com.fr.data.impl.Connection con = DataCoreXmlUtils.readXMLConnection(reader); |
||||||
} |
this.setDatabase(con); |
||||||
} else if ("Query".equals(tmpName)) { |
} |
||||||
tmpVal = reader.getElementValue(); |
} else if ("Query".equals(tmpName)) { |
||||||
if (isNotNullValue(tmpVal)) { |
tmpVal = reader.getElementValue(); |
||||||
this.setQuery(tmpVal); |
if (isNotNullValue(tmpVal)) { |
||||||
} |
this.setQuery(tmpVal); |
||||||
} |
} |
||||||
} |
} |
||||||
} |
} |
||||||
|
} |
||||||
@Override |
|
||||||
public void writeXML(XMLPrintWriter writer) { |
@Override |
||||||
super.writeXML(writer); |
public void writeXML(XMLPrintWriter writer) { |
||||||
GeneralXMLTools.writeXMLable(writer, dbIndex.get(), OrderValue.XML_TAG); |
super.writeXML(writer); |
||||||
if (this.database.get() != null) { |
GeneralXMLTools.writeXMLable(writer, dbIndex.get(), OrderValue.XML_TAG); |
||||||
DataCoreXmlUtils.writeXMLConnection(writer, this.database.get()); |
if (this.database.get() != null) { |
||||||
} |
DataCoreXmlUtils.writeXMLConnection(writer, this.database.get()); |
||||||
writer.startTAG("Query").textNode(getQuery()).end(); |
} |
||||||
} |
writer.startTAG("Query").textNode(getQuery()).end(); |
||||||
|
} |
||||||
private boolean isNotNullValue(String value) { |
|
||||||
return value != null && !"null".equals(value); |
private boolean isNotNullValue(String value) { |
||||||
} |
return value != null && !"null".equals(value); |
||||||
|
} |
||||||
@Override |
|
||||||
public Object clone() throws CloneNotSupportedException { |
@Override |
||||||
RedisTableData cloned = (RedisTableData) super.clone(); |
public Object clone() throws CloneNotSupportedException { |
||||||
cloned.database = (Conf<Connection>) database.clone(); |
RedisTableData cloned = (RedisTableData) super.clone(); |
||||||
cloned.query = (Conf<String>) query.clone(); |
cloned.database = (Conf<Connection>) database.clone(); |
||||||
cloned.dbIndex = (Conf<OrderValue>) dbIndex.clone(); |
cloned.query = (Conf<String>) query.clone(); |
||||||
return cloned; |
cloned.dbIndex = (Conf<OrderValue>) dbIndex.clone(); |
||||||
} |
return cloned; |
||||||
|
} |
||||||
} |
} |
Loading…
Reference in new issue