/*

Copyright (C) 2000-2010  Ministere de la culture et de la communication (France), AJLSM
See LICENCE file

*/

package fr.gouv.culture.sdx.repository;

import fr.gouv.culture.sdx.exception.SDXException;
import fr.gouv.culture.sdx.utils.configuration.ConfigurationUtils;
import fr.gouv.culture.sdx.utils.constants.Node;
import fr.gouv.culture.sdx.utils.save.SaveParameters;
import fr.gouv.culture.sdx.utils.save.Saveable;

import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;

/**The same as JDBCRespository, but the method for the table creation query is overridden.
 *
 */

public class MYSQLRepository extends JDBCRepository {

    /** Returns an SQL query that could create the table.
     * <p>
     * This query should not have any parameter.
     * @return The query.
     */

    protected String getTableCreationQuery() {
        // TODO: at some time, implement the proper handling of the original document (column data_t)
        //changed dataType of FIELD_DATA_TRANSFORMED from "LONGBLOB NOT NULL" TO  only "LONGBLOB"
        return "CREATE TABLE " + getTableName() + " ( " + FIELD_ID + " VARCHAR(255) NOT NULL, " + FIELD_DATA + " LONGBLOB, PRIMARY KEY (" + FIELD_ID + ")) TYPE=MYISAM";
    }

    public void configure(Configuration configuration) throws ConfigurationException {
        // We will need the datasource _identifier here, so calling super
        super.configure(configuration);
        //well we need a good dsi value to work with so we verify it
        ConfigurationUtils.checkConfAttributeValue(ATTRIBUTE_DSI, this.dsi, configuration.getLocation());
    }

    /** Save the repository
	 * @see fr.gouv.culture.sdx.utils.save.Saveable#backup(fr.gouv.culture.sdx.utils.save.SaveParameters)
	 */
	public void backup(SaveParameters save_config) throws SDXException {
		super.backup(save_config);
		if(save_config != null)
			if(save_config.getAttributeAsBoolean(Saveable.ALL_SAVE_ATTRIB,false))
			{
				save_config.setAttribute(Node.Name.TYPE,"MYSQL");	
			}
	}
	
	/** 
	 * @see fr.gouv.culture.sdx.utils.database.AbstractJDBCDatabase#getAllEntitiesWithLimitQuery(long, long)
	 * MYSQL implementation
	 * SELECT * FROM &gt;table_name&lt; LIMIT &gt;offset&lt;,&gt;number&lt; ;
	 */
	protected String getEntriesWithLimitQuery(long offset, long number) {
		String query = "SELECT * FROM " + getTableName();
		query += " LIMIT " + String.valueOf(offset) + "," + String.valueOf(number) + ";";
		return query;
	}
	
	/** Restore the repository
	 * @see fr.gouv.culture.sdx.utils.save.Saveable#restore(fr.gouv.culture.sdx.utils.save.SaveParameters)
	 */
	public void restore(SaveParameters save_config) throws SDXException {
		super.restore(save_config);
	}

}

