Skip to main content

Sql query builder interface

Project description

Koia

Installation

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

Methods

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.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 koia.FetchBuilder import FetchBuilder

cfg: dict = {
    "host": "host",
    "user": "root",
    "password": 12345,
    "database": "base"
}

fetch_builder: FetchBuilder = FetchBuilder(database=cfg)

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 koia.FetchBuilder import FetchBuilder

cfg: dict = {
    "host": "host",
    "user": "root",
    "password": 12345,
    "database": "base"
}

fetch_builder: FetchBuilder = FetchBuilder(database=cfg)

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 koia.FetchBuilder import FetchBuilder

cfg: dict = {
    "host": "host",
    "user": "root",
    "password": 12345,
    "database": "base"
}

fetch_builder: FetchBuilder = FetchBuilder(database=cfg)
(
    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 koia.FetchBuilder import FetchBuilder

cfg: dict = {
    "host": "host",
    "user": "root",
    "password": 12345,
    "database": "base"
}

fetch_builder: FetchBuilder = FetchBuilder(database=cfg)
(
    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.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

koia-0.0.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: koia-0.0.1.tar.gz
  • Upload date:
  • Size: 5.1 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.1.tar.gz
Algorithm Hash digest
SHA256 25b01e6be308bb6cc3a998bde4bfca0fc4a96ec3fce47e9f1d2fdd61afb620a8
MD5 2ad2f914319bd3a473e25387e91f9a52
BLAKE2b-256 ded30c592e68e07b20edbfd3f57779ee4d48123bfed19c7341795d0fe87247dc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: koia-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.2 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cc400351a89a0f0db5eb315a57ded0710b6a490d56a43e6c8bb889058f2bc5c4
MD5 5c1574a9a396da59c00f36cbf3d0f35c
BLAKE2b-256 cdeae52748d0f6a87712e8047f0b3133cd0d5f24945a77652afe4d4ba551a554

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