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.10.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

koia-0.0.10-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: koia-0.0.10.tar.gz
  • Upload date:
  • Size: 8.2 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.10.tar.gz
Algorithm Hash digest
SHA256 16a42192f6582a55edd10c919db37e2786f24e21a7b59ce2ae3798cff5a368b0
MD5 1c624e054c08e9adf9f40ab60fdefdaa
BLAKE2b-256 fcf449c2f61e993d9195d73cb6988077fd691df301955e7ca9fb74bced287e08

See more details on using hashes here.

File details

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

File metadata

  • Download URL: koia-0.0.10-py3-none-any.whl
  • Upload date:
  • Size: 9.9 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.10-py3-none-any.whl
Algorithm Hash digest
SHA256 0e3e835577d4783d540683429cd2ab957c23ddb28ad7a7551993132d743d6442
MD5 6edcabe16f7d5ddf70b7af59b84d5b63
BLAKE2b-256 513d3aa002b980ed1562517da9056f01f34f45867d637ac8daa80296f07aaa16

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