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

  • get method
  • count method
  • 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.3rc1.tar.gz (23.5 kB view details)

Uploaded Source

Built Distribution

msorm-0.0.3rc1-py3-none-any.whl (22.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: msorm-0.0.3rc1.tar.gz
  • Upload date:
  • Size: 23.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-0.0.3rc1.tar.gz
Algorithm Hash digest
SHA256 7fb6453d025ca4258c7a8fab3d4ead8aeff7ae9ce094f741968a620aa9d8523f
MD5 a7b3b49cbb045fc4ff0c5742294acff6
BLAKE2b-256 c77930afe5171c93d144a93b47ba4d233e53b77b72dc13b79dbcac0bfc278b69

See more details on using hashes here.

File details

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

File metadata

  • Download URL: msorm-0.0.3rc1-py3-none-any.whl
  • Upload date:
  • Size: 22.4 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.3rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 095ebef29e26d8e518d9d229320d7b0084ddfed19342e32850a87bddde776b0d
MD5 0f729bc0bc991a5356201f331690b96d
BLAKE2b-256 ab12375078dbb6dc232caad001e0e172b110a85c054109246fed03953f78f608

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