Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

org.objectweb.cjdbc.common.sql.filters.MacrosHandler Class Reference

List of all members.

Public Member Functions

 MacrosHandler (int replaceRand, long clockResolution, int now, int currentDate, int currentTime, int timeOfDay, int currentTimestamp)
String getXml ()
String macroDate (String originalSql, String macroPattern, int replacementPolicy, long currentClock, Integer[] idxs)
String macroRand (String originalSql, Integer[] idxs)
final String processMacros (String sql)

Static Public Member Functions

final int getIntRandLevel (String randLevel)
final String getStringRandLevel (int randLevel)
final int getIntDateLevel (String dateLevel)
final String getStringDateLevel (int dateLevel)

Static Public Attributes

final int UNKNOWN_INT_VALUE = -1
final String UNKNOWN_STRING_VALUE = "unknown"
final int RAND_OFF = 0
final int RAND_INT = 1
final int RAND_LONG = 2
final int RAND_FLOAT = 3
final int RAND_DOUBLE = 4
final int DATE_OFF = 0
final int DATE_DATE = 1
final int DATE_TIME = 2
final int DATE_TIMESTAMP = 3

Detailed Description

This class defines a MacrosHandler

Author:
Emmanuel Cecchet
Version:
1.0

Definition at line 43 of file MacrosHandler.java.


Constructor & Destructor Documentation

org.objectweb.cjdbc.common.sql.filters.MacrosHandler.MacrosHandler int  replaceRand,
long  clockResolution,
int  now,
int  currentDate,
int  currentTime,
int  timeOfDay,
int  currentTimestamp
 

Creates a new MacrosHandler object

Parameters:
replaceRand replacement of rand() macro
clockResolution clock resolution for date macros
now replacement of now()
currentDate replacement of current_date
currentTime replacement of current_time
timeOfDay replacement of timeofday()
currentTimestamp replacement of current_timestamp

Definition at line 109 of file MacrosHandler.java.

00111   {
00112     if ((replaceRand < RAND_OFF) || (replaceRand > RAND_DOUBLE))
00113       throw new RuntimeException("Invalid value for " + MACRO_RAND
00114           + " macro replacement (" + replaceRand + ")");
00115     this.replaceRand = replaceRand;
00116     if (clockResolution < 0)
00117       throw new RuntimeException(
00118           "Invalid negative value for clock resolution in date macros");
00119     this.clockResolution = clockResolution;
00120     if ((now < DATE_OFF) || (now > DATE_TIMESTAMP))
00121       throw new RuntimeException("Invalid value for " + MACRO_NOW
00122           + " macro replacement (" + now + ")");
00123     this.now = now;
00124     if ((currentDate < DATE_OFF) || (currentDate > DATE_DATE))
00125       throw new RuntimeException("Invalid value for " + MACRO_CURRENT_DATE
00126           + " macro replacement (" + currentDate + ")");
00127     this.currentDate = currentDate;
00128     if ((currentTime < DATE_OFF) || (currentTime > DATE_TIMESTAMP))
00129       throw new RuntimeException("Invalid value for " + MACRO_CURRENT_TIME
00130           + " macro replacement (" + currentTime + ")");
00131     this.currentTime = currentTime;
00132     if ((timeOfDay < DATE_OFF) || (timeOfDay > DATE_TIMESTAMP))
00133       throw new RuntimeException("Invalid value for " + MACRO_TIMEODFAY
00134           + " macro replacement (" + timeOfDay + ")");
00135     this.timeOfDay = timeOfDay;
00136     if ((currentTimestamp < DATE_OFF) || (currentTimestamp > DATE_TIMESTAMP))
00137       throw new RuntimeException("Invalid value for " + MACRO_CURRENT_TIMESTAMP
00138           + " macro replacement (" + currentTimestamp + ")");
00139     this.currentTimestamp = currentTimestamp;
00140     needsDateProcessing = (now + currentDate + timeOfDay + currentTimestamp) > 0;
00141     needsProcessing = needsDateProcessing || (replaceRand > 0);
00142   }


Member Function Documentation

final int org.objectweb.cjdbc.common.sql.filters.MacrosHandler.getIntDateLevel String  dateLevel  )  [static]
 

Convert the date level from string (xml value) to integer

Parameters:
dateLevel the date level
Returns:
an int corresponding to the string description

Definition at line 221 of file MacrosHandler.java.

00222   {
00223     if (dateLevel.equals(DatabasesXmlTags.VAL_off))
00224       return DATE_OFF;
00225     else if (dateLevel.equals(DatabasesXmlTags.VAL_date))
00226       return DATE_DATE;
00227     else if (dateLevel.equals(DatabasesXmlTags.VAL_time))
00228       return DATE_TIME;
00229     else if (dateLevel.equals(DatabasesXmlTags.VAL_timestamp))
00230       return DATE_TIMESTAMP;
00231     else
00232       return UNKNOWN_INT_VALUE;
00233   }

final int org.objectweb.cjdbc.common.sql.filters.MacrosHandler.getIntRandLevel String  randLevel  )  [static]
 

Convert the rand level from string (xml value) to integer

Parameters:
randLevel the rand level
Returns:
an int corresponding to the string description

Definition at line 150 of file MacrosHandler.java.

00151   {
00152     if (randLevel.equalsIgnoreCase(DatabasesXmlTags.VAL_off))
00153       return RAND_OFF;
00154     else if (randLevel.equalsIgnoreCase(DatabasesXmlTags.VAL_double))
00155       return RAND_DOUBLE;
00156     else if (randLevel.equalsIgnoreCase(DatabasesXmlTags.VAL_float))
00157       return RAND_FLOAT;
00158     else if (randLevel.equalsIgnoreCase(DatabasesXmlTags.VAL_int))
00159       return RAND_INT;
00160     else if (randLevel.equalsIgnoreCase(DatabasesXmlTags.VAL_long))
00161       return RAND_LONG;
00162     else
00163       return UNKNOWN_INT_VALUE;
00164   }

final String org.objectweb.cjdbc.common.sql.filters.MacrosHandler.getStringDateLevel int  dateLevel  )  [static]
 

Convert the date level from int (java code) to string (xml value)

Parameters:
dateLevel the date level
Returns:
a string description corresponding to that level

Definition at line 241 of file MacrosHandler.java.

References org.objectweb.cjdbc.common.sql.filters.MacrosHandler.DATE_DATE, org.objectweb.cjdbc.common.sql.filters.MacrosHandler.DATE_OFF, org.objectweb.cjdbc.common.sql.filters.MacrosHandler.DATE_TIME, and org.objectweb.cjdbc.common.sql.filters.MacrosHandler.DATE_TIMESTAMP.

Referenced by org.objectweb.cjdbc.common.sql.filters.MacrosHandler.getXml().

00242   {
00243     switch (dateLevel)
00244     {
00245       case DATE_OFF :
00246         return DatabasesXmlTags.VAL_off;
00247       case DATE_DATE :
00248         return DatabasesXmlTags.VAL_date;
00249       case DATE_TIME :
00250         return DatabasesXmlTags.VAL_time;
00251       case DATE_TIMESTAMP :
00252         return DatabasesXmlTags.VAL_timestamp;
00253       default :
00254         return UNKNOWN_STRING_VALUE;
00255     }
00256   }

final String org.objectweb.cjdbc.common.sql.filters.MacrosHandler.getStringRandLevel int  randLevel  )  [static]
 

Convert the rand level from int (java code) to string (xml value)

Parameters:
randLevel the rand level
Returns:
a string description corresponding to that level

Definition at line 196 of file MacrosHandler.java.

References org.objectweb.cjdbc.common.sql.filters.MacrosHandler.RAND_DOUBLE, org.objectweb.cjdbc.common.sql.filters.MacrosHandler.RAND_FLOAT, org.objectweb.cjdbc.common.sql.filters.MacrosHandler.RAND_INT, org.objectweb.cjdbc.common.sql.filters.MacrosHandler.RAND_LONG, and org.objectweb.cjdbc.common.sql.filters.MacrosHandler.RAND_OFF.

Referenced by org.objectweb.cjdbc.common.sql.filters.MacrosHandler.getXml().

00197   {
00198     switch (randLevel)
00199     {
00200       case RAND_OFF :
00201         return DatabasesXmlTags.VAL_off;
00202       case RAND_DOUBLE :
00203         return DatabasesXmlTags.VAL_double;
00204       case RAND_FLOAT :
00205         return DatabasesXmlTags.VAL_float;
00206       case RAND_INT :
00207         return DatabasesXmlTags.VAL_int;
00208       case RAND_LONG :
00209         return DatabasesXmlTags.VAL_long;
00210       default :
00211         return UNKNOWN_STRING_VALUE;
00212     }
00213   }

String org.objectweb.cjdbc.common.sql.filters.MacrosHandler.getXml  ) 
 

Return this MacrosHandler to the corresponding xml form

Returns:
the XML representation of this element

Definition at line 171 of file MacrosHandler.java.

References org.objectweb.cjdbc.common.sql.filters.MacrosHandler.getStringDateLevel(), and org.objectweb.cjdbc.common.sql.filters.MacrosHandler.getStringRandLevel().

00172   {
00173     StringBuffer sb = new StringBuffer();
00174     sb.append("<" + DatabasesXmlTags.ELT_MacroHandling + " "
00175         + DatabasesXmlTags.ATT_rand + "=\"" + getStringRandLevel(replaceRand)
00176         + "\" " + DatabasesXmlTags.ATT_now + "=\"" + getStringDateLevel(now)
00177         + "\" " + DatabasesXmlTags.ATT_currentDate + "=\""
00178         + getStringDateLevel(currentDate) + "\" "
00179         + DatabasesXmlTags.ATT_currentTime + "=\""
00180         + getStringDateLevel(currentTime) + "\" "
00181         + DatabasesXmlTags.ATT_currentTimestamp + "=\""
00182         + getStringDateLevel(currentTimestamp) + "\" "
00183         + DatabasesXmlTags.ATT_timeOfDay + "=\""
00184         + getStringDateLevel(timeOfDay) + "\" "
00185         + DatabasesXmlTags.ATT_timeResolution + "=\"" + clockResolution + "\" "
00186         + "/>");
00187     return sb.toString();
00188   }

String org.objectweb.cjdbc.common.sql.filters.MacrosHandler.macroDate String  originalSql,
String  macroPattern,
int  replacementPolicy,
long  currentClock,
Integer[]  idxs
 

Processes a date related macro using the given timestamp.

Parameters:
originalSql original SQL request
macroPattern macro text to look for
replacementPolicy DATE_DATE, DATE_TIME or DATE_TIMESTAMP
currentClock current time in ms
idxs quote indexes
Returns:
new SQL statement

Definition at line 268 of file MacrosHandler.java.

References org.objectweb.cjdbc.common.sql.filters.MacrosHandler.DATE_DATE, org.objectweb.cjdbc.common.sql.filters.MacrosHandler.DATE_TIME, and org.objectweb.cjdbc.common.sql.filters.MacrosHandler.DATE_TIMESTAMP.

Referenced by org.objectweb.cjdbc.common.sql.filters.MacrosHandler.processMacros().

00270   {
00271     if (idxs == null)
00272       idxs = getQuoteIndexes(originalSql);
00273     String lower = originalSql.toLowerCase();
00274     int idx = lower.indexOf(macroPattern.toLowerCase());
00275     if (idx == -1 || !shouldReplaceMacro(idx, idxs))
00276       return originalSql;
00277 
00278     String date;
00279     switch (replacementPolicy)
00280     {
00281       case DATE_DATE :
00282         date = "{d '" + new Date(currentClock).toString() + "'}";
00283         break;
00284       case DATE_TIME :
00285         date = "{t '" + new Time(currentClock).toString() + "'}";
00286         break;
00287       case DATE_TIMESTAMP :
00288         date = "{ts '" + new Timestamp(currentClock).toString() + "'}";
00289         break;
00290       default :
00291         throw new RuntimeException(
00292             "Unexpected replacement strategy for date macro ("
00293                 + replacementPolicy + ")");
00294     }
00295     return Strings.replaceCasePreserving(originalSql, macroPattern, date);
00296   }

String org.objectweb.cjdbc.common.sql.filters.MacrosHandler.macroRand String  originalSql,
Integer[]  idxs
 

Replaces rand() with a randomized value.

Parameters:
originalSql original SQL request
idxs quote indexes
Returns:
new SQL statement

Definition at line 305 of file MacrosHandler.java.

References org.objectweb.cjdbc.common.sql.filters.MacrosHandler.RAND_DOUBLE, org.objectweb.cjdbc.common.sql.filters.MacrosHandler.RAND_FLOAT, org.objectweb.cjdbc.common.sql.filters.MacrosHandler.RAND_INT, and org.objectweb.cjdbc.common.sql.filters.MacrosHandler.RAND_LONG.

Referenced by org.objectweb.cjdbc.common.sql.filters.MacrosHandler.processMacros().

00306   {
00307     if (idxs == null)
00308       idxs = getQuoteIndexes(originalSql);
00309     String lower = originalSql.toLowerCase();
00310     int idx = lower.indexOf(MACRO_RAND);
00311     if (idx > 0)
00312     {
00313       String rand;
00314       StringBuffer sql = new StringBuffer(originalSql);
00315       int shift = 0;
00316       do
00317       {
00318         if (shouldReplaceMacro(idx, idxs))
00319         {
00320           switch (replaceRand)
00321           {
00322             case RAND_INT :
00323               rand = Integer.toString(randGenerator.nextInt());
00324               break;
00325             case RAND_LONG :
00326               rand = Long.toString(randGenerator.nextLong());
00327               break;
00328             case RAND_FLOAT :
00329               rand = Float.toString(randGenerator.nextFloat());
00330               break;
00331             case RAND_DOUBLE :
00332               rand = Double.toString(randGenerator.nextDouble());
00333               break;
00334             default :
00335               throw new RuntimeException(
00336                   "Unexpected replacement strategy for rand() macro ("
00337                       + replaceRand + ")");
00338           }
00339           sql = sql.replace(idx + shift, idx + shift + MACRO_RAND.length(),
00340               rand);
00341           shift += rand.length() - MACRO_RAND.length();
00342         }
00343         idx = lower.indexOf(MACRO_RAND, idx + MACRO_RAND.length());
00344       }
00345       while (idx > 0);
00346       return sql.toString();
00347     }
00348     else
00349     {
00350       return originalSql;
00351     }
00352   }

final String org.objectweb.cjdbc.common.sql.filters.MacrosHandler.processMacros String  sql  ) 
 

Processes all macros in the given request and returns a new String with the processed macros. If no macro has to be processed, the original String is returned.

Parameters:
sql SQL statement to process
Returns:
processed statement

Definition at line 362 of file MacrosHandler.java.

References org.objectweb.cjdbc.common.sql.filters.MacrosHandler.macroDate(), and org.objectweb.cjdbc.common.sql.filters.MacrosHandler.macroRand().

00363   {
00364     if (!needsProcessing)
00365       return sql;
00366     if(sql==null)
00367       return null;
00368     Integer[] idxs = this.getQuoteIndexes(sql);
00369     if (replaceRand > RAND_OFF)
00370       sql = macroRand(sql, idxs);
00371     if (!needsDateProcessing)
00372       return sql;
00373     long currentClock = System.currentTimeMillis();
00374     if (clockResolution > 0)
00375       currentClock = currentClock - (currentClock % clockResolution);
00376     if (now > DATE_OFF)
00377       sql = macroDate(sql, MACRO_NOW, now, currentClock, idxs);
00378     if (currentDate > DATE_OFF)
00379       sql = macroDate(sql, MACRO_CURRENT_DATE, currentDate, currentClock, idxs);
00380     if (currentTimestamp > DATE_OFF)
00381       sql = macroDate(sql, MACRO_CURRENT_TIMESTAMP, currentTimestamp,
00382           currentClock, idxs);
00383     if (currentTime > DATE_OFF)
00384       sql = macroDate(sql, MACRO_CURRENT_TIME, currentTime, currentClock, idxs);
00385     if (timeOfDay > DATE_OFF)
00386       sql = macroDate(sql, MACRO_TIMEODFAY, timeOfDay, currentClock, idxs);
00387 
00388     return sql;
00389   }


Member Data Documentation

final int org.objectweb.cjdbc.common.sql.filters.MacrosHandler.DATE_DATE = 1 [static]
 

Value if date macro should be replaced by an java.sql.Date value

Definition at line 82 of file MacrosHandler.java.

Referenced by org.objectweb.cjdbc.common.sql.filters.MacrosHandler.getStringDateLevel(), and org.objectweb.cjdbc.common.sql.filters.MacrosHandler.macroDate().

final int org.objectweb.cjdbc.common.sql.filters.MacrosHandler.DATE_OFF = 0 [static]
 

Value if a date macro should not be replaced

Definition at line 80 of file MacrosHandler.java.

Referenced by org.objectweb.cjdbc.common.sql.filters.MacrosHandler.getStringDateLevel().

final int org.objectweb.cjdbc.common.sql.filters.MacrosHandler.DATE_TIME = 2 [static]
 

Value if date macro should be replaced by an java.sql.Time value

Definition at line 84 of file MacrosHandler.java.

Referenced by org.objectweb.cjdbc.common.sql.filters.MacrosHandler.getStringDateLevel(), and org.objectweb.cjdbc.common.sql.filters.MacrosHandler.macroDate().

final int org.objectweb.cjdbc.common.sql.filters.MacrosHandler.DATE_TIMESTAMP = 3 [static]
 

Value if date macro should be replaced by an java.sql.Timestamp value

Definition at line 86 of file MacrosHandler.java.

Referenced by org.objectweb.cjdbc.common.sql.filters.MacrosHandler.getStringDateLevel(), and org.objectweb.cjdbc.common.sql.filters.MacrosHandler.macroDate().

final int org.objectweb.cjdbc.common.sql.filters.MacrosHandler.RAND_DOUBLE = 4 [static]
 

Value if rand() macro should be replaced by an double value

Definition at line 62 of file MacrosHandler.java.

Referenced by org.objectweb.cjdbc.common.sql.filters.MacrosHandler.getStringRandLevel(), and org.objectweb.cjdbc.common.sql.filters.MacrosHandler.macroRand().

final int org.objectweb.cjdbc.common.sql.filters.MacrosHandler.RAND_FLOAT = 3 [static]
 

Value if rand() macro should be replaced by an float value (default)

Definition at line 60 of file MacrosHandler.java.

Referenced by org.objectweb.cjdbc.common.sql.filters.MacrosHandler.getStringRandLevel(), and org.objectweb.cjdbc.common.sql.filters.MacrosHandler.macroRand().

final int org.objectweb.cjdbc.common.sql.filters.MacrosHandler.RAND_INT = 1 [static]
 

Value if rand() macro should be replaced by an integer value

Definition at line 56 of file MacrosHandler.java.

Referenced by org.objectweb.cjdbc.common.sql.filters.MacrosHandler.getStringRandLevel(), and org.objectweb.cjdbc.common.sql.filters.MacrosHandler.macroRand().

final int org.objectweb.cjdbc.common.sql.filters.MacrosHandler.RAND_LONG = 2 [static]
 

Value if rand() macro should be replaced by an long value

Definition at line 58 of file MacrosHandler.java.

Referenced by org.objectweb.cjdbc.common.sql.filters.MacrosHandler.getStringRandLevel(), and org.objectweb.cjdbc.common.sql.filters.MacrosHandler.macroRand().

final int org.objectweb.cjdbc.common.sql.filters.MacrosHandler.RAND_OFF = 0 [static]
 

Value if rand() macro should not be replaced

Definition at line 54 of file MacrosHandler.java.

Referenced by org.objectweb.cjdbc.common.sql.filters.MacrosHandler.getStringRandLevel().

final int org.objectweb.cjdbc.common.sql.filters.MacrosHandler.UNKNOWN_INT_VALUE = -1 [static]
 

Used when level is unknown

Definition at line 46 of file MacrosHandler.java.

final String org.objectweb.cjdbc.common.sql.filters.MacrosHandler.UNKNOWN_STRING_VALUE = "unknown" [static]
 

Used when level is unknown

Definition at line 48 of file MacrosHandler.java.


The documentation for this class was generated from the following file:
Generated on Mon Apr 11 22:02:12 2005 for C-JDBC by  doxygen 1.3.9.1