bean.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # -*- coding:utf-8 -*-
  2. # Copyright 2017 Xiaomi, Inc.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. __author__ = 'Ulric Qin'
  16. from rrd.store import db
  17. class Bean(object):
  18. _tbl = ''
  19. _id = 'id'
  20. _cols = ''
  21. _db = db
  22. @classmethod
  23. def insert(cls, data=None):
  24. if not data:
  25. raise ValueError('argument data is invalid')
  26. size = len(data)
  27. keys = data.keys()
  28. safe_keys = ['`%s`' % k for k in keys]
  29. sql = 'INSERT INTO `%s`(%s) VALUES(%s)' % (cls._tbl, ','.join(safe_keys), '%s' + ',%s' * (size - 1))
  30. last_id = cls._db.insert(sql, [data[key] for key in keys])
  31. return last_id
  32. @classmethod
  33. def delete(cls, where=None, params=None):
  34. sql = 'DELETE FROM `%s`' % cls._tbl
  35. if not where:
  36. return cls._db.update(sql)
  37. sql += ' WHERE ' + where
  38. return cls._db.update(sql, params)
  39. @classmethod
  40. def delete_one(cls, pk=None):
  41. sql = 'DELETE FROM `%s` WHERE %s = %%s' % (cls._tbl, cls._id)
  42. return cls._db.update(sql, [pk])
  43. @classmethod
  44. def update(cls, clause=None, params=None):
  45. sql = 'UPDATE `%s` SET %s' % (cls._tbl, clause)
  46. return cls._db.update(sql, params)
  47. @classmethod
  48. def update_dict(cls, data=None, where='', params=None):
  49. if not data:
  50. raise ValueError('argument data is invalid')
  51. size = len(data)
  52. keys = data.keys()
  53. safe_keys = ['`%s`' % k for k in keys]
  54. values = [data[key] for key in keys]
  55. arr = ['%s=%%s' % key for key in safe_keys]
  56. if not where:
  57. return cls.update(','.join(arr), values)
  58. else:
  59. values.extend(params)
  60. return cls.update(', '.join(arr) + ' WHERE ' + where, values)
  61. @classmethod
  62. def select(cls, cols=None, where=None, params=None, order=None, limit=None, page=None, offset=None):
  63. if cols is None:
  64. cols = cls._cols
  65. if params is None:
  66. params = []
  67. sql = 'SELECT %s FROM `%s`' % (cols, cls._tbl)
  68. if where:
  69. sql = '%s WHERE %s' % (sql, where)
  70. if order:
  71. sql = '%s ORDER BY %s' % (sql, order)
  72. if limit is not None:
  73. sql = '%s LIMIT %s' % (sql, limit)
  74. if offset is not None:
  75. sql = '%s OFFSET %s' % (sql, offset)
  76. if page is not None:
  77. offset = (int(page) - 1) * int(limit)
  78. if offset < 0:
  79. offset = 0
  80. sql = '%s OFFSET %s' % (sql, offset)
  81. return cls._db.query_all(sql, params)
  82. @classmethod
  83. def select_vs(cls, where=None, params=None, order=None, limit=None, page=None, offset=None):
  84. rows = cls.select(where=where, params=params, order=order, limit=limit, page=page, offset=offset)
  85. return [cls(*row) for row in rows]
  86. @classmethod
  87. def read(cls, where=None, params=None):
  88. vs = cls.select_vs(where=where, params=params)
  89. if vs:
  90. return vs[0]
  91. else:
  92. return None
  93. @classmethod
  94. def get(cls, id_val):
  95. if not id_val:
  96. return None
  97. return cls.read('%s = %%s' % cls._id, [id_val])
  98. @classmethod
  99. def column(cls, col=None, where=None, params=None, order=None, limit=None, page=None, offset=None):
  100. rows = cls.select(col, where, params, order, limit, page, offset)
  101. return [row[0] for row in rows]
  102. @classmethod
  103. def total(cls, where=None, params=None):
  104. sql = 'SELECT COUNT(1) FROM `%s`' % cls._tbl
  105. if not where:
  106. ret = cls._db.query_column(sql)
  107. return ret[0]
  108. sql += ' WHERE ' + where
  109. ret = cls._db.query_column(sql, params)
  110. return ret[0]
  111. @classmethod
  112. def exists(cls, where=None, params=None):
  113. return cls.total(where, params) > 0