<commit_before>/* gom-command.c
 *
 * Copyright (C) 2011 <NAME> <<EMAIL>>
 *
 * This file 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; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This file 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glib/gi18n.h>
#include <sqlite3.h>
#include <string.h>

#include "gom-adapter.h"
#include "gom-command.h"

G_DEFINE_TYPE(GomCommand, gom_command, G_TYPE_OBJECT)

struct _GomCommandPrivate
{
   GomAdapter   *adapter;
   gchar        *sql;
   sqlite3_stmt *stmt;
   GHashTable   *params;
};

enum
{
   PROP_0,
   PROP_ADAPTER,
   PROP_SQL,
   LAST_PROP
};

static GParamSpec *gParamSpecs[LAST_PROP];

static void
gom_command_bind_param (GomCommand   *command,
                        guint         param,
                        const GValue *value)
{
   GomCommandPrivate *priv;

   g_return_if_fail(GOM_IS_COMMAND(command));
   g_return_if_fail(value != NULL);
   g_return_if_fail(G_VALUE_TYPE(value));

   priv = command->priv;

   switch (G_VALUE_TYPE(value)) {
   case G_TYPE_BOOLEAN:
      sqlite3_bind_int(priv->stmt, param, g_value_get_boolean(value));
      break;
   case G_TYPE_DOUBLE:
      sqlite3_bind_double(priv->stmt, param, g_value_get_double(value));
      break;
   case G_TYPE_FLOAT:
      sqlite3_bind_double(priv->stmt, param, g_value_get_float(value));
      break;
   case G_TYPE_INT:
      sqlite3_bind_int(priv->stmt, param, g_value_get_int(value));
      break;
   case G_TYPE_INT64:
      sqlite3_bind_int64(priv->stmt, param, g_value_get_int64(value));
      break;
   case G_TYPE_UINT:
      sqlite3_bind_int(priv->stmt, param, g_value_get_uint(value));
      break;
   case G_TYPE_UINT64:
      sqlite3_bind_int64(priv->stmt, param, g_value_get_uint64(value));
      break;
   case G_TYPE_STRING:
      sqlite3_bind_text(priv->stmt, param,
                        g_value_dup_string(value), -1, g_free);
      break;
   default:
      if (G_VALUE_TYPE(value) == G_TYPE_DATE_TIME) {
         GTimeVal tv = { 0 };
         GDateTime *dt = g_value_get_boxed(value);
         gchar *iso8601;
         if (dt) {
            g_date_time_to_timeval(dt, &tv);
         }
         iso8601 = g_time_val_to_iso8601(&tv);
         sqlite3_bind_text(priv->stmt, param, iso8601, -1, g_free);
         break;
      }
      if (G_VALUE_TYPE(value) == G_TYPE_STRV) {
         GByteArray *bytes = g_byte_array_new();
         gchar **strv = g_value_get_boxed(value);
         guchar null_byte[] = { 0 };
         guint i;

         if (strv) {
            for (i = 0; strv[i]; i++) {
               g_byte_array_append(bytes, (guchar *)strv[i], strlen(strv[i]) + 1);
            }
         }
         g_byte_array_append(bytes, null_byte, 1);
         sqlite3_bind_blob(priv->stmt, param, bytes->data, bytes->len, g_free);
         g_byte_array_free(bytes, FALSE);
         break;
      }
      if (g_type_is_a(G_VALUE_TYPE(value), G_TYPE_FLAGS)) {
         sqlite3_bind_int(priv->stmt, param, g_value_get_flags(value));
         break;
      }
      g_warning("Failed to bind gtype %s.", g_type_name(G_VALUE_TYPE(value)));
      g_assert_not_reached();
      break;
   }
}

static void
gom_command_bind_params (GomCommand *command)
{
   GomCommandPrivate *priv;
   GHashTableIter iter;
   gpointer key;
   gpointer value;

   g_return_if_fail(GOM_IS_COMMAND(command));
   g_return_if_fail(command->priv->stmt);

   priv = command->priv;

   if (priv->params) {
      g_hash_table_iter_init(&iter, priv->params);
      while (g_hash_table_iter_next(&iter, &key, &value)) {
         gom_command_bind_param(command, *(guint *)key, value);
      }
   }<commit_msg>command: add support for binding enum types to parameters.<commit_after>/* gom-command.c
 *
 * Copyright (C) 2011 <NAME> <<EMAIL>>
 *
 * This file 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; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This file 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glib/gi18n.h>
#include <sqlite3.h>
#include <string.h>

#include "gom-adapter.h"
#include "gom-command.h"

G_DEFINE_TYPE(GomCommand, gom_command, G_TYPE_OBJECT)

struct _GomCommandPrivate
{
   GomAdapter   *adapter;
   gchar        *sql;
   sqlite3_stmt *stmt;
   GHashTable   *params;
};

enum
{
   PROP_0,
   PROP_ADAPTER,
   PROP_SQL,
   LAST_PROP
};

static GParamSpec *gParamSpecs[LAST_PROP];

static void
gom_command_bind_param (GomCommand   *command,
                        guint         param,
                        const GValue *value)
{
   GomCommandPrivate *priv;

   g_return_if_fail(GOM_IS_COMMAND(command));
   g_return_if_fail(value != NULL);
   g_return_if_fail(G_VALUE_TYPE(value));

   priv = command->priv;

   switch (G_VALUE_TYPE(value)) {
   case G_TYPE_BOOLEAN:
      sqlite3_bind_int(priv->stmt, param, g_value_get_boolean(value));
      break;
   case G_TYPE_DOUBLE:
      sqlite3_bind_double(priv->stmt, param, g_value_get_double(value));
      break;
   case G_TYPE_FLOAT:
      sqlite3_bind_double(priv->stmt, param, g_value_get_float(value));
      break;
   case G_TYPE_INT:
      sqlite3_bind_int(priv->stmt, param, g_value_get_int(value));
      break;
   case G_TYPE_INT64:
      sqlite3_bind_int64(priv->stmt, param, g_value_get_int64(value));
      break;
   case G_TYPE_UINT:
      sqlite3_bind_int(priv->stmt, param, g_value_get_uint(value));
      break;
   case G_TYPE_UINT64:
      sqlite3_bind_int64(priv->stmt, param, g_value_get_uint64(value));
      break;
   case G_TYPE_STRING:
      sqlite3_bind_text(priv->stmt, param,
                        g_value_dup_string(value), -1, g_free);
      break;
   default:
      if (G_VALUE_TYPE(value) == G_TYPE_DATE_TIME) {
         GTimeVal tv = { 0 };
         GDateTime *dt = g_value_get_boxed(value);
         gchar *iso8601;
         if (dt) {
            g_date_time_to_timeval(dt, &tv);
         }
         iso8601 = g_time_val_to_iso8601(&tv);
         sqlite3_bind_text(priv->stmt, param, iso8601, -1, g_free);
         break;
      } else if (G_VALUE_TYPE(value) == G_TYPE_STRV) {
         GByteArray *bytes = g_byte_array_new();
         gchar **strv = g_value_get_boxed(value);
         guchar null_byte[] = { 0 };
         guint i;

         if (strv) {
            for (i = 0; strv[i]; i++) {
               g_byte_array_append(bytes, (guchar *)strv[i], strlen(strv[i]) + 1);
            }
         }
         g_byte_array_append(bytes, null_byte, 1);
         sqlite3_bind_blob(priv->stmt, param, bytes->data, bytes->len, g_free);
         g_byte_array_free(bytes, FALSE);
         break;
      } else if (g_type_is_a(G_VALUE_TYPE(value), G_TYPE_FLAGS)) {
         sqlite3_bind_int(priv->stmt, param, g_value_get_flags(value));
         break;
      } else if (g_type_is_a(G_VALUE_TYPE(value), G_TYPE_ENUM)) {
         sqlite3_bind_int(priv->stmt, param, g_value_get_enum(value));
         break;
      }
      g_warning("Failed to bind gtype %s.", g_type_name(G_VALUE_TYPE(value)));
      g_assert_not_reached();
      break;
   }
}

static void
gom_command_bind_params (GomCommand *command)
{
   GomCommandPrivate *priv;
   GHashTableIter iter;
   gpointer key;
   gpointer value;

   g_return_if_fail(GOM_IS_COMMAND(command));
   g_return_if_fail(command->priv->stmt);

   priv = command->priv;

   if (priv->params) {
      g_hash_table_iter_init(&iter, priv->params);
      while (g_hash_table_iter_next(&iter, &key, &value)) {
         gom_command_bind_param(command, *(guint *)key, value);
      }
   }
{'fraction_non_alphanumeric': 0.10843801848779332, 'fraction_numerical': 0.013628821995733587, 'mean_word_length': 2.9489939167056622, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 56, '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': '3466206', 'n_tokens_mistral': 3015, 'n_tokens_neox': 2934, 'n_words': 646}