Re: Can I use ESQL/C datetime type to define non-host variables / parameters (oops)
Posted in 2005
Topics: Connectivity: ESQL/C, 4GL & Embedded SQL, Data Types & Schema Design
I am still anxious on that. I do the best to make what I mean clear, the questions are: 1. Can I use real esql/c data types (no typedef) in function parameters. Do I need to put the parameters with esql/c datatypes between EXEC SQL BEGIN DECLARE SECTION; and EXEC SQL END DECLARE SECTION; . Because host variables should be put inside the long esql `BEGIN...END` declaration pair, and sometimes I don't want to use these parameters as host variables. So can I skip the esql `BEGIN...END` declaration pair. 2. Can I pass values with real esql/c data types directly when call a function. 3. Similar to item 1 above, can I declare other variables other than function parameters with real esql/c data types outside the EXEC SQL BEGIN DECLARE SECTION; and EXEC SQL END DECLARE SECTION; , as I don't use them as host variables. 1 /* Filename: test.h */ 2 EXEC SQL include sqltypes; 3 EXEC SQL include datetime; 4 EXEC SQL include decimal; 5 6 int test(long n_SubscriptionId, 7 long n_ProductId, 8 datetime *p_StartDate, //esqlc type, non host variable 10 boolean *p_Status, //esqlc type, non host variable 12 EXEC SQL BEGIN DECLARE SECTION; 13 PARAMETER datetime *p_StartDate, 14 PARAMETER boolean *p_Status 15 EXEC SQL END DECLARE SECTION; 16 ); 1 /* Filename: test.ec */ 2 EXEC SQL include sqltypes; 3 EXEC SQL include datetime; 4 EXEC SQL include decimal; 5 6 int test(long n_SubscriptionId, 7 long n_ProductId, 8 datetime *p_StartDate, //esqlc type, non host variable 10 boolean *p_Status, //esqlc type, non host variable 12 EXEC SQL BEGIN DECLARE SECTION; 13 PARAMETER datetime *p_StartDate, 14 PARAMETER boolean *p_Status 15 EXEC SQL END DECLARE SECTION; 16 ) 17 { 18 int ln_RetVal = 0; 19 boolean b1; //esqlc type, 20 // non host variable 21 datetime d2; //esqlc type 22 // non host variable 23 24 EXEC SQL BEGIN DECLARE SECTION; 25 boolean b3; 26 datetime d4; 27 EXEC SQL END DECLARE SECTION; 28 29 return ln_RetVal; 30 }
lovecreatesbeauty wrote: > I am still anxious on that. I do the best to make what I mean clear, > the questions are: > > 1. Can I use real esql/c data types (no typedef) in function > parameters. Do I need to put the parameters with esql/c datatypes > between EXEC SQL BEGIN DECLARE SECTION; and EXEC SQL END DECLARE > SECTION; . What's a real ESQL/C data type? They're all typedefs of some sort. If you want to write datetime as the type name without a typedef, then you have to use the ESQL/C preprocessor to change the datetime into dtime_t for you. Pointless, but that's your choice. You ask "can I use"; I answer "no", or "yes", or "it depends". But the direct answer to the blinkered view of your question is "no". It appears that giving you the "yes" and "it depends" parts of the answer is confusing you - so I should stick with the simpler answer: No. However, I regard the function parameters in between EXEC SQL BEGIN DECLARE SECTION and EXEC SQL END DECLARE SECTION as (a) completely obnoxious and (b) totally irrelevant. I never use them - I refuse point blank to do so, and would argue up, down, side-to-side, back-to-front, topsy-turvy and endlessly that they are wrong, misguided, foolish and not to be used. And if anyone was so misguided as to suggest that I should use them despite my eloquence and stubbornnes, then I'd rather leave the job than use the notation. (Well, in the abstract; it'd depend on the other circumstances of the job as to whether I stayed or not, but it would be vehemently objected to, and I'd attempt to ignore the directives, but...) One reason for this attitude is that the notations completely wreck anything looking like a prototype. However, the downside is that sometimes I have to do not-so-fun/clean things like: int somefunc(dtime_t *dp) { EXEC SQL BEGIN DECLARE SECTION; datetime year to second dv = *dp; EXEC SQL END DECLARE SECTION; > Because host variables should be put inside the long esql > `BEGIN...END` declaration pair, and sometimes I don't want to use > these parameters as host variables. So can I skip the esql > `BEGIN...END` declaration pair. There is nothing dreadfully magic about the ESQL/C preprocessor. Code goes in; modified code comes out. All the ESQL/C data types map to real C data types. You can use the 'real C' types however you want; many of the ESQL/C data types can only be used where the ESQL/C preprocessor will map them to real C types. However, the ESQL/C preprocessor only processes stuff between EXEC SQL and the following semi-colon (except for declare sections, of course), or the older, non-standard $ to semi-colon notation. Anything else is just C code - plain and simple. If you can find a type 'boolean' in a header, you can use it. If you don't find such a type, you can't use it. Did you find such a type? No, so you can't use it unless you define the type. ESQL/C does not define such a type; the ESQL/C preprocessor translates references to such a type into some other type. Similarly with 'datetime' - it gets mapped to dtime_t or something similar. > 2. Can I pass values with real esql/c data types directly when call a > function. Yes. See the example above. You can also, these days (meaning since about 1985) pass structures by value - so I could have written: int somefunc(dtime_t d) { ... dv = d; ... > 3. Similar to item 1 above, can I declare other variables other than > function parameters with real esql/c data types outside the EXEC > SQL BEGIN DECLARE SECTION; and EXEC SQL END DECLARE SECTION; , as I > don't use them as host variables. Yes. > 1 /* Filename: test.h */ > 2 EXEC SQL include sqltypes; > 3 EXEC SQL include datetime; > 4 EXEC SQL include decimal; Hmm; you can do that, but I usually avoid doing so. I'd rather use #include, so I do. Don't forget to add ".h" to the names. > 5 > 6 int test(long n_SubscriptionId, > 7 long n_ProductId, > 8 datetime *p_StartDate, //esqlc type, non host variable > 10 boolean *p_Status, //esqlc type, non host variable > 12 EXEC SQL BEGIN DECLARE SECTION; > 13 PARAMETER datetime *p_StartDate, > 14 PARAMETER boolean *p_Status > 15 EXEC SQL END DECLARE SECTION; > 16 ); I decline to have anything to do with this notation (the E.S.B.D.S and E.S.E.D.S plus PARAMETER) in a parameter list. I've never used the keyword parameter that I can recall - if I did, it was an experiment, but that's all. I refuse to use it. I have no particular interest in whether it works or not - it is too obnoxious for words and I completely refuse to even consider whether that is supposed to work with commas instead of semicolons. > 1 /* Filename: test.ec <http://test.ec> */ > 2 EXEC SQL include sqltypes; > 3 EXEC SQL include datetime; > 4 EXEC SQL include decimal; > 5 > 6 int test(long n_SubscriptionId, > 7 long n_ProductId, > 8 datetime *p_StartDate, //esqlc type, non host variable > 10 boolean *p_Status, //esqlc type, non host variable > 12 EXEC SQL BEGIN DECLARE SECTION; > 13 PARAMETER datetime *p_StartDate, > 14 PARAMETER boolean *p_Status > 15 EXEC SQL END DECLARE SECTION; > 16 ) > 17 { > 18 int ln_RetVal = 0; > 19 boolean b1; //esqlc type, > 20 // non host variable > 21 datetime d2; //esqlc type > 22 // non host variable > 23 Well, boolean won't work unless you define a type for it; the ESQL/C preprocessor only processes stuff marked with EXEC SQL or $. Anything else it leaves strictly alone. So, you need to ensure that there is a type boolean. The C99 standard does have support for 'bool', but not for boolean. So, if you want to use it, you have to ensure it is a defined type - for example: typedef enum { false = 1, true = 1 } boolean; Similarly with datetime; as I responded previously: typedef dtime_t datetime; You can see what types are lurking in the wings by looking at the ESQL/C headers. If you want to see how I do it, then you can poke around the SQLCMD code, or other code that I've created, in the IIUG Software Archive. > 24 EXEC SQL BEGIN DECLARE SECTION; > 25 boolean b3; > 26 datetime d4; > 27 EXEC SQL END DECLARE SECTION; > 28 > 29 return ln_RetVal; > 30 } > > I also thess questions in the original thread. > > http://groups.google.com/group/comp.databases.informix/browse_frm/thread/79c835c94e82bab5/e55bdf2ca7277a25#e55bdf2ca7277a25 Also available as: http://tinyurl.com/89agj > On 10/8/05, Jonathan Leffler <jleffler@earthlink.net> wrote: > > #include <dateti