|
| 1 | +// Copyright 2013 The StudyGolang Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | +// http://studygolang.com |
| 5 | +// Author:polaris [email protected] |
| 6 | + |
| 7 | +package model |
| 8 | + |
| 9 | +import ( |
| 10 | + "logger" |
| 11 | + "time" |
| 12 | + "util" |
| 13 | +) |
| 14 | + |
| 15 | +// 角色信息 |
| 16 | +type Wiki struct { |
| 17 | + Id int `json:"id"` |
| 18 | + Title string `json:"title"` |
| 19 | + Content string `json:"content"` |
| 20 | + Uri string `json:"uri"` |
| 21 | + Uid int `json:"uid"` |
| 22 | + Cuid string `json:"cuid"` |
| 23 | + Ctime string `json:"ctime"` |
| 24 | + Mtime string `json:"mtime"` |
| 25 | + |
| 26 | + // 数据库访问对象 |
| 27 | + *Dao |
| 28 | +} |
| 29 | + |
| 30 | +func NewWiki() *Wiki { |
| 31 | + return &Wiki{ |
| 32 | + Dao: &Dao{tablename: "wiki"}, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func (this *Wiki) Insert() (int64, error) { |
| 37 | + this.Ctime = time.Now().Format("2006-01-02 15:04:05") |
| 38 | + this.prepareInsertData() |
| 39 | + result, err := this.Dao.Insert() |
| 40 | + if err != nil { |
| 41 | + return 0, err |
| 42 | + } |
| 43 | + return result.LastInsertId() |
| 44 | +} |
| 45 | + |
| 46 | +func (this *Wiki) Find(selectCol ...string) error { |
| 47 | + return this.Dao.Find(this.colFieldMap(), selectCol...) |
| 48 | +} |
| 49 | + |
| 50 | +func (this *Wiki) FindAll(selectCol ...string) ([]*Wiki, error) { |
| 51 | + if len(selectCol) == 0 { |
| 52 | + selectCol = util.MapKeys(this.colFieldMap()) |
| 53 | + } |
| 54 | + rows, err := this.Dao.FindAll(selectCol...) |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + // TODO: |
| 59 | + wikiList := make([]*Wiki, 0, 10) |
| 60 | + logger.Debugln("selectCol", selectCol) |
| 61 | + colNum := len(selectCol) |
| 62 | + for rows.Next() { |
| 63 | + wiki := NewWiki() |
| 64 | + err = this.Scan(rows, colNum, wiki.colFieldMap(), selectCol...) |
| 65 | + if err != nil { |
| 66 | + logger.Errorln("Wiki FindAll Scan Error:", err) |
| 67 | + continue |
| 68 | + } |
| 69 | + wikiList = append(wikiList, wiki) |
| 70 | + } |
| 71 | + return wikiList, nil |
| 72 | +} |
| 73 | + |
| 74 | +// 为了支持连写 |
| 75 | +func (this *Wiki) Where(condition string) *Wiki { |
| 76 | + this.Dao.Where(condition) |
| 77 | + return this |
| 78 | +} |
| 79 | + |
| 80 | +// 为了支持连写 |
| 81 | +func (this *Wiki) Limit(limit string) *Wiki { |
| 82 | + this.Dao.Limit(limit) |
| 83 | + return this |
| 84 | +} |
| 85 | + |
| 86 | +// 为了支持连写 |
| 87 | +func (this *Wiki) Order(order string) *Wiki { |
| 88 | + this.Dao.Order(order) |
| 89 | + return this |
| 90 | +} |
| 91 | + |
| 92 | +func (this *Wiki) prepareInsertData() { |
| 93 | + this.columns = []string{"title", "content", "uri", "uid", "ctime"} |
| 94 | + this.colValues = []interface{}{this.Title, this.Content, this.Uri, this.Uid, this.Ctime} |
| 95 | +} |
| 96 | + |
| 97 | +func (this *Wiki) colFieldMap() map[string]interface{} { |
| 98 | + return map[string]interface{}{ |
| 99 | + "id": &this.Id, |
| 100 | + "title": &this.Title, |
| 101 | + "content": &this.Content, |
| 102 | + "uri": &this.Uri, |
| 103 | + "uid": &this.Uid, |
| 104 | + "cuid": &this.Cuid, |
| 105 | + "ctime": &this.Ctime, |
| 106 | + "mtime": &this.Mtime, |
| 107 | + } |
| 108 | +} |
0 commit comments