예제 #1
0
def _init_meta():
    mysql_pool.execute('''
    CREATE TABLE IF NOT EXISTS proto_table_meta (
    table_name VARCHAR(200) NOT NULL,
    db_column VARCHAR(200) NOT NULL,
    py_column VARCHAR(200) NOT NULL,
    type INT NOT NULL,
    PRIMARY KEY (table_name, db_column)
    );
    ''')
예제 #2
0
def _get_table_meta(table):
    rows = mysql_pool.execute('''
    SELECT * FROM proto_table_meta WHERE table_name = %s
    ''', (table,), return_one=False)

    colums = []
    for row in rows:
        colums.append({'db_column': row['db_column'], 'py_column': row['py_column'], 'type': row['type']})

    return colums
예제 #3
0
    def filter(self, **kwargs):
        def to_string(val, is_str):
            if isinstance(val, list):
                if is_str:
                    return "(" + ",".join(["'" + str(v) + "'"
                                           for v in val]) + ")"
                else:
                    return "(" + ",".join([str(v) for v in val]) + ")"
            elif is_str:
                return "'" + str(val) + "'"
            else:
                return str(val)

        wheres = []
        for key in kwargs:
            field = key.split('__')[0]
            op = key.split('__')[1]
            val = kwargs[key]
            if field not in self._fields:
                raise ValueError('No field {0}'.format(field))
            if op not in OP:
                raise ValueError('No operator {0}'.format(op))
            if self._fields[field][1] not in OP[op]:
                raise ValueError(
                    'Operator {0} is invalid for field {1} with type {2}'.
                    format(op, field, self._fields[field][1]))
            if op == 'in' and not isinstance(val, list):
                raise ValueError('In operator only accept list')
            wheres.append('c' + str(self._fields[field][0]) + OP[op][0] +
                          to_string(val, self._fields[field][1] == 9))

        q = Query().from_table(_get_table_names(self._m)).where(
            ' AND '.join(wheres))
        rows = mysql_pool.execute(str(q), return_one=False)

        return [_to_proto(self._m, row) for row in rows]
예제 #4
0
 def insert(self, obj):
     _, _, values = _to_value_dict(self._m, obj)
     q = Insert(values).into_table(_get_table_names(self._m))
     row = mysql_pool.execute(str(q), write=True)
     return row
예제 #5
0
 def get(self, id):
     q = Query().from_table(_get_table_names(self._m)).where(
         "{0}={1}".format(self.id_column, id))
     row = mysql_pool.execute(str(q), return_one=True)
     return _to_proto(self._m, row)