Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fiBvQ-0008Jw-7J for pgsql-interfaces@arkaria.postgresql.org; Wed, 25 Jul 2018 05:01:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fiBvO-0004dQ-9q for pgsql-interfaces@arkaria.postgresql.org; Wed, 25 Jul 2018 05:01:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fiBvO-0004dJ-49 for pgsql-interfaces@lists.postgresql.org; Wed, 25 Jul 2018 05:01:22 +0000 Received: from mail-it0-x244.google.com ([2607:f8b0:4001:c0b::244]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fiBvL-0000K3-3K for pgsql-interfaces@lists.postgresql.org; Wed, 25 Jul 2018 05:01:21 +0000 Received: by mail-it0-x244.google.com with SMTP id h23-v6so6927301ita.5 for ; Tue, 24 Jul 2018 22:01:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=OvzCIpnGNbXNFWytkS6bQINuI6T0ypyZHjd1ifH6gN4=; b=nsYmrDg+h2msZomtGPTVjDut3sY9u+2FfGobwDJbw5Rt2pyCaHfevn5pE9FrjBK7RW x581DIHRpGO73GWDpJD3npc5XuNzkfxXp7Dp0YHLt3prGwLJZAan516erEfdycAQp4H+ gL+BDj/y8WrMtY/pyg+D3lTNva/HRI6Wu0266Tl/3oFy/k3ukb4JwDmf+WY5TmgCcnLn S+Onw8mmBej9mjgmuPbqMXZLNSNvPV6xN6NfndAruKUZ8VWdoHSgiD4D8+gLvFtQPSCI c9KkerbSDv4IqhB6ShNi0ilKFzbYEwQXS5srocUECDb/Cequ4oYbkVinm/C9KlW7MbeT iNDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=OvzCIpnGNbXNFWytkS6bQINuI6T0ypyZHjd1ifH6gN4=; b=niY2eFt5gJO3VGyZLK0pl0NgwDlKZe/ed3OoNuBGP+PCKDFwAg72d7FyL850vQE69F IBSnRgHHKEHNlMs7Mh1Obiql4MI2PEIkyi9nG61RKZnNh3UhGO67QYCb3XfmsJubeX9R e9hoArZCeFJvCbB+oFkQdbGgkpTjyDeh06DMhzoKsILYPsCrH2k0WV48ySgkVSZXq0tw 9aFows+4kASKKNJVRH4rUlOWQZPjewGq2CJNEd8uzvtSktPb07q3MSt7UUZlvPpEHXMo JW/PNQwrFIWBALkEOYNPC323MIeju2rLo+chgNJ4gHB1l9xY8RRA9tPHTHQe9EVAzsU4 yWrw== X-Gm-Message-State: AOUpUlG6D22YVv7cE++O9zPPELrvHiATDkFCxpRrPBuSlfheZOJD8GHZ YJgOSsTwfCGKjSfMYT0RnvC+xFvm9o3bKctIe99qqWPc X-Google-Smtp-Source: AAOMgpeA5RCUZz7JeNq5AeRDDrma39Luh9vaW2EanYCwxEn1tdC4SVUxDfWsKXKpw4S8L6lIGlBbCuHAzE6qNspQwxY= X-Received: by 2002:a24:b302:: with SMTP id e2-v6mr5123959itf.132.1532494876861; Tue, 24 Jul 2018 22:01:16 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a02:5989:0:0:0:0:0 with HTTP; Tue, 24 Jul 2018 22:01:16 -0700 (PDT) In-Reply-To: References: From: Ashutosh Sharma Date: Wed, 25 Jul 2018 10:31:16 +0530 Message-ID: Subject: Re: Host variables in ecpg To: pgsql-interfaces@lists.postgresql.org Cc: Michael Meskes Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, Jul 24, 2018 at 4:16 PM, Ashutosh Sharma wrote: > Hi All, > > While trying to understand the concept of host variables in ecpg, I > came to know that the host variables in ecpg can be declared using the > following two methods, > > 1) Using the explicit declare section. > > EXEC SQL BEGIN DECLARE SECTION; > double d = 3.14; > EXEC SQL END DECLARE SECTION; > > 2) Using an implicit declare section. > > EXEC SQL double d = 3.14; > > > Incase of 1 (i.e. when using explicit declare section) the host > variables gets scanned in C state using '{identifier}' rule whereas > incase of 2 (i.e. when using implicit declare section) the host > variables gets scanned in SQL state using '{identifier}' rule. I > could observe that, in sql state, we perform SQL keyword lookup > followed by C keyword lookup. However, in case of C state, we just > perform C keyword lookup. Please refer to the following code snippets > in pgc.l file > > {identifier} { > const ScanKeyword *keyword; > > if ((!INFORMIX_MODE || !isinformixdefine()) && > !isdefine()) > { > keyword = ScanCKeywordLookup(yytext); > if (keyword != NULL) > return keyword->value; > else > { > base_yylval.str = mm_strdup(yytext); > return IDENT; > } > } > > > {identifier} { > const ScanKeyword *keyword; > > if (!isdefine()) > { > /* Is it an SQL/ECPG keyword? */ > keyword = ScanECPGKeywordLookup(yytext); > if (keyword != NULL) > return keyword->value; > > /* Is it a C keyword? */ > keyword = ScanCKeywordLookup(yytext); > if (keyword != NULL) > return keyword->value; > > /* > * None of the above. Return it as an identifier. > * > * The backend will attempt to truncate > and case-fold > * the identifier, but I see no good reason for ecpg > * to do so; that's just another way that > ecpg could get > * out of step with the backend. > */ > base_yylval.str = mm_strdup(yytext); > return IDENT; > > Shouldn't we perform both SQL and C table lookup in C state as well? > I spent some more time thinking over this and found that, we would actually never come to a situation where in C state we need to have a SQL keyword lookup and hence, SQL keywords lookup might not be required in C state. > Let's consider the following test where a double datatype is used > inside the declare section. > > EXEC SQL BEGIN DECLARE SECTION; > double d1; > EXEC SQL END DECLARE SECTION; > > In above case "double" would be scanned in C state by {identifiers} > rule which would search for double keyword in ScanCKeywords[] table > and since it doesn't find it, it returns some IDENT keyword for it. > > However, if above is written as "EXEC SQL double d;" then, double > would be scanned in SQL state by {identifiers} rule which would > first search for double in SQLScanKeywords[] followed by > ScanCKeywords[]. In this case, as double is declared in > SQLScanKeywords[], it would return some valid keyword for double. > > -- > With Regards, > Ashutosh Sharma > EnterpriseDB:http://www.enterprisedb.com