Search in sources :

Example 1 with DataSourceImpl

use of lucee.runtime.db.DataSourceImpl in project Lucee by lucee.

the class Query method doStartTag.

@Override
public int doStartTag() throws PageException {
    // default datasource
    if (datasource == null && (dbtype == null || !dbtype.equals("query"))) {
        Object obj = pageContext.getApplicationContext().getDefDataSource();
        if (StringUtil.isEmpty(obj)) {
            boolean isCFML = pageContext.getRequestDialect() == CFMLEngine.DIALECT_CFML;
            throw new ApplicationException("attribute [datasource] is required when attribute [dbtype] is not [query] and no default datasource is defined", "you can define a default datasource as attribute [defaultdatasource] of the tag " + (isCFML ? Constants.CFML_APPLICATION_TAG_NAME : Constants.LUCEE_APPLICATION_TAG_NAME) + " or as data member of the " + (isCFML ? Constants.CFML_APPLICATION_EVENT_HANDLER : Constants.LUCEE_APPLICATION_EVENT_HANDLER) + " (this.defaultdatasource=\"mydatasource\";)");
        }
        datasource = obj instanceof DataSource ? (DataSource) obj : pageContext.getDataSource(Caster.toString(obj));
    }
    // timeout
    if (datasource instanceof DataSourceImpl && ((DataSourceImpl) datasource).getAlwaysSetTimeout()) {
        TimeSpan remaining = PageContextUtil.remainingTime(pageContext, true);
        if (this.timeout == null || ((int) this.timeout.getSeconds()) <= 0 || timeout.getSeconds() > remaining.getSeconds()) {
            // not set
            this.timeout = remaining;
        }
    }
    // timezone
    if (timezone != null || (datasource != null && (timezone = datasource.getTimeZone()) != null)) {
        tmpTZ = pageContext.getTimeZone();
        pageContext.setTimeZone(timezone);
    }
    PageContextImpl pci = ((PageContextImpl) pageContext);
    // cache within
    if (StringUtil.isEmpty(cachedWithin)) {
        Object tmp = (pageContext).getCachedWithin(ConfigWeb.CACHEDWITHIN_QUERY);
        if (tmp != null)
            setCachedwithin(tmp);
    }
    // literal timestamp with TSOffset
    if (datasource instanceof DataSourceImpl)
        literalTimestampWithTSOffset = ((DataSourceImpl) datasource).getLiteralTimestampWithTSOffset();
    else
        literalTimestampWithTSOffset = false;
    previousLiteralTimestampWithTSOffset = pci.getTimestampWithTSOffset();
    pci.setTimestampWithTSOffset(literalTimestampWithTSOffset);
    return EVAL_BODY_BUFFERED;
}
Also used : TimeSpan(lucee.runtime.type.dt.TimeSpan) ApplicationException(lucee.runtime.exp.ApplicationException) DataSourceImpl(lucee.runtime.db.DataSourceImpl) PageContextImpl(lucee.runtime.PageContextImpl) DataSource(lucee.runtime.db.DataSource)

Example 2 with DataSourceImpl

use of lucee.runtime.db.DataSourceImpl in project Lucee by lucee.

the class Admin method doGetDatasource.

/**
 * @throws PageException
 */
private void doGetDatasource() throws PageException {
    String name = getString("admin", action, "name");
    Map ds = config.getDataSourcesAsMap();
    Iterator it = ds.keySet().iterator();
    while (it.hasNext()) {
        String key = (String) it.next();
        if (key.equalsIgnoreCase(name)) {
            DataSource d = (DataSource) ds.get(key);
            Struct sct = new StructImpl();
            ClassDefinition cd = d.getClassDefinition();
            sct.setEL(KeyConstants._name, key);
            sct.setEL(KeyConstants._host, d.getHost());
            sct.setEL("classname", cd.getClassName());
            sct.setEL("class", cd.getClassName());
            sct.setEL("bundleName", cd.getName());
            sct.setEL("bundleVersion", cd.getVersionAsString());
            sct.setEL("dsn", d.getDsnOriginal());
            sct.setEL("database", d.getDatabase());
            sct.setEL("port", d.getPort() < 1 ? "" : Caster.toString(d.getPort()));
            sct.setEL("dsnTranslated", d.getDsnTranslated());
            sct.setEL("timezone", toStringTimeZone(d.getTimeZone()));
            sct.setEL("password", d.getPassword());
            sct.setEL("passwordEncrypted", ConfigWebUtil.encrypt(d.getPassword()));
            sct.setEL("username", d.getUsername());
            sct.setEL("readonly", Caster.toBoolean(d.isReadOnly()));
            sct.setEL("select", Boolean.valueOf(d.hasAllow(DataSource.ALLOW_SELECT)));
            sct.setEL("delete", Boolean.valueOf(d.hasAllow(DataSource.ALLOW_DELETE)));
            sct.setEL("update", Boolean.valueOf(d.hasAllow(DataSource.ALLOW_UPDATE)));
            sct.setEL("insert", Boolean.valueOf(d.hasAllow(DataSource.ALLOW_INSERT)));
            sct.setEL("create", Boolean.valueOf(d.hasAllow(DataSource.ALLOW_CREATE)));
            sct.setEL("insert", Boolean.valueOf(d.hasAllow(DataSource.ALLOW_INSERT)));
            sct.setEL("drop", Boolean.valueOf(d.hasAllow(DataSource.ALLOW_DROP)));
            sct.setEL("grant", Boolean.valueOf(d.hasAllow(DataSource.ALLOW_GRANT)));
            sct.setEL("revoke", Boolean.valueOf(d.hasAllow(DataSource.ALLOW_REVOKE)));
            sct.setEL("alter", Boolean.valueOf(d.hasAllow(DataSource.ALLOW_ALTER)));
            sct.setEL("connectionLimit", d.getConnectionLimit() < 1 ? "" : Caster.toString(d.getConnectionLimit()));
            sct.setEL("connectionTimeout", d.getConnectionTimeout() < 1 ? "" : Caster.toString(d.getConnectionTimeout()));
            sct.setEL("metaCacheTimeout", Caster.toDouble(d.getMetaCacheTimeout()));
            sct.setEL("custom", d.getCustoms());
            sct.setEL("blob", Boolean.valueOf(d.isBlob()));
            sct.setEL("clob", Boolean.valueOf(d.isClob()));
            sct.setEL("validate", Boolean.valueOf(d.validate()));
            sct.setEL("storage", Boolean.valueOf(d.isStorage()));
            if (d instanceof DataSourceImpl) {
                DataSourceImpl di = ((DataSourceImpl) d);
                sct.setEL("literalTimestampWithTSOffset", Boolean.valueOf(di.getLiteralTimestampWithTSOffset()));
                sct.setEL("alwaysSetTimeout", Boolean.valueOf(di.getAlwaysSetTimeout()));
                sct.setEL("dbdriver", Caster.toString(di.getDbDriver(), ""));
            }
            pageContext.setVariable(getString("admin", action, "returnVariable"), sct);
            return;
        }
    }
    throw new ApplicationException("there is no datasource with name [" + name + "]");
}
Also used : StructImpl(lucee.runtime.type.StructImpl) ApplicationException(lucee.runtime.exp.ApplicationException) DataSourceImpl(lucee.runtime.db.DataSourceImpl) Iterator(java.util.Iterator) ClassDefinition(lucee.runtime.db.ClassDefinition) Map(java.util.Map) HashMap(java.util.HashMap) DataSource(lucee.runtime.db.DataSource) Struct(lucee.runtime.type.Struct)

Example 3 with DataSourceImpl

use of lucee.runtime.db.DataSourceImpl in project Lucee by lucee.

the class Admin method doUpdateDatasource.

/**
 * @throws PageException
 */
private void doUpdateDatasource() throws PageException {
    int allow = (getBoolV("allowed_select", false) ? DataSource.ALLOW_SELECT : 0) + (getBoolV("allowed_insert", false) ? DataSource.ALLOW_INSERT : 0) + (getBoolV("allowed_update", false) ? DataSource.ALLOW_UPDATE : 0) + (getBoolV("allowed_delete", false) ? DataSource.ALLOW_DELETE : 0) + (getBoolV("allowed_alter", false) ? DataSource.ALLOW_ALTER : 0) + (getBoolV("allowed_drop", false) ? DataSource.ALLOW_DROP : 0) + (getBoolV("allowed_revoke", false) ? DataSource.ALLOW_REVOKE : 0) + (getBoolV("allowed_grant", false) ? DataSource.ALLOW_GRANT : 0) + (getBoolV("allowed_create", false) ? DataSource.ALLOW_CREATE : 0);
    if (allow == 0)
        allow = DataSource.ALLOW_ALL;
    String cn = getString("admin", action, "classname");
    if ("com.microsoft.jdbc.sqlserver.SQLServerDriver".equals(cn)) {
        cn = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    }
    ClassDefinition cd = new ClassDefinitionImpl(cn, getString("bundleName", null), getString("bundleVersion", null), config.getIdentification());
    // customParameterSyntax
    Struct sct = getStruct("customParameterSyntax", null);
    ParamSyntax ps = (sct != null && sct.containsKey("delimiter") && sct.containsKey("separator")) ? ParamSyntax.toParamSyntax(sct) : ParamSyntax.DEFAULT;
    // 
    boolean literalTimestampWithTSOffset = getBoolV("literalTimestampWithTSOffset", false);
    boolean alwaysSetTimeout = getBoolV("alwaysSetTimeout", false);
    String dsn = getString("admin", action, "dsn");
    String name = getString("admin", action, "name");
    String newName = getString("admin", action, "newName");
    String username = getString("admin", action, "dbusername");
    String password = getString("admin", action, "dbpassword");
    String host = getString("host", "");
    String timezone = getString("timezone", "");
    String database = getString("database", "");
    int port = getInt("port", -1);
    int connLimit = getInt("connectionLimit", -1);
    int connTimeout = getInt("connectionTimeout", -1);
    long metaCacheTimeout = getLong("metaCacheTimeout", 60000);
    boolean blob = getBoolV("blob", false);
    boolean clob = getBoolV("clob", false);
    boolean validate = getBoolV("validate", false);
    boolean storage = getBoolV("storage", false);
    boolean verify = getBoolV("verify", true);
    Struct custom = getStruct("custom", new StructImpl());
    String dbdriver = getString("dbdriver", "");
    // config.getDatasourceConnectionPool().remove(name);
    DataSource ds = null;
    try {
        ds = new DataSourceImpl(config, null, name, cd, host, dsn, database, port, username, password, connLimit, connTimeout, metaCacheTimeout, blob, clob, allow, custom, false, validate, storage, null, dbdriver, ps, literalTimestampWithTSOffset, alwaysSetTimeout, config.getLog("application"));
    } catch (Exception e) {
        throw Caster.toPageException(e);
    }
    if (verify)
        _doVerifyDatasource(ds, username, password);
    // print.out("limit:"+connLimit);
    admin.updateDataSource(name, newName, cd, dsn, username, password, host, database, port, connLimit, connTimeout, metaCacheTimeout, blob, clob, allow, validate, storage, timezone, custom, dbdriver, ps, literalTimestampWithTSOffset, alwaysSetTimeout);
    store();
    adminSync.broadcast(attributes, config);
}
Also used : ClassDefinitionImpl(lucee.transformer.library.ClassDefinitionImpl) StructImpl(lucee.runtime.type.StructImpl) DataSourceImpl(lucee.runtime.db.DataSourceImpl) ClassDefinition(lucee.runtime.db.ClassDefinition) ParamSyntax(lucee.runtime.db.ParamSyntax) PageException(lucee.runtime.exp.PageException) SecurityException(lucee.runtime.exp.SecurityException) IOException(java.io.IOException) DeprecatedException(lucee.runtime.exp.DeprecatedException) BundleException(org.osgi.framework.BundleException) MalformedURLException(java.net.MalformedURLException) SMTPException(lucee.runtime.net.mail.SMTPException) ApplicationException(lucee.runtime.exp.ApplicationException) Struct(lucee.runtime.type.Struct) DataSource(lucee.runtime.db.DataSource)

Example 4 with DataSourceImpl

use of lucee.runtime.db.DataSourceImpl in project Lucee by lucee.

the class AppListenerUtil method toDataSource.

public static DataSource toDataSource(Config config, String name, Struct data, Log log) throws PageException {
    String user = Caster.toString(data.get(KeyConstants._username, null), null);
    String pass = Caster.toString(data.get(KeyConstants._password, ""), "");
    if (StringUtil.isEmpty(user)) {
        user = null;
        pass = null;
    } else {
        user = user.trim();
        pass = pass.trim();
    }
    // first check for {class:... , connectionString:...}
    Object oConnStr = data.get(CONNECTION_STRING, null);
    if (oConnStr != null) {
        String className = Caster.toString(data.get(KeyConstants._class));
        if ("com.microsoft.jdbc.sqlserver.SQLServerDriver".equals(className)) {
            className = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        }
        ClassDefinition cd = new ClassDefinitionImpl(className, Caster.toString(data.get(KeyConstants._bundleName, null), null), Caster.toString(data.get(KeyConstants._bundleVersion, null), null), ThreadLocalPageContext.getConfig().getIdentification());
        try {
            return ApplicationDataSource.getInstance(config, name, cd, Caster.toString(oConnStr), user, pass, Caster.toBooleanValue(data.get(BLOB, null), false), Caster.toBooleanValue(data.get(CLOB, null), false), Caster.toIntValue(data.get(CONNECTION_LIMIT, null), -1), Caster.toIntValue(data.get(CONNECTION_TIMEOUT, null), 1), Caster.toLongValue(data.get(META_CACHE_TIMEOUT, null), 60000L), Caster.toTimeZone(data.get(TIMEZONE, null), null), Caster.toIntValue(data.get(ALLOW, null), DataSource.ALLOW_ALL), Caster.toBooleanValue(data.get(STORAGE, null), false), Caster.toBooleanValue(data.get(READ_ONLY, null), false), log);
        } catch (Exception cnfe) {
            throw Caster.toPageException(cnfe);
        }
    }
    // then for {type:... , host:... , ...}
    String type = Caster.toString(data.get(KeyConstants._type));
    DataSourceDefintion dbt = DBUtil.getDataSourceDefintionForType(type, null);
    if (dbt == null)
        throw new ApplicationException("no datasource type [" + type + "] found");
    try {
        return new DataSourceImpl(config, null, name, dbt.classDefinition, Caster.toString(data.get(KeyConstants._host)), dbt.connectionString, Caster.toString(data.get(DATABASE)), Caster.toIntValue(data.get(KeyConstants._port, null), -1), user, pass, Caster.toIntValue(data.get(CONNECTION_LIMIT, null), -1), Caster.toIntValue(data.get(CONNECTION_TIMEOUT, null), 1), Caster.toLongValue(data.get(META_CACHE_TIMEOUT, null), 60000L), Caster.toBooleanValue(data.get(BLOB, null), false), Caster.toBooleanValue(data.get(CLOB, null), false), DataSource.ALLOW_ALL, Caster.toStruct(data.get(KeyConstants._custom, null), null, false), Caster.toBooleanValue(data.get(READ_ONLY, null), false), true, Caster.toBooleanValue(data.get(STORAGE, null), false), Caster.toTimeZone(data.get(TIMEZONE, null), null), "", ParamSyntax.toParamSyntax(data, ParamSyntax.DEFAULT), Caster.toBooleanValue(data.get("literalTimestampWithTSOffset", null), false), Caster.toBooleanValue(data.get("alwaysSetTimeout", null), false), log);
    } catch (Exception cnfe) {
        throw Caster.toPageException(cnfe);
    }
}
Also used : ClassDefinitionImpl(lucee.transformer.library.ClassDefinitionImpl) ApplicationException(lucee.runtime.exp.ApplicationException) DataSourceDefintion(lucee.runtime.db.DBUtil.DataSourceDefintion) DataSourceImpl(lucee.runtime.db.DataSourceImpl) ClassDefinition(lucee.runtime.db.ClassDefinition) PageException(lucee.runtime.exp.PageException) ApplicationException(lucee.runtime.exp.ApplicationException)

Aggregations

DataSourceImpl (lucee.runtime.db.DataSourceImpl)4 ApplicationException (lucee.runtime.exp.ApplicationException)4 ClassDefinition (lucee.runtime.db.ClassDefinition)3 DataSource (lucee.runtime.db.DataSource)3 PageException (lucee.runtime.exp.PageException)2 Struct (lucee.runtime.type.Struct)2 StructImpl (lucee.runtime.type.StructImpl)2 ClassDefinitionImpl (lucee.transformer.library.ClassDefinitionImpl)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 PageContextImpl (lucee.runtime.PageContextImpl)1 DataSourceDefintion (lucee.runtime.db.DBUtil.DataSourceDefintion)1 ParamSyntax (lucee.runtime.db.ParamSyntax)1 DeprecatedException (lucee.runtime.exp.DeprecatedException)1 SecurityException (lucee.runtime.exp.SecurityException)1 SMTPException (lucee.runtime.net.mail.SMTPException)1 TimeSpan (lucee.runtime.type.dt.TimeSpan)1