<commit_before>/**
 * 
 * Hoel database abstraction library
 * 
 * hoel.c: main functions
 * 
 * Copyright 2015-2020 <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation;
 * version 2.1 of the License.
 *
 * This library 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.  See the
 * GNU GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
#include <ctype.h>
#include <string.h>

#include "hoel.h"
#include "h-private.h"

/**
 * free data allocated by hoel functions
 */
void h_free(void * data) {
  o_free(data);
}

/**
 * Close a database connection
 * return H_OK on success
 */
int h_close_db(struct _h_connection * conn) {
  if (conn != NULL && conn->connection != NULL) {
    if (0) {
      /* Not happening */
#ifdef _HOEL_SQLITE
    } else if (conn->type == HOEL_DB_TYPE_SQLITE) {
      h_close_sqlite(conn);
      return H_OK;
#endif
#ifdef _HOEL_MARIADB
    } else if (conn->type == HOEL_DB_TYPE_MARIADB) {
      h_close_mariadb(conn);
      return H_OK;
#endif
#ifdef _HOEL_PGSQL
    } else if (conn->type == HOEL_DB_TYPE_PGSQL) {
      h_close_pgsql(conn);
      return H_OK;
#endif
    } else {
      return H_ERROR_PARAMS;
    }
  } else {
    return H_ERROR_PARAMS;
  }
}

/**
 * h_escape_string
 * Escapes a string
 * returned value must be free'd after use
 */
char * h_escape_string(const struct _h_connection * conn, const char * unsafe) {
  if (conn != NULL && conn->connection != NULL && unsafe != NULL) {
    if (0) {
      /* Not happening */
#ifdef _HOEL_SQLITE
    } else if (conn->type == HOEL_DB_TYPE_SQLITE) {
      return h_escape_string_sqlite(conn, unsafe);
#endif
#ifdef _HOEL_MARIADB
    } else if (conn->type == HOEL_DB_TYPE_MARIADB) {
      return h_escape_string_mariadb(conn, unsafe);
#endif
#ifdef _HOEL_PGSQL
    } else if (conn->type == HOEL_DB_TYPE_PGSQL) {
      return h_escape_string_pgsql(conn, unsafe);
#endif
    } else {
      return NULL;
    }
  } else {
    return NULL;
  }
}

/**
 * h_escape_string_with_quotes
 * Escapes a string and returns it ready to be inserted in the query
 * returned value must be h_h_free'd after use
 */
char * h_escape_string_with_quotes(const struct _h_connection * conn, const char * unsafe) {
  if (conn != NULL && conn->connection != NULL && unsafe != NULL) {
    if (0) {
      /* Not happening */
#ifdef _HOEL_SQLITE
    } else if (conn->type == HOEL_DB_TYPE_SQLITE) {
      return h_escape_string_with_quotes_sqlite(conn, unsafe);
#endif
#ifdef _HOEL_MARIADB
    } else if (conn->type == HOEL_DB_TYPE_MARIADB) {
      return h_escape_string_with_quotes_mariadb(conn, unsafe);
#endif
#ifdef _HOEL_PGSQL
    } else if (conn->type == HOEL_DB_TYPE_PGSQL) {
      return h_escape_string_with_quotes_pgsql(conn, unsafe);
#endif
    } else {
      return NULL;
    }
  } else {
    return NULL;
  }
}

/**
 * h_execute_query
 * Execute a query, set the result structure with the returned values if available
 * if result is NULL, the query is executed but no value will be returned
 * options available
 * H_OPTION_NONE (0): no option
 * H_OPTION_SELECT: Execute a prepare statement (sqlite only)
 * H_OPTION_EXEC: Execute an exec statement (sqlite only)
 * return H_OK on success
 */
int h_execute_query(const struct _h_connection * conn, const char * query, struct _h_result * result, int options) {
  if (conn != NULL && conn->connection != NULL && query != NULL) {
    if (0) {
      /* Not happening */
#ifdef _HOEL_SQLITE
    } else if (conn->type == HOEL_DB_TYPE_SQLITE) {
      if (options & H_OPTION_EXEC) {
        return h_exec_query_sqlite(conn, query);
      } else {
        return h_select_query_sqlite(conn, query, result);
      }
#else
      UNUSED(options);<commit_msg>Use h_execute_query_sqlite<commit_after>/**
 * 
 * Hoel database abstraction library
 * 
 * hoel.c: main functions
 * 
 * Copyright 2015-2020 <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation;
 * version 2.1 of the License.
 *
 * This library 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.  See the
 * GNU GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
#include <ctype.h>
#include <string.h>

#include "hoel.h"
#include "h-private.h"

/**
 * free data allocated by hoel functions
 */
void h_free(void * data) {
  o_free(data);
}

/**
 * Close a database connection
 * return H_OK on success
 */
int h_close_db(struct _h_connection * conn) {
  if (conn != NULL && conn->connection != NULL) {
    if (0) {
      /* Not happening */
#ifdef _HOEL_SQLITE
    } else if (conn->type == HOEL_DB_TYPE_SQLITE) {
      h_close_sqlite(conn);
      return H_OK;
#endif
#ifdef _HOEL_MARIADB
    } else if (conn->type == HOEL_DB_TYPE_MARIADB) {
      h_close_mariadb(conn);
      return H_OK;
#endif
#ifdef _HOEL_PGSQL
    } else if (conn->type == HOEL_DB_TYPE_PGSQL) {
      h_close_pgsql(conn);
      return H_OK;
#endif
    } else {
      return H_ERROR_PARAMS;
    }
  } else {
    return H_ERROR_PARAMS;
  }
}

/**
 * h_escape_string
 * Escapes a string
 * returned value must be free'd after use
 */
char * h_escape_string(const struct _h_connection * conn, const char * unsafe) {
  if (conn != NULL && conn->connection != NULL && unsafe != NULL) {
    if (0) {
      /* Not happening */
#ifdef _HOEL_SQLITE
    } else if (conn->type == HOEL_DB_TYPE_SQLITE) {
      return h_escape_string_sqlite(conn, unsafe);
#endif
#ifdef _HOEL_MARIADB
    } else if (conn->type == HOEL_DB_TYPE_MARIADB) {
      return h_escape_string_mariadb(conn, unsafe);
#endif
#ifdef _HOEL_PGSQL
    } else if (conn->type == HOEL_DB_TYPE_PGSQL) {
      return h_escape_string_pgsql(conn, unsafe);
#endif
    } else {
      return NULL;
    }
  } else {
    return NULL;
  }
}

/**
 * h_escape_string_with_quotes
 * Escapes a string and returns it ready to be inserted in the query
 * returned value must be h_h_free'd after use
 */
char * h_escape_string_with_quotes(const struct _h_connection * conn, const char * unsafe) {
  if (conn != NULL && conn->connection != NULL && unsafe != NULL) {
    if (0) {
      /* Not happening */
#ifdef _HOEL_SQLITE
    } else if (conn->type == HOEL_DB_TYPE_SQLITE) {
      return h_escape_string_with_quotes_sqlite(conn, unsafe);
#endif
#ifdef _HOEL_MARIADB
    } else if (conn->type == HOEL_DB_TYPE_MARIADB) {
      return h_escape_string_with_quotes_mariadb(conn, unsafe);
#endif
#ifdef _HOEL_PGSQL
    } else if (conn->type == HOEL_DB_TYPE_PGSQL) {
      return h_escape_string_with_quotes_pgsql(conn, unsafe);
#endif
    } else {
      return NULL;
    }
  } else {
    return NULL;
  }
}

/**
 * h_execute_query
 * Execute a query, set the result structure with the returned values if available
 * if result is NULL, the query is executed but no value will be returned
 * options available
 * H_OPTION_NONE (0): no option
 * H_OPTION_SELECT: Execute a prepare statement (sqlite only)
 * H_OPTION_EXEC: Execute an exec statement (sqlite only)
 * return H_OK on success
 */
int h_execute_query(const struct _h_connection * conn, const char * query, struct _h_result * result, int options) {
  UNUSED(result);
  if (conn != NULL && conn->connection != NULL && query != NULL) {
    if (0) {
      /* Not happening */
#ifdef _HOEL_SQLITE
    } else if (conn->type == HOEL_DB_TYPE_SQLITE) {
      if (options & H_OPTION_EXEC) {
        return h_execute_query_sqlite(conn, query);
      } else {
        return h_select_query_sqlite(conn, query, result);
      }
#else
      UNUSED(options);
{'fraction_non_alphanumeric': 0.10548780487804878, 'fraction_numerical': 0.003658536585365854, 'mean_word_length': 3.3185887309110056, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3453039', 'n_tokens_mistral': 2799, 'n_tokens_neox': 2761, 'n_words': 833}