def left_join(self, other, on): wh = "FROM" SqlBuilder.Hierarchy(self._ctx, other._ctx) self._ctx.make_alias(self.__class__) other._ctx.make_alias(other.__class__) self._ctx.press(wh, SqlBuilder.Join(lhs=self, rhs=other, on=on)) return self
def _build_update_sql(self): if not self._ctx.is_exist("WHERE"): pk = self._meta.pk pkv = getattr(self, pk, None) if pkv is None: raise SQLError("Update sql must be have WHERE condition") cls = self.__class__ pkf = getattr(cls, pk) self._ctx.press("WHERE", pkf == pkv) self._ctx.press("UPDATE", self._meta.full_path) sets = [] columns = self._meta.columns for k, field in columns.items(): val = getattr(self, k, None) if val is None: continue ex = "`%s` = %%s" % k self._ctx.press("SET", ex) sets.append(val) sql = SqlBuilder.update_sql(self._ctx) return sql, sets
def _build_insert_sql(self): values = [] path = self._meta.full_path self._ctx.press("INSERT INTO", "") validate_value = self.retrieve_entity_col() for k, v in validate_value.items(): field = self._meta.columns.get(k, None) if field is None: continue self._ctx.press(path, field) self._ctx.press("VALUES", "") # For query type values.append(v) # for each in self._en_column: # db_col_name = self._en_column.get(each) # field = self._meta.columns.get(db_col_name, None) # if field is None: continue # # value = getattr(self, db_col_name, None) # if not value: value = getattr(self, each, None) # # self._ctx.press(path, field) # self._ctx.press("VALUES", "") # For query type # # final_val = field.validate_self(value) # values.append(final_val) # # self._ctx.container("VALUES", value) sql = SqlBuilder.insert_sql(self._ctx, path=path) return sql, values
def __init__(self, *args, **kwargs): # self._ctx = dict() self._ctx = SqlBuilder.SqlContext() self.__data__ = dict() self._dirty = set(self.__data__) for k in kwargs: setattr(self, k, kwargs[k])
def _build_select_sql(self): if not self._ctx.is_exist("SELECT"): [ self._ctx.press("SELECT", '`%s`' % a) for a in self._meta.columns.keys() ] if not self._ctx.is_exist("FROM"): self._ctx.press("FROM", self._meta.full_path) return SqlBuilder.select_sql(self._ctx)
def create_table(self, en_cls, entity_name, db_name): en_col = en_cls.en_col_mapping() info_list = {} for field_name in en_col.values(): col_info = getattr(en_cls, field_name) col_name = field_name field_type = col_info.field_type field_len = col_info.max_length field_default = col_info.default null_able = col_info.null info_list[col_info._order] = [col_name, field_type, field_len, field_default, null_able] ctx = { "path": entity_name, "col_info": [each[1] for each in sorted(info_list.items())], "engine": "innodb", "charset": "utf8" } sql = SqlBuilder.create_sql(ctx) self.session.execute_insert(sql, db_name=db_name)
def _build_proc_sql(self, proc_name, parm): if not isinstance(parm, (list, tuple)): parm = (parm, ) sql = SqlBuilder.proc_sql(proc_name, parm) return sql