<commit_before>/*
  Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
                2013 MontyProgram AB

  The MySQL Connector/ODBC is licensed under the terms of the GPLv2
  <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
  MySQL Connectors. There are special exceptions to the terms and
  conditions of the GPLv2 as it is applied to this software, see the
  FLOSS License Exception
  <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; version 2 of the License.
  
  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. See the GNU General Public License
  for more details.
  
  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
*/

#include "tap.h"

/* result set demo */
ODBC_TEST(my_resultset)
{
  SQLRETURN   rc;
  SQLUINTEGER nRowCount=0;
  SQLULEN     pcColDef;
  SQLCHAR     szColName[MAX_NAME_LEN+1];
  SQLCHAR     szData[MAX_ROW_DATA_LEN+1];
  SQLSMALLINT nIndex,ncol,pfSqlType, pcbScale, pfNullable;

  /* drop table 'myodbc3_demo_result' if it already exists */
  OK_SIMPLE_STMT(Stmt, "DROP TABLE if exists myodbc3_demo_result");

  /* commit the transaction */
  rc = SQLEndTran(SQL_HANDLE_DBC, Connection, SQL_COMMIT);
  CHECK_DBC_RC(Connection,rc);

  /* create the table 'myodbc3_demo_result' */
  OK_SIMPLE_STMT(Stmt,"CREATE TABLE myodbc3_demo_result("
          "id int primary key auto_increment,name varchar(20))");

  rc = SQLEndTran(SQL_HANDLE_DBC, Connection, SQL_COMMIT);
  CHECK_DBC_RC(Connection,rc);

  /* insert 2 rows of data */
  OK_SIMPLE_STMT(Stmt, "INSERT INTO myodbc3_demo_result values(1,'MySQL')");
  OK_SIMPLE_STMT(Stmt, "INSERT INTO myodbc3_demo_result values(2,'MyODBC')");

  /* commit the transaction */
  rc = SQLEndTran(SQL_HANDLE_DBC, Connection, SQL_COMMIT);
  CHECK_DBC_RC(Connection,rc);

  /* update second row */
  OK_SIMPLE_STMT(Stmt, "UPDATE myodbc3_demo_result set name="
          "'MyODBC 3.51' where id=2");

  /* commit the transaction */
  rc = SQLEndTran(SQL_HANDLE_DBC, Connection, SQL_COMMIT);
  CHECK_DBC_RC(Connection,rc);

  /* now fetch back..*/
  OK_SIMPLE_STMT(Stmt, "SELECT * from myodbc3_demo_result");

  /* get total number of columns from the resultset */
  rc = SQLNumResultCols(Stmt,&ncol);
  CHECK_STMT_RC(Stmt,rc);

  diag("total columns in resultset:%d",ncol);

  /* print the column names  and do the row bind */
  for (nIndex = 1; nIndex <= ncol; nIndex++)
  {
      rc = SQLDescribeCol(Stmt,nIndex,szColName, MAX_NAME_LEN+1, NULL,
                          &pfSqlType,&pcColDef,&pcbScale,&pfNullable);
      CHECK_STMT_RC(Stmt,rc);

      printf("%s\t",szColName);

  }
  printf("\n");

  /* now fetch row by row */
  rc = SQLFetch(Stmt);
  while (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
  {
      nRowCount++;
      for (nIndex=1; nIndex<= ncol; nIndex++)
      {
          rc = SQLGetData(Stmt,nIndex, SQL_C_CHAR, szData,
                          MAX_ROW_DATA_LEN,NULL);
          CHECK_STMT_RC(Stmt,rc);
          printf("%s\t",szData);
      }

      printf("\n");
      rc = SQLFetch(Stmt);
  }
  SQLFreeStmt(Stmt,SQL_UNBIND);

  diag("total rows fetched:%d",nRowCount);

  /* free the statement row bind resources */
  rc = SQLFreeStmt(Stmt, SQL_UNBIND);
  CHECK_STMT_RC(Stmt,rc);

  /* free the statement cursor */
  rc = SQLFreeStmt(Stmt, SQL_CLOSE);
  CHECK_STMT_RC(Stmt,rc);

  OK_SIMPLE_STMT(Stmt, "DROP TABLE myodbc3_demo_result");

  return OK;
}


/* To test a convertion type */
ODBC_TEST(t_convert_type)
{
  SQLRETURN   rc;
  SQLSMALLINT SqlType, DateType;
  SQLCHAR     ColName[MAX_NAME_LEN];
  SQLCHAR     DbVersion[MAX_NAME_LEN];
  SQLINTEGER  OdbcVersion;

    rc = SQLGetEnvAttr(Env,SQL_ATTR_ODBC_VERSION,&OdbcVersion,0,NULL);
    CHECK_ENV_RC(Env,rc);

    fprintf(stdout,"# odbc version:");
    if (OdbcVersion == SQL_OV_ODBC2)
    {
      fprintf(stdout," SQL_OV_ODBC2\n");
      DateType= SQL_DATE;
    }
    else
    {
      fprintf(stdout," SQL_OV_ODBC3\n");
      DateType= SQL_TYPE_DATE;
    }

    rc = SQLGetInfo(Connection,SQL_DBMS_VER,DbVersion,MAX_NAME_LEN,NULL);
    CHECK_DBC_RC(Connection,rc);

  OK_SIMPLE_STMT(Stmt, "DROP TABLE IF EXISTS t_convert");

  OK_SIMPLE_STMT(Stmt, "CREATE TABLE t_convert(col0 int, col1 date, col2 char(10))");

  OK_SIMPLE_STMT(Stmt, "INSERT INTO t_convert VALUES(10,'2002-10-24','venu')");
  OK_SIMPLE_STMT(Stmt, "INSERT INTO t_convert VALUES(20,'2002-10-23','venu1')");
  OK_SIMPLE_STMT(Stmt, "INSERT INTO t_convert VALUES(30,'2002-10-25','venu2')");<commit_msg>Small test enhancement to check if correct length<commit_after>/*
  Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
                2013, 2018 MariaDB Corporation A

  The MySQL Connector/ODBC is licensed under the terms of the GPLv2
  <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
  MySQL Connectors. There are special exceptions to the terms and
  conditions of the GPLv2 as it is applied to this software, see the
  FLOSS License Exception
  <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; version 2 of the License.
  
  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. See the GNU General Public License
  for more details.
  
  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
*/

#include "tap.h"

/* result set demo */
ODBC_TEST(my_resultset)
{
  SQLRETURN   rc;
  SQLUINTEGER nRowCount=0;
  SQLULEN     pcColDef;
  SQLCHAR     szColName[MAX_NAME_LEN+1];
  SQLCHAR     szData[MAX_ROW_DATA_LEN+1];
  SQLSMALLINT nIndex,ncol,pfSqlType, pcbScale, pfNullable;

  /* drop table 'myodbc3_demo_result' if it already exists */
  OK_SIMPLE_STMT(Stmt, "DROP TABLE if exists myodbc3_demo_result");

  /* commit the transaction */
  rc = SQLEndTran(SQL_HANDLE_DBC, Connection, SQL_COMMIT);
  CHECK_DBC_RC(Connection,rc);

  /* create the table 'myodbc3_demo_result' */
  OK_SIMPLE_STMT(Stmt,"CREATE TABLE myodbc3_demo_result("
          "id int primary key auto_increment,name varchar(20))");

  rc = SQLEndTran(SQL_HANDLE_DBC, Connection, SQL_COMMIT);
  CHECK_DBC_RC(Connection,rc);

  /* insert 2 rows of data */
  OK_SIMPLE_STMT(Stmt, "INSERT INTO myodbc3_demo_result values(1,'MySQL')");
  OK_SIMPLE_STMT(Stmt, "INSERT INTO myodbc3_demo_result values(2,'MyODBC')");

  /* commit the transaction */
  rc = SQLEndTran(SQL_HANDLE_DBC, Connection, SQL_COMMIT);
  CHECK_DBC_RC(Connection,rc);

  /* update second row */
  OK_SIMPLE_STMT(Stmt, "UPDATE myodbc3_demo_result set name="
          "'MyODBC 3.51' where id=2");

  /* commit the transaction */
  rc = SQLEndTran(SQL_HANDLE_DBC, Connection, SQL_COMMIT);
  CHECK_DBC_RC(Connection,rc);

  /* now fetch back..*/
  OK_SIMPLE_STMT(Stmt, "SELECT * from myodbc3_demo_result");

  /* get total number of columns from the resultset */
  rc = SQLNumResultCols(Stmt,&ncol);
  CHECK_STMT_RC(Stmt,rc);

  diag("total columns in resultset:%d",ncol);

  /* print the column names  and do the row bind */
  for (nIndex = 1; nIndex <= ncol; nIndex++)
  {
      rc = SQLDescribeCol(Stmt,nIndex,szColName, MAX_NAME_LEN+1, NULL,
                          &pfSqlType,&pcColDef,&pcbScale,&pfNullable);
      CHECK_STMT_RC(Stmt,rc);

      printf("%s\t",szColName);

  }
  printf("\n");

  /* now fetch row by row */
  rc = SQLFetch(Stmt);
  while (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
  {
      nRowCount++;
      for (nIndex=1; nIndex<= ncol; nIndex++)
      {
          rc = SQLGetData(Stmt,nIndex, SQL_C_CHAR, szData,
                          MAX_ROW_DATA_LEN,NULL);
          CHECK_STMT_RC(Stmt,rc);
          printf("%s\t",szData);
      }

      printf("\n");
      rc = SQLFetch(Stmt);
  }
  SQLFreeStmt(Stmt,SQL_UNBIND);

  diag("total rows fetched:%d",nRowCount);

  /* free the statement row bind resources */
  rc = SQLFreeStmt(Stmt, SQL_UNBIND);
  CHECK_STMT_RC(Stmt,rc);

  /* free the statement cursor */
  rc = SQLFreeStmt(Stmt, SQL_CLOSE);
  CHECK_STMT_RC(Stmt,rc);

  OK_SIMPLE_STMT(Stmt, "DROP TABLE myodbc3_demo_result");

  return OK;
}


/* To test a convertion type */
ODBC_TEST(t_convert_type)
{
  SQLRETURN   rc;
  SQLSMALLINT SqlType, DateType, Length;
  SQLCHAR     ColName[MAX_NAME_LEN];
  SQLCHAR     DbVersion[MAX_NAME_LEN];
  SQLINTEGER  OdbcVersion;

  rc = SQLGetEnvAttr(Env,SQL_ATTR_ODBC_VERSION,&OdbcVersion,0,NULL);
  CHECK_ENV_RC(Env,rc);

  fprintf(stdout,"# odbc version:");
  if (OdbcVersion == SQL_OV_ODBC2)
  {
    fprintf(stdout," SQL_OV_ODBC2\n");
    DateType= SQL_DATE;
  }
  else
  {
    fprintf(stdout," SQL_OV_ODBC3\n");
    DateType= SQL_TYPE_DATE;
  }

  rc = SQLGetInfo(Connection,SQL_DBMS_VER,DbVersion,MAX_NAME_LEN, &Length);
  CHECK_DBC_RC(Connection,rc);
  diag("SQL_DBMS_VER: %s", DbVersion);
  is_num(Length, strlen(DbVersion));

  OK_SIMPLE_STMT(Stmt, "DROP TABLE IF EXISTS t_convert");

  OK_SIMPLE_STMT(Stmt, "CREATE TABLE t_convert(col0 int, col1 date, col2 char(10))");

  OK_SIMPLE_STMT(Stmt, "INSERT INTO t_convert VALUES(10,'2002-10-24','venu')");
  OK_SIMPLE_STMT(Stmt, "INSERT INTO t_convert VALUES(20,'2002-10-23','venu1')");
  OK_SIMPLE_STMT(Stmt, "INSERT INTO t_convert VALUES(30,'2002-10-25','venu2')");
{'fraction_non_alphanumeric': 0.11227754449110178, 'fraction_numerical': 0.01899620075984803, 'mean_word_length': 3.8253738543174145, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '1494166', 'n_tokens_mistral': 3691, 'n_tokens_neox': 3538, 'n_words': 879}