Loading ...
Sorry, an error occurred while loading the content.

Expression help over multiple lines and variables

Expand Messages
  • David Fishburn
    Vim 6.2.71 WinXP I have a file with the following in it: ALTER TABLE DBA.employee ADD PRIMARY KEY (emp_id); Or CREATE TABLE DBA . employee ( emp_id
    Message 1 of 5 , Sep 3, 2003
    • 0 Attachment
      Vim 6.2.71 WinXP

      I have a file with the following in it:

      ALTER TABLE DBA.employee
      ADD PRIMARY KEY (emp_id);

      Or

      CREATE TABLE "DBA"."employee"
      (
      "emp_id" integer NOT NULL ,
      "manager_id" integer NULL ,
      "emp_fname" char(20) NOT NULL ,
      "emp_lname" char(20) NOT NULL ,
      "dept_id" integer NOT NULL ,
      "street" char(40) NOT NULL ,
      "city" char(20) NOT NULL ,
      "state" char(16) NULL ,
      "zip_code" char(10) NULL ,
      "phone" char(10) NULL ,
      "status" char(2) NULL ,
      "ss_number" char(11) NULL ,
      "salary" numeric(20,3) NOT NULL ,
      "start_date" date NOT NULL ,
      "termination_date" date NULL ,
      "birth_date" date NULL ,
      "bene_health_ins" char(2) NULL ,
      "bene_life_ins" char(2) NULL ,
      "bene_day_care" char(2) NULL ,
      "sex" char(2) NULL ,
      PRIMARY KEY (emp_id)
      )
      go


      I want to:
      1. Find either an ALTER or CREATE statement
      2. For the employee table (excluding "DBA"." or DBA. from before it)
      3. PRIMARY KEY could be on new lines
      4. BEFORE the command terminator "\ngo"

      I am having a hard time.
      This is what I have tried:
      \(alter\|create\) table.*employee[\n\S\s]*go\s*$

      \(alter\|create\) table.*employee\_S*go\s*$

      \(alter\|create\) table.*employee.*primary.*go\s*$

      Besides these not working, they also do not take in account that a
      newline character must preceed the sql terminator "go".
      If this makes a difference, the sql terminator can be configured by the
      user, here are some examples:
      let g:sqlu_terminator=';'
      let g:sqlu_terminator="\ngo"

      So I was concatenating this command in my plugin.

      I might as well ask for the world while I am at it.
      I would like the cursor to be positioned at the beginning of "primary"
      or even better at the end of "primary key (".

      TIA.
      Dave
    • Yakov Lerner
      ... There are two ways. 1) / (alter |create ) table.*employee/e;/primary key/ ^^^ ... 2) using _.* instead of .* But with _.* you ll need to figure our
      Message 2 of 5 , Sep 4, 2003
      • 0 Attachment
        David Fishburn wrote:
        > Vim 6.2.71 WinXP
        >
        > I have a file with the following in it:
        >
        > ALTER TABLE DBA.employee
        > ADD PRIMARY KEY (emp_id);
        >
        > Or
        >
        > CREATE TABLE "DBA"."employee"
        > (
        > "emp_id" integer NOT NULL ,
        > "manager_id" integer NULL ,
        > "emp_fname" char(20) NOT NULL ,
        > "emp_lname" char(20) NOT NULL ,
        > "dept_id" integer NOT NULL ,
        > "street" char(40) NOT NULL ,
        > "city" char(20) NOT NULL ,
        > "state" char(16) NULL ,
        > "zip_code" char(10) NULL ,
        > "phone" char(10) NULL ,
        > "status" char(2) NULL ,
        > "ss_number" char(11) NULL ,
        > "salary" numeric(20,3) NOT NULL ,
        > "start_date" date NOT NULL ,
        > "termination_date" date NULL ,
        > "birth_date" date NULL ,
        > "bene_health_ins" char(2) NULL ,
        > "bene_life_ins" char(2) NULL ,
        > "bene_day_care" char(2) NULL ,
        > "sex" char(2) NULL ,
        > PRIMARY KEY (emp_id)
        > )
        > go
        >
        >
        > I want to:
        > 1. Find either an ALTER or CREATE statement
        > 2. For the employee table (excluding "DBA"." or DBA. from before it)
        > 3. PRIMARY KEY could be on new lines
        > 4. BEFORE the command terminator "\ngo"
        >
        > I am having a hard time.
        > This is what I have tried:
        > \(alter\|create\) table.*employee[\n\S\s]*go\s*$
        >
        > \(alter\|create\) table.*employee\_S*go\s*$
        >
        > \(alter\|create\) table.*employee.*primary.*go\s*$
        >
        > Besides these not working, they also do not take in account that a
        > newline character must preceed the sql terminator "go".
        > If this makes a difference, the sql terminator can be configured by the
        > user, here are some examples:
        > let g:sqlu_terminator=';'
        > let g:sqlu_terminator="\ngo"
        >
        > So I was concatenating this command in my plugin.
        >
        > I might as well ask for the world while I am at it.
        > I would like the cursor to be positioned at the beginning of "primary"
        > or even better at the end of "primary key (".
        >
        > TIA.
        > Dave
        >
        >
        >
        There are two ways.
        1)
        /\(alter\|create\) table.*employee/e;/primary key/
        ^^^
        references:
        :help search-offset
        :help //;

        2) using \_.* instead of .*
        But with \_.* you'll need to figure our where and how
        to stop it. Otherwise it'll match the wrong 'primary key'

        ref: :help /\_.


        Yakov
      • Gumnos (Tim Chase)
        ... Generally, when using the _. operator, it s usally handy to use the non-greedy {-} instead of the asterisk, as it will stop at the first match that can
        Message 3 of 5 , Sep 4, 2003
        • 0 Attachment
          > 2) using \_.* instead of .*
          > But with \_.* you'll need to figure our where and how
          > to stop it. Otherwise it'll match the wrong 'primary key'

          Generally, when using the \_. operator, it's usally handy to use the
          non-greedy \{-} instead of the asterisk, as it will stop at the first match
          that can complete the regex, rather than sucking up any further ones in the
          file.

          Something like the following <caveat>untested</caveat> mapping may do the
          trick:

          nmap <f8>
          /\(alter\|create\)\_s\{-1,}table\_s\{-1,}\_.\{-}primary\_.\{-}\(;\|\_^go\_$\
          )\c<cr>/primary\_s*key\c/e<cr>

          This may require a little tweaking to get the right number of escaping
          backslashes for the nmap side of things, but those two queries:
          /\(alter\|create\)\_s\{-1,}table\_s\{-1,}\_.\{-}primary\_.\{-}\(;\|\_^go\_$\
          )\c
          /primary\_s*key\c/e

          triggered in order should pop you right to the desired location :)

          -tim
        • Tim Chase
          ... After getting in to work this morning, I got around to ironing out the tricks of shoving it into a mapping: nmap
          Message 4 of 5 , Sep 4, 2003
          • 0 Attachment
            > I might as well ask for the world while I am at it.
            > I would like the cursor to be positioned at the beginning of "primary"
            > or even better at the end of "primary key (".

            After getting in to work this morning, I got around to ironing out the
            tricks of shoving it into a mapping:

            nmap <f8>
            /\(alter\\|create\)\_s\{-1,}table\_s\{-1,}\_.\{-}primary\_.\{-}\(;\\|\_^go
            \_$\)\c<cr>/primary\_s*key\c/e+1<cr>


            should do the trick. If you want it after the paren following Primary
            Key, just add

            \_s*(

            before the "\c/e+1" at the end

            -tim
          • David Fishburn
            I appreciate the help. I ended up with the following: Regular expression breakdown Ingore case and spaces line begins with either create or alter
            Message 5 of 5 , Sep 4, 2003
            • 0 Attachment
              I appreciate the help.
              I ended up with the following:

              " Regular expression breakdown
              " Ingore case and spaces
              " line begins with either create or alter
              " followed by table and table_name (on the same line)
              " Could be other lines inbetween these
              " Look for the primary key clause (must be one)
              " Start the match after the open paran
              " The column list could span multiple lines
              " End the match on the closing paran
              " Could be other lines inbetween these
              " Remove any newline characters for the command
              " terminator (ie "\ngo" )
              " Besides a CREATE TABLE statement, this expression
              " should find statements like:
              " ALTER TABLE SSD.D_CENTR_ALLOWABLE_DAYS
              " ADD PRIMARY KEY (CUST_NBR, CAL_NBR, GRP_NBR,
              " EVENT_NBR, ALLOW_REVIS_NBR, ROW_REVIS_NBR);

              let srch_table = '\c^[ \t]*' .
              \ '\(create\|alter\)' .
              \ '.*table.*' .
              \ table_name .
              \ '\_.\{-}' .
              \ '\%(primary key\)\{-1,}' .
              \ '\s*(\zs' .
              \ '\_.\{-}' .
              \ '\ze)' .
              \ '\_.\{-}' .
              \ substitute( g:sqlutil_cmd_terminator,
              \ "[\n]", '', "g" )

              if( search( srch_table, "W" ) ) > 0
              .....

              -----Original Message-----
              From: Tim Chase [mailto:gumnos@...]
              Sent: Thursday, September 04, 2003 8:51 AM
              To: David Fishburn; vim@...
              Subject: Re: Expression help over multiple lines and variables


              > I might as well ask for the world while I am at it.
              > I would like the cursor to be positioned at the beginning of "primary"

              > or even better at the end of "primary key (".

              After getting in to work this morning, I got around to ironing out the
              tricks of shoving it into a mapping:

              nmap <f8>
              /\(alter\\|create\)\_s\{-1,}table\_s\{-1,}\_.\{-}primary\_.\{-}\(;\\|\_^
              go
              \_$\)\c<cr>/primary\_s*key\c/e+1<cr>


              should do the trick. If you want it after the paren following Primary
              Key, just add

              \_s*(

              before the "\c/e+1" at the end

              -tim
            Your message has been successfully submitted and would be delivered to recipients shortly.