MySQL : Formater les identifiants de table et de colonne

Aller en bas

MySQL : Formater les identifiants de table et de colonne Empty MySQL : Formater les identifiants de table et de colonne

Message  Gus le Jeu 28 Avr - 9:36

Dans la documentation de MySQL refman (visitez le lien http://dev.mysql.com/doc/ pour obtenir la dernière version), le chapitre 8.3 intitulé "Reserved Words" indique que des précautions doivent être prises lorsqu'on utilise des mots-clés comme identifiant de table ou de colonne.

L'entité gus.database.mysql.formatname permet de formater les noms avant de les utiliser comme identifiant de table ou de colonne :

Code:
package gus05.entity.gus.database.mysql.formatname;

import gus05.framework.core.Entity;
import gus05.framework.features.Transform;

public class FormatName implements Entity, Transform {

    public String getName()         {return "gus.database.mysql.formatname";}
    public String getCreationDate()      {return "2011.01.02";}

    public Object transform(Object obj) throws Exception
    {
       String name = (String) obj;
       if(MYSQL_RESERVED_WORDS.isReservedWord(name))
          return "`"+name+"`";
       return name;
    }
}

Code:
package gus05.entity.gus.database.mysql.formatname;

public class MYSQL_RESERVED_WORDS {

   public static String ACCESSIBLE = "ACCESSIBLE";
   public static String ADD = "ADD";
   public static String ALL = "ALL";
   public static String ALTER = "ALTER";
   public static String ANALYZE = "ANALYZE";
   public static String AND = "AND";
   public static String AS = "AS";
   public static String ASC = "ASC";
   public static String ASENSITIVE = "ASENSITIVE";
   public static String BEFORE = "BEFORE";
   public static String BETWEEN = "BETWEEN";
   public static String BIGINT = "BIGINT";
   public static String BINARY = "BINARY";
   public static String BLOB = "BLOB";
   public static String BOTH = "BOTH";
   public static String BY = "BY";
   public static String CALL = "CALL";
   public static String CASCADE = "CASCADE";
   public static String CASE = "CASE";
   public static String CHANGE = "CHANGE";
   public static String CHAR = "CHAR";
   public static String CHARACTER = "CHARACTER";
   public static String CHECK = "CHECK";
   public static String COLLATE = "COLLATE";
   public static String COLUMN = "COLUMN";
   public static String CONDITION = "CONDITION";
   public static String CONSTRAINT = "CONSTRAINT";
   public static String CONTINUE = "CONTINUE";
   public static String CONVERT = "CONVERT";
   public static String CREATE = "CREATE";
   public static String CROSS = "CROSS";
   public static String CURRENT_DATE = "CURRENT_DATE";
   public static String CURRENT_TIME = "CURRENT_TIME";
   public static String CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP";
   public static String CURRENT_USER = "CURRENT_USER";
   public static String CURSOR = "CURSOR";
   public static String DATABASE = "DATABASE";
   public static String DATABASES = "DATABASES";
   public static String DAY_HOUR = "DAY_HOUR";
   public static String DAY_MICROSECOND = "DAY_MICROSECOND";
   public static String DAY_MINUTE = "DAY_MINUTE";
   public static String DAY_SECOND = "DAY_SECOND";
   public static String DEC = "DEC";
   public static String DECIMAL = "DECIMAL";
   public static String DECLARE = "DECLARE";
   public static String DEFAULT = "DEFAULT";
   public static String DELAYED = "DELAYED";
   public static String DELETE = "DELETE";
   public static String DESC = "DESC";
   public static String DESCRIBE = "DESCRIBE";
   public static String DETERMINISTIC = "DETERMINISTIC";
   public static String DISTINCT = "DISTINCT";
   public static String DISTINCTROW = "DISTINCTROW";
   public static String DIV = "DIV";
   public static String DOUBLE = "DOUBLE";
   public static String DROP = "DROP";
   public static String DUAL = "DUAL";
   public static String EACH = "EACH";
   public static String ELSE = "ELSE";
   public static String ELSEIF = "ELSEIF";
   public static String ENCLOSED = "ENCLOSED";
   public static String ESCAPED = "ESCAPED";
   public static String EXISTS = "EXISTS";
   public static String EXIT = "EXIT";
   public static String EXPLAIN = "EXPLAIN";
   public static String FALSE = "FALSE";
   public static String FETCH = "FETCH";
   public static String FLOAT = "FLOAT";
   public static String FLOAT4 = "FLOAT4";
   public static String FLOAT8 = "FLOAT8";
   public static String FOR = "FOR";
   public static String FORCE = "FORCE";
   public static String FOREIGN = "FOREIGN";
   public static String FROM = "FROM";
   public static String FULLTEXT = "FULLTEXT";
   public static String GRANT = "GRANT";
   public static String GROUP = "GROUP";
   public static String HAVING = "HAVING";
   public static String HIGH_PRIORITY = "HIGH_PRIORITY";
   public static String HOUR_MICROSECOND = "HOUR_MICROSECOND";
   public static String HOUR_MINUTE = "HOUR_MINUTE";
   public static String HOUR_SECOND = "HOUR_SECOND";
   public static String IF = "IF";
   public static String IGNORE = "IGNORE";
   public static String IN = "IN";
   public static String INDEX = "INDEX";
   public static String INFILE = "INFILE";
   public static String INNER = "INNER";
   public static String INOUT = "INOUT";
   public static String INSENSITIVE = "INSENSITIVE";
   public static String INSERT = "INSERT";
   public static String INT = "INT";
   public static String INT1 = "INT1";
   public static String INT2 = "INT2";
   public static String INT3 = "INT3";
   public static String INT4 = "INT4";
   public static String INT8 = "INT8";
   public static String INTEGER = "INTEGER";
   public static String INTERVAL = "INTERVAL";
   public static String INTO = "INTO";
   public static String IS = "IS";
   public static String ITERATE = "ITERATE";
   public static String JOIN = "JOIN";
   public static String KEY = "KEY";
   public static String KEYS = "KEYS";
   public static String KILL = "KILL";
   public static String LEADING = "LEADING";
   public static String LEAVE = "LEAVE";
   public static String LEFT = "LEFT";
   public static String LIKE = "LIKE";
   public static String LIMIT = "LIMIT";
   public static String LINEAR = "LINEAR";
   public static String LINES = "LINES";
   public static String LOAD = "LOAD";
   public static String LOCALTIME = "LOCALTIME";
   public static String LOCALTIMESTAMP = "LOCALTIMESTAMP";
   public static String LOCK = "LOCK";
   public static String LONG = "LONG";
   public static String LONGBLOB = "LONGBLOB";
   public static String LONGTEXT = "LONGTEXT";
   public static String LOOP = "LOOP";
   public static String LOW_PRIORITY = "LOW_PRIORITY";
   public static String MASTER_SSL_VERIFY_SERVER_CERT = "MASTER_SSL_VERIFY_SERVER_CERT";
   public static String MATCH = "MATCH";
   public static String MEDIUMBLOB = "MEDIUMBLOB";
   public static String MEDIUMINT = "MEDIUMINT";
   public static String MEDIUMTEXT = "MEDIUMTEXT";
   public static String MIDDLEINT = "MIDDLEINT";
   public static String MINUTE_MICROSECOND = "MINUTE_MICROSECOND";
   public static String MINUTE_SECOND = "MINUTE_SECOND";
   public static String MOD = "MOD";
   public static String MODIFIES = "MODIFIES";
   public static String NATURAL = "NATURAL";
   public static String NOT = "NOT";
   public static String NO_WRITE_TO_BINLOG = "NO_WRITE_TO_BINLOG";
   public static String NULL = "NULL";
   public static String NUMERIC = "NUMERIC";
   public static String ON = "ON";
   public static String OPTIMIZE = "OPTIMIZE";
   public static String OPTION = "OPTION";
   public static String OPTIONALLY = "OPTIONALLY";
   public static String OR = "OR";
   public static String ORDER = "ORDER";
   public static String OUT = "OUT";
   public static String OUTER = "OUTER";
   public static String OUTFILE = "OUTFILE";
   public static String PRECISION = "PRECISION";
   public static String PRIMARY = "PRIMARY";
   public static String PROCEDURE = "PROCEDURE";
   public static String PURGE = "PURGE";
   public static String RANGE = "RANGE";
   public static String READ = "READ";
   public static String READS = "READS";
   public static String READ_ONLY = "READ_ONLY";
   public static String READ_WRITE = "READ_WRITE";
   public static String REAL = "REAL";
   public static String REFERENCES = "REFERENCES";
   public static String REGEXP = "REGEXP";
   public static String RELEASE = "RELEASE";
   public static String RENAME = "RENAME";
   public static String REPEAT = "REPEAT";
   public static String REPLACE = "REPLACE";
   public static String REQUIRE = "REQUIRE";
   public static String RESTRICT = "RESTRICT";
   public static String RETURN = "RETURN";
   public static String REVOKE = "REVOKE";
   public static String RIGHT = "RIGHT";
   public static String RLIKE = "RLIKE";
   public static String SCHEMA = "SCHEMA";
   public static String SCHEMAS = "SCHEMAS";
   public static String SECOND_MICROSECOND = "SECOND_MICROSECOND";
   public static String SELECT = "SELECT";
   public static String SENSITIVE = "SENSITIVE";
   public static String SEPARATOR = "SEPARATOR";
   public static String SET = "SET";
   public static String SHOW = "SHOW";
   public static String SMALLINT = "SMALLINT";
   public static String SPATIAL = "SPATIAL";
   public static String SPECIFIC = "SPECIFIC";
   public static String SQL = "SQL";
   public static String SQLEXCEPTION = "SQLEXCEPTION";
   public static String SQLSTATE = "SQLSTATE";
   public static String SQLWARNING = "SQLWARNING";
   public static String SQL_BIG_RESULT = "SQL_BIG_RESULT";
   public static String SQL_CALC_FOUND_ROWS = "SQL_CALC_FOUND_ROWS";
   public static String SQL_SMALL_RESULT = "SQL_SMALL_RESULT";
   public static String SSL = "SSL";
   public static String STARTING = "STARTING";
   public static String STRAIGHT_JOIN = "STRAIGHT_JOIN";
   public static String TABLE = "TABLE";
   public static String TERMINATED = "TERMINATED";
   public static String THEN = "THEN";
   public static String TINYBLOB = "TINYBLOB";
   public static String TINYINT = "TINYINT";
   public static String TINYTEXT = "TINYTEXT";
   public static String TO = "TO";
   public static String TRAILING = "TRAILING";
   public static String TRIGGER = "TRIGGER";
   public static String TRUE = "TRUE";
   public static String UNDO = "UNDO";
   public static String UNION = "UNION";
   public static String UNIQUE = "UNIQUE";
   public static String UNLOCK = "UNLOCK";
   public static String UNSIGNED = "UNSIGNED";
   public static String UPDATE = "UPDATE";
   public static String USAGE = "USAGE";
   public static String USE = "USE";
   public static String USING = "USING";
   public static String UTC_DATE = "UTC_DATE";
   public static String UTC_TIME = "UTC_TIME";
   public static String UTC_TIMESTAMP = "UTC_TIMESTAMP";
   public static String VALUES = "VALUES";
   public static String VARBINARY = "VARBINARY";
   public static String VARCHAR = "VARCHAR";
   public static String VARCHARACTER = "VARCHARACTER";
   public static String VARYING = "VARYING";
   public static String WHEN = "WHEN";
   public static String WHERE = "WHERE";
   public static String WHILE = "WHILE";
   public static String WITH = "WITH";
   public static String WRITE = "WRITE";
   public static String XOR = "XOR";
   public static String YEAR_MONTH = "YEAR_MONTH";
   public static String ZEROFILL = "ZEROFILL";
   
   public static boolean isReservedWord(String word) throws Exception
   {
      try{MYSQL_RESERVED_WORDS.class.getField(word.toUpperCase());}
      catch(SecurityException e) {throw new Exception("A security exception occured inside "+MYSQL_RESERVED_WORDS.class,e);}
      catch(NoSuchFieldException e) {return false;}
      return true;
   }
}
Gus
Gus
Admin

Messages : 249
Date d'inscription : 01/09/2009

Voir le profil de l'utilisateur http://www.gus05.com

Revenir en haut Aller en bas

Revenir en haut


 
Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum