Skip to content

Commit

Permalink
Merge pull request #86 from carlos-rian/release/new-version
Browse files Browse the repository at this point in the history
Release/new version
  • Loading branch information
carlos-rian authored Oct 14, 2024
2 parents 8bd6548 + 878c071 commit 986da9b
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 18 deletions.
27 changes: 19 additions & 8 deletions src/py_types/converter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::errors::PySQLxInvalidParamError;
use super::value::PySQLxValue;
use bigdecimal::BigDecimal;
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, SecondsFormat, Utc};
use log::info;
use pyo3::prelude::*;
use pyo3::types::{PyAny, PyAnyMethods, PyDict, PyModule, PyTuple, PyType, PyTypeMethods};
Expand Down Expand Up @@ -66,8 +66,13 @@ impl PySQLxParamKind {

fn from(py: Python, value: &Bound<'_, PyAny>, provider: &str) -> Self {
// kind string is python class Type name
let mut py_type = get_python_type_name(value);
if Self::is_enum_instance(py, value) {
py_type = "Enum".to_string();
}

info!("{:?}", value);
match get_python_type_name(value).as_str() {
match py_type.as_str() {
"bool" => PySQLxParamKind::Boolean,
"str" => PySQLxParamKind::String,
"int" => PySQLxParamKind::Int,
Expand Down Expand Up @@ -185,7 +190,11 @@ impl Converters {
value: &Bound<'_, PyAny>,
) -> Result<JsonValue, PySQLxInvalidParamError> {
// the could be a PyDict, PyList, bool, int, float, str or None
match get_python_type_name(value).as_str() {
let mut py_type = get_python_type_name(value);
if PySQLxParamKind::is_enum_instance(py, value) {
py_type = "Enum".to_string();
}
match py_type.as_str() {
"dict" => {
let dict = value.extract::<HashMap<String, Bound<PyAny>>>().unwrap();
let mut map = serde_json::Map::new();
Expand Down Expand Up @@ -221,21 +230,23 @@ impl Converters {
}
"datetime" => {
let datetime: DateTime<Utc> = Self::convert_to_datetime(value);
Ok(JsonValue::String(datetime.to_rfc3339()))
Ok(JsonValue::String(
datetime.to_rfc3339_opts(SecondsFormat::Millis, true),
))
}
"uuid" => {
"UUID" => {
let rs_uuid = Self::convert_to_rs_uuid(value);
Ok(JsonValue::String(rs_uuid.to_string()))
}
"bytes" => {
let bytes = value.extract::<Vec<u8>>().unwrap();
Ok(JsonValue::String(base64::encode(bytes)))
}
"decimal" => {
let decimal = value.extract::<String>().unwrap();
"Decimal" => {
let decimal = value.to_string();
Ok(JsonValue::String(decimal))
}
"enum" => {
"Enum" => {
let enum_value = value
.getattr(intern!(py, "value"))
.unwrap()
Expand Down
129 changes: 119 additions & 10 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
from uuid import UUID
from enum import Enum
from pysqlx_core import new, PySQLxStatement
import asyncio
import uvloop
from pprint import pprint
import logging
from decimal import Decimal
Expand Down Expand Up @@ -66,6 +64,47 @@ class EnumColors(Enum):
GRAY = "gray"
BLACK = "black"

class EnumColorsAsStr(str, Enum):
BLUE = "blue"
RED = "red"
GRAY = "gray"
BLACK = "black"

ALL_TYPES = [
None,
3618, # int (smallint)
-3762183126230668, # int (bigint)
36, # int (serial)
36, # int (smallserial)
36, # int (bigserial)
130.3064, # float (numeric)
2159.912, # float (float)
1577.3155, # float (double)
Decimal("6803.77"), # float (money), parsed from string '$6,803.77'
"C", # str (char)
"ATYOLOUREPOJRSNOWKMULTTRHJPTCWOIYHQVVIXVUFZNCMEFJTRLCJZMKNJVAUYIEZYKVPWCWGGRDBUKKEDQHSEYPACMNGBOLHLC", # str (varchar)
"text", # str (text)
False, # bool (boolean)
date.fromisoformat("2022-10-27"), # str (date)
time.fromisoformat("00:00:21"), # str (time)
datetime.fromisoformat("2022-10-27 15:29:27.000000"),
datetime.utcnow(),
EnumColorsAsStr.BLUE,
UUID("19b3d203-e4b7-4b7b-8bf2-476abea92b04"),
{"cep": "01001-000"},
{"cep": "01001-000"},
"<note><to>Tove</to></note>",
"192.168.0.1",
b"DEADBEEF",
("name", "age"),
(1, 2, 3),
(date(2022, 10, 27), date(2022, 10, 27)),
(
UUID("7b97c8a6-7e5a-4412-a57d-78565a136582"),
UUID("7b97c8a6-7e5a-4412-a57d-78565a136583"),
),
]

return {
"type_int": None,
"type_smallint": 3618, # int (smallint)
Expand All @@ -87,9 +126,9 @@ class EnumColors(Enum):
"type_time": time.fromisoformat("00:00:21"), # str (time)
"type_datetime": datetime.fromisoformat("2022-10-27 15:29:27.000000"),
"type_datetimetz": datetime.utcnow(),
"type_enum": EnumColors.BLUE,
"type_enum": EnumColorsAsStr.BLUE,
"type_uuid": UUID("19b3d203-e4b7-4b7b-8bf2-476abea92b04"),
"type_json": {"cep": "01001-000"},
"type_json": ALL_TYPES,
"type_jsonb": {"cep": "01001-000"},
"type_xml": "<note><to>Tove</to></note>",
"type_inet": "192.168.0.1",
Expand Down Expand Up @@ -315,15 +354,85 @@ async def mysql():


async def main():
await mysql()
await psql()
await mysql()
await sqlite()


if __name__ == "__main__":
# asyncio.run(main())
# if __name__ == "__main__":
# asyncio.run(main())

uvloop.install()
asyncio.run(main())
# vloop.install()
# asyncio.run(main())

# trio.run(main)
# trio.run(main)


p = PySQLxStatement(
provider="postgresql",
sql="""
INSERT INTO pysqlx_table (
type_int,
type_smallint,
type_bigint,
type_serial,
type_smallserial,
type_bigserial,
type_numeric,
type_float,
type_double,
type_money,
type_char,
type_varchar,
type_text,
type_boolean,
type_date,
type_time,
type_datetime,
type_datetimetz,
type_enum,
type_uuid,
type_json,
type_jsonb,
type_xml,
type_inet,
type_bytes,
type_array_text,
type_array_integer,
type_array_date,
type_array_uuid
)
VALUES (
:type_int,
:type_smallint,
:type_bigint,
:type_serial,
:type_smallserial,
:type_bigserial,
:type_numeric,
:type_float,
:type_double,
:type_money,
:type_char,
:type_varchar,
:type_text,
:type_boolean,
:type_date,
:type_time,
:type_datetime,
:type_datetimetz,
:type_enum,
:type_uuid,
:type_json,
:type_jsonb,
:type_xml,
:type_inet,
:type_bytes,
:type_array_text,
:type_array_integer,
:type_array_date,
:type_array_uuid
);
""",
params=typ(),
)

0 comments on commit 986da9b

Please sign in to comment.