Gorose, most like Laravel's eloquent's Go database Operation Orm, the coquettish chain call, let you deep into extricate oneself

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. Gorose (Go ORM), a small and powerful Go language database operation Orm, inspired by the Laravel database operation Orm, that is eloquent, PHP, Python, Ruby, will like this ORM operation mode, Mainly chain operation is more coquettish-[English document] (https://github.com/gohouse/gorose)-[Chinese documents] (HTTPS://GITHUB.COM/GOHOUSE/GOROSE/BLOB /MASTER/README-ZH_CN.MD) # # Install-install gorose ' gogo get github.com/gohouse/gorose '-the Function toolkit used in install Gorose ' Gogo get Github.com/gohouse/utils ' # # Configuration and example-Multiple database connection configurations ' Goimport "Github.com/gohouse/gorose" var dbconfig = map[string]map[ string]string {"MySQL": {"host": "localhost", "username": "root", "password": "", "Port": "3306", "Database": "Test", "C Harset ":" UTF8 "," protocol ":" TCP ",}," Mysql_dev ": {" host ":" localhost "," username ":" root "," password ":" "," Port ":" 33 "," "Database": "Gorose", "CharSet": "UTF8", "protocol": "TCP",},}gorose. Open (dbconfig, "MySQL") var db gorose. Databasefunc Main () {res: = db. Table ("Users"). First () Fmt. Println (RES)} '-Simple but database configuration ' ' Gogorose. Open (map[string]string {"host": "localhost", "username": "Root", "Password": "", "Port": "3306", "Database": "Test", "CharSet": "UTF8", "protocol": "TCP", "{") "# # Usage Example # # # # # # # # # # # # # # # # # # # # # # # # # # # # # "Godb". Query ("SELECT * from user where id = 1") ' # # # # # # # # Godb. Table ("User"). Field ("ID, name"). Field Where ("id", ">", 1). Simple where WHERE (map[string]interface{}{"name": "Fizzday", "Age": 18}). where object where ([]map[string]interface{}{{"website", "like", "fizz"}, {"Job", "It"}}). Multi where WHERE ("head = 3 or rate is not NULL"). where string Orwhere ("Cash", "1000000"). or where ... Orwhere ("Score", "between", []string{50, 80}). Between Orwhere ("role", "Not in", []string{"admin", "read"}). In Group ("job"). Group Order ("Age ASC"). Order Limit (10). Limit Offset (1). Offset get ()//Fetch multi rows ' Get SQL result: ' goselect id,name from user where (id>1) and (Name= ' fizzday ' and age= ' (Website like '%fizz% ') and (job= ' it's)) and (head =3 or rate are NOT null) or (cash = ' 100000 ') or (score between ' 100 ') or (Role not in (' admin ', ' read ')) the group by job order by age ASC Limit offset 1 ' # # * More chained query example-get User Table object ' Gouser: = db. Table ("user") "-Queries A" ' Gouser.first ()//or DB. FISRT () ' Parse SQL result: ' SELECT * from user Limit 1 '-count statistic ' gouser.count ("*")//or (same as) db. The final SQL executed by count ("*") is: ' Select COUNT (*) as count from user '-Max ' ' Gouser.max ("Age") "" is ultimately executed by: ' Select Max (age) as Ma ' X from the user '-Min ' gouser.min ("Age") "" is the final execution of the SQL: ' Select min ' as min from user '-Avg ' ' Gouser.avg ("Age") "" L is: ' Select AVG (age) as Avg from user '-distinct ' ' Gouser.fields ("ID, name"). Distinct () "Finally executes the SQL: ' Select Distinct id,name from user ' # # # # nested where's query (where nested) ' Godb. Table ("User"). Where ("id", ">", 1). Where (func () {db). Where ("name", "Fizz"). Orwhere (func () {db). Where ("name", "FIZZ2"). Where (func () {db). Where ("name", "Fizz3"). Orwhere ("website", "Fizzday")})}). Where ("Job", "it"). The final SQL executed by first () is: ' goselect * from user WHERE ID > ' 1 ' and (name = ' Fizz ' or (name =' Fizz2 ' and (name = ' fizz3 ' or website like '%fizzday% ')) and job = ' It ' LIMIT 1 ' # # # # # # # transactions-standard usage ' godb. Begin () Res: = db. Table ("User"). Where ("id", 1). Data (map[string]interface{}{"age": 18}). Update () if (res = = 0) {db. Rollback ()}res2: = db. Table ("User"). Data (map[string]interface{}{"age": 18}). Insert () if (Res2 = = 0) {db. Rollback ()}db.commit () "-simple usage, implemented with closures, automatically starts a transaction, rolls back or commits the transaction ' ' Godb. Transaction (func () {db). Execute ("Update area set job= ' SADF ' where id=14") db. Table ("area"). Data (map[string]interface{}{"names": "Fizz3", "Age": 3}). Insert () db. Table ("area"). Data (map[string]interface{}{"names": "Fizz3", "Age": 3}). Where ("id", 10). Update ()}) ' # # # # # # # # # # # # # # # # Native SQL string ' Godb. Execute ("Update user set job= ' it2 ' where id=3") "# # # # # # # # # # # # # Godb. Table ("User"). Data (map[string]interface{}{"age": +, "job": "It3"}). Where ("id", 1). Orwhere ("Age", ">", 30). Update () "" Finally executes the SQL: ' Update user set age=17, job= ' Ite3 ' where (id=1) or (age>30) ' # # # # More additions and deletions to the usage-insert ' goUser.Dat A (map[string]interface{}{"Age": +, "job": "It3"}). Insert () user.data ([]map[string]interface{}{{"age": +, "job": "It3"},{"age": +, "job": "IT4"}). Insert () "' finally executes the sql: ' ' goinsert into user ' values (+, ' it3 ') insert INTO user (" Age, Job ") VALUES (1, ' it3 ') 7, ' IT4 ') '-delete ' gouser.where ("id", 5). Delete () "" will eventually execute SQL as: ' delete from user where id=5 ' # # Toggle Database Connection ' go//connect the first configuration of the second link (Mysql_dev is key) db. Connect ("Mysql_dev"). Table (). First ()//or direct Input connection configuration db. Connect (map[string]string {"host": "localhost", "username": "root", "password": "", "Port": "3306", "Database": "Test", " CharSet ":" UTF8 "," protocol ":" TCP ",}). Table (). First () "------------# # # # # [Click to see the latest update] (https://github.com/gohouse/gorose) 587 reads  ∙  1 likes  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.