columninfo.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package sqlite
  15. import (
  16. "yunion.io/x/log"
  17. "yunion.io/x/sqlchemy"
  18. )
  19. type sSqlColumnInfo struct {
  20. Cid int `json:"cid"`
  21. Name string `json:"name"`
  22. Type string `json:"type"`
  23. Notnull bool `json:"notnull"`
  24. DfltValue string `json:"dflt_value"`
  25. Pk bool `json:"pk"`
  26. }
  27. func (info *sSqlColumnInfo) getTagmap() map[string]string {
  28. tagmap := make(map[string]string)
  29. if info.Notnull {
  30. tagmap[sqlchemy.TAG_NULLABLE] = "false"
  31. } else {
  32. tagmap[sqlchemy.TAG_NULLABLE] = "true"
  33. }
  34. if len(info.DfltValue) > 0 {
  35. tagmap[sqlchemy.TAG_DEFAULT] = info.DfltValue
  36. if info.Type == "TEXT" {
  37. tagmap[sqlchemy.TAG_DEFAULT] = info.DfltValue[1 : len(info.DfltValue)-1]
  38. }
  39. }
  40. if info.Pk {
  41. tagmap[sqlchemy.TAG_PRIMARY] = "true"
  42. }
  43. return tagmap
  44. }
  45. func (info *sSqlColumnInfo) toColumnSpec() sqlchemy.IColumnSpec {
  46. switch info.Type {
  47. case "TEXT", "BLOB":
  48. c := NewTextColumn(info.Name, info.getTagmap(), false)
  49. return &c
  50. case "INTEGER":
  51. c := NewIntegerColumn(info.Name, info.getTagmap(), false)
  52. return &c
  53. case "INTEGER AUTO_INCREMENT":
  54. tagmap := info.getTagmap()
  55. tagmap[sqlchemy.TAG_AUTOINCREMENT] = "true"
  56. c := NewIntegerColumn(info.Name, tagmap, false)
  57. return &c
  58. case "REAL":
  59. c := NewFloatColumn(info.Name, info.getTagmap(), false)
  60. return &c
  61. default:
  62. log.Errorf("unsupported type %s", info.Type)
  63. }
  64. return nil
  65. }