github.com/goplus/yap@v0.8.1/ydb/demo/foo/gop_autogen.go (about)

     1  // Code generated by gop (Go+); DO NOT EDIT.
     2  
     3  package main
     4  
     5  import (
     6  	"crypto/hmac"
     7  	"crypto/sha256"
     8  	"encoding/base64"
     9  	"errors"
    10  	"github.com/goplus/yap/test"
    11  	"github.com/goplus/yap/ydb"
    12  	_ "github.com/goplus/yap/ydb/mysql"
    13  	_ "github.com/goplus/yap/ydb/sqlite3"
    14  	"github.com/qiniu/x/stringutil"
    15  	"log"
    16  	"math/rand"
    17  	"sort"
    18  	"strconv"
    19  	"time"
    20  )
    21  
    22  const _ = true
    23  
    24  type ArticleEntry struct {
    25  	Id     string `CHAR(32) UNIQUE`
    26  	Author string `CHAR(24) INDEX`
    27  	Title  string
    28  	Ctime  time.Time `DATETIME INDEX`
    29  }
    30  type Article struct {
    31  	ArticleEntry
    32  	Body []byte `LONGBLOB`
    33  }
    34  type Tag struct {
    35  	Name string `CHAR(24) UNIQUE(doc)`
    36  	Doc  string `CHAR(32)`
    37  }
    38  type User struct {
    39  	Id       string `id CHAR(32) UNIQUE`
    40  	Spwd     string
    41  	Salt     string
    42  	Nickname string
    43  	Email    string    `CHAR(64) INDEX`
    44  	Tel      string    `CHAR(16) INDEX`
    45  	Born     time.Time `INDEX`
    46  	Ctime    time.Time `DATETIME(6) INDEX`
    47  }
    48  type articles struct {
    49  	ydb.Class
    50  	*AppGen
    51  }
    52  type users struct {
    53  	ydb.Class
    54  	*AppGen
    55  }
    56  type AppGen struct {
    57  	ydb.AppGen
    58  }
    59  //line ydb/demo/foo/articles_ydb.gox:22:1
    60  func (this *articles) API_Add(doc Article) {
    61  //line ydb/demo/foo/articles_ydb.gox:23:1
    62  	this.Insert(doc)
    63  }
    64  //line ydb/demo/foo/articles_ydb.gox:26:1
    65  func (this *articles) API_Get(docId string) (doc Article, err error) {
    66  //line ydb/demo/foo/articles_ydb.gox:27:1
    67  	this.Query("id=?", docId)
    68  //line ydb/demo/foo/articles_ydb.gox:28:1
    69  	this.Ret(&doc)
    70  //line ydb/demo/foo/articles_ydb.gox:29:1
    71  	err = this.LastErr()
    72  //line ydb/demo/foo/articles_ydb.gox:30:1
    73  	return
    74  }
    75  //line ydb/demo/foo/articles_ydb.gox:33:1
    76  func (this *articles) API_SetTags(docId string, tags ...string) {
    77  //line ydb/demo/foo/articles_ydb.gox:34:1
    78  	var oldtags []string
    79  //line ydb/demo/foo/articles_ydb.gox:35:1
    80  	this.Query("tag.doc=?", docId)
    81  //line ydb/demo/foo/articles_ydb.gox:36:1
    82  	this.Ret("tag.name", &oldtags)
    83  //line ydb/demo/foo/articles_ydb.gox:38:1
    84  	tagsAdd, tagsDel := Diff(tags, oldtags)
    85  //line ydb/demo/foo/articles_ydb.gox:39:1
    86  	Info("oldtags:", oldtags, "tags:", tags, "add:", tagsAdd, "del:", tagsDel)
    87  //line ydb/demo/foo/articles_ydb.gox:41:1
    88  	this.Delete("tag.name=?", tagsDel)
    89  //line ydb/demo/foo/articles_ydb.gox:42:1
    90  	this.Insert("tag.doc", docId, "tag.name", tagsAdd)
    91  }
    92  //line ydb/demo/foo/articles_ydb.gox:45:1
    93  func (this *articles) API_Tags(docId string) (tags []string) {
    94  //line ydb/demo/foo/articles_ydb.gox:46:1
    95  	this.Query("tag.doc=?", docId)
    96  //line ydb/demo/foo/articles_ydb.gox:47:1
    97  	this.Ret("tag.name", &tags)
    98  //line ydb/demo/foo/articles_ydb.gox:48:1
    99  	return
   100  }
   101  //line ydb/demo/foo/articles_ydb.gox:51:1
   102  func (this *articles) API_ListByTag(tag string) (result []ArticleEntry) {
   103  //line ydb/demo/foo/articles_ydb.gox:52:1
   104  	var ids []string
   105  //line ydb/demo/foo/articles_ydb.gox:53:1
   106  	this.Query("tag.name=?", tag)
   107  //line ydb/demo/foo/articles_ydb.gox:54:1
   108  	this.Ret("tag.doc", &ids)
   109  //line ydb/demo/foo/articles_ydb.gox:56:1
   110  	this.Query("id=?", ids)
   111  //line ydb/demo/foo/articles_ydb.gox:57:1
   112  	this.Ret(&result)
   113  //line ydb/demo/foo/articles_ydb.gox:58:1
   114  	return
   115  }
   116  //line ydb/demo/foo/articles_ydb.gox:61:1
   117  func (this *articles) API_ListByAuthor(author string) (result []ArticleEntry) {
   118  //line ydb/demo/foo/articles_ydb.gox:62:1
   119  	this.Query("author=?", author)
   120  //line ydb/demo/foo/articles_ydb.gox:63:1
   121  	this.Ret(&result)
   122  //line ydb/demo/foo/articles_ydb.gox:64:1
   123  	return
   124  }
   125  //line ydb/demo/foo/articles_ydb.gox:67
   126  func (this *articles) Main() {
   127  //line ydb/demo/foo/articles_ydb.gox:67:1
   128  	this.Engine__0("mysql")
   129  //line ydb/demo/foo/articles_ydb.gox:69:1
   130  	ydb.Gopt_Sql_Gopx_Table[Article](this, "v0.1.0")
   131  //line ydb/demo/foo/articles_ydb.gox:70:1
   132  	ydb.Gopt_Sql_Gopx_Table[Tag](this, "v0.1.0")
   133  //line ydb/demo/foo/articles_ydb.gox:72:1
   134  	this.Use("article")
   135  //line ydb/demo/foo/articles_ydb.gox:74:1
   136  	doc1 := Article{}
   137  //line ydb/demo/foo/articles_ydb.gox:75:1
   138  	doc1.Id, doc1.Author, doc1.Title = "123", "abc", "title1"
   139  //line ydb/demo/foo/articles_ydb.gox:76:1
   140  	this.Gop_Exec("add", doc1)
   141  //line ydb/demo/foo/articles_ydb.gox:77:1
   142  	this.Ret()
   143  //line ydb/demo/foo/articles_ydb.gox:79:1
   144  	this.Gop_Exec("add", doc1)
   145  //line ydb/demo/foo/articles_ydb.gox:80:1
   146  	this.Ret(ydb.ErrDuplicated)
   147  //line ydb/demo/foo/articles_ydb.gox:82:1
   148  	doc2 := Article{}
   149  //line ydb/demo/foo/articles_ydb.gox:83:1
   150  	doc2.Id, doc2.Author, doc2.Title = "124", "efg", "title2"
   151  //line ydb/demo/foo/articles_ydb.gox:84:1
   152  	this.Gop_Exec("add", doc2)
   153  //line ydb/demo/foo/articles_ydb.gox:85:1
   154  	this.Ret()
   155  //line ydb/demo/foo/articles_ydb.gox:87:1
   156  	doc3 := Article{}
   157  //line ydb/demo/foo/articles_ydb.gox:88:1
   158  	doc3.Id, doc3.Author, doc3.Title = "125", "efg", "title3"
   159  //line ydb/demo/foo/articles_ydb.gox:89:1
   160  	this.Gop_Exec("add", doc3)
   161  //line ydb/demo/foo/articles_ydb.gox:90:1
   162  	this.Ret()
   163  //line ydb/demo/foo/articles_ydb.gox:92:1
   164  	doc4 := Article{}
   165  //line ydb/demo/foo/articles_ydb.gox:93:1
   166  	doc4.Id, doc4.Author, doc4.Title = "225", "abc", "title4"
   167  //line ydb/demo/foo/articles_ydb.gox:94:1
   168  	this.Gop_Exec("add", doc4)
   169  //line ydb/demo/foo/articles_ydb.gox:95:1
   170  	this.Ret()
   171  //line ydb/demo/foo/articles_ydb.gox:97:1
   172  	doc5 := Article{}
   173  //line ydb/demo/foo/articles_ydb.gox:98:1
   174  	doc5.Id, doc5.Author, doc5.Title = "555", "abc", "title5"
   175  //line ydb/demo/foo/articles_ydb.gox:99:1
   176  	this.Gop_Exec("add", doc5)
   177  //line ydb/demo/foo/articles_ydb.gox:100:1
   178  	this.Ret()
   179  //line ydb/demo/foo/articles_ydb.gox:102:1
   180  	this.Gop_Exec("get", doc1.Id)
   181  //line ydb/demo/foo/articles_ydb.gox:103:1
   182  	this.Ret(doc1)
   183  //line ydb/demo/foo/articles_ydb.gox:105:1
   184  	this.Gop_Exec("get", doc2.Id)
   185  //line ydb/demo/foo/articles_ydb.gox:106:1
   186  	this.Ret(doc2)
   187  //line ydb/demo/foo/articles_ydb.gox:108:1
   188  	this.Gop_Exec("get", "unknown")
   189  //line ydb/demo/foo/articles_ydb.gox:109:1
   190  	test.Gopt_Case_MatchAny(this, this.Out(1), ydb.ErrNoRows)
   191  //line ydb/demo/foo/articles_ydb.gox:111:1
   192  	this.Gop_Exec("setTags", doc1.Id, "tag1", "tag2")
   193  //line ydb/demo/foo/articles_ydb.gox:112:1
   194  	this.Ret()
   195  //line ydb/demo/foo/articles_ydb.gox:114:1
   196  	this.Gop_Exec("tags", doc1.Id)
   197  //line ydb/demo/foo/articles_ydb.gox:115:1
   198  	this.Ret(test.Set__0("tag2", "tag1"))
   199  //line ydb/demo/foo/articles_ydb.gox:117:1
   200  	this.Gop_Exec("setTags", doc1.Id, "tag1", "tag3")
   201  //line ydb/demo/foo/articles_ydb.gox:118:1
   202  	this.Ret()
   203  //line ydb/demo/foo/articles_ydb.gox:120:1
   204  	this.Gop_Exec("tags", doc1.Id)
   205  //line ydb/demo/foo/articles_ydb.gox:121:1
   206  	this.Ret(test.Set__0("tag1", "tag3"))
   207  //line ydb/demo/foo/articles_ydb.gox:123:1
   208  	this.Gop_Exec("setTags", doc2.Id, "tag1", "tag5")
   209  //line ydb/demo/foo/articles_ydb.gox:124:1
   210  	this.Gop_Exec("setTags", doc3.Id, "tag1", "tag3")
   211  //line ydb/demo/foo/articles_ydb.gox:125:1
   212  	this.Gop_Exec("setTags", doc4.Id, "tag2", "tag3")
   213  //line ydb/demo/foo/articles_ydb.gox:126:1
   214  	this.Gop_Exec("setTags", doc5.Id, "tag5", "tag3")
   215  //line ydb/demo/foo/articles_ydb.gox:128:1
   216  	this.Gop_Exec("listByTag", "tag1")
   217  //line ydb/demo/foo/articles_ydb.gox:129:1
   218  	this.Ret(test.Set__2(doc3.ArticleEntry, doc1.ArticleEntry, doc2.ArticleEntry))
   219  //line ydb/demo/foo/articles_ydb.gox:131:1
   220  	this.Gop_Exec("listByTag", "tag3")
   221  //line ydb/demo/foo/articles_ydb.gox:132:1
   222  	this.Ret(test.Set__2(doc3.ArticleEntry, doc4.ArticleEntry, doc1.ArticleEntry, doc5.ArticleEntry))
   223  //line ydb/demo/foo/articles_ydb.gox:134:1
   224  	this.Gop_Exec("listByAuthor", "eft")
   225  //line ydb/demo/foo/articles_ydb.gox:135:1
   226  	this.Ret(test.Set__2(doc2.ArticleEntry, doc3.ArticleEntry))
   227  }
   228  func (this *articles) Classfname() string {
   229  	return "articles"
   230  }
   231  
   232  var ErrNoEmailAndTel = errors.New("no email and telephone")
   233  var rnd = rand.New(rand.NewSource(time.Now().UnixMicro()))
   234  //line ydb/demo/foo/foo.gop:23:1
   235  func Rand() string {
   236  //line ydb/demo/foo/foo.gop:24:1
   237  	return strconv.FormatInt(rnd.Int63(), 36)
   238  }
   239  //line ydb/demo/foo/foo.gop:27:1
   240  func Hs256(pwd string, salt string) string {
   241  //line ydb/demo/foo/foo.gop:28:1
   242  	b := hmac.New(sha256.New, []byte(salt)).Sum([]byte(pwd))
   243  //line ydb/demo/foo/foo.gop:29:1
   244  	return base64.RawURLEncoding.EncodeToString(b)
   245  }
   246  //line ydb/demo/foo/foo.gop:32:1
   247  func Diff(new []string, old []string) (add []string, del []string) {
   248  //line ydb/demo/foo/foo.gop:33:1
   249  	sort.Strings(new)
   250  //line ydb/demo/foo/foo.gop:34:1
   251  	sort.Strings(old)
   252  //line ydb/demo/foo/foo.gop:35:1
   253  	return stringutil.Diff(new, old)
   254  }
   255  //line ydb/demo/foo/foo.gop:38:1
   256  // Info calls Output to print to the standard logger.
   257  // Arguments are handled in the manner of fmt.Println.
   258  func Info(args ...interface{}) {
   259  //line ydb/demo/foo/foo.gop:41:1
   260  	log.Println(args...)
   261  }
   262  //line ydb/demo/foo/users_ydb.gox:19:1
   263  func (this *users) API_Register(id string, pwd string, nickname string, email string, tel string, ctime time.Time) error {
   264  //line ydb/demo/foo/users_ydb.gox:20:1
   265  	if email == "" && tel == "" {
   266  //line ydb/demo/foo/users_ydb.gox:21:1
   267  		return ErrNoEmailAndTel
   268  	}
   269  //line ydb/demo/foo/users_ydb.gox:23:1
   270  	this.Limit__1(3, "email=?", email)
   271  //line ydb/demo/foo/users_ydb.gox:24:1
   272  	this.Limit__1(3, "tel=?", tel)
   273  //line ydb/demo/foo/users_ydb.gox:26:1
   274  	salt := Rand()
   275  //line ydb/demo/foo/users_ydb.gox:27:1
   276  	spwd := Hs256(pwd, salt)
   277  //line ydb/demo/foo/users_ydb.gox:28:1
   278  	this.Insert(&User{Id: id, Spwd: spwd, Salt: salt, Nickname: nickname, Email: email, Tel: tel, Ctime: ctime})
   279  //line ydb/demo/foo/users_ydb.gox:29:1
   280  	return nil
   281  }
   282  //line ydb/demo/foo/users_ydb.gox:32:1
   283  func (this *users) API_Login(id string, pwd string) bool {
   284  //line ydb/demo/foo/users_ydb.gox:33:1
   285  	var spwd, salt string
   286  //line ydb/demo/foo/users_ydb.gox:34:1
   287  	this.Query("id=?", id)
   288  //line ydb/demo/foo/users_ydb.gox:35:1
   289  	this.Ret("salt", &salt, "spwd", &spwd)
   290  //line ydb/demo/foo/users_ydb.gox:36:1
   291  	if this.NoRows() {
   292  //line ydb/demo/foo/users_ydb.gox:37:1
   293  		return false
   294  	}
   295  //line ydb/demo/foo/users_ydb.gox:39:1
   296  	return Hs256(pwd, salt) == spwd
   297  }
   298  //line ydb/demo/foo/users_ydb.gox:42
   299  func (this *users) Main() {
   300  //line ydb/demo/foo/users_ydb.gox:42:1
   301  	this.Engine__0("sqlite3")
   302  //line ydb/demo/foo/users_ydb.gox:44:1
   303  	ydb.Gopt_Sql_Gopx_Table[User](this, "user v0.1.0")
   304  //line ydb/demo/foo/users_ydb.gox:46:1
   305  	this.Use("user")
   306  //line ydb/demo/foo/users_ydb.gox:48:1
   307  	this.Gop_Exec("register", "user", "pwd", "nickname", "", "", time.Now())
   308  //line ydb/demo/foo/users_ydb.gox:49:1
   309  	this.Ret(ErrNoEmailAndTel)
   310  //line ydb/demo/foo/users_ydb.gox:50:1
   311  	this.Gop_Exec("register", "user", "pwd", "nickname", "user@foo.com", "", time.Now())
   312  //line ydb/demo/foo/users_ydb.gox:51:1
   313  	this.Ret(nil)
   314  //line ydb/demo/foo/users_ydb.gox:52:1
   315  	this.Gop_Exec("register", "user", "pwd", "nickname", "user@foo.com", "13500000000", time.Now())
   316  //line ydb/demo/foo/users_ydb.gox:53:1
   317  	this.Ret(ydb.ErrDuplicated)
   318  //line ydb/demo/foo/users_ydb.gox:55:1
   319  	this.Gop_Exec("login", "", "")
   320  //line ydb/demo/foo/users_ydb.gox:56:1
   321  	this.Ret(false)
   322  //line ydb/demo/foo/users_ydb.gox:57:1
   323  	this.Gop_Exec("login", "user", "pwd")
   324  //line ydb/demo/foo/users_ydb.gox:58:1
   325  	this.Ret(true)
   326  }
   327  func (this *users) Classfname() string {
   328  	return "users"
   329  }
   330  func (this *AppGen) Main() {
   331  	ydb.Gopt_AppGen_Main(this, new(articles), new(users))
   332  }
   333  func main() {
   334  	new(AppGen).Main()
   335  }