美文网首页
Pyorint 中 OGM使用

Pyorint 中 OGM使用

作者: jiaxiaolei | 来源:发表于2018-02-08 11:56 被阅读40次

基础数据:

image.png

测试脚本 1:

定义一个对应存储结构的class Animal

# -*- coding: utf-8 -*-

import sys
import unittest
import decimal
import os.path
from datetime import datetime

from pyorient import PyOrientCommandException, PyOrientSQLParsingException
from pyorient.ogm import Graph, Config
from pyorient.groovy import GroovyScripts

from pyorient.ogm.declarative import declarative_node, declarative_relationship
from pyorient.ogm.property import (
    String, Date, DateTime, Decimal, Double, Integer, EmbeddedMap, EmbeddedSet,
    Link, UUID)
from pyorient.ogm.what import expand, in_, out, distinct, sysdate

AnimalsNode = declarative_node()
AnimalsRelationship = declarative_relationship()

class Animal(AnimalsNode):
    element_type = 'animal'
    element_plural = 'animals'

    name = String(nullable=False, unique=True)
    specie = String(nullable=False)

def show_res(res):
    if not isinstance(res, list):
        res = [res]
    if not res:
        print 'res is empety, ignore it'


    for item in res:
        print 'name: %s, specie: %s' % (item.name, item.specie)

url = 'plocal://172.28.26.214:2424/test'
g = Graph(Config.from_url(url, 'root', 'root'))
                                           #initial_drop=True))
print 'g: %s, type: %s' % (g, type(g))
print 'Animal: %s, type: %s' % (Animal, type(Animal))

print 'test like'
sql_cmd  = g.query(Animal).filter(Animal.name.like('%测试%'))
print 'sql_cmd: %s, type: %s' % (sql_cmd, type(sql_cmd))    # 这里可以看到,在调用all(), one() 之前,OGM 会预先生成sql 语句
res = sql_cmd.all()
show_res(res)

print 'test like'
res  = g.query(Animal).filter(Animal.name.like('%m%')).all()  # all() 返回符合查询条件的所有结果
show_res(res)

print 'test endswith'
res  = g.query(Animal).filter(Animal.name.endswith('at') ).one()  # 这里主要做结果集唯一的情况做查询
show_res(res)

output:

g: <pyorient.ogm.graph.Graph object at 0x7effc340d350>, type: <class 'pyorient.ogm.graph.Graph'>
Animal: <class '__main__.Animal'>, type: <class 'pyorient.ogm.declarative.DeclarativeMeta'>
test like
sql_cmd: SELECT FROM animal WHERE name like "%\u6d4b\u8bd5%", type: <class 'pyorient.ogm.query.Query'>
name: jia测试, specie: 111
test like
name: mouse, specie: rodent
test endswith
name: rat, specie: rodent

测试脚本2:

# -*- coding: utf-8 -*-

import sys
import unittest
import decimal
import os.path
from datetime import datetime

from pyorient import PyOrientCommandException, PyOrientSQLParsingException
from pyorient.ogm import Graph, Config
from pyorient.groovy import GroovyScripts

from pyorient.ogm.declarative import declarative_node, declarative_relationship
from pyorient.ogm.property import (
    String, Date, DateTime, Decimal, Double, Integer, EmbeddedMap, EmbeddedSet,
    Link, UUID)
from pyorient.ogm.what import expand, in_, out, distinct, sysdate


url = 'plocal://172.28.26.214:2424/test'
graph = Graph(Config.from_url(url, 'root', 'root'))

Node = declarative_node()
Relationship = declarative_relationship()

# 用于更新graph的注册信息
def update_registy():
    """
    :return:
    """
    graph.clear_registry()
    classes_from_schema = graph.build_mapping(
        Node, Relationship, auto_plural=True)
    graph.include(classes_from_schema)


def get_class(class_id):
    """Get OGM Class by class_id.

    :param class_id:
    :return: class
    """
    if not hasattr(graph, class_id):
        update_registy()

    if hasattr(graph, class_id):
        return getattr(graph, class_id)
    else:
        return False


class_id = 'animal'
m_class= get_class(class_id)
print 'class', m_class
print 'class',type( m_class)

# okay
cmd  = m_class.query(name="rat")
print 'cmd', cmd
print cmd.all()

output:


class <pyorient.ogm.broker.VertexBroker object at 0x7f18d1b54e50>
class <class 'pyorient.ogm.broker.VertexBroker'>
cmd SELECT FROM animal WHERE name="rat"
cmd type <class 'pyorient.ogm.query.Query'>
[<pyorient.ogm.graph.animal object at 0x7f18d1b54fd0>]



扩展阅读

https://github.com/mogui/pyorient/blob/master/pyorient/ogm/operators.py

简介:

OGM 实现了大多数的操作(比如常见的equal, like, is, isnot, startswith), 部分高阶用法没有实现(比如 traverse)

相关文章

网友评论

      本文标题:Pyorint 中 OGM使用

      本文链接:https://www.haomeiwen.com/subject/fdtttftx.html