Coder Social home page Coder Social logo

pyorm's Introduction

pyorm

pyorm is a simple and basic Django-like ORM (Object Relational Mapper) written in Python from scratch. It supports PostgreSQL and MySQL databases.

Basic Usage

from orm import db_connector
from orm import models
# This is for postgresql database. import the MySQLManager instead for mysql database connection
from orm.managers import PostgreSQLManager
from orm.utils import Q


db_settings = {
    'host': '127.0.0.1', 
    'database': 'test_db', 
    'user': 'test_user', 
    'password': 'test_password'
}
db_server = 'postgresql' 
db_connector.connect(db_server, db_settings)


class User(models.Model):
    table_name = 'users'
    model_manager = PostgreSQLManager  # Use MySQLManager here for mysql database connection.


# SQL: SELECT * FROM users WHERE id = 1;
user = User.objects.get(id=1)

# SQL: SELECT first_name, last_name FROM users WHERE id = 1;
user = User.objects.get(['first_name', 'last_name'], id=1)

# SQL: SELECT * FROM users;
users = User.objects.all()

# SQL: SELECT id, first_name, last_name FROM users;
users = User.objects.all(['id', 'first_name', 'last_name'])

# SQL: SELECT age FROM users WHERE id <= 3;
users = User.objects.filter(['age'], id__lte=3)
users = User.objects.filter(['age'], condition=Q(id__lte=3))

# SQL: SELECT age FROM users WHERE id >= 2 AND id <= 4;
users = User.objects.filter(['age'], id__gte=2, id__lte=4)
users = User.objects.filter(['age'], condition=Q(id__gte=2, id__lte=4))
users = User.objects.filter(['age'], condition=Q(id__gte=2) & Q(id__lte=4))

# SQL: SELECT age FROM users WHERE id < 2 OR id > 3;
users = User.objects.filter('salary', condition=Q(id__lt=2) | Q(id__gt=3))

# SQL: SELECT * FROM users WHERE first_name = last_name;
users = USer.objects.filter(condition=Q(first_name=F('last_name')))

# SQL: SELECT * FROM users WHERE age < id * 10;
users = User.objects.filter(condition=Q(age__lt=F('id')*10))

# SQL: SELECT * FROM users WHERE id IN (3, 4);
users = User.objects.filter(id__in=[3, 4])
users = User.objects.filter(condition=Q(id__in=[3, 4]))

# SQL: INSERT INTO users (first_name, last_name, age)
#          VALUES ('Ahmad', 'Ameen', 26)
# ;
User.objects.create(first_name="Ahmad", last_name="Ameen", age=26)

# SQL: UPDATE user SET age = 27, email = [email protected] WHERE id = 1;
User.objects.update({'age': 27, 'email': '[email protected]'}, id=1) # or
user = User.objects.get(id=1)
user.age = 27
user.email = '[email protected]'
user.save()

# SQL DELETE FROM users WHERE age < 20;
User.objects.delete(age__lt=20)
User.objects.delete(condition=Q(age__lt=20))

# Get the list of the fields in your model
# It corresponds to the columns of the referenced table in database
Users.get_fields()

pyorm's People

Contributors

mameen7 avatar

Stargazers

 avatar

Watchers

 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.