Skip to main content

Sql query builder interface

Project description

Koia

Installation

Via github

git clone https://github.com/Kel0/koia.git
pip install invoke
inv install

Via pip

pip install koia

Methods

koia.connector.Database.generic_types() -> Tuple[
    TypeVar("T", Dict[str, str], Dict[str, int]),
    TypeVar("X", bool, bool)
]

koia.connector.Database.reconnect() -> None
    reconnect to database & recreate cursor

koia.connector.Database.close() -> None
    close connection to database & close cursor

koia.QueryBuilder.QueryBuilder.table(table: str) -> koia.QueryBuilder.QueryBuilder

koia.QueryBuilder.QueryBuilder.where(column: str, operation: str, value: Any) -> koia.QueryBuilder.QueryBuilder
    column = column_name
    operation = "= | > | < | >= | <= | !=" etc etc
    value = value    

koia.QueryBuilder.QueryBuilder.whereNotNull(column: str) -> koia.QueryBuilder.QueryBuilder

koia.QueryBuilder.QueryBuilder.orWhere(column: str, operation: str, value: Any) -> koia.QueryBuilder.QueryBuilder

koia.QueryBuilder.QueryBuilder.orderBy(column: str, option: str) -> koia.QueryBuilder.QueryBuilder

koia.QueryBuilder.QueryBuilder.take(count: int) -> koia.QueryBuilder.QueryBuilder

koia.QueryBuilder.QueryBuilder.innerJoin(table: str, specification: dict) -> koia.QueryBuilder.QueryBuilder
    .innerJoin(table="myTable2", specification={"myTable2.id": "myTable.id"})    

koia.FetchBuilder.FetchBuilder.get() -> Optional[List[Any]]

koia.FetchBuilder.FetchBuilder.insert(values: dict) -> None
    values = {"col_name": "value"}    

koia.FetchBuilder.FetchBuilder.update(values: dict) -> None
    values = {"col_name": "value"}

Attributes

koia.connector.ConnectorGenericTypes.T -> TypeVar("T", Dict[str, str], Dict[str, int])
koia.connector.ConnectorGenericTypes.X -> TypeVar("X", bool, bool)

koia.connector.Database.config: dict -> Your database config
koia.connector.Database.connection: MySQLconnection -> Database connection
koia.connector.Database.cursor: MySQLcursor -> Connection cursor

koia.QueryBuilder.QueryBuilder.database: koia.connector.Database -> koia.connector.Database instance
koia.QueryBuilder.QueryBuilder.query_conf: dict -> dict with sql attributes and params

koia.FetchBuilder.FetchBuilder -> koia.FetchBuilder.FetchBuilder(koia.QueryBuilder.QueryBuilder)

Code example

Get data

from typing import Dict

from koia.connector import Database
from koia.FetchBuilder import FetchBuilder

database: Database = Database(config={
    "host": "host",
    "user": "root",
    "password": "1234",
    "database": "base"
}, autocommit=False)
fetch_builder: FetchBuilder = FetchBuilder(database=database)

data: list = (
    fetch_builder.table("myTable")
    .where("column", "=", value)
    .orWhere("column", "LIKE", f"%{value}%")
    .whereNotNull("column")
    .whereIn("column", [value1, value2])
    .orderBy("column", "DESC")
    .take(2)
    .get()
)

Some details

(
    fetch_builder.table("myTable")
    .where("column", "=", value)
    .orWhere("column", "LIKE", f"%{value}%")
    .whereNotNull("column")
    .whereIn("column", [value1, value2])
    .orderBy("column", "DESC")
    .take(2)
    .get()
) -> SELECT * FROM myTable 
        WHERE `column` = value 
        OR `column` LIKE '%value%'
        AND `column` IS NOT NULL 
        AND `column` IN (value1, value2)
        ORDER BY `column` DESC
        LIMIT 2

Inner join

from typing import Dict

from koia.connector import Database
from koia.FetchBuilder import FetchBuilder

database: Database = Database(config={
    "host": "host",
    "user": "root",
    "password": "1234",
    "database": "base"
}, autocommit=False)
fetch_builder: FetchBuilder = FetchBuilder(database=database)

data: list = (
    fetch_builder.table("myTable")
    .innerJoin(table="myTable2", specification={
        "myTable.id": "myTable2.id", "myTable.col": "myTable2.col"
    })
    .innerJoin(table="myTable3", specification={
        "myTable.id": "myTable3.id", "myTable.col": "myTable3.col"
    })
    .whereNotNull("column")
    .get()
)

Some details

data = (
    fetch_builder.table("myTable")
    .innerJoin(table="myTable2", specification={
        "myTable2.id": "myTable.id", "myTable2.col": "myTable.col"
    })
    .innerJoin(table="myTable3", specification={
        "myTable3.id": "myTable.id", "myTable3.col": "myTable.col"
    })
    .whereNotNull("column")
    .get()
) -> SELECT * FROM myTable 
        INNER JOIN myTable2 ON (myTable2.id = myTable.id) and (myTable2.col = myTable.col)
        INNER JOIN myTable2 ON (myTable3.id = myTable.id) and (myTable3.col = myTable.col)
        WHERE `column` IS NOT NULL

Insert data

from typing import Dict

from koia.connector import Database
from koia.FetchBuilder import FetchBuilder

database: Database = Database(config={
    "host": "host",
    "user": "root",
    "password": "1234",
    "database": "base"
}, autocommit=False)
fetch_builder: FetchBuilder = FetchBuilder(database=database)
(
    fetch_builder.table("myTable")
    .insert({
        "column": "value"
    })
)

Some details

(
    fetch_builder.table("myTable")
    .insert({
        "column": "value"
    })
) -> INSERT INTO myTable
        (`column`) VALUES (value)

Update data

from typing import Dict

from koia.connector import Database
from koia.FetchBuilder import FetchBuilder

database: Database = Database(config={
    "host": "host",
    "user": "root",
    "password": "1234",
    "database": "base"
}, autocommit=False)
fetch_builder: FetchBuilder = FetchBuilder(database=database)
(
    fetch_builder.table("myTable")
    .update({
        "column": "value"
    })
    .where("column", "LIKE", "%{value}%")
)

Some details

(
    fetch_builder.table("myTable")
    .update({
        "column": "value"
    })
    .where("column", "LIKE", "%{value}%")
) -> UPDATE myTable SET `column` = value
        WHERE `column` LIKE '%value%'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

koia-0.0.6.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

koia-0.0.6-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file koia-0.0.6.tar.gz.

File metadata

  • Download URL: koia-0.0.6.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.0

File hashes

Hashes for koia-0.0.6.tar.gz
Algorithm Hash digest
SHA256 fa2cde88b3de812418c97d2f5e4f06010e7cbadef75ceb3550503344c2a5cd5c
MD5 0551bf1e9af66babc20735872d2a2af8
BLAKE2b-256 bff82ba973a62dbf6797e548d7402bc447316ae68960a33a03238870ec8bc939

See more details on using hashes here.

File details

Details for the file koia-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: koia-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.0

File hashes

Hashes for koia-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 25ad06f45a31714d797cfba1ab9a45e3ff3781da9b986a770586660e3946734d
MD5 2c007a000851ce2b562772abd2d9d412
BLAKE2b-256 665fe2dcd594803b6d2d79f58e672acdeb84ad683b10bcbff7bcfa21f32f362f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page