Giter Club home page Giter Club logo

tornpsql's Introduction

tornpsql

Build Status codecov.io

pip install tornpsql

Featuring

Usage

# Method 1
import tornpsql
db = tornpsql.Connection("postgres://postgres-user:[email protected]:5432/postgres-db")
import tornpsql
db = tornpsql.Connection("127.0.0.1", "database", "postgres", "postgres-user", 5432)
# get one
db.get("SELECT col from table where col = %s limit 1", value)
# >>> {"col": "value"}

# get many
db.query("SELECT col from table where col = %s limit 2", value)
# >>> [{"col": "value"}, {"col": "value"}]

Set search_path

Set the search_path for the duration of the proceeding query.

# set the "default" search_path to public
db = tornpsql.Connection(search_path="public")
results = db.path("another_schema").query("select column from mytable")
results = db.path("another_schema,and_another").query("select column from mytable")
# this will use search path: "public"
results = db.query("select column from mytable")

Query Files

-- main.sql
create table example (id serial primary key);
\ir other.sql
-- other.sql
insert into example values (1, 2, 3);
db = tornpsql.Connection()
db.file("main.sql")

PubSub

db = tornpsql.Connection()
pubsub = db.pubsub()
pubsub.subscribe( ("channel_1", ) )
for notify in pubsub.listen():
    print notify.pid, notify.channel, notify.payload

Via Threading

class PubSubThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.db = tornpsql.Connection()
        self.pubsub = self.db.pubsub()
        self.pubsub.subscribe( ("channel_1", "channel_2") )

    def run(self):
        for notify in self.pubsub.listen():
            print notify.pid, notify.channel, notify.payload


pubsub = PubSubThread()

tornpsql's People

Contributors

stevepeak avatar ysimonson avatar bdarnell avatar codecov-io avatar

Watchers

James Cloos avatar Eli Hooten avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.