o
    uvXjš  ã                   @   sL   d dl Z d dlZd dlmZ d dlmZmZ d dlmZ G dd„ deƒZ	dS )é    N)Úsettings)ÚBaseCommandÚCommandError)Úsignalcommandc                       s4   e Zd ZdZdZdZ‡ fdd„Zedd„ ƒZ‡  Z	S )ÚCommanda$  Generates the SQL to create your database for you, as specified in settings.py
The envisioned use case is something like this:

    ./manage.py sqlcreate [--router=<routername>] | mysql -u <db_administrator> -p
    ./manage.py sqlcreate [--router=<routername>] | psql -U <db_administrator> -WFTc                    s@   t t| ƒ |¡ |jddddddd� |jdd	d
dddd� d S )Nz-Rz--routerÚstoreÚrouterÚdefaultz:Use this router-database other then defined in settings.py)ÚactionÚdestr	   Úhelpz-Dz--dropÚ
store_trueÚdropFzCIf given, includes commands to drop any existing user and database.)Úsuperr   Úadd_argumentsÚadd_argument)ÚselfÚparser©Ú	__class__© úl/var/www/html/myproject/venv/lib/python3.10/site-packages/django_extensions/management/commands/sqlcreate.pyr      s   
þ

þzCommand.add_argumentsc                 O   sT  |d }t j |¡}|d u rtd| ƒ‚| d¡ d¡d }| d¡}| d¡}| d¡}| d	¡}	t ¡ }
|	s:d
}	|dkrVtj 	d¡ t
d| ƒ t
d|||
|f ƒ d S |dv r†|d rlt
d|f ƒ t
d|f ƒ t
d||f ƒ t
d||f ƒ t
d||f ƒ d S |dkr’tj 	d¡ d S tj 	d| ¡ t
d| ƒ t
d||f ƒ d S )Nr   zUnknown database router %sÚENGINEÚ.éÿÿÿÿÚUSERÚPASSWORDÚNAMEÚHOSTÚ	localhostÚmysqlz³-- WARNING!: https://docs.djangoproject.com/en/dev/ref/databases/#collation-settings
-- Please read this carefully! Collation will be set to utf8_bin to have case-sensitive data.
z7CREATE DATABASE %s CHARACTER SET utf8 COLLATE utf8_bin;z=GRANT ALL PRIVILEGES ON %s.* to '%s'@'%s' identified by '%s';)Ú
postgresqlÚpostgresql_psycopg2Úpostgisr   zDROP DATABASE IF EXISTS %s;zDROP USER IF EXISTS %s;z5CREATE USER %s WITH ENCRYPTED PASSWORD '%s' CREATEDB;z4CREATE DATABASE %s WITH ENCODING 'UTF-8' OWNER "%s";z*GRANT ALL PRIVILEGES ON DATABASE %s TO %s;Úsqlite3zG-- manage.py syncdb will automatically create a sqlite3 database file.
z6-- Don't know how to handle '%s' falling back to SQL.
zCREATE DATABASE %s;z*GRANT ALL PRIVILEGES ON DATABASE %s to %s;)r   Ú	DATABASESÚgetr   ÚsplitÚsocketÚgethostnameÚsysÚstderrÚwriteÚprint)r   ÚargsÚoptionsr   ÚdbinfoÚengineÚdbuserÚdbpassÚdbnameÚdbhostÚdbclientr   r   r   Úhandle    s<   



ÿzCommand.handle)
Ú__name__Ú
__module__Ú__qualname__r   Úrequires_system_checksÚcan_import_settingsr   r   r7   Ú__classcell__r   r   r   r   r      s    r   )
r(   r*   Údjango.confr   Údjango.core.management.baser   r   Ú"django_extensions.management.utilsr   r   r   r   r   r   Ú<module>   s   