Package railo.runtime.db

Examples of railo.runtime.db.SQLImpl


    @Override
    public void log(int level, String application, String message) {
      DatasourceConnection dc=null;
      try {
      dc = pool.getDatasourceConnection(ThreadLocalPageContext.get(),datasource, username, password);
      SQLImpl sql = new SQLImpl(INSERT);
      sql.addItems(new SQLItemImpl(application,CFTypes.VARCHAR));
      sql.addItems(new SQLItemImpl(message,CFTypes.VARCHAR));
      sql.addItems(new SQLItemImpl(new DateTimeImpl(),CFTypes.DATE));
      new QueryImpl(ThreadLocalPageContext.get(),dc,sql,-1,-1,-1,"query");
    }
      catch (PageException e) {
      console.log(level, application, message);
    }
View Full Code Here


  @Override
  public Query select(Config config,String cfid,String applicationName,DatasourceConnection dc, int type,Log log, boolean createTableIfNotExist) throws PageException {
    String strType = VariableInterpreter.scopeInt2String(type);
    Query query=null;
      SQL sqlSelect=new SQLImpl("select data from "+PREFIX+"_"+strType+"_data where cfid=? and name=? and expires > ?"
        ,new SQLItem[]{
       new SQLItemImpl(cfid,Types.VARCHAR),
      new SQLItemImpl(applicationName,Types.VARCHAR),
      new SQLItemImpl(now(config),Types.VARCHAR)
    });
     
      PageContext pc = ThreadLocalPageContext.get();
   
    try {
      query = new QueryImpl(pc,dc,sqlSelect,-1,-1,-1,"query");
    }
      catch (DatabaseException de) {
        if(dc==null || !createTableIfNotExist) throw de;
        try {
          SQL sql = createSQL(dc,"text",strType);
          ScopeContext.info(log,sql.toString());
        new QueryImpl(pc,dc,sql,-1,-1,-1,"query");
        }
        catch (DatabaseException _de) {
          try {
            SQL sql = createSQL(dc,"memo",strType);
            ScopeContext.info(log,sql.toString());
          new QueryImpl(pc,dc,sql,-1,-1,-1,"query");
          }
          catch (DatabaseException __de) {
            SQL sql = createSQL(dc,"clob",strType);
            ScopeContext.info(log,sql.toString());
            new QueryImpl(pc,dc,sql,-1,-1,-1,"query");
          }
        }
        query = new QueryImpl(pc,dc,sqlSelect,-1,-1,-1,"query");
    }
      ScopeContext.info(log,sqlSelect.toString());
    return query;
  }
View Full Code Here

  }
 
  private static int _update(Config config,Connection conn,String cfid, String applicationName, String strSQL,Struct data, long timeSpan, Log log, TimeZone tz) throws SQLException, PageException {
    //String appName = pc.getApplicationContext().getName();
    try{
      SQLImpl sql = new SQLImpl(strSQL,new SQLItem[]{
        new SQLItemImpl(createExpires(config,timeSpan),Types.VARCHAR),
        new SQLItemImpl(new ScriptConverter().serializeStruct(data,ignoreSet),Types.VARCHAR),
        new SQLItemImpl(cfid,Types.VARCHAR),
        new SQLItemImpl(applicationName,Types.VARCHAR)
      });
      ScopeContext.info(log,sql.toString());
     
      return execute(conn, sql,tz);
    }
    catch(ConverterException ce){
      throw Caster.toPageException(ce);
View Full Code Here

 
  @Override
  public void delete(Config config, String cfid, String applicationName, DatasourceConnection dc, int type, Log log) throws PageException, SQLException {
    String strType = VariableInterpreter.scopeInt2String(type);
    String strSQL="delete from "+PREFIX+"_"+strType+"_data where cfid=? and name=?";
    SQLImpl sql = new SQLImpl(strSQL,new SQLItem[]{
        new SQLItemImpl(cfid,Types.VARCHAR),
        new SQLItemImpl(applicationName,Types.VARCHAR)
      });
    execute(dc.getConnection(), sql,ThreadLocalPageContext.getTimeZone());
    ScopeContext.info(log,sql.toString());
   
  }
View Full Code Here

  @Override
  public void clean(Config config, DatasourceConnection dc, int type,StorageScopeEngine engine,DatasourceStorageScopeCleaner cleaner,StorageScopeListener listener, Log log) throws PageException {
    String strType = VariableInterpreter.scopeInt2String(type);
    // select
      SQL sqlSelect=new SQLImpl("select cfid,name from "+PREFIX+"_"+strType+"_data where expires<=?"
            ,new SQLItem[]{
           new SQLItemImpl(System.currentTimeMillis(),Types.VARCHAR)
        });
      QueryImpl query;
      try{
        query = new QueryImpl(ThreadLocalPageContext.get(),dc,sqlSelect,-1,-1,-1,"query");
    }
    catch(Throwable t){
      // possible that the table not exist, if not there is nothing to clean
      return;
    }
   
    int recordcount=query.getRecordcount();
   
    String cfid,name;
    for(int row=1;row<=recordcount;row++){
      cfid=Caster.toString(query.getAt(KeyConstants._cfid, row, null),null);
      name=Caster.toString(query.getAt(KeyConstants._name, row, null),null);
     
      if(listener!=null)listener.doEnd(engine, cleaner,name, cfid);
     
     
      ScopeContext.info(log,"remove "+strType+"/"+name+"/"+cfid+" from datasource "+dc.getDatasource().getName());
      engine.remove(type,name,cfid);
      SQLImpl sql = new SQLImpl("delete from "+StorageScopeDatasource.PREFIX+"_"+strType+"_data where cfid=? and name=?",new SQLItem[]{
          new SQLItemImpl(cfid,Types.VARCHAR),
          new SQLItemImpl(name,Types.VARCHAR)
          });
      new QueryImpl(ThreadLocalPageContext.get(),dc,sql,-1,-1,-1,"query");
     
View Full Code Here

    else if(isOracle)sb.append("CLOB ");
    else sb.append(textType+" ");
    sb.append(" NOT NULL");
   
      sb.append(")");
    return new SQLImpl(sb.toString());
  }
View Full Code Here

    // add returnValue to params
    if(returnValue!=null){
      params.add(0,returnValue);
    }
   
    SQLImpl _sql=new SQLImpl(sql.toString());
    CallableStatement callStat=null;
    try {
        callStat = dc.getConnection().prepareCall(sql.toString());
            //ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            //ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
       
        if(blockfactor>0)callStat.setFetchSize(blockfactor);
        if(timeout>0)callStat.setQueryTimeout(timeout);
       
  // set IN register OUT
        Iterator<ProcParamBean> it = params.iterator();
      ProcParamBean param;
      int index=1;
        while(it.hasNext()) {
          param= it.next();
          param.setIndex(index);
          _sql.addItems(new SQLItemImpl(param.getValue()));
          if(param.getDirection()!=ProcParamBean.DIRECTION_OUT) {
            SQLCaster.setValue(pageContext.getTimeZone(),callStat, index, param);
          }
          if(param.getDirection()!=ProcParamBean.DIRECTION_IN) {
            registerOutParameter(callStat,param);
          }
          index++;
      }
       
  // cache
        boolean isFromCache=false;
        boolean hasCached=cachedbefore!=null || cachedafter!=null;
        Object cacheValue=null;
        String dsn = ds instanceof DataSource?((DataSource)ds).getName():Caster.toString(ds);
      if(clearCache) {
        hasCached=false;
        pageContext.getQueryCache().remove(pageContext,_sql,dsn,username,password);
      }
      else if(hasCached) {
        cacheValue = pageContext.getQueryCache().get(pageContext,_sql,dsn,username,password,cachedafter);
      }
      int count=0;
      if(cacheValue==null){
        // execute
        boolean isResult=callStat.execute();
       
          Struct cache=hasCached?new StructImpl():null;
 
          // resultsets
          ProcResultBean result;
         
          index=1;
        do {
            if(isResult){
              ResultSet rs=callStat.getResultSet();
              if(rs!=null) {
              try{
                result=(ProcResultBean) results.get(index++,null);
                if(result!=null) {
                  railo.runtime.type.Query q = new QueryImpl(rs,result.getMaxrows(),result.getName(),pageContext.getTimeZone())
                  count+=q.getRecordcount();
                  setVariable(result.getName(), q);
                  if(hasCached)cache.set(KeyImpl.getInstance(result.getName()), q);
                }
              }
              finally{
                IOUtil.closeEL(rs);
              }
            }
            }
          }
          while((isResult=callStat.getMoreResults()) || (callStat.getUpdateCount() != -1));

          // params
          it = params.iterator();
          while(it.hasNext()) {
            param= it.next();
            if(param.getDirection()!=ProcParamBean.DIRECTION_IN){
              Object value=null;
              if(!StringUtil.isEmpty(param.getVariable())){
                try{
                  value=SQLCaster.toCFType(callStat.getObject(param.getIndex()));
                }
                catch(Throwable t){}
                value=emptyIfNull(value);
               
                if(param==STATUS_CODE) res.set(STATUSCODE, value);
                else setVariable(param.getVariable(), value);
                if(hasCached)cache.set(KeyImpl.getInstance(param.getVariable()), value);
              }
            }
        }
          if(hasCached){
            cache.set(COUNT, Caster.toDouble(count));
            pageContext.getQueryCache().set(pageContext,_sql,dsn,username,password,cache,cachedbefore);
          }
         
      }
      else if(cacheValue instanceof Struct) {
        Struct sctCache = (Struct) cacheValue;
        count=Caster.toIntValue(sctCache.removeEL(COUNT),0);
       
        Iterator<Entry<Key, Object>> cit = sctCache.entryIterator();
        Entry<Key, Object> ce;
        while(cit.hasNext()){
          ce = cit.next();
          if(STATUS_CODE.getVariable().equals(ce.getKey().getString()))
            res.set(KEY_SC, ce.getValue());
          else setVariable(ce.getKey().getString(), ce.getValue());
        }
        isFromCache=true;
      }
     
        // result
        long exe;
       
        setVariable(this.result, res);
        res.set(KeyConstants._executionTime,Caster.toDouble(exe=(System.nanoTime()-startNS)));
        res.set(KeyConstants._cached,Caster.toBoolean(isFromCache));
       
        if(pageContext.getConfig().debug() && debug) {
          boolean logdb=((ConfigImpl)pageContext.getConfig()).hasDebugOptions(ConfigImpl.DEBUG_DATABASE);
        if(logdb)
          pageContext.getDebugger().addQuery(null,dsn,procedure,_sql,count,pageContext.getCurrentPageSource(),(int)exe);
      }
       
       
    }
    catch (SQLException e) {
        throw new DatabaseException(e,new SQLImpl(sql.toString()),dc);
    }
    finally {
        if(callStat!=null){
          try {
          callStat.close();
View Full Code Here

        sql.append(names);
        sql.append(")values(");
        sql.append(values);
        sql.append(")");
       
        return new SQLImpl(sql.toString(),(SQLItem[])items.toArray(new SQLItem[items.size()]));
    }
View Full Code Here

        }
        sql.append(tablename);
        sql.append(set);
        sql.append(where);
       
        return new SQLImpl(sql.toString(),arrayMerge(setItems,whereItems));
    }
View Full Code Here

      else if(Decision.isStruct(params))
        sql=QueryParamConverter.convert(strSQL, Caster.toStruct(params));
      else
        throw new DatabaseException("value of the attribute [params] has to be a struct or a array",null,null,null);
    }
    else sql=items.size()>0?new SQLImpl(strSQL,items.toArray(new SQLItem[items.size()])):new SQLImpl(strSQL);
   
    railo.runtime.type.Query query=null;
    long exe=0;
    boolean hasCached=cachedWithin!=null || cachedafter!=null;
   
View Full Code Here

TOP

Related Classes of railo.runtime.db.SQLImpl

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.