xml to sql server issue - sql-server

I have one xml code which i would use for insert data to sql server table. I want to parse this xml and insert to sql server table.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red_50">#FFEBEE</color>
<color name="red_100">#FFCDD2</color>
<color name="red_200">#EF9A9A</color>
<color name="red_300">#E57373</color>
<color name="red_400">#EF5350</color>
<color name="red_500">#F44336</color>
<color name="red_600">#E53935</color>
<color name="red_700">#D32F2F</color>
<color name="red_800">#C62828</color>
<color name="red_900">#B71C1C</color>
<color name="red_A100">#FF8A80</color>
<color name="red_A200">#FF5252</color>
<color name="red_A400">#FF1744</color>
<color name="red_A700">#D50000</color>
</resources>
Example:
For
<color name="orange_50">#FFF3E0</color>
The main problem is to get :
name:orange_50, hue:orange, value:50, hexcolor:#FFF3E0 and save all data to table.
This is what I have tried :
DECLARE #XML XML = '<resources>
<color name="red_600">#E53935</color>
<color name="red_700">#D32F2F</color>
<color name="red_800">#C62828</color>
<color name="red_900">#B71C1C</color>
<color name="red_A100">#FF8A80</color>
<color name="red_A200">#FF5252</color>
<color name="red_A400">#FF1744</color>
<color name="red_A700">#D50000</color>
</resources>
'
SELECT NAME = Events.value('#name', 'varchar(25)')
FROM #XML.nodes('ressources/color') AS XTbl(Events)
i try to get color name and work fine but for get hexadecimal i don't know how do. And i want to extract in red_800 for example, red only for hue column and 800 for value column.
Thanks

This is one possible way :
SELECT
NAME = Events.value('#name', 'varchar(25)')
,HUE = SUBSTRING(Events.value('#name', 'varchar(25)'), 1, CHARINDEX('_', Events.value('#name', 'varchar(25)'))-1)
,VALUE = SUBSTRING(Events.value('#name', 'varchar(25)'), CHARINDEX('_', Events.value('#name', 'varchar(25)')) + 1, LEN(Events.value('#name', 'varchar(25)')))
,HEXCOLOR = Events.value('.', 'varchar(25)')
FROM #XML.nodes('/resources/color') AS XTbl(Events)
Sqlfiddle Demo
output :
| NAME | HUE | VALUE | HEXCOLOR |
|----------|-----|-------|----------|
| red_600 | red | 600 | #E53935 |
| red_700 | red | 700 | #D32F2F |
| red_800 | red | 800 | #C62828 |
| red_900 | red | 900 | #B71C1C |
| red_A100 | red | A100 | #FF8A80 |
| red_A200 | red | A200 | #FF5252 |
| red_A400 | red | A400 | #FF1744 |
| red_A700 | red | A700 | #D50000 |
Related discussion : T-SQL get substring after specific character?*
*) The same trick used here to get part of the name attribute value that is located after _ for VALUE, and before _ for HUE columns

Related

CannotPlanException after "CROSS JOIN UNNEST"

When I create a VIEW as a result of "CROSS JOIN UNNEST" and then use the condition in the WHERE clause of the VIEW, it throws an exception "org.apache.calcite.plan.RelOptPlanner$CannotPlanException".
Why am I getting this exception and how should I handle it the right way?
The following is the test code in which an error occurs.
it should "filter with object_key" in {
tEnv.executeSql(
s"""CREATE TABLE s3_put_event (
| Records ARRAY<
| ROW<
| s3 ROW<
| bucket ROW<name STRING>,
| object ROW<key STRING, size BIGINT>
| >
| >
| >
|) WITH (
| 'connector' = 'datagen',
| 'number-of-rows' = '3',
| 'rows-per-second' = '1',
| 'fields.Records.element.s3.bucket.name.length' = '8',
| 'fields.Records.element.s3.object.key.length' = '15',
| 'fields.Records.element.s3.object.size.min' = '1',
| 'fields.Records.element.s3.object.size.max' = '1000'
|)
|""".stripMargin
)
tEnv.executeSql(
s"""CREATE TEMPORARY VIEW s3_objects AS
|SELECT object_key, bucket_name
|FROM (
| SELECT
| r.s3.bucket.name AS bucket_name,
| r.s3.object.key AS object_key,
| r.s3.object.size AS object_size
| FROM s3_put_event
| CROSS JOIN UNNEST(s3_put_event.Records) AS r(s3)
|) rs
|WHERE object_size > 0
|""".stripMargin
)
tEnv.executeSql(
s"""CREATE TEMPORARY VIEW filtered_s3_objects AS
|SELECT bucket_name, object_key
|FROM s3_objects
|WHERE object_key > ''
|""".stripMargin)
val result = tEnv.sqlQuery("SELECT * FROM filtered_s3_objects")
tEnv.toChangelogStream(result).print()
env.execute()
}
If I remove the condition object_key > '' in the "filtered_s3_objects" VIEW, and do it in the "s3_objects" VIEW, no exception is thrown.
However, my actual query is complicated, so it is not easy to move the condition of the WHERE clause like this. It's hard to use especially if I need to separate the output stream.
I'm not sure that you can use a CROSS JOIN UNNEST on an array with a nested hierarchy (given that you have a ROW in your ARRAY). Either way, could you file a Jira ticket for this? https://issues.apache.org/jira/projects/FLINK/issues/

Is there a documented list of Snowflake query types?

I am working with the view SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY. It would be extremely helpful to have an exhaustive list of query types that might appear in the column QUERY_TYPE, with the type of commands that generate them. For example, does a PUT command generate a PUT query type? Or is it something like "LOAD"?
If anyone knows where such a list can be found, please post a link. Snowflake's documentation of the view does not provide any list.
Thanks all who have answered so far. Since the consensus is that no such list exists, here is a merge of the entries provided so far with the values found in my own database. Please keep posting additional answers if your DB contains entries not found below. This way, sooner or later, we will have a fairly complete list:
QUERY_TYPE
CREATE_USER
REVOKE
DROP_CONSTRAINT
RENAME_SCHEMA
UPDATE
CREATE_VIEW
CREATE_TASK
RENAME_TABLE
INSERT
ALTER_TABLE_ADD_COLUMN
RENAME_COLUMN
MERGE
BEGIN_TRANSACTION
ALTER_VIEW_MODIFY_SECURITY
GRANT
ALTER_SESSION
DELETE
DROP_ROLE
DESCRIBE
UNKNOWN
TRUNCATE_TABLE
DROP
SHOW
ALTER_WAREHOUSE_SUSPEND
GET_FILES
UNLOAD
CREATE_NETWORK_POLICY
ALTER_TABLE_DROP_COLUMN
CREATE
REMOVE_FILES
ALTER
ALTER_USER
PUT_FILES
COPY
ALTER_ACCOUNT
DROP_TASK
CREATE_CONSTRAINT
DESCRIBE_QUERY
SELECT
RENAME_USER
COMMIT
RENAME_VIEW
USE
CREATE_TABLE
ALTER_NETWORK_POLICY
CREATE_ROLE
ALTER_TABLE_MODIFY_COLUMN
SET
ALTER_USER_ABORT_ALL_JOBS
ROLLBACK
LIST_FILES
UNSET
CREATE_TABLE_AS_SELECT
DROP_USER
ALTER_WAREHOUSE_RESUME
QUERY_TYPE
ALTER_PIPE
ALTER_ROLE
ALTER_TABLE
ALTER_TABLE_DROP_CLUSTERING_KEY
ALTER_USER_RESET_PASSWORD
CREATE_EXTERNAL_TABLE
CREATE_MASKING_POLICY
CREATE_SEQUENCE
CREATE_STREAM
DROP_STREAM
RENAME_DATABASE
RENAME_FILE_FORMAT
RENAME_ROLE
RENAME_WAREHOUSE
RESTORE
By the looks of it there is no complete list of query types that show up in this table. Best I can do is give you a list from my own database, which still doesn't contain things like alter role etc. To answer your other question a PUT command is actually PUT_FILES by the looks of it:
select distinct query_type from SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY;
+-------------------------+
|QUERY_TYPE |
+-------------------------+
|ALTER |
|ALTER_SESSION |
|ALTER_TABLE_ADD_COLUMN |
|ALTER_TABLE_DROP_COLUMN |
|ALTER_TABLE_MODIFY_COLUMN|
|ALTER_USER |
|ALTER_WAREHOUSE_RESUME |
|ALTER_WAREHOUSE_SUSPEND |
|BEGIN_TRANSACTION |
|COMMIT |
|COPY |
|CREATE |
|CREATE_CONSTRAINT |
|CREATE_EXTERNAL_TABLE |
|CREATE_MASKING_POLICY |
|CREATE_ROLE |
|CREATE_SEQUENCE |
|CREATE_STREAM |
|CREATE_TABLE |
|CREATE_TABLE_AS_SELECT |
|CREATE_USER |
|CREATE_VIEW |
|DELETE |
|DESCRIBE |
|DESCRIBE_QUERY |
|DROP |
|DROP_CONSTRAINT |
|DROP_STREAM |
|DROP_USER |
|GET_FILES |
|GRANT |
|INSERT |
|LIST_FILES |
|MERGE |
|PUT_FILES |
|REMOVE_FILES |
|RENAME_COLUMN |
|RENAME_DATABASE |
|RENAME_TABLE |
|RESTORE |
|REVOKE |
|ROLLBACK |
|SELECT |
|SET |
|SHOW |
|TRUNCATE_TABLE |
|UNKNOWN |
|UNLOAD |
|UPDATE |
|USE |
+-------------------------+
Added ours ... 16 extra's ... pass it on :-)
QUERY_TYPE
ALTER
ALTER_ACCOUNT
ALTER_PIPE
ALTER_ROLE
ALTER_SESSION
ALTER_TABLE
ALTER_TABLE_ADD_COLUMN
ALTER_TABLE_DROP_CLUSTERING_KEY
ALTER_TABLE_DROP_COLUMN
ALTER_TABLE_MODIFY_COLUMN
ALTER_USER
ALTER_USER_ABORT_ALL_JOBS
ALTER_USER_RESET_PASSWORD
ALTER_WAREHOUSE_RESUME
ALTER_WAREHOUSE_SUSPEND
BEGIN_TRANSACTION
COMMIT
COPY
CREATE
CREATE_CONSTRAINT
CREATE_EXTERNAL_TABLE
CREATE_MASKING_POLICY
CREATE_NETWORK_POLICY
CREATE_ROLE
CREATE_SEQUENCE
CREATE_STREAM
CREATE_TABLE
CREATE_TABLE_AS_SELECT
CREATE_TASK
CREATE_USER
CREATE_VIEW
DELETE
DESCRIBE
DESCRIBE_QUERY
DROP
DROP_CONSTRAINT
DROP_ROLE
DROP_STREAM
DROP_TASK
DROP_USER
GET_FILES
GRANT
INSERT
LIST_FILES
MERGE
PUT_FILES
REMOVE_FILES
RENAME_COLUMN
RENAME_DATABASE
RENAME_FILE_FORMAT
RENAME_ROLE
RENAME_SCHEMA
RENAME_TABLE
RENAME_USER
RENAME_VIEW
RENAME_WAREHOUSE
RESTORE
REVOKE
ROLLBACK
SELECT
SET
SHOW
TRUNCATE_TABLE
UNKNOWN
UNLOAD
UNSET
UPDATE
USE
Here are some additional ones:
ALTER_AUTO_RECLUSTER
ALTER_SET_TAG
ALTER_TABLE_MODIFY_CONSTRAINT
ALTER_UNSET_TAG
CALL
DROP_SESSION_POLICY
RECLUSTER

How to get access time in inotify

In inotify I see the following opcodes:
const agnosticEvents = unix.IN_MOVED_TO | unix.IN_MOVED_FROM |
unix.IN_CREATE | unix.IN_ATTRIB | unix.IN_MODIFY |
unix.IN_MOVE_SELF | unix.IN_DELETE | unix.IN_DELETE_SELF
Is there a way to get when a file was opened/accessed? For example, if I typed in cat <file> or open <file> or vim <file>. How would I get that event to flow-through, or is it not possible?

modernWPFUI + powershell = System.Windows.Markup.StaticResourceHolder

I want to use the ModernWPFUI Theme on Powershell.
Generally, i use only XAML in powershell for a better GUI, so i really don't know too much
Now, this is my test code:
XAML(test.xaml):
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ui="http://schemas.modernwpf.com/2019"
x:Name="Window"
Title="Test" Height="68.293" Width="462.195">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/ModernWpf;component/ControlsResources.xaml" />
<ResourceDictionary Source="pack://application:,,,/ModernWpf;component/ThemeResources/Light.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
</Window>
PS1(run.ps1):
[System.Reflection.Assembly]::LoadWithPartialName('presentationframework') | out-null
[System.Reflection.Assembly]::LoadFrom('.\assembly\ModernWpf.dll') | out-null
[System.Reflection.Assembly]::LoadFrom('.\assembly\ModernWpf.Controls.dll') | out-null
[System.Reflection.Assembly]::LoadFrom('.\assembly\System.ValueTuple.dll') | out-null
function LoadXml ($global:filename)
{
$XamlLoader=(New-Object System.Xml.XmlDocument)
$XamlLoader.Load($filename)
return $XamlLoader
}
$XamlMainWindow=LoadXml("test.xaml")
$Reader=(New-Object System.Xml.XmlNodeReader $XamlMainWindow)
$Form=[Windows.Markup.XamlReader]::Load($Reader)
$Global:Current_Folder =(get-location).path
$Form.ShowDialog()
result:
MethodInvocationException: (path)\run.ps1: 15: 1
Line |
15 | $ Form = [Windows.Markup.XamlReader] :: Load ($ Reader)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Exception calling "Load" with arguments "1": "An exception was thrown when specifying a value on 'System.Windows.Markup.StaticResourceHolder'."
Invalid operation: (path)\run.ps1: 22: 1
Line |
22 | $ Form.ShowDialog ()
| ~~~~~~~~~~~~~~~~~~
| A method cannot be called on a null-valued expression.
I tried to edit in or add it "clr-namespace:ModernWpf;assembly=ModernWpf" with the same result.

DataGrid Text auto scroll

In the DataGrid, there is a CheckBoxColumn and a TextColumn, that displays file paths:
| | |
| x |C:\docs\etc\somefile.txt |
| |C:\programs\misc\files\2.0\oth| <- cut off, too long
| x | |
I would prefer if long strings would scroll to the end, so the user can see the filename:
| | |
| x |..misc\files\2.0\otherfile.zip|
| | |
Is there a way to do this? Thanks
Another solution could be to use a textblock in the column template. Set texttrimming to ellipsis and put the long text in the tooltip property. http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.texttrimming.aspx
If you really want the ellipsis to the left like in your example, you may need to do some code behind measuring, see Length of string that will fit in a specific width

Resources