Skip to content

Toy Orm,All basic function are .... not be implemented

Notifications You must be signed in to change notification settings

DrunkenWhale/Parsley

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Parsley

Delicate Orm,All basic function are .... not be implemented

Connect

import com.parsley.connect.DataBaseManager
import com.parsley.connect.connection.MysqlConnection

DataBaseManager.register(
  MysqlConnection(database = "DatabaseName", password = "password")
)

CURD

Create

use table[T]

import com.parsley.orm.DSL

case class Person(name: String, age: Int)
// model must be case class
// for the pure function!

val persons = table[Person]

on(persons)(person => declare(
    person.name is primaryKey,
    person.age is indexed
))

create(persons)

this code equals execute SQL:

CREATE TABLE IF NOT EXISTS `Person`(
    `name` CHAR(128) PRIMARY KEY ,
    `age` INT,
    INDEX(`age`)
);

Query

query all columns:

persons.query(*)

return List[Person]

with limit:

persons.query("age" === 114514 limit 1)

will return List[Person] include all columns that age = 114514

Insert

val person = Person("野兽前辈",114514)
persons.insert(person)

insert new column in Person table this column's name = "野兽前辈",age=114514

Update

persons.update("age"==>114514)(*)

this operation will set all columns' age == 114514 in table Person

Delete

delete all columns in table Person

persons.delete(*)

delete special column

persons.delete("age"===114514)

this operation will delete all columns that age == 114514

OneToMany

for example,you have two tables like this:

val books = table[Book]
val students = table[Student]
on(students)(student => declare(
    student.name is primaryKey
))
on(books)(book => declare(
    book.name is primaryKey
))

now, you can declare relation about them before create

students <== books

you will build a OneToMany between table students and books

                +---- book(aaa)  
student(???)--------- book(bbb)  
                +---- book(ccc)  

one student can relate many book

relate two object

val student = Student("反射魔典", 514)
val book = Book("昏睡红茶",1145141919)
DataBase.students.insertRelation(student)(book)

now you will insert a new column in table Book,and you can use this student instance to get it

query by relation

DataBase.students.queryRelation[Book](student)

you will get all columns which is related to instance student

ManyToMany

Create

students <==> books

Insert

students.relatedManyToMany(student)(book)

create a ManyToMany RelationShip between instance student and book

query

students.queryManyToManyRelation[Book](student)

get a List[Book] have all instance related to this student instance

About

Toy Orm,All basic function are .... not be implemented

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages