def __generate_sql(model):
        """
        Generate the SQL for the given model.

        :param model: The model class
        :type :class:`Model <dragonfly.db.models.model.Model>`

        :return: The generated SQL
        """
        depends = []

        sql = f"CREATE TABLE {model.meta['table_name']} (\n"

        # Get all fields
        for key, value in model.fields.items():
            sql += f"{key} {value.to_database_type()},\n"

        # Get any meta information
        for key, value in model.meta.items():

            if isinstance(value, ForeignKey):
                to_append = Table.foreign_key(key, value.table,
                                              value.local_keys,
                                              value.foreign_keys)
                # If foreign key present this table must depend __on the existence of another
                depends.append(value.table)
                sql += f"{to_append}, \n"

            elif isinstance(value, Unique):
                to_append = Table.unique(*value.fields, constraint_name=key)
                sql += f"{to_append}, \n"

            elif isinstance(value, PrimaryKey):
                to_append = Table.primary_key(*value.fields)
                sql += f"{to_append}, \n"

        sql = sql.rstrip()
        sql = sql[:-1]
        sql += "\n)"

        return depends, sql
Example #2
0
 def to_database_type(self):
     return Table.set(*self.__set)
Example #3
0
 def to_database_type(self):
     return Table.longblob(**self.default_parameters)
Example #4
0
 def to_database_type(self):
     return Table.enum(*self.enum)
Example #5
0
 def to_database_type(self):
     return Table.varbinary(self._length, **self.default_parameters)
Example #6
0
 def to_database_type(self):
     return Table.mediumtext(**self.default_parameters)
Example #7
0
 def to_database_type(self):
     return Table.year(**self.default_parameters)
Example #8
0
 def to_database_type(self):
     return Table.text(self._length, **self.default_parameters)
Example #9
0
 def to_database_type(self):
     return Table.time(self.__fsp, **self.default_parameters)
Example #10
0
 def to_database_type(self):
     return Table.float(self.__digits, **self.decimal_parameters,
                        **self.default_parameters)
Example #11
0
 def to_database_type(self):
     return Table.double(self.__digits, self.__decimal_places,
                         **self.decimal_parameters,
                         **self.default_parameters)
Example #12
0
 def to_database_type(self):
     return Table.tinyint(self._length, **self.integer_parameters,
                          **self.default_parameters)
Example #13
0
 def to_database_type(self):
     return Table.boolean(**self.default_parameters)