Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2009-2014 Pavel Ponec
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 
 package org.ujorm.orm.dialect;
 
 import java.util.List;
Dialect for the MySQL since release 5.0 for the InnoDB engine.
http://dev.mysql.com/
 
 public class MySqlDialect extends SqlDialect {

    
The Max length of VARCHAR database type
 
     public static final int VARCHAR_MAX_LENGTH = 21845;
 
     @Override
     public String getJdbcUrl() {
         return "jdbc:mysql://127.0.0.1:3306/db1";
     }
 
     @Override
     public String getJdbcDriver() {
         return "com.mysql.jdbc.Driver";
     }

    
Does the database support a catalog? The feature supports: MySqlDialect and MSSqlDialect.

Returns:
The default value is true.
 
     @Override
     public boolean isCatalog() {
         return true;
     }

   
Print an SQL DELETE statement.
 
     @Override
     public Appendable printDelete
         ( CriterionDecoder decoder
         , Appendable out
         ) throws IOException
     {
         out.append("DELETE ");
         printQuotedName(decoder.getBaseTable().getAlias(), out);
         out.append(" FROM ");
 
         final TableWrapper[] tables = decoder.getTablesSorted();
         if (tables.length==1) {
             tables[0] = new TableWrapperImpl(decoder.getBaseTable(), "");
         }
         for (int i = 0; i < tables.lengthi++) {
             if (i > 0) { out.append(", "); }
             printTableAliasDefinition(tables[i], out);
         }
         return printWhere(decodertablesout);
     }
 
     @Override
     public Appendable printUpdate
             ( List<MetaColumnchangedColumns
             , CriterionDecoder decoder
             , Appendable out
             ) throws IOException {
 
         final TableWrapper[] tables = decoder.getTablesSorted();
         if (tables.length==1) {
             tables[0] = new TableWrapperImpl(decoder.getBaseTable(), "");
         }
 
         out.append("UPDATE ");
         for (int i = 0; i < tables.lengthi++) {
             if (i > 0) { out.append(", "); }
             printTableAliasDefinition(tables[i], out);
         }
         out.append("\n\tSET ");
 
         for (int i=0; i<changedColumns.size(); i++) {
            MetaColumn ormColumn = changedColumns.get(i);
            if (ormColumn.isPrimaryKey()) {
                throw new IllegalStateException("Primary key can not be changed: " + ormColumn);
            }
            out.append(i==0 ? "" :  ", ");
            out.append(ormColumn.getColumnAlias());
            out.append("=?");
        }
        return printWhere(decodertablesout);
    }

    
Print where condition for DELETE / UPDATE TODO: FIX THE IMPLEMENTATION - probably in the CriterionDecoder class
    protected Appendable printWhere(CriterionDecoder decoderfinal TableWrapper[] tablesAppendable outthrows IOException {
        out.append(" WHERE ");
        String where = decoder.getWhere();
        if (tables.length==1) {
            String fullTableName = printFullTableName(decoder.getBaseTable(), new StringBuilder(64)).toString();
            String tableAlias = printQuotedName(decoder.getBaseTable().getAlias(), new StringBuilder(64)).toString();
            where = where.replace(tableAlias + '.'fullTableName + '.');
        }
        out.append(where);
        return out;
    }
    @Override
    public Appendable printSequenceTable(MetaDatabase dbAppendable outthrows IOException {
        return super.printSequenceTable(dbout)
              .append(' ')
              .append(getEngine(null))
              ;
    }
    @Override
    public Appendable printTable(MetaTable tableAppendable outthrows IOException {
        return super.printTable(tableout)
              .append(' ')
              .append(getEngine(table))
              ;
    }

    
Returns a MySQL enginge.
The default value is " ENGINE = InnoDB".
    protected String getEngine(final MetaTable table) {
              .of(.getParameters());
        return result;
    }

    
MySQL dialect uses a database type DATETIME (instead of the TIMESTAMP) for the java.util.Date.
    @Override
    protected String getColumnType(final MetaColumn column) {
        switch (..of(column)) {
            case :
                return "DATETIME";
            case :
                //mysql dont have clob but text
                //http://dev.mysql.com/doc/refman/5.0/en/blob.html
               //http://www.htmlite.com/mysql003.php
                return "LONGTEXT";
            case :
                return "LONGBLOB";
            case :
                return column.getMaxLength() > 
                        ? "TEXT"
                        : super.getColumnType(column);
            default:
                return super.getColumnType(column);
        }
    }

    
Print a Comment on the table
    @Override
    public Appendable printComment(MetaTable tableAppendable outthrows IOException {
        out.append("ALTER TABLE ");
        printFullTableName(tableout);
        out.append(" COMMENT = '");
        escape(..of(table), out);
        out.append("'");
        return out;
    }

    
Important note for MySQL: the change of column modifies all another column attributes so the comment update can revert some hand-made changes different from meta-model.
See the official MySQL documentation for more information. The column comments can be suppresed by the overwritting the method with an empty body.
    @Override
    public Appendable printComment(MetaColumn columnAppendable outthrows IOException {
        out.append("ALTER TABLE ");
        printFullTableName(column.getTable(), out);
        out.append(" MODIFY COLUMN ");
        if (column.isPrimaryKey()) {
            String pk = " PRIMARY KEY"// Due:  Multiple primary key defined.
            String statement = printColumnDeclaration(columnnullnew StringBuilder()).toString();
            out.append(statement.replaceAll(pk" "));
        } else if(column.isForeignKey()) {
            printFKColumnsDeclaration(columnout);
        } else {
            printColumnDeclaration(columnnullout);
        }
        out.append(" COMMENT '");
        escape(..of(column), out);
        out.append("'");
        return out;
    }

    
    @Override
    public Appendable printQuotedNameAlways(CharSequence nameAppendable sqlthrows IOException {
        sql.append('`'); // quotation start character based on SQL dialect
        sql.append(name);
        sql.append('`'); // quotation end character based on SQL dialect
        return sql;
    }

    
Create a SQL script for the NEXT SEQUENCE from a native database sequencer
TIP: SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = $dbName AND TABLE_NAME = $tblName.
    @Override
    public Appendable printNextSequence(String sequenceNameMetaTable tableAppendable outthrows IOException {
        out.append("SELECT AUTO_INCREMENT ");
        out.append("FROM information_schema.TABLES WHERE TABLE_SCHEMA = '");
        out.append(..of(table));
        out.append("' AND TABLE_NAME = '");
        out.append(..of(table));
        out.append("'");
        return out;
    }
New to GrepCode? Check out our FAQ X