conditions.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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 sqlchemy
  15. import (
  16. "bytes"
  17. "fmt"
  18. "yunion.io/x/pkg/util/reflectutils"
  19. )
  20. type sNoop struct{}
  21. var noop = &sNoop{}
  22. func (s sNoop) WhereClause() string {
  23. return ""
  24. }
  25. func (s sNoop) Variables() []interface{} {
  26. return nil
  27. }
  28. func (s sNoop) database() *SDatabase {
  29. return nil
  30. }
  31. func Noop() ICondition {
  32. return noop
  33. }
  34. // ICondition is the interface representing a condition for SQL query
  35. // e.g. WHERE a1 = b1 is a condition of equal
  36. // the condition support nested condition, with AND, OR and NOT boolean operators
  37. type ICondition interface {
  38. WhereClause() string
  39. Variables() []interface{}
  40. database() *SDatabase
  41. }
  42. // SCompoundConditions is a Compound condition represents AND or OR boolean operation
  43. // Compound condition also follows the ICondition interface
  44. type SCompoundConditions struct {
  45. conditions []ICondition
  46. }
  47. func compoundWhereClause(c *SCompoundConditions, op string) string {
  48. var buf bytes.Buffer
  49. for _, cond := range c.conditions {
  50. if buf.Len() > 0 {
  51. buf.WriteByte(' ')
  52. buf.WriteString(op)
  53. buf.WriteByte(' ')
  54. }
  55. buf.WriteByte('(')
  56. buf.WriteString(cond.WhereClause())
  57. buf.WriteByte(')')
  58. }
  59. return buf.String()
  60. }
  61. // WhereClause implementation of SCompoundConditions for ICondition
  62. func (c *SCompoundConditions) WhereClause() string {
  63. return ""
  64. }
  65. // Variables implementation of SCompoundConditions for ICondition
  66. func (c *SCompoundConditions) Variables() []interface{} {
  67. vars := make([]interface{}, 0)
  68. for _, cond := range c.conditions {
  69. nvars := cond.Variables()
  70. if len(nvars) > 0 {
  71. vars = append(vars, nvars...)
  72. }
  73. }
  74. return vars
  75. }
  76. // database implementation of SCompoundConditions for ICondition
  77. func (c *SCompoundConditions) database() *SDatabase {
  78. for _, c := range c.conditions {
  79. db := c.database()
  80. if db != nil {
  81. return db
  82. }
  83. }
  84. return nil
  85. }
  86. // SAndConditions represents the AND condition, which is a SCompoundConditions
  87. type SAndConditions struct {
  88. SCompoundConditions
  89. }
  90. // WhereClause implementation of SAndConditions for IConditionq
  91. func (c *SAndConditions) WhereClause() string {
  92. return compoundWhereClause(&c.SCompoundConditions, SQL_OP_AND)
  93. }
  94. // SOrConditions represents the OR condition, which is a SCompoundConditions
  95. type SOrConditions struct {
  96. SCompoundConditions
  97. }
  98. // WhereClause implementation of SOrConditions for ICondition
  99. func (c *SOrConditions) WhereClause() string {
  100. return compoundWhereClause(&c.SCompoundConditions, SQL_OP_OR)
  101. }
  102. // AND method that combines many conditions with AND operator
  103. func AND(cond ...ICondition) ICondition {
  104. conds := make([]ICondition, 0)
  105. for _, c := range cond {
  106. if c == nil || c == noop {
  107. continue
  108. }
  109. andCond, ok := c.(*SAndConditions)
  110. if ok {
  111. conds = append(conds, andCond.conditions...)
  112. } else {
  113. conds = append(conds, c)
  114. }
  115. }
  116. cc := SAndConditions{SCompoundConditions{conditions: conds}}
  117. return &cc
  118. }
  119. // OR method that combines many conditions with OR operator
  120. func OR(cond ...ICondition) ICondition {
  121. conds := make([]ICondition, 0)
  122. for _, c := range cond {
  123. if c == nil || c == noop {
  124. conds = conds[0:0]
  125. break
  126. }
  127. orCond, ok := c.(*SOrConditions)
  128. if ok {
  129. conds = append(conds, orCond.conditions...)
  130. } else {
  131. conds = append(conds, c)
  132. }
  133. }
  134. cc := SOrConditions{SCompoundConditions{conditions: conds}}
  135. return &cc
  136. }
  137. // SNotCondition represents the NOT condition, which is a boolean operator
  138. type SNotCondition struct {
  139. condition ICondition
  140. }
  141. // WhereClause implementationq of SNotCondition for ICondition
  142. func (c *SNotCondition) WhereClause() string {
  143. if c.condition == nil || c.condition == noop {
  144. return "1!=1"
  145. }
  146. return fmt.Sprintf("%s (%s)", SQL_OP_NOT, c.condition.WhereClause())
  147. }
  148. // Variables implementation of SNotCondition for ICondition
  149. func (c *SNotCondition) Variables() []interface{} {
  150. if c.condition == nil {
  151. return nil
  152. }
  153. return c.condition.Variables()
  154. }
  155. // database implementation of SNotCondition for ICondition
  156. func (c *SNotCondition) database() *SDatabase {
  157. if c.condition == nil {
  158. return nil
  159. }
  160. return c.condition.database()
  161. }
  162. // NOT method that makes negative operator on a condition
  163. func NOT(cond ICondition) ICondition {
  164. cc := SNotCondition{condition: cond}
  165. return &cc
  166. }
  167. // SSingleCondition represents a kind of condition that composed of one query field
  168. type SSingleCondition struct {
  169. field IQueryField
  170. }
  171. // Variables implementation of SSingleCondition for ICondition
  172. func (c *SSingleCondition) Variables() []interface{} {
  173. return c.field.Variables()
  174. }
  175. // database implementation of SSingleCondition for ICondition
  176. func (c *SSingleCondition) database() *SDatabase {
  177. return c.field.database()
  178. }
  179. // NewSingleCondition returns an instance of SSingleCondition
  180. func NewSingleCondition(field IQueryField) SSingleCondition {
  181. return SSingleCondition{field: field}
  182. }
  183. // SIsNullCondition is a condition representing a comparison with null, e.g. a is null
  184. type SIsNullCondition struct {
  185. SSingleCondition
  186. }
  187. // WhereClause implementation for SIsNullCondition for ICondition
  188. func (c *SIsNullCondition) WhereClause() string {
  189. return fmt.Sprintf("%s IS NULL", c.field.Reference())
  190. }
  191. // IsNull methods that justifies a field is null
  192. func IsNull(f IQueryField) ICondition {
  193. c := SIsNullCondition{NewSingleCondition(f)}
  194. return &c
  195. }
  196. // SIsNotNullCondition is a condition represents a comparison with not null, e.g. a is not null
  197. type SIsNotNullCondition struct {
  198. SSingleCondition
  199. }
  200. // WhereClause implementation of SIsNotNullCondition for ICondition
  201. func (c *SIsNotNullCondition) WhereClause() string {
  202. return fmt.Sprintf("%s IS NOT NULL", c.field.Reference())
  203. }
  204. // IsNotNull methods that justifies a field is not null
  205. func IsNotNull(f IQueryField) ICondition {
  206. c := SIsNotNullCondition{NewSingleCondition(f)}
  207. return &c
  208. }
  209. // SIsEmptyCondition is a condition representing the empty status of a field
  210. type SIsEmptyCondition struct {
  211. SSingleCondition
  212. }
  213. // WhereClause implementation of SIsEmptyCondition for ICondition
  214. func (c *SIsEmptyCondition) WhereClause() string {
  215. // DAMENG LENGTH('') = NULL
  216. return fmt.Sprintf("%s = 0 OR %s IS NULL", c.field.Reference(), c.field.Reference())
  217. }
  218. // IsEmpty method that justifies where a text field is empty, e.g. length is zero
  219. func IsEmpty(f IQueryField) ICondition {
  220. c := SIsEmptyCondition{NewSingleCondition(LENGTH("", f))}
  221. return &c
  222. }
  223. // SIsNullOrEmptyCondition is a condition that justifies a field is null or empty
  224. type SIsNullOrEmptyCondition struct {
  225. SSingleCondition
  226. }
  227. // WhereClause implementation of SIsNullOrEmptyCondition for ICondition
  228. func (c *SIsNullOrEmptyCondition) WhereClause() string {
  229. originField := c.field.(*SFunctionFieldBase).queryFields()[0]
  230. // DAMENG: LENGTH('') = NULL
  231. return fmt.Sprintf("%s IS NULL OR %s = 0 OR %s IS NULL", originField.Reference(), c.field.Reference(), c.field.Reference())
  232. }
  233. // IsNullOrEmpty is the ethod justifies a field is null or empty, e.g. a is null or length(a) == 0
  234. func IsNullOrEmpty(f IQueryField) ICondition {
  235. c := SIsNullOrEmptyCondition{NewSingleCondition(LENGTH("", f))}
  236. return &c
  237. }
  238. // SIsNotEmptyCondition represents a condition that represents a field is not empty
  239. type SIsNotEmptyCondition struct {
  240. SSingleCondition
  241. }
  242. // WhereClause implementation of SIsNotEmptyCondition for ICondition
  243. func (c *SIsNotEmptyCondition) WhereClause() string {
  244. originField := c.field.(*SFunctionFieldBase).queryFields()[0]
  245. return fmt.Sprintf("%s IS NOT NULL AND %s > 0", originField.Reference(), c.field.Reference())
  246. }
  247. // IsNotEmpty method justifies a field is not empty
  248. func IsNotEmpty(f IQueryField) ICondition {
  249. c := SIsNotEmptyCondition{NewSingleCondition(LENGTH("", f))}
  250. return &c
  251. }
  252. // SIsTrueCondition represents a boolean field (TINYINT) is true, e.g. a == 1
  253. type SIsTrueCondition struct {
  254. SSingleCondition
  255. }
  256. // WhereClause implementation of SIsTrueCondition for ICondition
  257. func (c *SIsTrueCondition) WhereClause() string {
  258. return fmt.Sprintf("%s = 1", c.field.Reference())
  259. }
  260. // IsTrue method that justifies a field is true, e.g. field == 1
  261. func IsTrue(f IQueryField) ICondition {
  262. c := SIsTrueCondition{NewSingleCondition(f)}
  263. return &c
  264. }
  265. // SIsFalseCondition represents a boolean is false
  266. type SIsFalseCondition struct {
  267. SSingleCondition
  268. }
  269. // WhereClause implementation of SIsFalseCondition for ICondition
  270. func (c *SIsFalseCondition) WhereClause() string {
  271. return fmt.Sprintf("%s = 0", c.field.Reference())
  272. }
  273. // IsFalse method justifies a boolean is false
  274. func IsFalse(f IQueryField) ICondition {
  275. c := SIsFalseCondition{NewSingleCondition(f)}
  276. return &c
  277. }
  278. // SNoLaterThanCondition compares a DATETIME field with current time and ensure the field is no later than now, e.g. a <= NOW()
  279. type SNoLaterThanCondition struct {
  280. SSingleCondition
  281. }
  282. // WhereClause implementation of SNoLaterThanCondition for ICondition
  283. func (c *SNoLaterThanCondition) WhereClause() string {
  284. nowStr := c.field.database().backend.CurrentTimeStampString()
  285. return fmt.Sprintf("%s <= %s", c.field.Reference(), nowStr)
  286. }
  287. // NoLaterThan method justifies a DATETIME field is before current time
  288. func NoLaterThan(f IQueryField) ICondition {
  289. c := SNoLaterThanCondition{NewSingleCondition(f)}
  290. return &c
  291. }
  292. // SNoEarlierThanCondition compares a field with current time and ensure the field is no earlier than NOW, e.g. a >= NOW()
  293. type SNoEarlierThanCondition struct {
  294. SSingleCondition
  295. }
  296. // WhereClause implementation of SNoEarlierThanCondition for ICondition
  297. func (c *SNoEarlierThanCondition) WhereClause() string {
  298. nowStr := c.field.database().backend.CurrentTimeStampString()
  299. return fmt.Sprintf("%s >= %s", c.field.Reference(), nowStr)
  300. }
  301. // NoEarlierThan justifies a field is no earlier than current time
  302. func NoEarlierThan(f IQueryField) ICondition {
  303. c := SNoEarlierThanCondition{NewSingleCondition(f)}
  304. return &c
  305. }
  306. // STupleCondition is a base condition that composed of two fields
  307. type STupleCondition struct {
  308. left IQueryField
  309. right interface{}
  310. }
  311. func tupleConditionWhereClause(t *STupleCondition, op string) string {
  312. return tupleConditionWhereClauseInternal(t, op, "")
  313. }
  314. func TupleConditionWhereClauseWithFuncname(t *STupleCondition, funcName string) string {
  315. return tupleConditionWhereClauseInternal(t, ",", funcName)
  316. }
  317. func tupleConditionWhereClauseInternal(t *STupleCondition, op string, funcName string) string {
  318. if isFieldRequireAscii(t.left) && !isVariableAscii(t.right) {
  319. return "0"
  320. }
  321. var buf bytes.Buffer
  322. if len(funcName) > 0 {
  323. buf.WriteString(funcName)
  324. buf.WriteByte('(')
  325. }
  326. buf.WriteString(t.left.Reference())
  327. buf.WriteByte(' ')
  328. buf.WriteString(op)
  329. buf.WriteByte(' ')
  330. buf.WriteString(VarConditionWhereClause(t.right))
  331. if len(funcName) > 0 {
  332. buf.WriteByte(')')
  333. }
  334. return buf.String()
  335. }
  336. func questionMark(count int) string {
  337. if count == 0 {
  338. return ""
  339. } else if count == 1 {
  340. return " ? "
  341. } else {
  342. var buf bytes.Buffer
  343. buf.WriteString("( ")
  344. for i := 0; i < count; i++ {
  345. if i > 0 {
  346. buf.WriteString(", ")
  347. }
  348. buf.WriteString("?")
  349. }
  350. buf.WriteString(" )")
  351. return buf.String()
  352. }
  353. }
  354. func VarConditionWhereClause(v interface{}) string {
  355. switch q := v.(type) {
  356. case IQueryField:
  357. return q.Reference()
  358. case *SQuery:
  359. return fmt.Sprintf("(%s)", q.String())
  360. case *SSubQuery:
  361. return q.Expression()
  362. default:
  363. expandV := reflectutils.ExpandInterface(v)
  364. return questionMark(len(expandV))
  365. }
  366. }
  367. func varConditionVariables(v interface{}) []interface{} {
  368. switch vv := v.(type) {
  369. case IQueryField:
  370. return []interface{}{}
  371. case *SQuery:
  372. return vv.Variables()
  373. case *SSubQuery:
  374. return vv.query.Variables()
  375. default:
  376. return reflectutils.ExpandInterface(v)
  377. }
  378. }
  379. // NewTupleCondition returns an instance of tuple condition
  380. func NewTupleCondition(l IQueryField, r interface{}) STupleCondition {
  381. return STupleCondition{left: l, right: r}
  382. }
  383. func (t *STupleCondition) GetLeft() IQueryField {
  384. return t.left
  385. }
  386. func (t *STupleCondition) GetRight() interface{} {
  387. return t.right
  388. }
  389. // Variables implementation of STupleCondition for ICondition
  390. func (t *STupleCondition) Variables() []interface{} {
  391. if isFieldRequireAscii(t.left) && !isVariableAscii(t.right) {
  392. return []interface{}{}
  393. }
  394. vars := varConditionVariables(t.right)
  395. for i := range vars {
  396. vars[i] = t.left.ConvertFromValue(vars[i])
  397. }
  398. return vars
  399. }
  400. // database implementation of STupleCondition for ICondition
  401. func (t *STupleCondition) database() *SDatabase {
  402. return t.left.database()
  403. }
  404. // SInCondition represents a IN operation in SQL query
  405. type SInCondition struct {
  406. STupleCondition
  407. op string
  408. }
  409. func inConditionWhereClause(t *STupleCondition, op string) string {
  410. v := VarConditionWhereClause(t.right)
  411. if len(v) != 0 {
  412. return tupleConditionWhereClause(t, op)
  413. }
  414. return "0"
  415. }
  416. // WhereClause implementation of SInCondition for ICondition
  417. func (t *SInCondition) WhereClause() string {
  418. return inConditionWhereClause(&t.STupleCondition, t.op)
  419. }
  420. // In SQL operator
  421. func In(f IQueryField, v interface{}) ICondition {
  422. switch v.(type) {
  423. case IQueryField, *SQuery, *SSubQuery:
  424. default:
  425. expandV := reflectutils.ExpandInterface(v)
  426. switch len(expandV) {
  427. case 0:
  428. return &SFalseCondition{}
  429. case 1:
  430. return Equals(f, expandV[0])
  431. default:
  432. }
  433. }
  434. c := SInCondition{
  435. NewTupleCondition(f, v),
  436. SQL_OP_IN,
  437. }
  438. return &c
  439. }
  440. // NotIn SQL operator
  441. func NotIn(f IQueryField, v interface{}) ICondition {
  442. switch v.(type) {
  443. case IQueryField, *SQuery, *SSubQuery:
  444. default:
  445. expandV := reflectutils.ExpandInterface(v)
  446. switch len(expandV) {
  447. case 0:
  448. return &STrueCondition{}
  449. case 1:
  450. return NotEquals(f, expandV[0])
  451. default:
  452. }
  453. }
  454. c := SInCondition{
  455. NewTupleCondition(f, v),
  456. SQL_OP_NOTIN,
  457. }
  458. return &c
  459. }
  460. // SLikeCondition represents LIKE operation in a SQL query
  461. type SLikeCondition struct {
  462. STupleCondition
  463. }
  464. func likeEscape(s string) string {
  465. var res bytes.Buffer
  466. for i := 0; i < len(s); i++ {
  467. if s[i] == '_' || s[i] == '%' {
  468. res.WriteByte('\\')
  469. }
  470. res.WriteByte(s[i])
  471. }
  472. return res.String()
  473. }
  474. // WhereClause implementation for SLikeCondition for ICondition
  475. func (t *SLikeCondition) WhereClause() string {
  476. op := t.left.database().backend.CaseInsensitiveLikeString()
  477. return tupleConditionWhereClause(&t.STupleCondition, op)
  478. }
  479. // Like SQL operator
  480. func Like(f IQueryField, v string) ICondition {
  481. c := SLikeCondition{NewTupleCondition(f, v)}
  482. return &c
  483. }
  484. // SRegexpConition represents REGEXP operation in a SQL query
  485. type SRegexpConition struct {
  486. STupleCondition
  487. }
  488. // WhereClause implementation for SRegexpConition for ICondition
  489. func (t *SRegexpConition) WhereClause() string {
  490. return t.left.database().backend.RegexpWhereClause(t)
  491. }
  492. // Regexp SQL operator
  493. func Regexp(f IQueryField, v string) ICondition {
  494. c := SRegexpConition{NewTupleCondition(f, v)}
  495. return &c
  496. }
  497. // ContainsAny is a OR combination of serveral Contains conditions
  498. func ContainsAny(f IQueryField, v []string) ICondition {
  499. conds := make([]ICondition, len(v))
  500. for i := range v {
  501. conds[i] = Contains(f, v[i])
  502. }
  503. return OR(conds...)
  504. }
  505. // Contains method is a shortcut of LIKE method, Contains represents the condtion that a field contains a substring
  506. func Contains(f IQueryField, v string) ICondition {
  507. v = likeEscape(v)
  508. nv := fmt.Sprintf("%%%s%%", v)
  509. return Like(f, nv)
  510. }
  511. // Startswith method is a shortcut of LIKE method, Startswith represents the condition that field starts with a substring
  512. func Startswith(f IQueryField, v string) ICondition {
  513. v = likeEscape(v)
  514. nv := fmt.Sprintf("%s%%", v)
  515. return Like(f, nv)
  516. }
  517. // Endswith method is a shortcut of LIKE condition, Endswith represents that condition that field endswith a substring
  518. func Endswith(f IQueryField, v string) ICondition {
  519. v = likeEscape(v)
  520. nv := fmt.Sprintf("%%%s", v)
  521. return Like(f, nv)
  522. }
  523. // SEqualsCondition represents equal operation between two fields
  524. type SEqualsCondition struct {
  525. STupleCondition
  526. }
  527. // WhereClause implementation of SEqualsCondition for ICondition
  528. func (t *SEqualsCondition) WhereClause() string {
  529. return tupleConditionWhereClause(&t.STupleCondition, SQL_OP_EQUAL)
  530. }
  531. // Equals method represents equal of two fields
  532. func Equals(f IQueryField, v interface{}) ICondition {
  533. return f.database().backend.Equals(f, v)
  534. }
  535. // SNotEqualsCondition is the opposite of equal condition
  536. type SNotEqualsCondition struct {
  537. STupleCondition
  538. }
  539. // WhereClause implementation of SNotEqualsCondition for ICondition
  540. func (t *SNotEqualsCondition) WhereClause() string {
  541. return tupleConditionWhereClause(&t.STupleCondition, SQL_OP_NOTEQUAL)
  542. }
  543. // NotEquals method represents not equal of two fields
  544. func NotEquals(f IQueryField, v interface{}) ICondition {
  545. c := SNotEqualsCondition{NewTupleCondition(f, v)}
  546. return &c
  547. }
  548. // SGreatEqualCondition represents >= operation on two fields
  549. type SGreatEqualCondition struct {
  550. STupleCondition
  551. }
  552. // WhereClause implementation of SGreatEqualCondition for ICondition
  553. func (t *SGreatEqualCondition) WhereClause() string {
  554. return tupleConditionWhereClause(&t.STupleCondition, SQL_OP_GE)
  555. }
  556. // GE method represetns operation of Greate Than Or Equal to, e.g. a >= b
  557. func GE(f IQueryField, v interface{}) ICondition {
  558. c := SGreatEqualCondition{NewTupleCondition(f, v)}
  559. return &c
  560. }
  561. // SGreatThanCondition represetns > operation on two fields
  562. type SGreatThanCondition struct {
  563. STupleCondition
  564. }
  565. // WhereClause implementation of SGreatThanCondition for ICondition
  566. func (t *SGreatThanCondition) WhereClause() string {
  567. return tupleConditionWhereClause(&t.STupleCondition, SQL_OP_GT)
  568. }
  569. // GT method represents operation of Great Than, e.g. a > b
  570. func GT(f IQueryField, v interface{}) ICondition {
  571. c := SGreatThanCondition{NewTupleCondition(f, v)}
  572. return &c
  573. }
  574. // SLessEqualCondition represents <= operation on two fields
  575. type SLessEqualCondition struct {
  576. STupleCondition
  577. }
  578. // WhereClause implementation of SLessEqualCondition for ICondition
  579. func (t *SLessEqualCondition) WhereClause() string {
  580. return tupleConditionWhereClause(&t.STupleCondition, SQL_OP_LE)
  581. }
  582. // LE method represents operation of Less Than Or Equal to, e.q. a <= b
  583. func LE(f IQueryField, v interface{}) ICondition {
  584. c := SLessEqualCondition{NewTupleCondition(f, v)}
  585. return &c
  586. }
  587. // SLessThanCondition represents < operation on two fields
  588. type SLessThanCondition struct {
  589. STupleCondition
  590. }
  591. // WhereClause implementation of SLessThanCondition for ICondition
  592. func (t *SLessThanCondition) WhereClause() string {
  593. return tupleConditionWhereClause(&t.STupleCondition, SQL_OP_LT)
  594. }
  595. // LT method represents operation of Less Than, e.g. a < b
  596. func LT(f IQueryField, v interface{}) ICondition {
  597. c := SLessThanCondition{NewTupleCondition(f, v)}
  598. return &c
  599. }
  600. // STripleCondition represents a base condition that composed of THREE fields
  601. type STripleCondition struct {
  602. STupleCondition
  603. right2 interface{}
  604. }
  605. // Variables implementation of STripleCondition for ICondition
  606. func (t *STripleCondition) Variables() []interface{} {
  607. ret := make([]interface{}, 0)
  608. vars := varConditionVariables(t.right)
  609. for i := range vars {
  610. ret = append(ret, t.left.ConvertFromValue(vars[i]))
  611. }
  612. vars = varConditionVariables(t.right2)
  613. for i := range vars {
  614. ret = append(ret, t.left.ConvertFromValue(vars[i]))
  615. }
  616. return ret
  617. }
  618. // NewTripleCondition return an instance of STripleCondition
  619. func NewTripleCondition(l IQueryField, r interface{}, r2 interface{}) STripleCondition {
  620. return STripleCondition{STupleCondition: NewTupleCondition(l, r),
  621. right2: r2}
  622. }
  623. // SBetweenCondition represents BETWEEN operator, e.g. c between a and b
  624. type SBetweenCondition struct {
  625. STripleCondition
  626. }
  627. // WhereClause implementation of SBetweenCondition for ICondition
  628. func (t *SBetweenCondition) WhereClause() string {
  629. ret := tupleConditionWhereClause(&t.STupleCondition, SQL_OP_BETWEEN)
  630. return fmt.Sprintf("%s AND %s", ret, VarConditionWhereClause(t.right2))
  631. }
  632. // Between SQL operator
  633. func Between(f IQueryField, r1, r2 interface{}) ICondition {
  634. c := SBetweenCondition{NewTripleCondition(f, r1, r2)}
  635. return &c
  636. }
  637. // STrueCondition represents a dummy condition that is always true
  638. type STrueCondition struct{}
  639. // WhereClause implementation of STrueCondition for ICondition
  640. func (t *STrueCondition) WhereClause() string {
  641. return "1"
  642. }
  643. // Variables implementation of STrueCondition for ICondition
  644. func (t *STrueCondition) Variables() []interface{} {
  645. return nil
  646. }
  647. func (t *STrueCondition) database() *SDatabase {
  648. return nil
  649. }
  650. // SFalseCondition is a dummy condition that is always false
  651. type SFalseCondition struct{}
  652. // WhereClause implementation of SFalseCondition for ICondition
  653. func (t *SFalseCondition) WhereClause() string {
  654. return "0"
  655. }
  656. // Variables implementation of SFalseCondition for ICondition
  657. func (t *SFalseCondition) Variables() []interface{} {
  658. return nil
  659. }
  660. func (t *SFalseCondition) database() *SDatabase {
  661. return nil
  662. }
  663. var (
  664. AlwaysTrue = &STrueCondition{}
  665. AlwaysFalse = &SFalseCondition{}
  666. )