<commit_before><?php

namespace Nectary\Daos;

/**
 * Select_SQL_Query_Builder - Helper class for building PDO select statements
 *
 * @author <NAME>
 */
class Select_SQL_Query_Builder {

  /**
   * Internal state for this object
   */
  private $columns_to_select, $from, $joins, $where_clause, $values_to_bind, $order_by, $limit;

  /**
   * Sets up default state
   */
  public function __construct() {
    $this->columns_to_select = array();
    $this->from              = 'TABLE_NAME';
    $this->joins             = ' ';
    $this->where_clause      = '1=1 ';
    $this->values_to_bind    = array();
    $this->group_by          = array();
    $this->order_by          = '';
    $this->limit             = 100;
  }

  /**
   * This function takes either a string or an one dimensional array.
   * the string can be a single column name or multiple columns comma seperated.
   *
   * @param $new_columns : string or array of column names to select
   */
  public function add_columns( $new_columns ) {
    $this->columns_to_select = array_merge( $this->columns_to_select, to_array( $new_columns ) );
  }

  /**
   * Mainly for debugging, probably wont need this
   *
   * @return the current colums this query will select from
   */
  public function get_columns() {
    return $this->columns_to_select;
  }

  /**
   * For setting what table to select from, can only be one table. For joins see joins()
   *
   * @param $table : string - name of table to select
   */
  public function from( $table ) {
    $this->from = $table;
  }

  /**
   * For adding a join, can be called multiple times for multiple joins.
   *
   * @param $join_statement : string - sql fragment of the join statement
   */
  public function joins( $join_statement ) {
    $this->joins .= $join_statement . ' ';
  }

  /**
   * For setting a limit on the query other than the default
   * To bind limit as int if passed as string use PDO binding parameter
   * Example: select->bind_value( ':limit_to', array(intval( $options['limit'] ), \PDO::PARAM_INT ) )
   *
   * @param $new_limit
   */
  public function limit( $new_limit ) {
    $this->limit = $new_limit;
  }

  /**
   * For setting an order on the query other than the default. Currently Can't have multiple.
   *
   * @param $order : string : eg: "first_name ASC"
   */
  public function order_by( $order ) {
    $this->order_by = $order;
  }

  /**
   * For setting an group by on the query other than the default.
   *
   * @param $order : string : eg: "first_name"
   */
  public function group_by( $group ) {
    $this->group_by[] = $group;
  }

  /**
   * Adds a string to the where clause, it must start with an 'AND' or 'OR' to chain together
   * criteria.
   *
   * @param $string : eg: "AND blah = foo"
   */
  public function where( $string ) {
    $this->where_clause .= $string . ' ';
  }

  /**
   * Adds a string to the where clause with an 'AND'
   *
   * @param $string : eg: "blah = foo"
   */
  public function and_where( $string ) {
    $this->where( 'AND ' . $string );
  }

  /**
   * Adds a string to the where clause with an 'AND'
   *
   * @param $string : eg: "blah = foo"
   */
  public function or_where( $string ) {
    $this->where( 'OR ' . $string );
  }

  /**
   * Adds a variable to be bound to in the prepared statement
   *
   * @param $name : string
   * @param $value : object
   */
  public function bind_value( $name, $value ) {
    $this->values_to_bind[ $name ] = $value;
  }

  /**
   * Uses the current state of the object to build the sql statement
   *
   * @return the sql select statement as a string
   */
  public function get_sql() {
    if ( empty( $this->columns_to_select ) ) {
      // this should be the default behavor
      $this->columns_to_select = [ '*' ];
    }

    $from   = ' FROM ' . $this->from . ' ';

    if ( ! empty( ltrim( $this->joins ) ) ) {
      $from  .= ltrim( $this->joins );
    }

    $from  .= 'WHERE ' . $this->where_clause;
    $select = 'SELECT ' . implode( ', ', $this->columns_to_select ) . $from;

    if ( ! empty( $this->group_by ) ) {
      $select .= 'GROUP BY ' . implode( ', ', $this->group_by ) . ' ';
    }

    if ( $this->order_by !== '' ) {
      $select .= 'ORDER BY ' . $this->order_by . ' ';
    }

    $select .= 'LIMIT ' . $this->limit;
    return $select;
  }

  /**
   * Calls get_sql() to generate the sql and then creates the prepared statement
   * and binds its values.
   *
   * @param $db : a pdo database connection
   * @return pdo::Statement object with the query and values bound
   */
  public function get_statement( $db ) {
    $statement = $db->prepare( $this->get_sql() );
    foreach ( $this->values_to_bind as $name => $value ) {
      if ( is_array( $value ) && 2 === count( $value ) ) {
        $statement->bindValue( $name, $value[0], $value[1] );
      } else {
        $statement->bindValue( $name, $value );
      };
    }
    return $statement;
  }
}
<commit_msg>bind value with default paramter to set pdo type<commit_after><?php

namespace Nectary\Daos;

/**
 * Select_SQL_Query_Builder - Helper class for building PDO select statements
 *
 * @author <NAME>
 */
class Select_SQL_Query_Builder {

  /**
   * Internal state for this object
   */
  private $columns_to_select, $from, $joins, $where_clause, $values_to_bind, $order_by, $limit;

  /**
   * Sets up default state
   */
  public function __construct() {
    $this->columns_to_select = array();
    $this->from              = 'TABLE_NAME';
    $this->joins             = ' ';
    $this->where_clause      = '1=1 ';
    $this->values_to_bind    = array();
    $this->group_by          = array();
    $this->order_by          = '';
    $this->limit             = 100;
  }

  /**
   * This function takes either a string or an one dimensional array.
   * the string can be a single column name or multiple columns comma seperated.
   *
   * @param $new_columns : string or array of column names to select
   */
  public function add_columns( $new_columns ) {
    $this->columns_to_select = array_merge( $this->columns_to_select, to_array( $new_columns ) );
  }

  /**
   * Mainly for debugging, probably wont need this
   *
   * @return the current colums this query will select from
   */
  public function get_columns() {
    return $this->columns_to_select;
  }

  /**
   * For setting what table to select from, can only be one table. For joins see joins()
   *
   * @param $table : string - name of table to select
   */
  public function from( $table ) {
    $this->from = $table;
  }

  /**
   * For adding a join, can be called multiple times for multiple joins.
   *
   * @param $join_statement : string - sql fragment of the join statement
   */
  public function joins( $join_statement ) {
    $this->joins .= $join_statement . ' ';
  }

  /**
   * For setting a limit on the query other than the default
   *
   * @param $new_limit : intger | string
   */
  public function limit( $new_limit ) {
    if ( false === stripos( $new_limit . '', ':' ) ) {
      $this->limit = intval( $new_limit );
    } else {
      $this->limit = $new_limit;
    }
  }

  /**
   * For setting an order on the query other than the default. Currently Can't have multiple.
   *
   * @param $order : string : eg: "first_name ASC"
   */
  public function order_by( $order ) {
    $this->order_by = $order;
  }

  /**
   * For setting an group by on the query other than the default.
   *
   * @param $order : string : eg: "first_name"
   */
  public function group_by( $group ) {
    $this->group_by[] = $group;
  }

  /**
   * Adds a string to the where clause, it must start with an 'AND' or 'OR' to chain together
   * criteria.
   *
   * @param $string : eg: "AND blah = foo"
   */
  public function where( $string ) {
    $this->where_clause .= $string . ' ';
  }

  /**
   * Adds a string to the where clause with an 'AND'
   *
   * @param $string : eg: "blah = foo"
   */
  public function and_where( $string ) {
    $this->where( 'AND ' . $string );
  }

  /**
   * Adds a string to the where clause with an 'AND'
   *
   * @param $string : eg: "blah = foo"
   */
  public function or_where( $string ) {
    $this->where( 'OR ' . $string );
  }

  /**
   * Adds a variable to be bound to in the prepared statement
   *
   * @param $name : string
   * @param $value : object
   */
  public function bind_value( $name, $value, $data_type = PDO::PARAM_STR ) {
    $this->values_to_bind[ $name ] = array( $value, $data_type );
  }

  /**
   * Uses the current state of the object to build the sql statement
   *
   * @return the sql select statement as a string
   */
  public function get_sql() {
    if ( empty( $this->columns_to_select ) ) {
      // this should be the default behavor
      $this->columns_to_select = [ '*' ];
    }

    $from   = ' FROM ' . $this->from . ' ';

    if ( ! empty( ltrim( $this->joins ) ) ) {
      $from  .= ltrim( $this->joins );
    }

    $from  .= 'WHERE ' . $this->where_clause;
    $select = 'SELECT ' . implode( ', ', $this->columns_to_select ) . $from;

    if ( ! empty( $this->group_by ) ) {
      $select .= 'GROUP BY ' . implode( ', ', $this->group_by ) . ' ';
    }

    if ( $this->order_by !== '' ) {
      $select .= 'ORDER BY ' . $this->order_by . ' ';
    }

    $select .= 'LIMIT ' . $this->limit;
    return $select;
  }

  /**
   * Calls get_sql() to generate the sql and then creates the prepared statement
   * and binds its values.
   *
   * @param $db : a pdo database connection
   * @return pdo::Statement object with the query and values bound
   */
  public function get_statement( $db ) {
    $statement = $db->prepare( $this->get_sql() );
    foreach ( $this->values_to_bind as $name => $value ) {
      if ( is_array( $value ) && 2 === count( $value ) ) {
        $statement->bindValue( $name, $value[0], $value[1] );
      } else {
        $statement->bindValue( $name, $value );
      };
    }
    return $statement;
  }
}

{'fraction_non_alphanumeric': 0.14262658866249747, 'fraction_numerical': 0.0016138793625176519, 'mean_word_length': 2.409559834938102, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3285725', 'n_tokens_mistral': 3316, 'n_tokens_neox': 3242, 'n_words': 977}