schemas
1 year ago
ActivatePlugin.php
1 year ago
ActivateTheme.php
1 year ago
InstallPlugin.php
1 year ago
InstallTheme.php
1 year ago
RunSql.php
4 days ago
SetSiteOptions.php
1 year ago
Step.php
1 year ago
RunSql.php
128 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Blueprint\Steps; |
| 4 | |
| 5 | use Automattic\WooCommerce\Blueprint\Util; |
| 6 | |
| 7 | /** |
| 8 | * Class RunSql |
| 9 | * |
| 10 | * @package Automattic\WooCommerce\Blueprint\Steps |
| 11 | */ |
| 12 | class RunSql extends Step { |
| 13 | /** |
| 14 | * Placeholder for the database table prefix. |
| 15 | * |
| 16 | * Exported SQL uses this placeholder in place of the source site's table |
| 17 | * prefix. When the Blueprint is imported, the placeholder is replaced with |
| 18 | * the importing site's prefix, so Blueprints remain portable across sites |
| 19 | * that use different database table prefixes. |
| 20 | * |
| 21 | * @var string |
| 22 | */ |
| 23 | public const TABLE_PREFIX_PLACEHOLDER = '{WC_BLUEPRINT_TABLE_PREFIX}'; |
| 24 | |
| 25 | /** |
| 26 | * Sql code to run. |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | protected string $sql = ''; |
| 31 | |
| 32 | /** |
| 33 | * Name of the sql file. |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | protected string $name = 'schema.sql'; |
| 38 | |
| 39 | /** |
| 40 | * Constructor. |
| 41 | * |
| 42 | * @param string $sql Sql code to run. |
| 43 | * @param string $name Name of the sql file. |
| 44 | */ |
| 45 | public function __construct( string $sql, $name = 'schema.sql' ) { |
| 46 | $this->sql = $sql; |
| 47 | $this->name = $name; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Build a RunSql step for a database row, using the portable table-prefix |
| 52 | * placeholder in place of the live database prefix. |
| 53 | * |
| 54 | * Pass the unprefixed table name (e.g. 'woocommerce_shipping_zones'). The |
| 55 | * placeholder is prepended for you, so exported SQL stays portable across |
| 56 | * sites with different table prefixes. On import, ImportRunSql resolves the |
| 57 | * placeholder back to the importing site's prefix. |
| 58 | * |
| 59 | * @param array $row Row data keyed by column name. |
| 60 | * @param string $table Unprefixed table name. |
| 61 | * @param string $type One of insert, insert ignore, replace into. |
| 62 | * @return self |
| 63 | */ |
| 64 | public static function from_table_row( array $row, string $table, string $type = 'replace into' ): self { |
| 65 | return new self( (string) Util::array_to_insert_sql( $row, self::TABLE_PREFIX_PLACEHOLDER . $table, $type ) ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns the name of this step. |
| 70 | * |
| 71 | * @return string The step name. |
| 72 | */ |
| 73 | public static function get_step_name(): string { |
| 74 | return 'runSql'; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns the schema for the JSON representation of this step. |
| 79 | * |
| 80 | * @param int $version The version of the schema to return. |
| 81 | * @return array The schema array. |
| 82 | */ |
| 83 | public static function get_schema( int $version = 1 ): array { |
| 84 | return array( |
| 85 | 'type' => 'object', |
| 86 | 'properties' => array( |
| 87 | 'step' => array( |
| 88 | 'type' => 'string', |
| 89 | 'enum' => array( static::get_step_name() ), |
| 90 | ), |
| 91 | 'sql' => array( |
| 92 | 'type' => 'object', |
| 93 | 'required' => array( 'contents', 'resource', 'name' ), |
| 94 | 'properties' => array( |
| 95 | 'resource' => array( |
| 96 | 'type' => 'string', |
| 97 | 'enum' => array( 'literal' ), |
| 98 | ), |
| 99 | 'name' => array( |
| 100 | 'type' => 'string', |
| 101 | ), |
| 102 | 'contents' => array( |
| 103 | 'type' => 'string', |
| 104 | ), |
| 105 | ), |
| 106 | ), |
| 107 | ), |
| 108 | 'required' => array( 'step', 'sql' ), |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Prepares an associative array for JSON encoding. |
| 114 | * |
| 115 | * @return array Array of data to be encoded as JSON. |
| 116 | */ |
| 117 | public function prepare_json_array(): array { |
| 118 | return array( |
| 119 | 'step' => static::get_step_name(), |
| 120 | 'sql' => array( |
| 121 | 'resource' => 'literal', |
| 122 | 'name' => $this->name, |
| 123 | 'contents' => $this->sql, |
| 124 | ), |
| 125 | ); |
| 126 | } |
| 127 | } |
| 128 |