<commit_before>/*
 * This code was created by <NAME> (mostly during Christmas 98/99).
 * This code is LGPL. Please ensure that this message remains in future
 * distributions and uses of this code (thats about all I get out of it).
 * - <NAME> <EMAIL>
 * 
 * This file has been modified for the MonetDB project.  See the file
 * Copyright in this directory for more information.
 */

/**********************************************************************
 * SQLPrimaryKeys()
 * CLI Compliance: ODBC (Microsoft)
 *
 * Note: catalogs are not supported, we ignore any value set for
 * szCatalogName.
 *
 * Author: <NAME>
 * Date  : 30 aug 2002
 *
 **********************************************************************/

#include "ODBCGlobal.h"
#include "ODBCStmt.h"
#include "ODBCUtil.h"

static SQLRETURN
SQLPrimaryKeys_(ODBCStmt *stmt,
		SQLCHAR *szCatalogName, SQLSMALLINT nCatalogNameLength,
		SQLCHAR *szSchemaName, SQLSMALLINT nSchemaNameLength,
		SQLCHAR *szTableName, SQLSMALLINT nTableNameLength)
{
	RETCODE rc;

	/* buffer for the constructed query to do meta data retrieval */
	char *query = NULL;
	char *query_end = NULL;	/* pointer to end of built-up query */

	/* deal with SQL_NTS and SQL_NULL_DATA */
	fixODBCstring(szCatalogName, nCatalogNameLength, addStmtError, stmt);
	fixODBCstring(szSchemaName, nSchemaNameLength, addStmtError, stmt);
	fixODBCstring(szTableName, nTableNameLength, addStmtError, stmt);

	/* check if a valid (non null, not empty) table name is supplied */
	if (szTableName == NULL) {
		/* Invalid use of null pointer */
		addStmtError(stmt, "HY009", NULL, 0);
		return SQL_ERROR;
	}

#ifdef ODBCDEBUG
	ODBCLOG("\"%.*s\" \"%.*s\" \"%.*s\"\n",
		nCatalogNameLength, szCatalogName,
		nSchemaNameLength, szSchemaName,
		nTableNameLength, szTableName);
#endif

	/* construct the query */
	query = (char *) malloc(1000 + nTableNameLength + nSchemaNameLength);
	assert(query);
	query_end = query;

	/* SQLPrimaryKeys returns a table with the following columns:
	   VARCHAR	table_cat
	   VARCHAR	table_schem
	   VARCHAR	table_name NOT NULL
	   VARCHAR	column_name NOT NULL
	   SMALLINT	key_seq NOT NULL
	   VARCHAR	pk_name
	 */
	strcpy(query_end,
	       "select "
	       "cast(null as varchar) as table_cat, "
	       "cast(s.\"name\" as varchar) as table_schem, "
	       "cast(t.\"name\" as varchar) as table_name, "
	       "cast(kc.\"column\" as varchar) as column_name, "
	       "cast(k.\"type\" + 1 as smallint) as key_seq, "
	       "cast(k.\"name\" as varchar) as pk_name "
	       "from sys.\"schemas\" s, sys.\"tables\" t, "
	       "sys.\"keys\" k, sys.\"keycolumns\" kc "
	       "where k.\"id\" = kc.\"id\" and "
	       "k.\"table_id\" = t.\"id\" and "
	       "t.\"schema_id\" = s.\"id\" and "
	       "k.\"type\" = 0");
	query_end += strlen(query_end);

	/* Construct the selection condition query part */
	/* search pattern is not allowed for table name so use = and not LIKE */
	sprintf(query_end, " and t.\"name\" = '%.*s'",
		nTableNameLength, szTableName);
	query_end += strlen(query_end);

	if (szSchemaName != NULL) {
		/* filtering requested on schema name */
		/* search pattern is not allowed so use = and not LIKE */
		sprintf(query_end, " and s.\"name\" = '%.*s'",
			nSchemaNameLength, szSchemaName);
		query_end += strlen(query_end);
	}

	/* add the ordering */<commit_msg><NAME>: Changed the query such that key_seq gets its value from sys.keycolumns.nr instead of sys.keys.type.<commit_after>/*
 * This code was created by <NAME> (mostly during Christmas 98/99).
 * This code is LGPL. Please ensure that this message remains in future
 * distributions and uses of this code (thats about all I get out of it).
 * - <NAME> <EMAIL>
 * 
 * This file has been modified for the MonetDB project.  See the file
 * Copyright in this directory for more information.
 */

/**********************************************************************
 * SQLPrimaryKeys()
 * CLI Compliance: ODBC (Microsoft)
 *
 * Note: catalogs are not supported, we ignore any value set for
 * szCatalogName.
 *
 * Author: <NAME>
 * Date  : 30 aug 2002
 *
 **********************************************************************/

#include "ODBCGlobal.h"
#include "ODBCStmt.h"
#include "ODBCUtil.h"

static SQLRETURN
SQLPrimaryKeys_(ODBCStmt *stmt,
		SQLCHAR *szCatalogName, SQLSMALLINT nCatalogNameLength,
		SQLCHAR *szSchemaName, SQLSMALLINT nSchemaNameLength,
		SQLCHAR *szTableName, SQLSMALLINT nTableNameLength)
{
	RETCODE rc;

	/* buffer for the constructed query to do meta data retrieval */
	char *query = NULL;
	char *query_end = NULL;	/* pointer to end of built-up query */

	/* deal with SQL_NTS and SQL_NULL_DATA */
	fixODBCstring(szCatalogName, nCatalogNameLength, addStmtError, stmt);
	fixODBCstring(szSchemaName, nSchemaNameLength, addStmtError, stmt);
	fixODBCstring(szTableName, nTableNameLength, addStmtError, stmt);

	/* check if a valid (non null, not empty) table name is supplied */
	if (szTableName == NULL) {
		/* Invalid use of null pointer */
		addStmtError(stmt, "HY009", NULL, 0);
		return SQL_ERROR;
	}

#ifdef ODBCDEBUG
	ODBCLOG("\"%.*s\" \"%.*s\" \"%.*s\"\n",
		nCatalogNameLength, szCatalogName,
		nSchemaNameLength, szSchemaName,
		nTableNameLength, szTableName);
#endif

	/* construct the query */
	query = (char *) malloc(1000 + nTableNameLength + nSchemaNameLength);
	assert(query);
	query_end = query;

	/* SQLPrimaryKeys returns a table with the following columns:
	   VARCHAR	table_cat
	   VARCHAR	table_schem
	   VARCHAR	table_name NOT NULL
	   VARCHAR	column_name NOT NULL
	   SMALLINT	key_seq NOT NULL
	   VARCHAR	pk_name
	 */
	strcpy(query_end,
	       "select "
	       "cast(null as varchar) as table_cat, "
	       "cast(s.\"name\" as varchar) as table_schem, "
	       "cast(t.\"name\" as varchar) as table_name, "
	       "cast(kc.\"column\" as varchar) as column_name, "
	       "cast(kc.\"nr\" + 1 as smallint) as key_seq, "
	       "cast(k.\"name\" as varchar) as pk_name "
	       "from sys.\"schemas\" s, sys.\"tables\" t, "
	       "sys.\"keys\" k, sys.\"keycolumns\" kc "
	       "where k.\"id\" = kc.\"id\" and "
	       "k.\"table_id\" = t.\"id\" and "
	       "t.\"schema_id\" = s.\"id\" and "
	       "k.\"type\" = 0");
	query_end += strlen(query_end);

	/* Construct the selection condition query part */
	/* search pattern is not allowed for table name so use = and not LIKE */
	sprintf(query_end, " and t.\"name\" = '%.*s'",
		nTableNameLength, szTableName);
	query_end += strlen(query_end);

	if (szSchemaName != NULL) {
		/* filtering requested on schema name */
		/* search pattern is not allowed so use = and not LIKE */
		sprintf(query_end, " and s.\"name\" = '%.*s'",
			nSchemaNameLength, szSchemaName);
		query_end += strlen(query_end);
	}

	/* add the ordering */
{'fraction_non_alphanumeric': 0.16590270589974862, 'fraction_numerical': 0.005914534969688009, 'mean_word_length': 3.503328894806924, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '6629422', 'n_tokens_mistral': 2312, 'n_tokens_neox': 2141, 'n_words': 676}