Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Simple and Powerful ORM for Go Language

License

NotificationsYou must be signed in to change notification settings

MehmetEminKaymaz/Orca

Repository files navigation

Image of Orca

Orca

Simple and Powerful ORM for Go Language (MongoDB,SQLite,RedisDB and Memcache)

Install

 go get github.com/MehmetEminKaymaz/Orca

Define your structs

typePersonstruct {NamestringAgeintMyHeadHeadFriendsName []string}typeHeadstruct {ValueBrain}typeBrainstruct {IQint}

then use Orca to perform CRUD operations

Database Operations

Firstly create some variables.

//create a personsomeone:=Person{Age:99,Name:"Gandalf",FriendsName: []string{"Frodo","Sam","Galadriel","Radagast"},MyHead:Head{Value:Brain{IQ:1000,//:)},},}newPerson:=someonenewPerson.Name="Saruman"

SQLite

SQLiteDB,err:=Orca.Use(Orca.SQLite,Orca.SetSQLiteOptions("sqlite3","./MyDb.db"))//get database_=errmyPersonList:=SQLiteDB.GetCollection(Person{},"Person")//if there is no table in the database to store a person, it creates it//if there is a table, it goes to the table and returns a collection of personmyPersonList.Add(someone)//it inserts person to databasemyPersonList.Update(someone,newPerson)//updated!myPersonList.Delete(newPerson)//newPerson deleted from database.myPersonList.Clear()//it delete all tuples in table(person table).myPersonList.ToSlice()//it return a slice of person

MongoDB

MyMongoDB,err:=Orca.Use(Orca.MongoDB,Orca.SetMongoDBOptions("mongodb://127.0.0.1","myMongo"))//get database_=errmyPersonList:=MyMongoDB.GetCollection(Person{},"Person")//if there is no collection in the database to store a person, it creates it//if there is a collection, it goes to the collection and returns a collection of personmyPersonList.Add(someone)//it inserts person to databasemyPersonList.AddRange(SliceOfPerson)//it inserts multiple person in one transactionmyPersonList.Update(someone,newPerson)//updated!myPersonList.Delete(newPerson)//newPerson deleted from database.myPersonList.Clear()//it delete all tuples in table(person table).myPersonList.ToSlice()//it return a slice of person

RedisDB

MyRedisDB,err:=Orca.Use(Orca.Redis,Orca.SetRedisDBOptions("localhost:6379","","0"))//get database_=errmyPersonList:=MyRedisDB.GetCollection(":","Person")//first parameter is seperator , second is relationmyPersonList.Add(Orca.NewRedisVariable{Key :"150316",Value :"Gandalf",ExpirationTime:0,})//it insert to database as => Key =  Person:150316 and Value = Gandalf//so , result of the 'get "Person:150316"' code is  "Gandalf".//if you do not want to use relation and seperator set empty all , example :myPersonList:=MyRedisDB.GetCollection("","")myPersonList.AddRange([]Orca.NewRedisVariable{...})//add multiple key and values to databasemyPersonList.Delete(...)//it delete from database.myPersonList.Clear()//it deletes only person if you use relation , otherwise it delete all key-value pairsmyPerson.Update(oldValue,newValue)//updated!

Memcache

Orca also support Add,AddRange,Clear,Delete,Update method for Memcache DB.The only difference between Redis and memcache usage is Orca.NewMemCacheVariable declaration for key-value pairs.But you can use Orca.NewMemCacheVariable in Redis operations or Orca.NewRedisVariable in Memcache operations.

Example :

MemCachedDB,err:=Orca.Use(Orca.MemCached,Orca.SetMemCachedDBOptions("localhost:11211"))_=errmyPersonList:=MemCachedDB.GetCollection(":","Person")//same features (seperator and relation)myPersonList.Add(Orca.NewRedisVariable{..})//correct!myPersonList.Add(Orca.NewMemCacheVariable{..})//correct!//others same...(AddRange,Clear,Update,Delete,ToSlice etc.)

LocalHook Usage in ORCA

Orca offers the programmer the following localhooks :

  • BeforeAdd
  • AfterAdd
  • BeforeAddRange
  • AfterAddRange
  • BeforeUpdate
  • AfterUpdate
  • BeforeDelete
  • AfterDelete

What is LocalHook ?

If you need run a method/function inside crud operation functions(Add,AddRange,Delete etc.) , you must use LocalHooks.

Orca.NewLocalHook :
First parameter is LocalHook id (like key) , if you need to find your hook you need the key.
Second parameter is Priority you can add many BeforeAdd hook , Orca execute all hooks in order.
0 is Higher priority.
Third is Hook location (beforeAdd,afterAdd, etc.)
The last is function.

LocalHook Example with SQLite

SQLiteDB,err:=Orca.Use(Orca.SQLite,Orca.SetSQLiteOptions("sqlite3","./MyDb.db"))//get database_=errSQLiteDB.AddLocalHook(Orca.NewLocalHook("myHook",0,Orca.BeforeAdd,func(iinterface{})interface{} {returni}))//add BeforeAdd localhook.//The function run before all add operations and it takes the value to be added parameter as empty interface.//you can add the other hooks using the same way.SQLiteDB.AddLocalHooks(...)//add multiple localhooks at same time.SQliteDB.DeleteHook(string)//delete hook using idSQliteDB.DeleteHooks(...string)//delete hooks

LocalHook usage is same for the other databases (RedisDB,MongoDB,Memcache...)

Generic Tools in ORCA

Orca offers 4 type of generic tool to manipulate results without change database.These Tools are :

  • LazyList : LazyList is lazy :) it does not perform until call Do function.It stores functions to execute after called do function.
  • LinkedList : A list implemented by using go/list package.
  • ImmutableList : ImmutableList is easy to make thread safe operations.Methods access the copy of the source.
  • MutableList :MutableList methods access the source using pointers.

Example :

ImmutableList.NewImmutableList(myPersonList.ToSlice())MutableList.NewMutableList(myPersonList.ToSlice())LazyList.NewLazyList(myPersonList.ToSlice())LinkedList.NewLinkedList(myPersonList.ToSlice())
mylist:=MutableList.NewMutableList(myPersonList.ToSlice())mylist.Where(func(xinterface{})bool {returnx.(Person).Age<30    })//it returns slice of person where age smaller than 30//and more (Contains,Distict,ElementAt,Exist,Foreach,RemoveAt,Skip,Take etc..)

Finally print the result :

       fmt.println(mylist.ToSlice())

[8]ページ先頭

©2009-2025 Movatter.jp