Skip to main content
edited tags, grammar and markup
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Parsing YamlYAML to create dynamic queries in Shellshell

I am very new to scripting, and I have been tasked with figuring out a way to script a shell script that will create sqlSQL queries based on a yaml that is provided YAML document. I was able tocould trace down the parser to yqyq parser to useuse; however, I am stuck aton how to use individual values from the yamlYAML children nodes. Sample yamlYAML that iI need to parse is:

config:
  - system1:
      database_name: database1
      port: '1234'
      table:
        - name: table1
          values: 'table_id, value1, 30, randomValue'
        - name: table2
          values: 'table_id, value1, randomValue, randomValue, value2'
        - name: table2
          values: 'table_id, value2, randomValue, randomValue'
  - system2:
      database_name: database2
      port: '12345'
      table:
        - name: table4
          values: 'table_id, value3, 30, randomValue'
        - name: table5
          values: 'table_id, randomValue, randomValue, value4'
        - name: table6
          values: 'table_id, value3, value4'

config:
  - system1:
      database_name: database1
      port: '1234'
      table:
        - name: table1
          values: 'table_id, value1, 30, randomValue'
        - name: table2
          values: 'table_id, value1, randomValue, randomValue, value2'
        - name: table2
          values: 'table_id, value2, randomValue, randomValue'
  - system2:
      database_name: database2
      port: '12345'
      table:
        - name: table4
          values: 'table_id, value3, 30, randomValue'
        - name: table5
          values: 'table_id, randomValue, randomValue, value4'
        - name: table6
          values: 'table_id, value3, value4'

The script That ithat I am trying to write currently is nothing more than selectSELECT statements, but it will evolve as i start more on itlater:

//# This will later be used to create insert/update queries based on the values so 
//# will be needing a handle for that too.
psql -h localhost -p $PORT -d $DATABaSE$DATABASE << EOF
select * from $TABLE LIMIT 10;
EOF

If possible, without sounding needy i, I would hope to see suggestions on how iI can use yqyq library, given that it supports a bunch of operations that iI will need to do to this script once the sqlSQL part is completed.

Parsing Yaml to create dynamic queries in Shell

I am very new to scripting and I have been tasked with figuring out a way to script a shell script that will create sql queries based on a yaml that is provided. I was able to trace down the parser to yq parser to use however I am stuck at how to use individual values from the yaml children nodes. Sample yaml that i need to parse is:

config:
  - system1:
      database_name: database1
      port: '1234'
      table:
        - name: table1
          values: 'table_id, value1, 30, randomValue'
        - name: table2
          values: 'table_id, value1, randomValue, randomValue, value2'
        - name: table2
          values: 'table_id, value2, randomValue, randomValue'
  - system2:
      database_name: database2
      port: '12345'
      table:
        - name: table4
          values: 'table_id, value3, 30, randomValue'
        - name: table5
          values: 'table_id, randomValue, randomValue, value4'
        - name: table6
          values: 'table_id, value3, value4'

The script That i am trying to write currently is nothing more than select statements, but it will evolve as i start more on it

// This will later be used to create insert/update queries based on the values so 
// will be needing a handle for that too.
psql -h localhost -p $PORT -d $DATABaSE << EOF
select * from $TABLE LIMIT 10;
EOF

If possible without sounding needy i would hope to see suggestions on how i can use yq library, given that it supports a bunch of operations that i will need to do to this script once the sql part is completed.

Parsing YAML to create dynamic queries in shell

I am very new to scripting, and I have been tasked with figuring out a way to script a shell script that will create SQL queries based on a provided YAML document. I could trace down the yq parser to use; however, I am stuck on how to use individual values from the YAML children nodes. Sample YAML that I need to parse is:

config:
  - system1:
      database_name: database1
      port: '1234'
      table:
        - name: table1
          values: 'table_id, value1, 30, randomValue'
        - name: table2
          values: 'table_id, value1, randomValue, randomValue, value2'
        - name: table2
          values: 'table_id, value2, randomValue, randomValue'
  - system2:
      database_name: database2
      port: '12345'
      table:
        - name: table4
          values: 'table_id, value3, 30, randomValue'
        - name: table5
          values: 'table_id, randomValue, randomValue, value4'
        - name: table6
          values: 'table_id, value3, value4'

The script that I am trying to write currently is nothing more than SELECT statements, but it will evolve later:

# This will later be used to create insert/update queries based on the values so 
# will be needing a handle for that too.
psql -h localhost -p $PORT -d $DATABASE << EOF
select * from $TABLE LIMIT 10;
EOF

If possible, without sounding needy, I would hope to see suggestions on how I can use yq library, given that it supports a bunch of operations that I will need to do to this script once the SQL part is completed.

Source Link
Akash
  • 111
  • 3

Parsing Yaml to create dynamic queries in Shell

I am very new to scripting and I have been tasked with figuring out a way to script a shell script that will create sql queries based on a yaml that is provided. I was able to trace down the parser to yq parser to use however I am stuck at how to use individual values from the yaml children nodes. Sample yaml that i need to parse is:

config:
  - system1:
      database_name: database1
      port: '1234'
      table:
        - name: table1
          values: 'table_id, value1, 30, randomValue'
        - name: table2
          values: 'table_id, value1, randomValue, randomValue, value2'
        - name: table2
          values: 'table_id, value2, randomValue, randomValue'
  - system2:
      database_name: database2
      port: '12345'
      table:
        - name: table4
          values: 'table_id, value3, 30, randomValue'
        - name: table5
          values: 'table_id, randomValue, randomValue, value4'
        - name: table6
          values: 'table_id, value3, value4'

The script That i am trying to write currently is nothing more than select statements, but it will evolve as i start more on it

// This will later be used to create insert/update queries based on the values so 
// will be needing a handle for that too.
psql -h localhost -p $PORT -d $DATABaSE << EOF
select * from $TABLE LIMIT 10;
EOF

If possible without sounding needy i would hope to see suggestions on how i can use yq library, given that it supports a bunch of operations that i will need to do to this script once the sql part is completed.

I apologise if the question is too stupid, it would be my first time on shell scripting.

I am using Ubuntu 18.4 Distribution if that's relevant.