|
| 1 | +""" |
| 2 | +Using `records` with CrateDB: All data types. |
| 3 | +
|
| 4 | + pip install --upgrade records sqlalchemy-cratedb |
| 5 | +
|
| 6 | +An end-to-end lifecycle, defining a table, inserting data, and querying it. |
| 7 | +This example uses all data types supported by CrateDB. |
| 8 | +
|
| 9 | +- https://pypi.org/project/records/ |
| 10 | +- https://cratedb.com/docs/crate/reference/en/latest/general/ddl/data-types.html#supported-types |
| 11 | +""" |
| 12 | + |
| 13 | +from copy import deepcopy |
| 14 | + |
| 15 | +import pytest |
| 16 | +import records |
| 17 | + |
| 18 | + |
| 19 | +# The record that is inserted into the database. |
| 20 | +RECORD_IN = dict( |
| 21 | + null_integer=None, |
| 22 | + integer=42, |
| 23 | + bigint=42, |
| 24 | + float=42.42, |
| 25 | + double=42.42, |
| 26 | + decimal=42.42, |
| 27 | + bit="01010101", |
| 28 | + bool=True, |
| 29 | + text="foobar", |
| 30 | + char="foo", |
| 31 | + timestamp_tz="1970-01-02T00:00:00+01:00", |
| 32 | + timestamp_notz="1970-01-02T00:00:00", |
| 33 | + ip="127.0.0.1", |
| 34 | + array=["foo", "bar"], |
| 35 | + object={"for": "bar"}, |
| 36 | + geopoint=[85.43, 66.23], |
| 37 | + geoshape="POLYGON ((5 5, 10 5, 10 10, 5 10, 5 5))", |
| 38 | + float_vector=[1.0, 2.0, 3.0], |
| 39 | +) |
| 40 | + |
| 41 | +# When querying it, a few values will be canonicalized. |
| 42 | +RECORD_OUT = deepcopy(RECORD_IN) |
| 43 | +RECORD_OUT.update( |
| 44 | + dict( |
| 45 | + bit="B'01010101'", |
| 46 | + char="foo ", |
| 47 | + timestamp_tz=82800000, |
| 48 | + timestamp_notz=86400000, |
| 49 | + geopoint=[pytest.approx(85.43), pytest.approx(66.23)], |
| 50 | + geoshape={ |
| 51 | + "coordinates": [ |
| 52 | + [[5.0, 5.0], [5.0, 10.0], [10.0, 10.0], [10.0, 5.0], [5.0, 5.0]] |
| 53 | + ], |
| 54 | + "type": "Polygon", |
| 55 | + }, |
| 56 | + ) |
| 57 | +) |
| 58 | + |
| 59 | + |
| 60 | +def records_ddl_dml_dql(): |
| 61 | + """ |
| 62 | + Validate all types of CrateDB. |
| 63 | +
|
| 64 | + https://cratedb.com/docs/crate/reference/en/latest/general/ddl/data-types.html#supported-types |
| 65 | + """ |
| 66 | + db = records.Database("crate://", echo=True) |
| 67 | + |
| 68 | + # DDL |
| 69 | + db.query("DROP TABLE IF EXISTS testdrive.example;") |
| 70 | + db.query(""" |
| 71 | + CREATE TABLE testdrive.example ( |
| 72 | + -- Numeric types |
| 73 | + null_integer INT, |
| 74 | + integer INT, |
| 75 | + bigint BIGINT, |
| 76 | + float FLOAT, |
| 77 | + double DOUBLE, |
| 78 | + decimal DECIMAL(8, 2), |
| 79 | + -- Other scalar types |
| 80 | + bit BIT(8), |
| 81 | + bool BOOLEAN, |
| 82 | + text TEXT, |
| 83 | + char CHARACTER(5), |
| 84 | + timestamp_tz TIMESTAMP WITH TIME ZONE, |
| 85 | + timestamp_notz TIMESTAMP WITHOUT TIME ZONE, |
| 86 | + ip IP, |
| 87 | + -- Container types |
| 88 | + "array" ARRAY(STRING), |
| 89 | + "object" OBJECT(DYNAMIC), |
| 90 | + -- Geospatial types |
| 91 | + geopoint GEO_POINT, |
| 92 | + geoshape GEO_SHAPE, |
| 93 | + -- Vector type |
| 94 | + "float_vector" FLOAT_VECTOR(3) |
| 95 | + ); |
| 96 | + """) |
| 97 | + |
| 98 | + # DML |
| 99 | + db.query( |
| 100 | + """ |
| 101 | + INSERT INTO testdrive.example ( |
| 102 | + null_integer, |
| 103 | + integer, |
| 104 | + bigint, |
| 105 | + float, |
| 106 | + double, |
| 107 | + decimal, |
| 108 | + bit, |
| 109 | + bool, |
| 110 | + text, |
| 111 | + char, |
| 112 | + timestamp_tz, |
| 113 | + timestamp_notz, |
| 114 | + ip, |
| 115 | + "array", |
| 116 | + "object", |
| 117 | + geopoint, |
| 118 | + geoshape, |
| 119 | + float_vector |
| 120 | + ) VALUES ( |
| 121 | + :null_integer, |
| 122 | + :integer, |
| 123 | + :bigint, |
| 124 | + :float, |
| 125 | + :double, |
| 126 | + :decimal, |
| 127 | + :bit, |
| 128 | + :bool, |
| 129 | + :text, |
| 130 | + :char, |
| 131 | + :timestamp_tz, |
| 132 | + :timestamp_notz, |
| 133 | + :ip, |
| 134 | + :array, |
| 135 | + :object, |
| 136 | + :geopoint, |
| 137 | + :geoshape, |
| 138 | + :float_vector |
| 139 | + ); |
| 140 | + """, |
| 141 | + **RECORD_IN, |
| 142 | + ) |
| 143 | + |
| 144 | + # DQL |
| 145 | + db.query("REFRESH TABLE testdrive.example") |
| 146 | + rows = db.query("SELECT * FROM testdrive.example") |
| 147 | + data = rows.all() |
| 148 | + return data |
0 commit comments