Skip to main content

ORM support for Mssql in python3

Reason this release was yanked:

completed alpha version is out

Project description

# Microsoft SQL Server Object Related Mapping

MSORM

msorm is a basic MSSQL Object Related Mapper for Python3.x versions. With msorm, existing mssql database can be easily mapped with django style model system. It is still an alpha project Click For source code and pre-releases of msorm.

Installation

You can install the msorm from PyPI:

pip install msorm

msorm is supported on Python 3.7 and above.

How to use

To use msorm first of all you have to create python file (which is prefered name is "models.py"). After that you have to initialize msorm :

models.init(ip_adress or server_name, database_name,username, password)

After initialization, you can start writing your own models. But models' name must have the same name as their representatives in MSSQL. (without dbo tag in front of the table name).

But before creating the models you should know the fields. The only field that came from MSSQL is the Foreign key for the alpha version. The other fields are only reflections of MSSQL fields on python variable types.

FIELDS

Every field but the foreign key has a value parameter to be assigned as the value of the key in the database. Fields:

from msorm.type_fields import Fields

#value parameter is not a requirement
Fields.int(value=default_value)

#value parameter is not a requirement
Fields.str(value=default_value)

#value parameter is not a requirement
Fields.bool(value=default_value)

#value parameter is not a requirement
Fields.float(value=default_value)

#value parameter is not a requirement
#model variable is a requirement. And it uses for accesing the foreign table from id.
Fields.foreignKey(value=default_value,model=table_model)

In newer versions of msorm, fields probably will be changed with their better version that has better capabilities to manage MSSQL with ORM

CREATING THE MODELS

from msorm import models  
from msorm.type_fields import Fields  

models.init(server, database, username, password)  

class Server(models.Model):  
    serverID = Fields.int()  
    languageID = Fields.int()  
    active = Fields.bool()  
    welcomeMessage = Fields.str()
	welcomeMessagePrivate = Fields.bool()  
    premiumEndDate = Fields.str()  
    isBlockedAllURL = Fields.bool()  
    description = Fields.str()  
    tags = Fields.str()  
    website = Fields.str()  
    updateUUID = Fields.str()  


class Announce(models.Model):  
    announceID = Fields.int() 
    serverID = Fields.foreignKey(model=Server, name="serverID")  
    channelID = Fields.int()  
    loopHours = Fields.int()  
    text = Fields.str()  
    title = Fields.str()  

HOW TO MAKE QUERIES

To make queries, In the alpha version msorm has only two methods. These are "where" and "all". Both methods have 'fields' parameter:

'fields' PARAMETER

'fields' parameter gets a tuple which includes fields that are wanted to be pulled from the database . When filled 'fields' parameter with field names, msorm sets values of the fields as none which is not included in the tuple.

WHAT DO METHODS RETURN?

They return the collection of the model, which encapsulated as QueryDict

QueryDict

A QueryDict which holds a collection of models, which all() method used on, and it can be iterated with for loop. Also QueryDict has six methods for easy accessing and managing the collections of the method. These methods are find(field, value), get(field, value), remove(field, value), pop(field, value), values(*fields), dicts(*fields).

field: The name of the field holds the value
value: wanted value of field

find(field, value)

find(field, value) method is used for filtering the model instances after queries. And it returns a new QueryDict filled with filtered models.

get(field, value)

get(field, value) method is used for filtering the model instances after queries but it returns first model instance it found.

remove(field, value)

remove(field, value) method is used for removing the model instance. The method remove the first instance it found.

pop(field, value)

pop(field, value) method is used for removing the model instance. The method removes and returns the first instance it found.

values(*fields)

values(*fields) method is used for retrieving values from QueryDict as a collection of tuples. 'fields' parameter gets a tuple which includes fields that are wanted to be pulled from the QueryDict.

dicts(*fields)

dicts(*fields) method is used for retrieving values from QueryDict as a tuple of dictionaries that hold fields and their values for every instance. 'fields' parameter gets a tuple which includes fields that are wanted to be pulled from the QueryDict.

HOW TO USE ALL(*fields) AND WHERE(*args,**kwargs) METHODS

HOW TO USE ALL(*fields) METHOD

Announce.all() # Without using 'fields' parameter
Announce.all("field_name")

HOW TO USE WHERE(*args,**kwargs)

To use where you can use **kwargs variable which represents collections of {field_name/filters:value}. But to be able to use all featuers of where(*args,**kwargs) method, use filters.

Filters

filedname	    : SELECT * FROM table WHERE (key=value) 

fieldname__gt   : SELECT * FROM table WHERE (key>value)

fieldname__gte  : SELECT * FROM table WHERE (key>=value)

fieldname__lt   : SELECT * FROM table WHERE (key<value)

fieldname__lte  : SELECT * FROM table WHERE (key<=value)

fieldname__not	: SELECT * FROM table WHERE (key!=value)

fieldname__in 	: SELECT * FROM table WHERE (key IN (tuple of given values))

fieldname__not_in: SELECT * FROM table WHERE (key NOT IN (tuple of given values))
#NOT IMPLEMENTED YET (v0.0.2a0)#
fieldname__like : SELECT * FROM table WHERE (key LIKE given pattern)

with filters where(*args,**kwargs) method can be used like these:

# There is no limit for fields or filters combination #

model_name.where(field1=value,field2=value2,field3=value3)

model_name.where(field1=value,field2__gt=value2,field3__gte=value3)

model_name.where(field1=value,field2__lt=value2,field3__lte=value3)

model_name.where(field1=value,field2__not=value2,field3__notin=value3)

model_name.where(field1=value,field2__in=value2,field3__like=value3)

Also where(*args,**kwargs) method supports the special operators but all of them will probably be deprecated except OR(*other_operators, **kwargs) in newer versions.

HOW TO USE OR(*other_operators, **kwargs)

other_operators parameter will probably deprecated in the newer versions. **kwargs variable represents collections of {field_name/filters:value}. OR operators can be used like this:

# There is no limit for fields or filters combination #

model_name.where(OR(field1=value)|OR(field1=value2)|OR(field1=value3))

model_name.where(OR(field1=value)|OR(field1__gt=value2)|OR(field1__gte=value3))

model_name.where(OR(field1=value)|OR(field1__lt=value2)|OR(field1__lte=value3))

model_name.where(OR(field1=value)|OR(field1__not=value2)|OR(field1__notin=value3))

model_name.where(OR(field1=value)|OR(field1__in=value2)|OR(field1__like=value3))

TARGET FEATURES FOR VERSION 1.0.4a0

  • Added get method
  • Added first method
  • Added count method to Model class
  • Improved QueryDict class's dicts and values method
  • Added dict() and values() method to Model class
  • For dict() and dicts() methods, added depth parameter. (with depth parameter, related tables can be serialize as dictionary)
  • Added metadata via __init_subclass to make it easier to access the field's value except for foreign field.
  • Version data changed for better understanding of distributions of msorm.
  • QueryObject's find, get, remove and pop functions are changed. Now, to find the wanted obj you have to give a lambda function. Ex: QueryObject.get(lambda x: x.field_name == 1)
  • if QueryObject's get function can't succeed to find the wanted obj, it will raise ItemNotFound (ItemNotFoundException)
  • Operators removed and OR was moved to models.py.
  • Added __safe parameter for model _init_ function to understand if _init_ is called by a query or hand
  • type_fields.py renamed as mssql_fields.py
  • All fields were moved to Fields class which is in models.py
  • Added developer usage for field class, now it can be called directly for creating test models
  • Added settings.py
  • With settings.py, added _MFA_ and _MFL_ to check if value is suitable for the field
  • Now, all fields except foreign key have produce method to check if value is suitable for the field.
  • Added most of the fields of MSSQL to msorm
  • Changed versioning system to Semantic Versioning
  • Still working on automatic model creation from existing databases.
  • save system
  • Migration Support

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

msorm-1.0.4rc1.tar.gz (26.5 kB view details)

Uploaded Source

Built Distribution

msorm-1.0.4rc1-py3-none-any.whl (27.0 kB view details)

Uploaded Python 3

File details

Details for the file msorm-1.0.4rc1.tar.gz.

File metadata

  • Download URL: msorm-1.0.4rc1.tar.gz
  • Upload date:
  • Size: 26.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.1rc2

File hashes

Hashes for msorm-1.0.4rc1.tar.gz
Algorithm Hash digest
SHA256 27639bd33c8dc67d98da6fa87bcc94be1c860d1a3b4a94bc25d2122ad2f3396b
MD5 b185a0f44722250a4b5b726c8beedfe6
BLAKE2b-256 15343088f682ca7a1596ce956c159c2f2bae2c77bdb106c932ac39e43247f247

See more details on using hashes here.

File details

Details for the file msorm-1.0.4rc1-py3-none-any.whl.

File metadata

  • Download URL: msorm-1.0.4rc1-py3-none-any.whl
  • Upload date:
  • Size: 27.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.7.1rc2

File hashes

Hashes for msorm-1.0.4rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 7215c1772c179a23751dc53394bfa7519295fcc22024a7393c5c735b9b24b141
MD5 f87282cdcfffe0126749918a7704fb0b
BLAKE2b-256 96ae538b643aa9cdc16f9789d8b015425f87d544b5e5060f7e0ee50a0823204f

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