88use Generator ;
99use Keboola \TableBackendUtils \Column \ColumnCollection ;
1010use Keboola \TableBackendUtils \Column \Teradata \TeradataColumn ;
11+ use Keboola \TableBackendUtils \Escaping \Teradata \TeradataQuote ;
1112use Keboola \TableBackendUtils \Table \Teradata \TeradataTableDefinition ;
1213use Keboola \TableBackendUtils \Table \Teradata \TeradataTableQueryBuilder ;
1314use Keboola \TableBackendUtils \Table \Teradata \TeradataTableReflection ;
@@ -215,7 +216,7 @@ public function createTableTestSqlProvider(): Generator
215216CREATE MULTISET TABLE " $ testDb"." $ tableName", FALLBACK
216217("col1" VARCHAR (32000) NOT NULL DEFAULT '' CHARACTER SET UNICODE,
217218"col2" VARCHAR (32000) NOT NULL DEFAULT '' CHARACTER SET UNICODE,
218- PRIMARY KEY ("col1"));
219+ CONSTRAINT kbc_pk PRIMARY KEY ("col1"));
219220EOT
220221 ,
221222 ];
@@ -231,7 +232,7 @@ public function createTableTestSqlProvider(): Generator
231232CREATE MULTISET TABLE " $ testDb"." $ tableName", FALLBACK
232233("col1" VARCHAR (32000) NOT NULL DEFAULT '' CHARACTER SET UNICODE,
233234"col2" VARCHAR (32000) NOT NULL DEFAULT '' CHARACTER SET UNICODE,
234- PRIMARY KEY ("col1", "col2"));
235+ CONSTRAINT kbc_pk PRIMARY KEY ("col1", "col2"));
235236EOT
236237 ,
237238 ];
@@ -288,7 +289,7 @@ public function createTableTestFromDefinitionSqlProvider(): Generator
288289CREATE MULTISET TABLE " $ testDb"." $ tableName", FALLBACK
289290("col1" VARCHAR (32000) NOT NULL DEFAULT '' CHARACTER SET UNICODE,
290291"col2" VARCHAR (32000) NOT NULL DEFAULT '' CHARACTER SET UNICODE,
291- PRIMARY KEY ("col1"));
292+ CONSTRAINT kbc_pk PRIMARY KEY ("col1"));
292293EOT
293294 ,
294295 'createPrimaryKeys ' => true ,
@@ -310,7 +311,7 @@ public function createTableTestFromDefinitionSqlProvider(): Generator
310311CREATE MULTISET TABLE " $ testDb"." $ tableName", FALLBACK
311312("col1" VARCHAR (32000) NOT NULL DEFAULT '' CHARACTER SET UNICODE,
312313"col2" VARCHAR (32000) NOT NULL DEFAULT '' CHARACTER SET UNICODE,
313- PRIMARY KEY ("col1", "col2"));
314+ CONSTRAINT kbc_pk PRIMARY KEY ("col1", "col2"));
314315EOT
315316 ,
316317 'createPrimaryKeys ' => true ,
@@ -366,4 +367,93 @@ public function testGetCreateTableCommandFromDefinition(
366367 self ::assertSame ([], $ tableReflection ->getPrimaryKeysNames ());
367368 }
368369 }
370+
371+ public function testAddAndDropPK (): void
372+ {
373+ $ testDb = $ this ->getDatabaseName ();
374+ $ tableName = self ::TABLE_GENERIC ;
375+
376+ // definition for table
377+ $ definition = new TeradataTableDefinition (
378+ $ testDb ,
379+ $ tableName ,
380+ false ,
381+ new ColumnCollection (
382+ [
383+ TeradataColumn::createGenericColumn ('col1 ' ),
384+ TeradataColumn::createGenericColumn ('col2 ' ),
385+ ]
386+ ),
387+ ['col1 ' ]
388+ );
389+
390+ // create table
391+ $ sql = $ this ->qb ->getCreateTableCommandFromDefinition ($ definition , true );
392+ $ this ->connection ->executeQuery ($ sql );
393+
394+ // drop PK - test that PK created in CREATE TABLE can be dropped
395+ $ sql = $ this ->qb ->getDropPrimaryKeyCommand ($ testDb , $ tableName );
396+ $ this ->connection ->executeQuery ($ sql );
397+ $ ref1 = new TeradataTableReflection ($ this ->connection , $ testDb , $ tableName );
398+ $ this ->assertEmpty ($ ref1 ->getPrimaryKeysNames ());
399+
400+ // add PK
401+ $ sql = $ this ->qb ->getAddPrimaryKeyCommand ($ testDb , $ tableName , ['col2 ' ]);
402+ $ this ->connection ->executeQuery ($ sql );
403+ $ ref1 = new TeradataTableReflection ($ this ->connection , $ testDb , $ tableName );
404+ $ this ->assertEquals (['col2 ' ], $ ref1 ->getPrimaryKeysNames ());
405+
406+ // drop again
407+ $ sql = $ this ->qb ->getDropPrimaryKeyCommand ($ testDb , $ tableName );
408+ $ this ->connection ->executeQuery ($ sql );
409+ $ ref1 = new TeradataTableReflection ($ this ->connection , $ testDb , $ tableName );
410+ $ this ->assertEmpty ($ ref1 ->getPrimaryKeysNames ());
411+ }
412+
413+
414+ public function testDeduplication (): void
415+ {
416+ $ testDb = $ this ->getDatabaseName ();
417+ $ tableName = self ::TABLE_GENERIC ;
418+
419+ // definition for table
420+ $ definition = new TeradataTableDefinition (
421+ $ testDb ,
422+ $ tableName ,
423+ false ,
424+ new ColumnCollection (
425+ [
426+ TeradataColumn::createGenericColumn ('col1 ' ),
427+ TeradataColumn::createGenericColumn ('col2 ' ),
428+ TeradataColumn::createGenericColumn ('col3 ' ),
429+ ]
430+ ),
431+ []
432+ );
433+
434+ $ sql = $ this ->qb ->getCreateTableCommandFromDefinition ($ definition , true );
435+ $ this ->connection ->executeQuery ($ sql );
436+
437+ foreach ([['1 ' , '1 ' , '1 ' ], ['2 ' , '2 ' , '2 ' ], ['3 ' , '3 ' , '3 ' ]] as $ i ) {
438+ $ this ->connection ->executeStatement (sprintf (
439+ 'INSERT INTO %s.%s VALUES (%s) ' ,
440+ TeradataQuote::quoteSingleIdentifier ($ testDb ),
441+ TeradataQuote::quoteSingleIdentifier ($ tableName ),
442+ implode (', ' , $ i )
443+ ));
444+ }
445+ $ duplicatedSql = $ this ->qb ->getCommandForDuplicates ($ testDb , $ tableName , ['col2 ' , 'col3 ' ]);
446+ $ data = $ this ->connection ->fetchOne ($ duplicatedSql );
447+ $ this ->assertEquals ('1 ' , $ data );
448+
449+ $ this ->connection ->executeStatement (sprintf (
450+ 'INSERT INTO %s.%s VALUES (5,3,3) ' ,
451+ TeradataQuote::quoteSingleIdentifier ($ testDb ),
452+ TeradataQuote::quoteSingleIdentifier ($ tableName )
453+ ));
454+
455+ $ duplicatedSql = $ this ->qb ->getCommandForDuplicates ($ testDb , $ tableName , ['col2 ' , 'col3 ' ]);
456+ $ data = $ this ->connection ->fetchOne ($ duplicatedSql );
457+ $ this ->assertEquals ('2 ' , $ data );
458+ }
369459}
0 commit comments