/****************************************************************************
**
** sqlunify - interface for sqlunify
** Copyright (C) 2005, SILVERCODERS Adrian Smarzewski
** http://silvercoders.com
**
** Project homepage: http://sqlunify.com
** Project authors:  Rafał 'Vogel' Malinowski
**
** This program may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file COPYING.GPL included in the
** packaging of this file.
**
** Please remember that any attempt to workaround the GNU General Public
** License using wrappers, pipes, client/server protocols, and so on
** is considered as license violation. If your program, published on license
** other than GNU General Public License version 2, calls some part of this
** code directly or indirectly, you have to buy commerial license.
** If you do not like our point of view, simply do not use the product.
**
** Licensees holding valid commercial license for this product
** may use this file in accordance with the license published by
** Silvercoders and appearing in the file COPYING.COM
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**
*****************************************************************************/

#ifndef _SQLUNIFY_H_
#define _SQLUNIFY_H_

/**
 * Initialize sql-builders and internal data. This function must be called before any other from sqlunify lib.
 */
void sqlunify_init();

/**
 * Finalize sql-builders and inernal data. This function must be called after any other from sqlunify lib.
 */
void sqlunify_done();

/**
 * Information about builders.
 */
struct sqlunify_builder_info
{
    char *builder_name;
    char *builder_description;
    /**
     * Id of builder. This could be used in sqlunify_translate_query as last parameter after fetching all builders'
     * information. It is an index in builders' array.
     */
    int builder_id;
    /**
     * internal
     */
    struct builder * (*constructor) (void);
    /**
     * internal
     */
    int (* get_error_code) (void);
    /**
     * internal
     */
    char * (* get_error_description) (void);
};

/**
 * Returns count of installed sql-builders.
 *
 * @return count of installed sql-builders.
 */
int sqlunify_get_builder_count();

/**
 * Returns information about builder_id'th builder.
 *
 * @param builder_id id of builder
 * @return information about builder_id'th builder.
 */
struct sqlunify_builder_info * sqlunify_get_builder_info(int builder_id);

/**
 * Returns id of builder with given name.
 *
 * @param name of builder
 * @return id of builder with given name or -SQLUNIFY_ERR_NO_SUCH_BUILDER
 */
int sqlunify_get_builder_id(char *name);

/**
 * General sqlunify error codes. When sqlunify fails it returns something like -SQLUNIFY_ERR_XXXXX.
 * The description of error could be fetched by sqlunify_get_error_description function.
 *
 * A sql-builder can provide a detailed error code and description (see sqlunify_get_detailed_error_code
 * and sqlunify_get_detailed_error_description).
 *
 * @see error_descriptions
 */
#define SQLUNIFY_ERR_NO_SUCH_BUILDER 1
#define SQLUNIFY_ERR_INVALID_QUERY 2
#define SQLUNIFY_ERR_CAN_NOT_TRANSLATE 3

/**
 * Translates sql query from postgresql dialect to another one.
 *
 * <code>
 * char *result_query;
 * int result = sqlunify_translate_query("SELECT a FROM b", &result_query, sqlunify_get_builder_id("mysql"));
 * if (result < 0) return;
 * ...
 * free(result_query);
 * </code>
 *
 * @param input sql query string in postgresql dialect
 * @param output pointer to result string
 * @param builder_id id of builder used to translate query
 */
int sqlunify_translate_query(char *input, char **output, int builder_id);

/**
 * Returns description of last translation error (or empty string).
 *
 * @return description of last translation error (or empty string)
 */
char * sqlunify_get_error_description();

/**
 * Return detailed (from builder) code of last translation error (or empty string).
 *
 * @return detailed (from builder) code of last translation error (or empty string)
 */
int sqlunify_get_detailed_error_code();

/**
 * Return detailed (from builder) description of last translation error (or empty string).
 *
 * @return detailed (from builder) description of last translation error (or empty string)
 */
char * sqlunify_get_detailed_error_description();

#endif // _SQLUNIFY_H_
