1818
1919import static org .junit .jupiter .api .Assertions .assertNotNull ;
2020
21+ import com .google .cloud .ServiceOptions ;
22+ import com .google .cloud .bigquery .BigQuery ;
23+ import com .google .cloud .bigquery .BigQueryOptions ;
24+ import com .google .cloud .bigquery .QueryJobConfiguration ;
2125import com .google .cloud .bigquery .jdbc .BigQueryJdbcBaseTest ;
26+ import java .sql .Connection ;
2227import java .sql .ResultSet ;
2328import java .sql .SQLException ;
29+ import java .sql .Statement ;
30+ import java .util .ArrayList ;
31+ import java .util .List ;
2432
2533public class ITBase extends BigQueryJdbcBaseTest {
2634
35+ public static final String DEFAULT_CATALOG = ServiceOptions .getDefaultProjectId ();
36+ public static String connectionUrl =
37+ "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;ProjectId="
38+ + DEFAULT_CATALOG
39+ + ";OAuthType=3;Timeout=3600;" ;
40+
41+ public static final String createDatasetQuery =
42+ "CREATE SCHEMA IF NOT EXISTS `%s.%s` OPTIONS(default_table_expiration_days = 5)" ;
43+ public static final String dropSchema = "DROP SCHEMA IF EXISTS `%s.%s` CASCADE;" ;
44+ public static final String createTableQuery =
45+ "CREATE OR REPLACE TABLE "
46+ + " `%s.%s.%s` "
47+ + " (\n "
48+ + "`StringField` STRING,\n "
49+ + "`BytesField` BYTES,\n "
50+ + "`IntegerField` INTEGER,\n "
51+ + "`FloatField` FLOAT64,\n "
52+ + "`NumericField` NUMERIC,\n "
53+ + "`BigNumericField` BIGNUMERIC,\n "
54+ + "`BooleanField` BOOLEAN,\n "
55+ + "`TimestampField` TIMESTAMP,\n "
56+ + "`DateField` DATE,\n "
57+ + "`TimeField` TIME,\n "
58+ + "`DateTimeField` DATETIME,\n "
59+ + "`GeographyField` GEOGRAPHY,\n "
60+ + "`RecordField` STRUCT<Name STRING, Age INT64>,\n "
61+ + "`JsonField` JSON,\n "
62+ + ");" ;
63+ public static final String insertQuery1 =
64+ "INSERT INTO "
65+ + " `%s.%s.%s` "
66+ + " (\n "
67+ + "StringField, BytesField,IntegerField,FloatField,NumericField,BigNumericField,BooleanField,\n "
68+ + "TimestampField,DateField,TimeField,DateTimeField,GeographyField,RecordField,JsonField )\n "
69+ + "VALUES('string1',CAST ('string1' AS BYTES),111,1.1, CAST('11.1E11' AS NUMERIC), \n "
70+ + "CAST('1.1E37' AS BIGNUMERIC), TRUE,CAST('2001-05-1 8:05:01' AS TIMESTAMP), \n "
71+ + "CAST('2001-05-1' AS DATE),CAST('5:1:11.041' AS TIME), CAST('2001-05-1 11:31:45' AS DATETIME), \n "
72+ + "CAST(ST_GEOGFROMTEXT('POINT(1.500989010415034 -1.11471081311336843)') AS GEOGRAPHY), \n "
73+ + "CAST(('name1', 1) AS STRUCT<STRING,INT64>), \n "
74+ + " JSON \" \" \" {\n "
75+ + " \" name\" : \" Alice1\" ,\n "
76+ + " \" items\" : [\n "
77+ + " {\" product\" : \" book1\" , \" price\" : 1},\n "
78+ + " {\" product\" : \" food1\" , \" price\" : 1}\n "
79+ + " ]\n "
80+ + " }\" \" \" \n "
81+ + ");" ;
82+ public static final String insertQuery2 =
83+ "INSERT INTO "
84+ + " `%s.%s.%s` "
85+ + " (\n "
86+ + " StringField, BytesField,IntegerField,FloatField,NumericField,BigNumericField,BooleanField,\n "
87+ + " TimestampField,DateField,TimeField,DateTimeField,GeographyField,RecordField,JsonField )\n "
88+ + " VALUES('string2',CAST ('string2' AS BYTES),222,2.2, CAST('22.2E22' AS NUMERIC),\n "
89+ + " CAST('2.2E37' AS BIGNUMERIC), TRUE,CAST('2002-05-2 8:05:02' AS TIMESTAMP),\n "
90+ + " CAST('2002-05-2' AS DATE),CAST('5:2:22.042' AS TIME), CAST('2002-05-2 22:32:45' AS DATETIME),\n "
91+ + " CAST(ST_GEOGFROMTEXT('POINT(2.500989020425034 -2.22472082322336843)') AS GEOGRAPHY),\n "
92+ + " CAST(('name2', 2) AS STRUCT<STRING,INT64>),\n "
93+ + " JSON \" \" \" {\n "
94+ + " \" name\" : \" Alice2\" ,\n "
95+ + " \" items\" : [\n "
96+ + " {\" product\" : \" book2\" , \" price\" : 2},\n "
97+ + " {\" product\" : \" food2\" , \" price\" : 2}\n "
98+ + " ]\n "
99+ + " }\" \" \" \n "
100+ + " );" ;
101+
102+ public static final String createProcedure =
103+ "CREATE OR REPLACE PROCEDURE `%s.%s`.create_customer() \n "
104+ + "\t BEGIN\n "
105+ + "\t \t DECLARE id STRING;\n "
106+ + "\t \t SET id = GENERATE_UUID();\n "
107+ + "\t \t INSERT INTO `%s.%s.%s` (StringField) VALUES(id);\n "
108+ + "\t \t SELECT FORMAT(\" Created customer.\" );\n "
109+ + "\t END" ;
110+
111+ public static void setUpProcedure (String dataset , String table ) throws InterruptedException {
112+ {
113+ BigQuery bigQuery = BigQueryOptions .getDefaultInstance ().getService ();
114+ bigQuery .query (
115+ QueryJobConfiguration .of (
116+ String .format (
117+ createProcedure , DEFAULT_CATALOG , dataset , DEFAULT_CATALOG , dataset , table )));
118+ }
119+ }
120+
121+ public static void setUpDataset (String dataset ) throws InterruptedException {
122+ BigQuery bigQuery = BigQueryOptions .getDefaultInstance ().getService ();
123+ bigQuery .query (
124+ QueryJobConfiguration .of (String .format (createDatasetQuery , DEFAULT_CATALOG , dataset )));
125+ }
126+
127+ public static void setUpTable (String dataset , String table ) throws InterruptedException {
128+ BigQuery bigQuery = BigQueryOptions .getDefaultInstance ().getService ();
129+ bigQuery .query (
130+ QueryJobConfiguration .of (String .format (createTableQuery , DEFAULT_CATALOG , dataset , table )));
131+ bigQuery .query (
132+ QueryJobConfiguration .of (String .format (insertQuery1 , DEFAULT_CATALOG , dataset , table )));
133+ bigQuery .query (
134+ QueryJobConfiguration .of (String .format (insertQuery2 , DEFAULT_CATALOG , dataset , table )));
135+ }
136+
137+ public static void cleanUp (String dataset ) throws InterruptedException {
138+ BigQuery bigQuery = BigQueryOptions .getDefaultInstance ().getService ();
139+ bigQuery .query (QueryJobConfiguration .of (String .format (dropSchema , DEFAULT_CATALOG , dataset )));
140+ }
141+
27142 protected static String requireEnvVar (String varName ) {
28143 String value = System .getenv (varName );
29144 assertNotNull (
@@ -39,4 +154,18 @@ protected int resultSetRowCount(ResultSet resultSet) throws SQLException {
39154 }
40155 return rowCount ;
41156 }
157+
158+ public static List <String > getInfoBySQL (Connection connection , String sqlCmd )
159+ throws SQLException {
160+ List <String > result = new ArrayList <>();
161+ try (Statement st = connection .createStatement ();
162+ ResultSet rs = st .executeQuery (sqlCmd )) {
163+ while (rs .next ()) {
164+ result .add (rs .getString (1 ));
165+ }
166+ } catch (SQLException e ) {
167+ throw e ;
168+ }
169+ return result ;
170+ }
42171}
0 commit comments