c++.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2017 The Xorm 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. package main
  5. import (
  6. "strings"
  7. "text/template"
  8. "github.com/go-xorm/core"
  9. )
  10. var (
  11. CPlusTmpl LangTmpl = LangTmpl{
  12. template.FuncMap{"Mapper": mapper.Table2Obj,
  13. "Type": cPlusTypeStr,
  14. "UnTitle": unTitle,
  15. },
  16. nil,
  17. genCPlusImports,
  18. }
  19. )
  20. func cPlusTypeStr(col *core.Column) string {
  21. tp := col.SQLType
  22. name := strings.ToUpper(tp.Name)
  23. switch name {
  24. case core.Bit, core.TinyInt, core.SmallInt, core.MediumInt, core.Int, core.Integer, core.Serial:
  25. return "int"
  26. case core.BigInt, core.BigSerial:
  27. return "__int64"
  28. case core.Char, core.Varchar, core.TinyText, core.Text, core.MediumText, core.LongText:
  29. return "tstring"
  30. case core.Date, core.DateTime, core.Time, core.TimeStamp:
  31. return "time_t"
  32. case core.Decimal, core.Numeric:
  33. return "tstring"
  34. case core.Real, core.Float:
  35. return "float"
  36. case core.Double:
  37. return "double"
  38. case core.TinyBlob, core.Blob, core.MediumBlob, core.LongBlob, core.Bytea:
  39. return "tstring"
  40. case core.Bool:
  41. return "bool"
  42. default:
  43. return "tstring"
  44. }
  45. return ""
  46. }
  47. func genCPlusImports(tables []*core.Table) map[string]string {
  48. imports := make(map[string]string)
  49. for _, table := range tables {
  50. for _, col := range table.Columns() {
  51. switch cPlusTypeStr(col) {
  52. case "time_t":
  53. imports[`<time.h>`] = `<time.h>`
  54. case "tstring":
  55. imports["<string>"] = "<string>"
  56. //case "__int64":
  57. // imports[""] = ""
  58. }
  59. }
  60. }
  61. return imports
  62. }