Skip to content

Commit

Permalink
Add postgreSql character length functions (#4121)
Browse files Browse the repository at this point in the history
* Add Integration test for PostgreSql Dialect length function

Test for length function can be executed standalone.

Test for Nullable type support is generated

* Add Character length functions for PostgreSql Dialect

Returns number of characters in the string or will return NULL if expression is NULL.
In PostgreSql the following are synonymous and are Ansi Sql supported.
length ( In MySql the length function is in bytes )
char_length
character_length

char_length is ANSI SQL Standard and the explicitly named character_length,
as multibyte character counts as a single character not as bytes.

Examples

char_length ( text ) -> integer
char_length('jose') -> 4
char_length('海豚') -> 2
char_length(NULL) -> NULL

* Add Integration test fixture

* Add test fixture for PostgreSql length function

Support for all character length functions can be used in check constraint.

* Remove repetitive declaratiion

* Fix failing test

lowercase to compile - not sure if this should matter

* Length Function test

Shows the error

Type mismatch: inferred type is String but Int? was expected

* Revert "Length Function test"

This reverts commit 04ad12f.
  • Loading branch information
griffio authored May 3, 2023
1 parent 1b084ad commit 32d6c51
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class PostgreSqlTypeResolver(private val parentResolver: TypeResolver) : TypeRes
"date_trunc" -> encapsulatingType(exprList, TIMESTAMP_TIMEZONE, TIMESTAMP)
"now" -> IntermediateType(TIMESTAMP_TIMEZONE)
"gen_random_uuid" -> IntermediateType(PostgreSqlType.UUID)
"length", "character_length", "char_length" -> IntermediateType(PostgreSqlType.INTEGER).nullableIf(resolvedType(exprList[0]).javaType.isNullable)
else -> null
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CREATE TABLE t1 (
c1 INTEGER
c1 INTEGER,
t1 TEXT,
t2 VARCHAR(255),
t3 CHAR(10)
);

ALTER TABLE t1
ADD CONSTRAINT chk_c1 CHECK (c1 > 0);
ADD CONSTRAINT chk_c1 CHECK (c1 > 0),
ADD CONSTRAINT chk_t1 CHECK (LENGTH(t1) > 0),
ADD CONSTRAINT chk_t2 CHECK (CHAR_LENGTH(t2) > 0),
ADD CONSTRAINT chk_t3 CHECK (CHARACTER_LENGTH(t3) > 0);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TABLE characters(
name TEXT NOT NULL,
description TEXT
);

insertCharacter:
INSERT INTO characters (name, description) VALUES (:name, :description);

selectNameLength:
SELECT length(name) FROM characters;

selectDescriptionLength:
SELECT length(description) FROM characters;
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,12 @@ class PostgreSqlTest {
val uuid: UUID = database.uuidsQueries.randomUuid().executeAsOne()
assertThat(uuid).isNotNull()
}

@Test fun lengthFunction() {
database.charactersQueries.insertCharacter("abcdef", null)
val name = database.charactersQueries.selectNameLength().executeAsOne()
assertThat(name).isEqualTo(6)
val desc = database.charactersQueries.selectDescriptionLength().executeAsOne()
assertThat(desc.length).isNull()
}
}

0 comments on commit 32d6c51

Please sign in to comment.