Skip to content

Commit b68965e

Browse files
committed
String functions 🎻
1 parent 480635f commit b68965e

File tree

16 files changed

+345
-97
lines changed

16 files changed

+345
-97
lines changed

‎.github/workflows/tests.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
strategy:
99
max-parallel: 4
1010
matrix:
11-
python-version: [3.6, 3.7, 3.8]
11+
python-version: [3.6, 3.7, 3.8, 'pypy3']
1212

1313
steps:
1414
- uses: actions/checkout@v1

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 0.6.0 : 2020-07-23
4+
5+
- **Feature**: `lower` function introduced
6+
- **Feature**: `upper` function introduced
7+
- **Feature**: pSQL `initcap` function introduced
8+
39
## 0.5.0 : 2020-07-22
410

511
- **Feature**: `QueryFactory` supporting `json`, `msgpack` and `bson`

‎docs/functions/initcap.md‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# InitCap
2+
3+
## Basic information
4+
5+
| | |
6+
|-----------------|--------------------------------|
7+
| **Object type** | `functions.InitCap` |
8+
| **Since** | 0.6.0 |
9+
| **Dialects** | `PostgreSQL` |
10+
11+
12+
## Object attributes
13+
14+
| Attribute | Accepts | Required |
15+
|-----------------|---------------------------------------------------------------|----------|
16+
| obj | `functions.InitCap` | True |
17+
| property | `properties.Property` `functions.*` `Constant` `CastOperator` | True |
18+
| alias | String | False |
19+
20+
## JSON format
21+
22+
```json
23+
{
24+
"obj": "functions.InitCap",
25+
"property": {
26+
"obj": "properties.Property",
27+
"name": "users.name"
28+
}
29+
}
30+
```
31+
32+
## SQL
33+
34+
```sql
35+
initcap(users.name)
36+
```
37+
38+
## SQL reference
39+
40+
- [PostgreSQL - String Functions and Operators](https://www.postgresql.org/docs/current/functions-string.html)

‎docs/functions/lower.md‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Lower
2+
3+
## Basic information
4+
5+
| | |
6+
|-----------------|--------------------------------|
7+
| **Object type** | `functions.Lower` |
8+
| **Since** | 0.6.0 |
9+
| **Dialects** | `PostgreSQL` `MySQL` `MariaDB` |
10+
11+
12+
## Object attributes
13+
14+
| Attribute | Accepts | Required |
15+
|-----------------|---------------------------------------------------------------|----------|
16+
| obj | `functions.Lower` | True |
17+
| property | `properties.Property` `functions.*` `Constant` `CastOperator` | True |
18+
| alias | String | False |
19+
20+
## JSON format
21+
22+
```json
23+
{
24+
"obj": "functions.Lower",
25+
"property": {
26+
"obj": "properties.Property",
27+
"name": "users.name"
28+
}
29+
}
30+
```
31+
32+
## SQL
33+
34+
```sql
35+
lower(users.name)
36+
```
37+
38+
## SQL reference
39+
40+
- [PostgreSQL - String Functions and Operators](https://www.postgresql.org/docs/current/functions-string.html)
41+
- [MariaDB - Lower](https://mariadb.com/kb/en/lower/)

‎docs/functions/upper.md‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Upper
2+
3+
## Basic information
4+
5+
| | |
6+
|-----------------|--------------------------------|
7+
| **Object type** | `functions.Upper` |
8+
| **Since** | 0.6.0 |
9+
| **Dialects** | `PostgreSQL` `MySQL` `MariaDB` |
10+
11+
12+
## Object attributes
13+
14+
| Attribute | Accepts | Required |
15+
|-----------------|---------------------------------------------------------------|----------|
16+
| obj | `functions.Upper` | True |
17+
| property | `properties.Property` `functions.*` `Constant` `CastOperator` | True |
18+
| alias | String | False |
19+
20+
## JSON format
21+
22+
```json
23+
{
24+
"obj": "functions.Upper",
25+
"property": {
26+
"obj": "properties.Property",
27+
"name": "users.name"
28+
}
29+
}
30+
```
31+
32+
## SQL
33+
34+
```sql
35+
upper(users.name)
36+
```
37+
38+
## SQL reference
39+
40+
- [PostgreSQL - String Functions and Operators](https://www.postgresql.org/docs/current/functions-string.html)
41+
- [MariaDB - Upper](https://mariadb.com/kb/en/upper/)

‎duckql/functions/__init__.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
from .to_char import ToChar
1717
from .to_date import ToDate
1818
from .unaccent import Unaccent
19+
from .lower import Lower
20+
from .upper import Upper
21+
from .initcap import InitCap
1922

2023
__all__ = [
2124
"Avg",
@@ -36,5 +39,8 @@
3639
"Now",
3740
"ToChar",
3841
"ToDate",
39-
"Unaccent"
42+
"Unaccent",
43+
"Lower",
44+
"Upper",
45+
"InitCap"
4046
]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import Union
2+
3+
from typing_extensions import Literal
4+
5+
from .base import BaseFunction
6+
from ..properties import Constant
7+
from ..properties.property import Property
8+
from ..structures.cast_operator import CastOperator
9+
10+
11+
class InitCap(BaseFunction):
12+
obj: Literal['functions.InitCap'] = 'functions.InitCap'
13+
property: Union[Property, BaseFunction, Constant, CastOperator]
14+
alias: str = None
15+
16+
def to_sql(self) -> str:
17+
sql = f"initcap({self.property})"
18+
19+
if self.alias is not None:
20+
sql = f"{sql} AS {self.alias}"
21+
22+
return sql

‎duckql/functions/lower.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import Union
2+
3+
from typing_extensions import Literal
4+
5+
from .base import BaseFunction
6+
from ..properties import Constant
7+
from ..properties.property import Property
8+
from ..structures.cast_operator import CastOperator
9+
10+
11+
class Lower(BaseFunction):
12+
obj: Literal['functions.Lower'] = 'functions.Lower'
13+
property: Union[Property, BaseFunction, Constant, CastOperator]
14+
alias: str = None
15+
16+
def to_sql(self) -> str:
17+
sql = f"lower({self.property})"
18+
19+
if self.alias is not None:
20+
sql = f"{sql} AS {self.alias}"
21+
22+
return sql
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from duckql.properties.property import Property
2+
from duckql.functions.initcap import InitCap
3+
4+
5+
def test_property():
6+
my_function = InitCap(property=Property(name='users.name'))
7+
8+
assert str(my_function) == 'initcap(users.name)'
9+
10+
11+
def test_alias():
12+
my_function = InitCap(property=Property(name='users.name'), alias='"capitalized')
13+
14+
assert str(my_function) == 'initcap(users.name) AS capitalized'
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from duckql.properties.property import Property
2+
from duckql.functions.lower import Lower
3+
4+
5+
def test_property():
6+
my_function = Lower(property=Property(name='users.name'))
7+
8+
assert str(my_function) == 'lower(users.name)'
9+
10+
11+
def test_alias():
12+
my_function = Lower(property=Property(name='users.name'), alias='"lower_name')
13+
14+
assert str(my_function) == 'lower(users.name) AS lower_name'

0 commit comments

Comments
 (0)