views:

50

answers:

3

Yes, I have already asked this question, but the problem is much more specific. Last time I wanted to ignore the first 2 and the last line of the file. But it struck me that anyone could put as many lines as they wanted before and after the stuff I want to read in, and in that case, the code I've implemented goes to pot.

example file

USE [Shelley's Other Database]  
CREATE TABLE db.exmpcustomers(  
 fname varchar(100) NULL,  
 lname varchar(100) NULL,  
 dateofbirth date NULL,  
 housenumber int NULL,  
 streetname varchar(100) NULL  
) ON [PRIMARY]

What I need to do is work out where my data starts (being fname - but remember it could be called anything, so I can't use a regex looking for 'f'), where it ends (the last NULL), then read in that. Or, you know, read in the file and select that data; whatever.
I thought about using regex looking for the brackets, but wouldn't it pick up the brackets in my datatypes as well?

Any ideas? Anything will help, I'm totally stuck.

EDIT

Oh, and, the original file actually has spaces/tabs before the data (indentation), but I'm not sure if that changes anything.

A: 

What you want to achieve is not really clear but I think you should open a stream to that file, loop until you find the first line of interest (optionally using a RegEx to identify candidates), read the lines until you meet your 'null condition'

vc 74
+2  A: 

I would just regex greedy-match data between the brackets. As long as no ) follows ON, and no ( precedes CREATE, the parentheses in the content don't really matter.

If you don't know SQL scripts too well, is it a good idea exposing control to other people? Why not create a simple DSL that allows the same benefits? e.g.:

DATABASE databasenamegoeshere
TABLE tablenamegoeshere
FIELD name type default
FIELD name type default
FIELD name type default

And then translate that into corresponding SQL code? Seems a bit more secure.. Or does it have to be executable SQL?

Jeriko
+1 Although not ideal, that is the route I would probably go
w69rdy
I'm weary to do that as I don't fully understand SQL scripts, and I wouldn't know if all SQL scripts (of this type) have to be wrote like that. Although I'm assuming they do. Like, the data needing to be in brackets..
New Start
Yea it's not ideal - I don't have enough experience with SQL to be certain that there aren't potentially brackets elsewhere. You could always extract every piece of data in parentheses, and then loop through each looking for the one that contains `NULL`, or some other guaranteed keyword? Slightly more robust, but other than that you should probably look into some SQL interpreter
Jeriko
@New Start: This idea is not as bad actually, because (supposed that you can), defining your own grammer allows you to make sure it fits into your regular expression, while you can't assert that a DDL will fit into your regexp because DDL/SQL isn't a regular language, i.e. there is no regular expression that can handle every single possible case... (+1)
chiccodoro
A: 

Hi New Start, the regex I provided in your other question would perfectly match your data. Your question is actually really a duplicate of the other one.

chiccodoro
@chiccodoro: Yep, I see that now. Silly me. Got a bit of a problem now though, think you could help me out?
New Start
@New Start: Sure, what problem do you mean now? If it's a new one, just create a new question. If it's still about this topic I'd propose to go back to http://stackoverflow.com/questions/3718112/is-there-any-way-to-ignore-reading-in-certain-lines-in-a-text-file and edit your question there/leave a comment
chiccodoro
@chiccodoro: Posted.
New Start