Skip to main content

Allows you to create objects for parts of SQL query commands. Also to combine these objects by joining them, adding or removing parts...

Project description

SQL_Blocks

1 - You can assemble a simple object that will then be converted into an SQL command:

a = Select('Actor') # --> SELECT * FROM Actor act

Note that an alias "act" has been added.

You can specify your own alias: a = Select('Actor a')


2 - You can also add a field, like this...

  • a = Select('Actor a', name=Field)

  • Here are another ways to add a field:

    • Select('Actor a', name=Distinct )

    • Select('Actor a', name=NamedField('actors_name'))

    • Select( 'Actor a', name=NamedField('actors_name', Distinct) )


3 - To set conditions, use Where:

  • For example, a = Select(... age=Where.gt(45) )

    Some possible conditions:

    • field=Where.eq(value) - ...the field is EQUAL to the value;
    • field=Where.gt(value) - ...the field is GREATER than the value;
    • field=Where.lt(value) - ...the field is LESS than the value;

    3.1 -- If you want to filter the field on a range of values:

    a = Select( 'Actor a', age=Between(45, 69) )

    3.2 -- Sub-queries:

query = Select('Movie m', title=Field,
    id=SubSelect(
        'Review r',
        rate=Where.gt(4.5),
        movie_id=Distinct
    )
)

>> print(query)

    SELECT
        m.title
    FROM
        Movie m
    WHERE
        m.id IN (
            SELECT DISTINCT r.movie
            FROM Review r WHERE r.rate > 4.5
        )

3.3 -- Optional conditions:

    OR=Options(
        genre=Where.eq("Sci-Fi"),
        awards=Where.like("Oscar")
    )

Could be AND=Options(...)

3.4 -- Negative conditions use the Not class instead of Where

franchise
based_on_book=Not.is_null()

3.5 -- List of values hash_tag=Where.list(['space', 'monster', 'gore'])


4 - A field can be two things at the same time:

  • m = Select('Movie m' release_date=[Field, OrderBy])
    • This means that the field will appear in the results and also that the query will be ordered by that field.
  • Applying GROUP BY to item 3.2, it would look like this:
    SubSelect(
        'Review r', movie=[GroupBy, Distinct],
        rate=Having.avg(Where.gt(4.5))
    )
    

5 - Relationships:

    query = Select('Actor a', name=Field,
        cast=Select('Cast c', id=PrimaryKey)
    )

>> print(query)

SELECT
    a.name
FROM
    Actor a
    JOIN Cast c ON (a.cast = c.id)    

6 - The reverse process (parse):

text = """
        SELECT
                cas.role,
                m.title,
                m.release_date,
                a.name as actors_name
        FROM
                Actor a
                LEFT JOIN Cast cas ON (a.cast = cas.id)
                LEFT JOIN Movie m ON (cas.movie = m.id)
        WHERE
                (
                    m.genre = 'Sci-Fi'
                    OR
                    m.awards LIKE '%Oscar%'
                )
                AND a.age <= 69 AND a.age >= 45
        ORDER BY
                m.release_date DESC
"""

a, c, m = Select.parse(text)

6.1 --- print(a)

    SELECT
            a.name as actors_name
    FROM
            Actor a
    WHERE
            a.age <= 69
            AND a.age >= 45

6.2 --- print(c)

SELECT
        c.role
FROM
        Cast c

6.3 --- print(m)

SELECT
        m.title,
        m.release_date
FROM
        Movie m
WHERE
        ( m.genre = 'Sci-Fi' OR m.awards LIKE '%Oscar%' )
ORDER BY
        m.release_date DESC

6.4 --- print(a+c)

SELECT
        a.name as actors_name,
        cas.role
FROM
        Actor a
        JOIN Cast cas ON (a.cast = cas.id)
WHERE
        a.age >= 45
        AND a.age <= 69

6.5 --- print(c+m)

... or print(m+c)

SELECT
        cas.role,
        m.title,
        m.release_date,
        m.director
FROM
        Cast cas
        JOIN Movie m ON (cas.movie = m.id)
WHERE
        ( m.genre = 'Sci-Fi' OR m.awards LIKE '%Oscar%' )
        AND m.director LIKE '%Coppola%'
ORDER BY
        m.release_date,
        m.director

7 - You can add or delete attributes directly in objects:

  • a(gender=Field)
  • m.delete('director')

8 - Defining relationship on separate objects:

a = Select...
c = Select...
m = Select...

a + c => ERROR: "No relationship found between Actor and Cast"

8.1 - But...

a( cast=ForeignKey('Cast') )
c(id=PrimaryKey)

a + c => Ok!

8.2

c( movie=ForeignKey('Movie') )
m(id=PrimaryKey)

c + m => Ok!

m + c => Ok!


9 - Comparing objects

9.1

        a1 = Select.parse('''
                SELECT gender, max(age) FROM Actor act
                WHERE act.age <= 69 AND act.age >= 45
                GROUP BY gender
            ''')[0]

        a2 = Select('Actor',
            age=Between(45, 69), gender=GroupBy,
            gender=[GroupBy, Field]
        )       

a1 == a2 # --- True!

9.2

    m1 = Select.parse("""
        SELECT title, release_date FROM Movie m ORDER BY release_date 
        WHERE m.genre = 'Sci-Fi' AND m.awards LIKE '%Oscar%'
    """)[0]

    m2 = Select.parse("""
        SELECT release_date, title
        FROM Movie m
        WHERE m.awards LIKE '%Oscar%' AND m.genre = 'Sci-Fi'
        ORDER BY release_date 
    """)[0]

m1 == m2 # --- True!

9.3

best_movies = SubSelect(
    Review=Table('role'),
    rate=[GroupBy, Having.avg(Where.gt(4.5))]
)
m1 = Select(
    Movie=Table('title,release_date),
    id=best_movies
)

sql = "SELECT rev.role FROM Review rev GROUP BY rev.rate HAVING Avg(rev.rate) > 4.5"
m2 = Select(
    'Movie', release_date=Field, title=Field,
    id=Where(f"IN ({sql})")
)

m1 == m2 # --- True!


10 - CASE...WHEN...THEN

Select( 'Product', label=Case('price').when( lt(50), 'cheap' ).when( gt(100), 'expensive' ).else_value( 'normal' ) )

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

sql_blocks-0.0.3.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

sql_blocks-0.0.3-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file sql_blocks-0.0.3.tar.gz.

File metadata

  • Download URL: sql_blocks-0.0.3.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for sql_blocks-0.0.3.tar.gz
Algorithm Hash digest
SHA256 2e0ffe68c71ee100cab6478f610e22cf368350526ea8720581d3e3170ea42bce
MD5 f8e63745db88d0afe7a0436e157c45b6
BLAKE2b-256 2b6bc65df2ed5352bacfbdabded969aaf2291003a5b8a866ef11a327fd289609

See more details on using hashes here.

File details

Details for the file sql_blocks-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: sql_blocks-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for sql_blocks-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e70109cb7c779c57051e28470ba7f8b9db56d734f63b25d7b327472e58da95f7
MD5 f1fcf92ae9b3daed7c487d2588254172
BLAKE2b-256 f2702c3ef549076d7cf08a36694126a1d511f605b42444825c7014e29a6a6e11

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