models_fields.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. // Copyright 2014 beego Author. All Rights Reserved.
  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 orm
  15. import (
  16. "fmt"
  17. "strconv"
  18. "time"
  19. )
  20. // Define the Type enum
  21. const (
  22. TypeBooleanField = 1 << iota
  23. TypeCharField
  24. TypeTextField
  25. TypeTimeField
  26. TypeDateField
  27. TypeDateTimeField
  28. TypeBitField
  29. TypeSmallIntegerField
  30. TypeIntegerField
  31. TypeBigIntegerField
  32. TypePositiveBitField
  33. TypePositiveSmallIntegerField
  34. TypePositiveIntegerField
  35. TypePositiveBigIntegerField
  36. TypeFloatField
  37. TypeDecimalField
  38. TypeJSONField
  39. TypeJsonbField
  40. RelForeignKey
  41. RelOneToOne
  42. RelManyToMany
  43. RelReverseOne
  44. RelReverseMany
  45. )
  46. // Define some logic enum
  47. const (
  48. IsIntegerField = ^-TypePositiveBigIntegerField >> 5 << 6
  49. IsPositiveIntegerField = ^-TypePositiveBigIntegerField >> 9 << 10
  50. IsRelField = ^-RelReverseMany >> 17 << 18
  51. IsFieldType = ^-RelReverseMany<<1 + 1
  52. )
  53. // BooleanField A true/false field.
  54. type BooleanField bool
  55. // Value return the BooleanField
  56. func (e BooleanField) Value() bool {
  57. return bool(e)
  58. }
  59. // Set will set the BooleanField
  60. func (e *BooleanField) Set(d bool) {
  61. *e = BooleanField(d)
  62. }
  63. // String format the Bool to string
  64. func (e *BooleanField) String() string {
  65. return strconv.FormatBool(e.Value())
  66. }
  67. // FieldType return BooleanField the type
  68. func (e *BooleanField) FieldType() int {
  69. return TypeBooleanField
  70. }
  71. // SetRaw set the interface to bool
  72. func (e *BooleanField) SetRaw(value interface{}) error {
  73. switch d := value.(type) {
  74. case bool:
  75. e.Set(d)
  76. case string:
  77. v, err := StrTo(d).Bool()
  78. if err != nil {
  79. e.Set(v)
  80. }
  81. return err
  82. default:
  83. return fmt.Errorf("<BooleanField.SetRaw> unknown value `%s`", value)
  84. }
  85. return nil
  86. }
  87. // RawValue return the current value
  88. func (e *BooleanField) RawValue() interface{} {
  89. return e.Value()
  90. }
  91. // verify the BooleanField implement the Fielder interface
  92. var _ Fielder = new(BooleanField)
  93. // CharField A string field
  94. // required values tag: size
  95. // The size is enforced at the database level and in models’s validation.
  96. // eg: `orm:"size(120)"`
  97. type CharField string
  98. // Value return the CharField's Value
  99. func (e CharField) Value() string {
  100. return string(e)
  101. }
  102. // Set CharField value
  103. func (e *CharField) Set(d string) {
  104. *e = CharField(d)
  105. }
  106. // String return the CharField
  107. func (e *CharField) String() string {
  108. return e.Value()
  109. }
  110. // FieldType return the enum type
  111. func (e *CharField) FieldType() int {
  112. return TypeCharField
  113. }
  114. // SetRaw set the interface to string
  115. func (e *CharField) SetRaw(value interface{}) error {
  116. switch d := value.(type) {
  117. case string:
  118. e.Set(d)
  119. default:
  120. return fmt.Errorf("<CharField.SetRaw> unknown value `%s`", value)
  121. }
  122. return nil
  123. }
  124. // RawValue return the CharField value
  125. func (e *CharField) RawValue() interface{} {
  126. return e.Value()
  127. }
  128. // verify CharField implement Fielder
  129. var _ Fielder = new(CharField)
  130. // TimeField A time, represented in go by a time.Time instance.
  131. // only time values like 10:00:00
  132. // Has a few extra, optional attr tag:
  133. //
  134. // auto_now:
  135. // Automatically set the field to now every time the object is saved. Useful for “last-modified” timestamps.
  136. // Note that the current date is always used; it’s not just a default value that you can override.
  137. //
  138. // auto_now_add:
  139. // Automatically set the field to now when the object is first created. Useful for creation of timestamps.
  140. // Note that the current date is always used; it’s not just a default value that you can override.
  141. //
  142. // eg: `orm:"auto_now"` or `orm:"auto_now_add"`
  143. type TimeField time.Time
  144. // Value return the time.Time
  145. func (e TimeField) Value() time.Time {
  146. return time.Time(e)
  147. }
  148. // Set set the TimeField's value
  149. func (e *TimeField) Set(d time.Time) {
  150. *e = TimeField(d)
  151. }
  152. // String convert time to string
  153. func (e *TimeField) String() string {
  154. return e.Value().String()
  155. }
  156. // FieldType return enum type Date
  157. func (e *TimeField) FieldType() int {
  158. return TypeDateField
  159. }
  160. // SetRaw convert the interface to time.Time. Allow string and time.Time
  161. func (e *TimeField) SetRaw(value interface{}) error {
  162. switch d := value.(type) {
  163. case time.Time:
  164. e.Set(d)
  165. case string:
  166. v, err := timeParse(d, formatTime)
  167. if err != nil {
  168. e.Set(v)
  169. }
  170. return err
  171. default:
  172. return fmt.Errorf("<TimeField.SetRaw> unknown value `%s`", value)
  173. }
  174. return nil
  175. }
  176. // RawValue return time value
  177. func (e *TimeField) RawValue() interface{} {
  178. return e.Value()
  179. }
  180. var _ Fielder = new(TimeField)
  181. // DateField A date, represented in go by a time.Time instance.
  182. // only date values like 2006-01-02
  183. // Has a few extra, optional attr tag:
  184. //
  185. // auto_now:
  186. // Automatically set the field to now every time the object is saved. Useful for “last-modified” timestamps.
  187. // Note that the current date is always used; it’s not just a default value that you can override.
  188. //
  189. // auto_now_add:
  190. // Automatically set the field to now when the object is first created. Useful for creation of timestamps.
  191. // Note that the current date is always used; it’s not just a default value that you can override.
  192. //
  193. // eg: `orm:"auto_now"` or `orm:"auto_now_add"`
  194. type DateField time.Time
  195. // Value return the time.Time
  196. func (e DateField) Value() time.Time {
  197. return time.Time(e)
  198. }
  199. // Set set the DateField's value
  200. func (e *DateField) Set(d time.Time) {
  201. *e = DateField(d)
  202. }
  203. // String convert datatime to string
  204. func (e *DateField) String() string {
  205. return e.Value().String()
  206. }
  207. // FieldType return enum type Date
  208. func (e *DateField) FieldType() int {
  209. return TypeDateField
  210. }
  211. // SetRaw convert the interface to time.Time. Allow string and time.Time
  212. func (e *DateField) SetRaw(value interface{}) error {
  213. switch d := value.(type) {
  214. case time.Time:
  215. e.Set(d)
  216. case string:
  217. v, err := timeParse(d, formatDate)
  218. if err != nil {
  219. e.Set(v)
  220. }
  221. return err
  222. default:
  223. return fmt.Errorf("<DateField.SetRaw> unknown value `%s`", value)
  224. }
  225. return nil
  226. }
  227. // RawValue return Date value
  228. func (e *DateField) RawValue() interface{} {
  229. return e.Value()
  230. }
  231. // verify DateField implement fielder interface
  232. var _ Fielder = new(DateField)
  233. // DateTimeField A date, represented in go by a time.Time instance.
  234. // datetime values like 2006-01-02 15:04:05
  235. // Takes the same extra arguments as DateField.
  236. type DateTimeField time.Time
  237. // Value return the datatime value
  238. func (e DateTimeField) Value() time.Time {
  239. return time.Time(e)
  240. }
  241. // Set set the time.Time to datatime
  242. func (e *DateTimeField) Set(d time.Time) {
  243. *e = DateTimeField(d)
  244. }
  245. // String return the time's String
  246. func (e *DateTimeField) String() string {
  247. return e.Value().String()
  248. }
  249. // FieldType return the enum TypeDateTimeField
  250. func (e *DateTimeField) FieldType() int {
  251. return TypeDateTimeField
  252. }
  253. // SetRaw convert the string or time.Time to DateTimeField
  254. func (e *DateTimeField) SetRaw(value interface{}) error {
  255. switch d := value.(type) {
  256. case time.Time:
  257. e.Set(d)
  258. case string:
  259. v, err := timeParse(d, formatDateTime)
  260. if err != nil {
  261. e.Set(v)
  262. }
  263. return err
  264. default:
  265. return fmt.Errorf("<DateTimeField.SetRaw> unknown value `%s`", value)
  266. }
  267. return nil
  268. }
  269. // RawValue return the datatime value
  270. func (e *DateTimeField) RawValue() interface{} {
  271. return e.Value()
  272. }
  273. // verify datatime implement fielder
  274. var _ Fielder = new(DateTimeField)
  275. // FloatField A floating-point number represented in go by a float32 value.
  276. type FloatField float64
  277. // Value return the FloatField value
  278. func (e FloatField) Value() float64 {
  279. return float64(e)
  280. }
  281. // Set the Float64
  282. func (e *FloatField) Set(d float64) {
  283. *e = FloatField(d)
  284. }
  285. // String return the string
  286. func (e *FloatField) String() string {
  287. return ToStr(e.Value(), -1, 32)
  288. }
  289. // FieldType return the enum type
  290. func (e *FloatField) FieldType() int {
  291. return TypeFloatField
  292. }
  293. // SetRaw converter interface Float64 float32 or string to FloatField
  294. func (e *FloatField) SetRaw(value interface{}) error {
  295. switch d := value.(type) {
  296. case float32:
  297. e.Set(float64(d))
  298. case float64:
  299. e.Set(d)
  300. case string:
  301. v, err := StrTo(d).Float64()
  302. if err != nil {
  303. e.Set(v)
  304. }
  305. default:
  306. return fmt.Errorf("<FloatField.SetRaw> unknown value `%s`", value)
  307. }
  308. return nil
  309. }
  310. // RawValue return the FloatField value
  311. func (e *FloatField) RawValue() interface{} {
  312. return e.Value()
  313. }
  314. // verify FloatField implement Fielder
  315. var _ Fielder = new(FloatField)
  316. // SmallIntegerField -32768 to 32767
  317. type SmallIntegerField int16
  318. // Value return int16 value
  319. func (e SmallIntegerField) Value() int16 {
  320. return int16(e)
  321. }
  322. // Set the SmallIntegerField value
  323. func (e *SmallIntegerField) Set(d int16) {
  324. *e = SmallIntegerField(d)
  325. }
  326. // String convert smallint to string
  327. func (e *SmallIntegerField) String() string {
  328. return ToStr(e.Value())
  329. }
  330. // FieldType return enum type SmallIntegerField
  331. func (e *SmallIntegerField) FieldType() int {
  332. return TypeSmallIntegerField
  333. }
  334. // SetRaw convert interface int16/string to int16
  335. func (e *SmallIntegerField) SetRaw(value interface{}) error {
  336. switch d := value.(type) {
  337. case int16:
  338. e.Set(d)
  339. case string:
  340. v, err := StrTo(d).Int16()
  341. if err != nil {
  342. e.Set(v)
  343. }
  344. default:
  345. return fmt.Errorf("<SmallIntegerField.SetRaw> unknown value `%s`", value)
  346. }
  347. return nil
  348. }
  349. // RawValue return smallint value
  350. func (e *SmallIntegerField) RawValue() interface{} {
  351. return e.Value()
  352. }
  353. // verify SmallIntegerField implement Fielder
  354. var _ Fielder = new(SmallIntegerField)
  355. // IntegerField -2147483648 to 2147483647
  356. type IntegerField int32
  357. // Value return the int32
  358. func (e IntegerField) Value() int32 {
  359. return int32(e)
  360. }
  361. // Set IntegerField value
  362. func (e *IntegerField) Set(d int32) {
  363. *e = IntegerField(d)
  364. }
  365. // String convert Int32 to string
  366. func (e *IntegerField) String() string {
  367. return ToStr(e.Value())
  368. }
  369. // FieldType return the enum type
  370. func (e *IntegerField) FieldType() int {
  371. return TypeIntegerField
  372. }
  373. // SetRaw convert interface int32/string to int32
  374. func (e *IntegerField) SetRaw(value interface{}) error {
  375. switch d := value.(type) {
  376. case int32:
  377. e.Set(d)
  378. case string:
  379. v, err := StrTo(d).Int32()
  380. if err != nil {
  381. e.Set(v)
  382. }
  383. default:
  384. return fmt.Errorf("<IntegerField.SetRaw> unknown value `%s`", value)
  385. }
  386. return nil
  387. }
  388. // RawValue return IntegerField value
  389. func (e *IntegerField) RawValue() interface{} {
  390. return e.Value()
  391. }
  392. // verify IntegerField implement Fielder
  393. var _ Fielder = new(IntegerField)
  394. // BigIntegerField -9223372036854775808 to 9223372036854775807.
  395. type BigIntegerField int64
  396. // Value return int64
  397. func (e BigIntegerField) Value() int64 {
  398. return int64(e)
  399. }
  400. // Set the BigIntegerField value
  401. func (e *BigIntegerField) Set(d int64) {
  402. *e = BigIntegerField(d)
  403. }
  404. // String convert BigIntegerField to string
  405. func (e *BigIntegerField) String() string {
  406. return ToStr(e.Value())
  407. }
  408. // FieldType return enum type
  409. func (e *BigIntegerField) FieldType() int {
  410. return TypeBigIntegerField
  411. }
  412. // SetRaw convert interface int64/string to int64
  413. func (e *BigIntegerField) SetRaw(value interface{}) error {
  414. switch d := value.(type) {
  415. case int64:
  416. e.Set(d)
  417. case string:
  418. v, err := StrTo(d).Int64()
  419. if err != nil {
  420. e.Set(v)
  421. }
  422. default:
  423. return fmt.Errorf("<BigIntegerField.SetRaw> unknown value `%s`", value)
  424. }
  425. return nil
  426. }
  427. // RawValue return BigIntegerField value
  428. func (e *BigIntegerField) RawValue() interface{} {
  429. return e.Value()
  430. }
  431. // verify BigIntegerField implement Fielder
  432. var _ Fielder = new(BigIntegerField)
  433. // PositiveSmallIntegerField 0 to 65535
  434. type PositiveSmallIntegerField uint16
  435. // Value return uint16
  436. func (e PositiveSmallIntegerField) Value() uint16 {
  437. return uint16(e)
  438. }
  439. // Set PositiveSmallIntegerField value
  440. func (e *PositiveSmallIntegerField) Set(d uint16) {
  441. *e = PositiveSmallIntegerField(d)
  442. }
  443. // String convert uint16 to string
  444. func (e *PositiveSmallIntegerField) String() string {
  445. return ToStr(e.Value())
  446. }
  447. // FieldType return enum type
  448. func (e *PositiveSmallIntegerField) FieldType() int {
  449. return TypePositiveSmallIntegerField
  450. }
  451. // SetRaw convert Interface uint16/string to uint16
  452. func (e *PositiveSmallIntegerField) SetRaw(value interface{}) error {
  453. switch d := value.(type) {
  454. case uint16:
  455. e.Set(d)
  456. case string:
  457. v, err := StrTo(d).Uint16()
  458. if err != nil {
  459. e.Set(v)
  460. }
  461. default:
  462. return fmt.Errorf("<PositiveSmallIntegerField.SetRaw> unknown value `%s`", value)
  463. }
  464. return nil
  465. }
  466. // RawValue returns PositiveSmallIntegerField value
  467. func (e *PositiveSmallIntegerField) RawValue() interface{} {
  468. return e.Value()
  469. }
  470. // verify PositiveSmallIntegerField implement Fielder
  471. var _ Fielder = new(PositiveSmallIntegerField)
  472. // PositiveIntegerField 0 to 4294967295
  473. type PositiveIntegerField uint32
  474. // Value return PositiveIntegerField value. Uint32
  475. func (e PositiveIntegerField) Value() uint32 {
  476. return uint32(e)
  477. }
  478. // Set the PositiveIntegerField value
  479. func (e *PositiveIntegerField) Set(d uint32) {
  480. *e = PositiveIntegerField(d)
  481. }
  482. // String convert PositiveIntegerField to string
  483. func (e *PositiveIntegerField) String() string {
  484. return ToStr(e.Value())
  485. }
  486. // FieldType return enum type
  487. func (e *PositiveIntegerField) FieldType() int {
  488. return TypePositiveIntegerField
  489. }
  490. // SetRaw convert interface uint32/string to Uint32
  491. func (e *PositiveIntegerField) SetRaw(value interface{}) error {
  492. switch d := value.(type) {
  493. case uint32:
  494. e.Set(d)
  495. case string:
  496. v, err := StrTo(d).Uint32()
  497. if err != nil {
  498. e.Set(v)
  499. }
  500. default:
  501. return fmt.Errorf("<PositiveIntegerField.SetRaw> unknown value `%s`", value)
  502. }
  503. return nil
  504. }
  505. // RawValue return the PositiveIntegerField Value
  506. func (e *PositiveIntegerField) RawValue() interface{} {
  507. return e.Value()
  508. }
  509. // verify PositiveIntegerField implement Fielder
  510. var _ Fielder = new(PositiveIntegerField)
  511. // PositiveBigIntegerField 0 to 18446744073709551615
  512. type PositiveBigIntegerField uint64
  513. // Value return uint64
  514. func (e PositiveBigIntegerField) Value() uint64 {
  515. return uint64(e)
  516. }
  517. // Set PositiveBigIntegerField value
  518. func (e *PositiveBigIntegerField) Set(d uint64) {
  519. *e = PositiveBigIntegerField(d)
  520. }
  521. // String convert PositiveBigIntegerField to string
  522. func (e *PositiveBigIntegerField) String() string {
  523. return ToStr(e.Value())
  524. }
  525. // FieldType return enum type
  526. func (e *PositiveBigIntegerField) FieldType() int {
  527. return TypePositiveIntegerField
  528. }
  529. // SetRaw convert interface uint64/string to Uint64
  530. func (e *PositiveBigIntegerField) SetRaw(value interface{}) error {
  531. switch d := value.(type) {
  532. case uint64:
  533. e.Set(d)
  534. case string:
  535. v, err := StrTo(d).Uint64()
  536. if err != nil {
  537. e.Set(v)
  538. }
  539. default:
  540. return fmt.Errorf("<PositiveBigIntegerField.SetRaw> unknown value `%s`", value)
  541. }
  542. return nil
  543. }
  544. // RawValue return PositiveBigIntegerField value
  545. func (e *PositiveBigIntegerField) RawValue() interface{} {
  546. return e.Value()
  547. }
  548. // verify PositiveBigIntegerField implement Fielder
  549. var _ Fielder = new(PositiveBigIntegerField)
  550. // TextField A large text field.
  551. type TextField string
  552. // Value return TextField value
  553. func (e TextField) Value() string {
  554. return string(e)
  555. }
  556. // Set the TextField value
  557. func (e *TextField) Set(d string) {
  558. *e = TextField(d)
  559. }
  560. // String convert TextField to string
  561. func (e *TextField) String() string {
  562. return e.Value()
  563. }
  564. // FieldType return enum type
  565. func (e *TextField) FieldType() int {
  566. return TypeTextField
  567. }
  568. // SetRaw convert interface string to string
  569. func (e *TextField) SetRaw(value interface{}) error {
  570. switch d := value.(type) {
  571. case string:
  572. e.Set(d)
  573. default:
  574. return fmt.Errorf("<TextField.SetRaw> unknown value `%s`", value)
  575. }
  576. return nil
  577. }
  578. // RawValue return TextField value
  579. func (e *TextField) RawValue() interface{} {
  580. return e.Value()
  581. }
  582. // verify TextField implement Fielder
  583. var _ Fielder = new(TextField)
  584. // JSONField postgres json field.
  585. type JSONField string
  586. // Value return JSONField value
  587. func (j JSONField) Value() string {
  588. return string(j)
  589. }
  590. // Set the JSONField value
  591. func (j *JSONField) Set(d string) {
  592. *j = JSONField(d)
  593. }
  594. // String convert JSONField to string
  595. func (j *JSONField) String() string {
  596. return j.Value()
  597. }
  598. // FieldType return enum type
  599. func (j *JSONField) FieldType() int {
  600. return TypeJSONField
  601. }
  602. // SetRaw convert interface string to string
  603. func (j *JSONField) SetRaw(value interface{}) error {
  604. switch d := value.(type) {
  605. case string:
  606. j.Set(d)
  607. default:
  608. return fmt.Errorf("<JSONField.SetRaw> unknown value `%s`", value)
  609. }
  610. return nil
  611. }
  612. // RawValue return JSONField value
  613. func (j *JSONField) RawValue() interface{} {
  614. return j.Value()
  615. }
  616. // verify JSONField implement Fielder
  617. var _ Fielder = new(JSONField)
  618. // JsonbField postgres json field.
  619. type JsonbField string
  620. // Value return JsonbField value
  621. func (j JsonbField) Value() string {
  622. return string(j)
  623. }
  624. // Set the JsonbField value
  625. func (j *JsonbField) Set(d string) {
  626. *j = JsonbField(d)
  627. }
  628. // String convert JsonbField to string
  629. func (j *JsonbField) String() string {
  630. return j.Value()
  631. }
  632. // FieldType return enum type
  633. func (j *JsonbField) FieldType() int {
  634. return TypeJsonbField
  635. }
  636. // SetRaw convert interface string to string
  637. func (j *JsonbField) SetRaw(value interface{}) error {
  638. switch d := value.(type) {
  639. case string:
  640. j.Set(d)
  641. default:
  642. return fmt.Errorf("<JsonbField.SetRaw> unknown value `%s`", value)
  643. }
  644. return nil
  645. }
  646. // RawValue return JsonbField value
  647. func (j *JsonbField) RawValue() interface{} {
  648. return j.Value()
  649. }
  650. // verify JsonbField implement Fielder
  651. var _ Fielder = new(JsonbField)