Skip to main content

ORM support for Mssql in python3

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

  • 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)
  • save system
  • auto model creator from existing table
  • 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-0.0.4rc0.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

msorm-0.0.4rc0-py3-none-any.whl (22.7 kB view details)

Uploaded Python 3

File details

Details for the file msorm-0.0.4rc0.tar.gz.

File metadata

  • Download URL: msorm-0.0.4rc0.tar.gz
  • Upload date:
  • Size: 23.8 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-0.0.4rc0.tar.gz
Algorithm Hash digest
SHA256 07d62108582e6b2b52a9f546b052ac64bfdac7dd4b6dca326826cbf8f743cae6
MD5 f8e4cbcbe3c5682bc3335e23d2fb28ea
BLAKE2b-256 07335d0de47a0da7c2a5589b5d0a56c427991fed1f882de7be413caabf3e5e26

See more details on using hashes here.

File details

Details for the file msorm-0.0.4rc0-py3-none-any.whl.

File metadata

  • Download URL: msorm-0.0.4rc0-py3-none-any.whl
  • Upload date:
  • Size: 22.7 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-0.0.4rc0-py3-none-any.whl
Algorithm Hash digest
SHA256 dde1df0f3c3677ff8f0ae904b178d068121ed6619e4929c0685e7eb2ed938685
MD5 6c9a55cf7ccabb1b088b846721d27c78
BLAKE2b-256 7139010e1eff171bc290d8c04b78e90e2202643d5318ddfa45b88b6b8bfebbed

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