diff --git a/examples/official-site/content/components/form/examples/06-select.md b/examples/official-site/content/components/form/examples/06-select.md new file mode 100644 index 000000000..5979e3118 --- /dev/null +++ b/examples/official-site/content/components/form/examples/06-select.md @@ -0,0 +1,32 @@ +--- +rows: + - component: form + action: examples/show_variables.sql + - name: Fruit + type: select + empty_option: Select a fruit... + options: '[{"label": "Orange", "value": 0}, {"label": "Apple", "value": 1}, {"label": "Banana", "value": + 3}]' +--- + +This example illustrates the use of the `select` type. +In this select input, the various options are hardcoded, but they could also be loaded from a database table, +[using a function to convert the rows into a json array](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide) like + - `json_group_array()` in SQLite, + - `json_agg()` in Postgres, + - `JSON_ARRAYAGG()` in MySQL, or + - `FOR JSON PATH` in Microsoft SQL Server. + + +In SQLite, the query would look like +```sql +SELECT + 'select' as type, + 'Select a fruit...' as empty_option, + 'cherry' as prefix_icon, + json_group_array(json_object( + 'label', name, + 'value', id + )) as options +FROM fruits +``` diff --git a/examples/official-site/content/components/form/examples/07-multi-select.md b/examples/official-site/content/components/form/examples/07-multi-select.md new file mode 100644 index 000000000..d7e75a4c8 --- /dev/null +++ b/examples/official-site/content/components/form/examples/07-multi-select.md @@ -0,0 +1,82 @@ +--- +rows: + - component: form + action: examples/show_variables.sql + reset: Reset + - name: component + type: select + value: form + options: + - label: Form + value: form + options_source: examples/from_component_options_source.sql?category=component + description: Start typing the name of a component like 'map' or 'form'... +--- + +### Multi-select +You can authorize the user to select multiple options by setting the `multiple` property to `true`. +This creates a more compact (but arguably less user-friendly) alternative to a series of checkboxes. +In this case, you should add square brackets to the name of the field (e.g. `'my_field[]' as name`). +The target page will then receive the value as a JSON array of strings, which you can iterate over using + - the `json_each` function [in SQLite](https://www.sqlite.org/json1.html) and [Postgres](https://www.postgresql.org/docs/9.3/functions-json.html), + - the [`OPENJSON`](https://learn.microsoft.com/fr-fr/sql/t-sql/functions/openjson-transact-sql?view=sql-server-ver16) function in Microsoft SQL Server. + - in MySQL, json manipulation is less straightforward: see [the SQLPage MySQL json example](https://github.com/sqlpage/SQLPage/tree/main/examples/mysql%20json%20handling) + +[More information on how to handle JSON in SQL](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide). + +The target page could then look like this: + +```sql +insert into best_fruits(id) -- INSERT INTO ... SELECT ... runs the SELECT query and inserts the results into the table +select CAST(value AS integer) as id -- all values are transmitted by the browser as strings +from json_each($my_field); -- in SQLite, json_each returns a table with a "value" column for each element in the JSON array +``` + +### Example multiselect generated from a database table + +If you have a table of all possible options (`my_options(id int, label text)`), +and want to generate a multi-select field from it, you have two options: +- if the number of options is not too large, you can use the `options` parameter to return them all as a JSON array in the SQL query +- if the number of options is large (e.g. more than 1000), you can use `options_source` to load options dynamically from a different SQL query as the user types + +#### Embedding all options in the SQL query + +Let's say you have a table that contains the selected options per user (`my_user_options(user_id int, option_id int)`). +You can use a query like this to generate the multi-select field: + +```sql +select 'select' as type, true as multiple, json_group_array(json_object( + 'label', my_options.label, + 'value', my_options.id, + 'selected', my_user_options.option_id is not null +)) as options +from my_options +left join my_user_options + on my_options.id = my_user_options.option_id + and my_user_options.user_id = $user_id +``` + +This will generate a json array of objects, each containing the label, value and selected status of each option. + +#### Loading options dynamically from a different SQL query with `options_source` + +If the `my_options` table has a large number of rows, you can use the `options_source` parameter to load options dynamically from a different SQL query as the user types. + +We'll write a second SQL file, `options_source.sql`, that will receive the user's search string as a parameter named `$search`, + and return a json array of objects, each containing the label and value of each option. + +When both `options` and `options_source` are set, the local `options` are loaded first. Search results from `options_source` are loaded into the same option list; a result with the same `value` updates the existing option. This is useful to set a default preselected value with `options_source`. + +`options_source` may already contain query parameters. SQLPage preserves them and adds or replaces the `search` parameter when loading options. + +##### `options_source.sql` + +```sql +select 'json' as component; + +select id as value, label as label +from my_options +where label like $search || '%'; +``` + +##### `form` diff --git a/examples/official-site/content/components/form/examples/08-radio.md b/examples/official-site/content/components/form/examples/08-radio.md new file mode 100644 index 000000000..be9fe906c --- /dev/null +++ b/examples/official-site/content/components/form/examples/08-radio.md @@ -0,0 +1,41 @@ +--- +rows: + - component: form + method: GET + - name: component + type: radio + value: form + description: Read user input in SQL + label: Form + - name: component + type: radio + value: map + checked: true + description: Display a map based on database data + label: Map + - name: component + type: radio + value: chart + description: Interactive plots of SQL query results + label: Chart +--- + +This example illustrates the use of the `radio` type. +The `name` parameter is used to group the radio buttons together. +The `value` parameter is used to set the value that will be submitted when the user selects the radio button. +The `label` parameter is used to display a friendly name for the radio button. +The `description` parameter is used to display a helper text near the radio button. + +We could also save all the options in a database table, and then run a simple query like + +```sql +SELECT 'form' AS component; +SELECT + 'radio' as type, + 'db' as name, + option_name as label, + option_id as value +FROM my_options; +``` + +In this example, depending on what the user clicks, the page will be reloaded with a the variable `$component` set to the string "form", "map", or "chart". diff --git a/examples/official-site/content/components/form/examples/09-refresh-on-change.md b/examples/official-site/content/components/form/examples/09-refresh-on-change.md new file mode 100644 index 000000000..0de27b1e9 --- /dev/null +++ b/examples/official-site/content/components/form/examples/09-refresh-on-change.md @@ -0,0 +1,24 @@ +--- +rows: + - component: form + auto_submit: true + - name: component + type: select + autocomplete: false + options: + - label: Form + value: form + selected: true + - label: Map + value: map + - label: Chart + value: chart + description: Choose a component to view its documentation. No need to click a button, the page will be + reloaded automatically. + label: Component +--- + +### Dynamically refresh the page when the user changes the form + +The form will be automatically submitted when the user changes any of its fields, and the page will be reloaded with the new value. +The validation button is removed. diff --git a/examples/official-site/content/components/form/examples/10-hidden-input.md b/examples/official-site/content/components/form/examples/10-hidden-input.md new file mode 100644 index 000000000..ba4aced2e --- /dev/null +++ b/examples/official-site/content/components/form/examples/10-hidden-input.md @@ -0,0 +1,22 @@ +--- +rows: + - component: form + validate: Delete + validate_color: red + - type: hidden + name: resource_id + value: '1234' + - name: confirm + label: Please type "sensitive resource" here to confirm the deletion + required: true +--- + +When you want to include some information in the form data, but not display it to the user, you can use a hidden field. + +This can be used to track simple data such as the current user's id, +or to implement more complex flows, such as a multi-step form, +where the user is redirected to a different page after each step. + +This can also be used to implement [CSRF protection](https://en.wikipedia.org/wiki/Cross-site_request_forgery#Synchronizer_token_pattern), +if your website has authenticated users that can perform sensitive actions through simple links. +But note that SQLPage cookies already have the `SameSite=strict` attribute by default, which protects you against CSRF attacks by default in most cases. diff --git a/examples/official-site/content/components/form/examples/11-custom-submit.md b/examples/official-site/content/components/form/examples/11-custom-submit.md new file mode 100644 index 000000000..6bfbd9407 --- /dev/null +++ b/examples/official-site/content/components/form/examples/11-custom-submit.md @@ -0,0 +1,38 @@ +--- +rows: + - component: form + title: User + validate: Create new user + validate_color: green + reset: Clear + - name: first_name + label: First name + placeholder: John + width: 4 + - name: middle_name + label: Middle name + placeholder: Fitzgerald + width: 4 + - name: last_name + label: Last name + placeholder: Doe + width: 4 + - name: email + label: Email + placeholder: john.doe@gmail.com + width: 12 + - name: password + label: Password + type: password + width: 6 + - name: password_confirmation + label: Password confirmation + type: password + width: 6 + - name: terms + label: I accept the terms and conditions + type: checkbox + required: true +--- + +This example illustrates the use of custom validation buttons and half-width fields. diff --git a/examples/official-site/content/components/form/examples/12-file-upload.md b/examples/official-site/content/components/form/examples/12-file-upload.md new file mode 100644 index 000000000..b4ad61b6e --- /dev/null +++ b/examples/official-site/content/components/form/examples/12-file-upload.md @@ -0,0 +1,31 @@ +--- +rows: + - component: form + enctype: multipart/form-data + title: Upload a picture + validate: Upload + action: examples/handle_picture_upload.sql + - name: my_file + type: file + accept: image/png, image/jpeg + label: Picture + description: Upload a small picture + required: true +--- + +## File upload + +You can use the `file` type to allow the user to upload a file. + +The file will be uploaded to the server, and you will be able to access it using the +[`sqlpage.uploaded_file_path`](functions.sql?function=uploaded_file_path) function. + +Here is how you could save the uploaded file to a table in the database: + +```sql +INSERT INTO uploaded_file(name, data) +VALUES ( + sqlpage.uploaded_file_name('my_file'), + sqlpage.read_file_as_data_url(sqlpage.uploaded_file_path('my_file')) +); +``` diff --git a/examples/official-site/content/components/form/examples/13-encoding.md b/examples/official-site/content/components/form/examples/13-encoding.md new file mode 100644 index 000000000..ff0387ace --- /dev/null +++ b/examples/official-site/content/components/form/examples/13-encoding.md @@ -0,0 +1,42 @@ +--- +rows: + - component: form + method: post + enctype: multipart/form-data + title: Submit with different encoding types + validate: Submit with form encoding type + action: examples/handle_enctype.sql + - name: data + type: text + label: Data + required: true + - name: percent_encoded + type: submit + label: Submit as + width: 4 + formaction: examples/handle_enctype.sql + formenctype: application/x-www-form-urlencoded + value: application/x-www-form-urlencoded + - name: multipart_form_data + type: submit + label: Submit as + width: 4 + formaction: examples/handle_enctype.sql + formenctype: multipart/form-data + value: multipart/form-data +--- + +## Form Encoding + +You can specify the way form data should be encoded by setting the `enctype` +top-level property on the form. + +You may also specify `formenctype` on `submit` and `image` type inputs. +This will take precedence over the `enctype` specified on the form and is +useful in the case there are multiple `submit` buttons on the form. +For example, an external site may have specific requirements on encoding type. + +As a rule of thumb, ``multipart/form-data`` is best when fields may contain +copious non-ascii characters or for binary data such as an image or a file. +However, ``application/x-www-form-urlencoded`` creates less overhead when +many short ascii text values are submitted. diff --git a/examples/official-site/content/components/form/examples/14-bulk-insert.md b/examples/official-site/content/components/form/examples/14-bulk-insert.md new file mode 100644 index 000000000..3aabf7323 --- /dev/null +++ b/examples/official-site/content/components/form/examples/14-bulk-insert.md @@ -0,0 +1,61 @@ +--- +rows: + - component: form + title: CSV import + validate: Load data + action: examples/handle_csv_upload.sql + - name: product_data_input + type: file + accept: text/csv + label: Products + description: Upload a CSV with a name, description, and price columns + required: true +--- + +## Bulk data insertion + +You can use the `file` type to allow the user to upload a [CSV](https://en.wikipedia.org/wiki/Comma-separated_values) +file containing data to insert in a table. + +SQLPage can load data from a CSV file and insert it into a database table. +SQLPage re-uses PostgreSQL's [`COPY` syntax](https://www.postgresql.org/docs/current/sql-copy.html) +to specify the format of the CSV file, but makes it work with all supported databases. + +> When connected to a PostgreSQL database, SQLPage will use the native `COPY` statement, +> for super fast and efficient on-database CSV parsing. +> But it will also work transparently with other databases, by +> parsing the CSV locally and emulating the same behavior with simple `INSERT` statements. + +Here is how you could easily copy data from a CSV to a table in the database: + +```sql +copy product(name, description, price) from 'product_data_input' +with (header true, delimiter ',', quote '"'); +``` + +If you want to pre-process the data before inserting it into the final table, +you can use a temporary table to store the data, and then insert it into the final table: + +```sql +-- temporarily store the data in a table with text columns +create temporary table if not exists product_tmp(name text, description text, price text); +delete from product_tmp; + +-- copy the data from the CSV file into the temporary table +copy product_tmp(name, description, price) from 'product_data_input' with (header true); + +-- insert the data into the final table, converting the price column to an integer +insert into product(name, description, price) +select name, description, CAST(price AS integer) from product_tmp +where price is not null and description is not null and length(description) > 10; +``` + +This will load the processed CSV into the product table, provided it has the following structure: + +```csv +name,description,price +"SQLPage","A tool to create websites using SQL",0 +"PostgreSQL","A powerful open-source relational database",0 +"SQLite","A lightweight relational database",0 +"MySQL","A popular open-source relational database",0 +``` diff --git a/examples/official-site/sqlpage/migrations/01_documentation.sql b/examples/official-site/sqlpage/migrations/01_documentation.sql index 051310b7d..277e5620f 100644 --- a/examples/official-site/sqlpage/migrations/01_documentation.sql +++ b/examples/official-site/sqlpage/migrations/01_documentation.sql @@ -319,290 +319,7 @@ When loading the page, the value for `:username` will be `NULL` if no value has {"type": "switch", "label": "Dark theme", "name": "dark", "description": "Enable dark theme"}, {"type": "switch", "label": "A required toggle switch", "name": "my_checkbox", "required": true,"checked": true}, {"type": "switch", "label": "A disabled toggle switch", "name": "my_field", "disabled": true} - ]')), - - ('form', 'This example illustrates the use of the `select` type. -In this select input, the various options are hardcoded, but they could also be loaded from a database table, -[using a function to convert the rows into a json array](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide) like - - `json_group_array()` in SQLite, - - `json_agg()` in Postgres, - - `JSON_ARRAYAGG()` in MySQL, or - - `FOR JSON PATH` in Microsoft SQL Server. - - -In SQLite, the query would look like -```sql -SELECT - ''select'' as type, - ''Select a fruit...'' as empty_option, - ''cherry'' as prefix_icon, - json_group_array(json_object( - ''label'', name, - ''value'', id - )) as options -FROM fruits -``` -', json('[{"component":"form", "action":"examples/show_variables.sql"}, - {"name": "Fruit", "type": "select", - "empty_option": "Select a fruit...", - "options": - "[{\"label\": \"Orange\", \"value\": 0}, {\"label\": \"Apple\", \"value\": 1}, {\"label\": \"Banana\", \"value\": 3}]"} - ]')), - ('form', '### Multi-select -You can authorize the user to select multiple options by setting the `multiple` property to `true`. -This creates a more compact (but arguably less user-friendly) alternative to a series of checkboxes. -In this case, you should add square brackets to the name of the field (e.g. `''my_field[]'' as name`). -The target page will then receive the value as a JSON array of strings, which you can iterate over using - - the `json_each` function [in SQLite](https://www.sqlite.org/json1.html) and [Postgres](https://www.postgresql.org/docs/9.3/functions-json.html), - - the [`OPENJSON`](https://learn.microsoft.com/fr-fr/sql/t-sql/functions/openjson-transact-sql?view=sql-server-ver16) function in Microsoft SQL Server. - - in MySQL, json manipulation is less straightforward: see [the SQLPage MySQL json example](https://github.com/sqlpage/SQLPage/tree/main/examples/mysql%20json%20handling) - -[More information on how to handle JSON in SQL](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide). - -The target page could then look like this: - -```sql -insert into best_fruits(id) -- INSERT INTO ... SELECT ... runs the SELECT query and inserts the results into the table -select CAST(value AS integer) as id -- all values are transmitted by the browser as strings -from json_each($my_field); -- in SQLite, json_each returns a table with a "value" column for each element in the JSON array -``` - -### Example multiselect generated from a database table - -If you have a table of all possible options (`my_options(id int, label text)`), -and want to generate a multi-select field from it, you have two options: -- if the number of options is not too large, you can use the `options` parameter to return them all as a JSON array in the SQL query -- if the number of options is large (e.g. more than 1000), you can use `options_source` to load options dynamically from a different SQL query as the user types - -#### Embedding all options in the SQL query - -Let''s say you have a table that contains the selected options per user (`my_user_options(user_id int, option_id int)`). -You can use a query like this to generate the multi-select field: - -```sql -select ''select'' as type, true as multiple, json_group_array(json_object( - ''label'', my_options.label, - ''value'', my_options.id, - ''selected'', my_user_options.option_id is not null -)) as options -from my_options -left join my_user_options - on my_options.id = my_user_options.option_id - and my_user_options.user_id = $user_id -``` - -This will generate a json array of objects, each containing the label, value and selected status of each option. - -#### Loading options dynamically from a different SQL query with `options_source` - -If the `my_options` table has a large number of rows, you can use the `options_source` parameter to load options dynamically from a different SQL query as the user types. - -We''ll write a second SQL file, `options_source.sql`, that will receive the user''s search string as a parameter named `$search`, - and return a json array of objects, each containing the label and value of each option. - -When both `options` and `options_source` are set, the local `options` are loaded first. Search results from `options_source` are loaded into the same option list; a result with the same `value` updates the existing option. This is useful to set a default preselected value with `options_source`. - -`options_source` may already contain query parameters. SQLPage preserves them and adds or replaces the `search` parameter when loading options. - -##### `options_source.sql` - -```sql -select ''json'' as component; - -select id as value, label as label -from my_options -where label like $search || ''%''; -``` - -##### `form` - -', json('[{"component":"form", "action":"examples/show_variables.sql", "reset": "Reset"}, - {"name": "component", "type": "select", - "value": "form", - "options": [{"label": "Form", "value": "form"}], - "options_source": "examples/from_component_options_source.sql?category=component", - "description": "Start typing the name of a component like ''map'' or ''form''..." - }]')), - ('form', 'This example illustrates the use of the `radio` type. -The `name` parameter is used to group the radio buttons together. -The `value` parameter is used to set the value that will be submitted when the user selects the radio button. -The `label` parameter is used to display a friendly name for the radio button. -The `description` parameter is used to display a helper text near the radio button. - -We could also save all the options in a database table, and then run a simple query like - -```sql -SELECT ''form'' AS component; -SELECT - ''radio'' as type, - ''db'' as name, - option_name as label, - option_id as value -FROM my_options; -``` - -In this example, depending on what the user clicks, the page will be reloaded with a the variable `$component` set to the string "form", "map", or "chart". - - ', json('[{"component":"form", "method": "GET"}, - {"name": "component", "type": "radio", "value": "form", "description": "Read user input in SQL", "label": "Form"}, - {"name": "component", "type": "radio", "value": "map", "checked": true, "description": "Display a map based on database data", "label": "Map"}, - {"name": "component", "type": "radio", "value": "chart", "description": "Interactive plots of SQL query results", "label": "Chart"} - ]')), - ('form', ' -### Dynamically refresh the page when the user changes the form - -The form will be automatically submitted when the user changes any of its fields, and the page will be reloaded with the new value. -The validation button is removed. -', json('[{"component":"form", "auto_submit": true}, - {"name": "component", "type": "select", "autocomplete": false, "options": [ - {"label": "Form", "value": "form", "selected": true}, - {"label": "Map", "value": "map"}, - {"label": "Chart", "value": "chart"} - ], "description": "Choose a component to view its documentation. No need to click a button, the page will be reloaded automatically.", "label": "Component"} - ]')), - ('form', 'When you want to include some information in the form data, but not display it to the user, you can use a hidden field. - -This can be used to track simple data such as the current user''s id, -or to implement more complex flows, such as a multi-step form, -where the user is redirected to a different page after each step. - -This can also be used to implement [CSRF protection](https://en.wikipedia.org/wiki/Cross-site_request_forgery#Synchronizer_token_pattern), -if your website has authenticated users that can perform sensitive actions through simple links. -But note that SQLPage cookies already have the `SameSite=strict` attribute by default, which protects you against CSRF attacks by default in most cases. - -', json('[{"component":"form", "validate": "Delete", "validate_color": "red"}, - {"type": "hidden", "name": "resource_id", "value": "1234"}, - {"name": "confirm", "label": "Please type \"sensitive resource\" here to confirm the deletion", "required": true} - ]')), - ('form', 'This example illustrates the use of custom validation buttons and half-width fields.', - json('[{"component":"form", "title": "User", "validate": "Create new user", "validate_color": "green", "reset": "Clear"}, - {"name": "first_name", "label": "First name", "placeholder": "John", "width": 4}, - {"name": "middle_name", "label": "Middle name", "placeholder": "Fitzgerald", "width": 4}, - {"name": "last_name", "label": "Last name", "placeholder": "Doe", "width": 4}, - {"name": "email", "label": "Email", "placeholder": "john.doe@gmail.com", "width": 12}, - {"name": "password", "label": "Password", "type": "password", "width": 6}, - {"name": "password_confirmation", "label": "Password confirmation", "type": "password", "width": 6}, - {"name": "terms", "label": "I accept the terms and conditions", "type": "checkbox", "required": true} - ]')), - ('form', ' -## File upload - -You can use the `file` type to allow the user to upload a file. - -The file will be uploaded to the server, and you will be able to access it using the -[`sqlpage.uploaded_file_path`](functions.sql?function=uploaded_file_path) function. - -Here is how you could save the uploaded file to a table in the database: - -```sql -INSERT INTO uploaded_file(name, data) -VALUES ( - sqlpage.uploaded_file_name(''my_file''), - sqlpage.read_file_as_data_url(sqlpage.uploaded_file_path(''my_file'')) -); -``` -', - json('[{"component":"form", "enctype": "multipart/form-data", "title": "Upload a picture", "validate": "Upload", "action": "examples/handle_picture_upload.sql"}, - {"name": "my_file", "type": "file", "accept": "image/png, image/jpeg", "label": "Picture", "description": "Upload a small picture", "required": true} - ]')), - ('form', ' -## Form Encoding - -You can specify the way form data should be encoded by setting the `enctype` -top-level property on the form. - -You may also specify `formenctype` on `submit` and `image` type inputs. -This will take precedence over the `enctype` specified on the form and is -useful in the case there are multiple `submit` buttons on the form. -For example, an external site may have specific requirements on encoding type. - -As a rule of thumb, ``multipart/form-data`` is best when fields may contain -copious non-ascii characters or for binary data such as an image or a file. -However, ``application/x-www-form-urlencoded`` creates less overhead when -many short ascii text values are submitted. -', - json('[ - { - "component": "form", - "method": "post", - "enctype": "multipart/form-data", - "title": "Submit with different encoding types", - "validate": "Submit with form encoding type", - "action": "examples/handle_enctype.sql" - }, - {"name": "data", "type": "text", "label": "Data", "required": true}, - { - "name": "percent_encoded", - "type": "submit", - "label": "Submit as", - "width": 4, - "formaction": "examples/handle_enctype.sql", - "formenctype": "application/x-www-form-urlencoded", - "value": "application/x-www-form-urlencoded" - }, - { - "name": "multipart_form_data", - "type": "submit", - "label": "Submit as", - "width": 4, - "formaction": "examples/handle_enctype.sql", - "formenctype": "multipart/form-data", - "value": "multipart/form-data" - } -]')), - ('form', ' -## Bulk data insertion - -You can use the `file` type to allow the user to upload a [CSV](https://en.wikipedia.org/wiki/Comma-separated_values) -file containing data to insert in a table. - -SQLPage can load data from a CSV file and insert it into a database table. -SQLPage re-uses PostgreSQL''s [`COPY` syntax](https://www.postgresql.org/docs/current/sql-copy.html) -to specify the format of the CSV file, but makes it work with all supported databases. - -> When connected to a PostgreSQL database, SQLPage will use the native `COPY` statement, -> for super fast and efficient on-database CSV parsing. -> But it will also work transparently with other databases, by -> parsing the CSV locally and emulating the same behavior with simple `INSERT` statements. - -Here is how you could easily copy data from a CSV to a table in the database: - -```sql -copy product(name, description, price) from ''product_data_input'' -with (header true, delimiter '','', quote ''"''); -``` - -If you want to pre-process the data before inserting it into the final table, -you can use a temporary table to store the data, and then insert it into the final table: - -```sql --- temporarily store the data in a table with text columns -create temporary table if not exists product_tmp(name text, description text, price text); -delete from product_tmp; - --- copy the data from the CSV file into the temporary table -copy product_tmp(name, description, price) from ''product_data_input'' with (header true); - --- insert the data into the final table, converting the price column to an integer -insert into product(name, description, price) -select name, description, CAST(price AS integer) from product_tmp -where price is not null and description is not null and length(description) > 10; -``` - -This will load the processed CSV into the product table, provided it has the following structure: - -```csv -name,description,price -"SQLPage","A tool to create websites using SQL",0 -"PostgreSQL","A powerful open-source relational database",0 -"SQLite","A lightweight relational database",0 -"MySQL","A popular open-source relational database",0 -``` -', - json('[{"component":"form", "title": "CSV import", "validate": "Load data", "action": "examples/handle_csv_upload.sql"}, - {"name": "product_data_input", "type": "file", "accept": "text/csv", "label": "Products", "description": "Upload a CSV with a name, description, and price columns", "required": true} - ]')) -; + ]')); INSERT INTO component(name, icon, description) VALUES ('chart', 'timeline', 'A component that plots data. Line, area, bar, and pie charts are all supported. Each item in the component is a data point in the graph.');