<commit_before>/*

   Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.metadata_test

   Copyright 1999, 2004 The Apache Software Foundation or its licensors, as applicable.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */

package org.apache.derbyTesting.functionTests.tests.jdbcapi;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.sql.Timestamp;
import java.sql.Time;
import java.sql.Date;
import java.math.BigDecimal;

import java.util.Properties;
import java.util.StringTokenizer;

import org.apache.derby.tools.ij;
import org.apache.derbyTesting.functionTests.util.TestUtil;

/**
 * Test of database meta-data.  This program simply calls each of the meta-data
 * methods, one by one, and prints the results.  The test passes if the printed
 * results match a previously stored "master".  Thus this test cannot actually
 * discern whether it passes or not.
 *
 */

public abstract class metadata_test {

	// Ids for the Derby internal procedures that are used to fetch
	// some of the metadata.

	protected static final int GET_PROCEDURES = 0;
 	protected static final int GET_PROCEDURE_COLUMNS = 1;
 	protected static final int GET_TABLES = 2;
 	protected static final int GET_COLUMNS = 3;
 	protected static final int GET_COLUMN_PRIVILEGES = 5;
 	protected static final int GET_TABLE_PRIVILEGES = 6;
 	protected static final int GET_BEST_ROW_IDENTIFIER = 7;
 	protected static final int GET_VERSION_COLUMNS = 8;
 	protected static final int GET_PRIMARY_KEYS = 9;
 	protected static final int GET_IMPORTED_KEYS = 10;
 	protected static final int GET_EXPORTED_KEYS = 11;
 	protected static final int GET_CROSS_REFERENCE = 12;
 	protected static final int GET_TYPE_INFO = 13;
 	protected static final int GET_INDEX_INFO = 14;

	protected static final int IGNORE_PROC_ID = -1;
	
	//Used for JSR169
	private static boolean HAVE_DRIVER_CLASS;
	static{
		try{
			Class.forName("java.sql.Driver");
			HAVE_DRIVER_CLASS = true;
		}
		catch(ClassNotFoundException e){
			//Used for JSR169
			HAVE_DRIVER_CLASS = false;
		}
	}

	// We leave it up to the classes which extend this one to
	// initialize the following fields at construct time.
	public Connection con;
	public static Statement s;
	
	/*
	** Escaped function testing
	*/
	private static final String[][] NUMERIC_FUNCTIONS =
	{
		// Section C.1 JDBC 3.0 spec.
		{ "ABS", "-25.67" },
		{ "ACOS", "0.0707" },
		{ "ASIN", "0.997" },
		{ "ATAN", "14.10" },
		{ "ATAN2", "0.56", "1.2" },
		{ "CEILING", "3.45" },
		{ "COS", "1.2" },
		{ "COT", "3.4" },
		{ "DEGREES", "2.1" },
		{ "EXP", "2.3" },
		{ "FLOOR", "3.22" },
		{ "LOG", "34.1" },
		{ "LOG10", "18.7" },
		{ "MOD", "124", "7" },
		{ "PI" },
		{ "POWER", "2", "3" },
		{ "RADIANS", "54" },
		{ "RAND", "17" }, 
		{ "ROUND", "345.345", "1" }, 
		{ "SIGN", "-34" },
		{ "SIN", "0.32" },
		{ "SQRT", "6.22" },
		{ "TAN", "0.57", },
		{ "TRUNCATE", "345.395", "1" }
	};
	
	private static final String[][] TIMEDATE_FUNCTIONS =
	{	
		// Section C.3 JDBC 3.0 spec.
		{ "CURDATE" },
		{ "CURTIME" },
		{ "DAYNAME", "{d '1995-12-19'h}" },
		{ "DAYOFMONTH", "{d '1995-12-19'}" },
		{ "DAYOFWEEK", "{d '1995-12-19'}" },
		{ "DAYOFYEAR", "{d '1995-12-19'}" },
		{ "HOUR", "{t '16:13:03'}" },
		{ "MINUTE", "{t '16:13:03'}" },
		{ "MONTH", "{d '1995-12-19'}" },
		{ "MONTHNAME", "{d '1995-12-19'}" },
		{ "NOW" },
		{ "QUARTER", "{d '1995-12-19'}" },
		{ "SECOND", "{t '16:13:03'}" },
		{ "TIMESTAMPADD", "SQL_TSI_DAY", "7", "{ts '1995-12-19 12:15:54'}" },
		{ "TIMESTAMPDIFF", "SQL_TSI_DAY", "{ts '1995-12-19 12:15:54'}", "{ts '1997-11-02 00:15:23'}" },
		{ "WEEK", "{d '1995-12-19'}" },
		{ "YEAR", "{d '1995-12-19'}" },
		
	};

	private static final String[][] SYSTEM_FUNCTIONS =
	{	
		// Section C.4 JDBC 3.0 spec.
		{ "DATABASE" },
		{ "IFNULL", "'this'", "'that'" },
		{ "USER"},
		};	
	
	private static final String[][] STRING_FUNCTIONS =
	{	
		// Section C.2 JDBC 3.0 spec.
		{ "ASCII" , "'Yellow'" },
		{ "CHAR", "65" },
		{ "CONCAT", "'hello'", "'there'" },
		{ "DIFFERENCE", "'Pires'", "'Piers'" },
		{ "INSERT", "'<NAME>'", "4", "'William'" },
		{ "LCASE", "'<NAME>'" },
		{ "LEFT", "'Bonjour'", "3" },
		{ "LENGTH", "'four    '" } ,
		{ "LOCATE", "'jour'", "'Bonjour'" },
		{ "LTRIM", "'   left trim   '"},
		{ "REPEAT", "'echo'", "3" },
		{ "REPLACE", "'to be or not to be'", "'be'", "'England'" },
		{ "RTRIM", "'  right trim   '"},
		{ "SOUNDEX", "'Derby'" },
		{ "SPACE", "12"},
		{ "SUBSTRING", "'Ruby the Rubicon Jeep'", "10", "7", },
		{ "UCASE", "'<NAME>'" }
		};

	public void runTest() {

		DatabaseMetaData met;
		ResultSet rs;
		ResultSetMetaData rsmet;

		System.out.println("Test metadata starting");

		try
		{

			// test decimal type and other numeric types precision, scale,
			// and display width after operations, beetle 3875, 3906
			s.execute("create table t (i int, s smallint, r real, "+
				"d double precision, dt date, t time, ts timestamp, "+
				"c char(10), v varchar(40) not null, dc dec(10,2))");
			s.execute("insert into t values(1,2,3.3,4.4,date('1990-05-05'),"+
						 "time('12:06:06'),timestamp('1990-07-07 07:07:07.07'),"+
						 "'eight','nine', 11.1)");

			// test decimal type and other numeric types precision, scale,
			// and display width after operations, beetle 3875, 3906
			//rs = s.executeQuery("select dc from t where tn = 10 union select dc from t where i = 1");
			rs = s.executeQuery("select dc from t where dc = 11.1 union select dc from t where i = 1");
			rsmet = rs.getMetaData();
			metadata_test.showNumericMetaData("Union Result", rsmet, 1);
			rs.close();

			rs = s.executeQuery("select dc, r, d, r+dc, d-dc, dc-d from t");
			rsmet = rs.getMetaData();<commit_msg>DERBY-1076  Test update to cleanup metadata tests in preparation for upgrade testing work.<commit_after>/*

   Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.metadata_test

   Copyright 1999, 2004 The Apache Software Foundation or its licensors, as applicable.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */

package org.apache.derbyTesting.functionTests.tests.jdbcapi;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.sql.Timestamp;
import java.sql.Time;
import java.sql.Date;
import java.math.BigDecimal;

import java.util.Properties;
import java.util.StringTokenizer;

import org.apache.derby.tools.ij;
import org.apache.derbyTesting.functionTests.util.TestUtil;

/**
 * Test of database meta-data.  This program simply calls each of the meta-data
 * methods, one by one, and prints the results.  The test passes if the printed
 * results match a previously stored "master".  Thus this test cannot actually
 * discern whether it passes or not.
 *
 */

public abstract class metadata_test {

	// Ids for the Derby internal procedures that are used to fetch
	// some of the metadata.

	protected static final int GET_PROCEDURES = 0;
 	protected static final int GET_PROCEDURE_COLUMNS = 1;
 	protected static final int GET_TABLES = 2;
 	protected static final int GET_COLUMNS = 3;
 	protected static final int GET_COLUMN_PRIVILEGES = 5;
 	protected static final int GET_TABLE_PRIVILEGES = 6;
 	protected static final int GET_BEST_ROW_IDENTIFIER = 7;
 	protected static final int GET_VERSION_COLUMNS = 8;
 	protected static final int GET_PRIMARY_KEYS = 9;
 	protected static final int GET_IMPORTED_KEYS = 10;
 	protected static final int GET_EXPORTED_KEYS = 11;
 	protected static final int GET_CROSS_REFERENCE = 12;
 	protected static final int GET_TYPE_INFO = 13;
 	protected static final int GET_INDEX_INFO = 14;

	protected static final int IGNORE_PROC_ID = -1;
	
	//Used for JSR169
	private static boolean HAVE_DRIVER_CLASS;
	static{
		try{
			Class.forName("java.sql.Driver");
			HAVE_DRIVER_CLASS = true;
		}
		catch(ClassNotFoundException e){
			//Used for JSR169
			HAVE_DRIVER_CLASS = false;
		}
	}

	// We leave it up to the classes which extend this one to
	// initialize the following fields at construct time.
	public Connection con;
	public static Statement s;
	
	/*
	** Escaped function testing
	*/
	private static final String[][] NUMERIC_FUNCTIONS =
	{
		// Section C.1 JDBC 3.0 spec.
		{ "ABS", "-25.67" },
		{ "ACOS", "0.0707" },
		{ "ASIN", "0.997" },
		{ "ATAN", "14.10" },
		{ "ATAN2", "0.56", "1.2" },
		{ "CEILING", "3.45" },
		{ "COS", "1.2" },
		{ "COT", "3.4" },
		{ "DEGREES", "2.1" },
		{ "EXP", "2.3" },
		{ "FLOOR", "3.22" },
		{ "LOG", "34.1" },
		{ "LOG10", "18.7" },
		{ "MOD", "124", "7" },
		{ "PI" },
		{ "POWER", "2", "3" },
		{ "RADIANS", "54" },
		{ "RAND", "17" }, 
		{ "ROUND", "345.345", "1" }, 
		{ "SIGN", "-34" },
		{ "SIN", "0.32" },
		{ "SQRT", "6.22" },
		{ "TAN", "0.57", },
		{ "TRUNCATE", "345.395", "1" }
	};
	
	private static final String[][] TIMEDATE_FUNCTIONS =
	{	
		// Section C.3 JDBC 3.0 spec.
		{ "CURDATE" },
		{ "CURTIME" },
		{ "DAYNAME", "{d '1995-12-19'h}" },
		{ "DAYOFMONTH", "{d '1995-12-19'}" },
		{ "DAYOFWEEK", "{d '1995-12-19'}" },
		{ "DAYOFYEAR", "{d '1995-12-19'}" },
		{ "HOUR", "{t '16:13:03'}" },
		{ "MINUTE", "{t '16:13:03'}" },
		{ "MONTH", "{d '1995-12-19'}" },
		{ "MONTHNAME", "{d '1995-12-19'}" },
		{ "NOW" },
		{ "QUARTER", "{d '1995-12-19'}" },
		{ "SECOND", "{t '16:13:03'}" },
		{ "TIMESTAMPADD", "SQL_TSI_DAY", "7", "{ts '1995-12-19 12:15:54'}" },
		{ "TIMESTAMPDIFF", "SQL_TSI_DAY", "{ts '1995-12-19 12:15:54'}", "{ts '1997-11-02 00:15:23'}" },
		{ "WEEK", "{d '1995-12-19'}" },
		{ "YEAR", "{d '1995-12-19'}" },
		
	};

	private static final String[][] SYSTEM_FUNCTIONS =
	{	
		// Section C.4 JDBC 3.0 spec.
		{ "DATABASE" },
		{ "IFNULL", "'this'", "'that'" },
		{ "USER"},
		};	
	
	private static final String[][] STRING_FUNCTIONS =
	{	
		// Section C.2 JDBC 3.0 spec.
		{ "ASCII" , "'Yellow'" },
		{ "CHAR", "65" },
		{ "CONCAT", "'hello'", "'there'" },
		{ "DIFFERENCE", "'Pires'", "'Piers'" },
		{ "INSERT", "'<NAME>'", "4", "'William'" },
		{ "LCASE", "'<NAME>'" },
		{ "LEFT", "'Bonjour'", "3" },
		{ "LENGTH", "'four    '" } ,
		{ "LOCATE", "'jour'", "'Bonjour'" },
		{ "LTRIM", "'   left trim   '"},
		{ "REPEAT", "'echo'", "3" },
		{ "REPLACE", "'to be or not to be'", "'be'", "'England'" },
		{ "RTRIM", "'  right trim   '"},
		{ "SOUNDEX", "'Derby'" },
		{ "SPACE", "12"},
		{ "SUBSTRING", "'Ruby the Rubicon Jeep'", "10", "7", },
		{ "UCASE", "'<NAME>'" }
		};

	public void runTest() {

		DatabaseMetaData met;
		ResultSet rs;
		ResultSetMetaData rsmet;

		System.out.println("Test metadata starting");

		try
		{
			//Cleanup any leftover database objects from previous test run
			cleanUp(s);

			// test decimal type and other numeric types precision, scale,
			// and display width after operations, beetle 3875, 3906
			s.execute("create table t (i int, s smallint, r real, "+
				"d double precision, dt date, t time, ts timestamp, "+
				"c char(10), v varchar(40) not null, dc dec(10,2))");
			s.execute("insert into t values(1,2,3.3,4.4,date('1990-05-05'),"+
						 "time('12:06:06'),timestamp('1990-07-07 07:07:07.07'),"+
						 "'eight','nine', 11.1)");

			// test decimal type and other numeric types precision, scale,
			// and display width after operations, beetle 3875, 3906
			//rs = s.executeQuery("select dc from t where tn = 10 union select dc from t where i = 1");
			rs = s.executeQuery("select dc from t where dc = 11.1 union select dc from t where i = 1");
			rsmet = rs.getMetaData();
			metadata_test.showNumericMetaData("Union Result", rsmet, 1);
			rs.close();

			rs = s.executeQuery("select dc, r, d, r+dc, d-dc, dc-d from t");
			rsmet = rs.getMetaData();
{'fraction_non_alphanumeric': 0.17311530784584678, 'fraction_numerical': 0.05297098111469369, 'mean_word_length': 3.495169082125604, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '683294', 'n_tokens_mistral': 5431, 'n_tokens_neox': 4694, 'n_words': 1361}