I plan to embed SQL into my C-code using the PostgreSQL SPI, and I would like to the reference NAMEDATALEN const, so my code will be flexible enough to change with the database.
Is there a #include <> I can put in my C-code to reference this const?
Answer referenced from: Complete C-type Table (PostgreSQL v.9.1)
----------------------------------
| SQL Type | C Type | Defined In |
----------------------------------
| name | Name | postgres.h |
----------------------------------
It looks as though I need to do the following, to achieve my goal...
#include <postgres.h>
//char my_name[NAMEDATALEN]; // Theoretically, this should also be valid.
Name my_name;
EDIT: After some more digging, I was able to prove my hunch correct.
Pasted from postgres.h
00047 #include "c.h"
Pasted from c.h
00443 /*
00444 * Representation of a Name: effectively just a C string, but null-padded to
00445 * exactly NAMEDATALEN bytes. The use of a struct is historical.
00446 */
00447 typedef struct nameData
00448 {
00449 char data[NAMEDATALEN];
00450 } NameData;
00451 typedef NameData *Name;
Related
The codes:
// very complex where clause combined from several runtime variables.
let query: String = String.from("where ..........");
let rows_num: i64 = sqlx::query!( &*query).fetch_one(connection)
The error from compiler:
error: expected string literal
--> src/abc.rs:80:25
|
80 | let rows_num: i64 = sqlx::query!(
| ____________^
81 | | &*query,
82 | | ).fetch_one(connection)
| |^
|
= note: this error originates in the macro sqlx::query (in Nightly builds, run with -Z macro-backtrace for more info)
And the doc states:
The query must be a string literal or else it cannot be introspected (and thus cannot be dynamic or the result of another macro).
I know the sqlx computes at compile time, my where clause computation is at run time.
I really want to use variable, because the where clause depends other several conditions.
Are there any ways to use variable in sqlx?
Using sqlx::query() function instead of the sqlx::query!() macro. The document doesn't mention it in their page.
I tried to create a simple user defined function (UDF) for Firebird 2.5 with C++ Builder 2010 but I don't manage to get it to work in Firebird.
Creating a DLL project with default setting in C++ Builder 2010.
Adding a unit with my example UDF including "ibase.h" and "ib_util.h":
extern "C" __declspec(dllexport) int __stdcall MYFUNC ( int i )
{
int result = 2 * i;
return result;
}
Building the DLL FBUDFMBD.dll in path C:\Program Files (x86)\Firebird\Firebird_2_5\UDF
Registering my UDF via IBExpert in a sample db with
DECLARE EXTERNAL FUNCTION F_MYFUNC
INTEGER
RETURNS INTEGER
ENTRY_POINT 'MYFUNC' MODULE_NAME 'FBUDFMBD';
Calling the UDF with
select F_MYFUNC( 3 ) from RDB$DATABASE;
results in error message
Invalid token.
invalid request BLR at offset 36.
function F_MYFUNC is not defined.
module name or entrypoint could not be found.
With the tool GExperts - PE Information I can see my UDF as DLL-Export MYFUNC ordinal $1 and entry point $1538.
What I am doing wrong, Firebird can't register my DLL and its UDF correctly?
Is there anything in my DLL project to change regarding to default compiler options?
Thanks a lot! I got it by your help.
top 2: Corrected C++-Code is:
extern "C" __declspec(dllexport) int MYFUNC ( int * val )
{
int result = 2 * *val;
return result;
}
Pay attention to reference call of the input parameter.
top 4: Register the UDF in a firebird 2.5 db by
DECLARE EXTERNAL FUNCTION F_MYFUNC
INTEGER
RETURNS INTEGER BY VALUE
ENTRY_POINT '_MYFUNC' MODULE_NAME 'FBUDFMBD';
Pay attention to the leading underscore at the function name!
top 5: select F_MYFUNC( 3 ) from RDB$DATABASE; works really fine!
In Delphi, you can write cdecl and not stdcall
i.e
function ExisteBase(const aBase:PChar):Integer; cdecl;
Not
function ExisteBase(const aBase:PChar):Integer; stdcall;
Maybe on C++ __cdecl
I hope I helped in some
I'm new in learning stored procedures in SQL.
I want to create a stored procedure for inserting values from automatic data by calculation.
Table Attendance:
EMPL_KODE |EMPL_NAME |DATE_IN |TIME_IN |TIME_OUT|TIME_IN |TIME_OUT
001 | Michel |25.04.2016 |06:50 |15:40 | |
002 | Clara |25.04.2016 |06:15 |15:43 | |
003 | Rafael |25.04.2016 |06:25 |15:45 | |
001 | Michel |26.04.2016 |06:23 |15:42 | |
002 | Clara |26.04.2016 |06:10 |15:41 | |
003 | Rafael |26.04.2016 |06:30 |15:42 | |
001 | Michel |27.04.2016 |06:33 |15:42 | |
002 | Clara |27.04.2016 |06:54 |15:44 | |
003 | Rafael |27.04.2016 |07:00 |15:45 | |
I want to fill TIME_IN and TIME_OUT values automatically by creating a stored procedure. Here is the code :
CREATE PROCEDURE InsertTotalEmployee
#TOTAL_MINUTES int,
#TOTAL_HOURS float
AS
BEGIN
INSERT INTO ATTENDANCE (TOTAL_MINUTES, TOTAL_HOURS)
VALUES (
SELECT
DATEDIFF(MINUTE, ATTENDANCE.TIME_IN, ATTENDANCE.TIME_OUT),
DATEDIFF(MINUTE, ATTENDANCE.TIME_IN, ATTENDANCE.TIME_OUT) / 60.0
)
END
After I write and execute my statement, a message error occurs:
Token unknown - line 2, column 5 #
I run the code using Flamerobin.
It looks like you are trying to use Microsoft SQL Server syntax in Firebird, that is not going to work.
For one, the # is not allowed like that in identifiers (unless you use double quotes around them), and the list of parameters must be enclosed in parentheses.
See the syntax of CREATE PROCEDURE. You need to change it to:
CREATE PROCEDURE InsertTotalEmployee(TOTAL_MINUTES int, TOTAL_HOURS float)
You also might want to change the datatype float to double precision, and the body of your stored procedure seems to be incomplete because you are selecting from nothing (a select requires a table to select from), and are missing a semicolon at the end of the statement.
All in all I suggest you study the Firebird language reference, then try to create a functioning insert and only then create a stored procedure around it.
Also note that when creating a stored procedure in Flamerobin, that you must switch statement terminators using set term otherwise Flamerobin can't send the stored procedure correctly, see also the first section in Procedural SQL (PSQL) Statements.
I'm having a lot of trouble getting sml-mode in Emacs to sanely indent my code. For example, here is a block where it is behaving particularly strangely:
datatype type_node
= Param of TyParam.t
| LongId of LongId.t
| Offset of field * field list option
| List of type_t list
| Fun of type_t list * t list
| Any
| VProc
| Cont of TyArg.t list option
| Addr of type_t
and dataconsdef_node
= ConsDef of BomId.t * type_t option
and field_node
= Immutable of int * type_t
| Mutable of int * type_t
and fundef_node
= Def of Attrs.t option * BomId.t * TyParam.t list option
* Param.t list option * Param.t list option * type_t * exp_t
and varpat_node
= Wild
| Var of BomId.t * type_t option
and caserule_node
= LongRule of LongId.t * varpat_t list * exp_t
| LiteralRule Literal.t * exp_t
| DefaultRule of varpat_t * exp_t
and tycaserule_node
= TyRule of type_t * exp_t
| Default of exp_t
and simpleexp_node
= PrimOp of 'var Prim.prim * simpleexp_t list
| AllocId of LongId.t * simpleexp_t list
| AllocType of Type.t * simpleexp_t list
| AtIndex of int * simpleexp_t * simpleexp_t option
| TypeCast of Type.t * simpleexp_t
| HostVproc
| VpLoad of int * simpleexp_t
| VpAddr of int * simpleexp_t
| VpStore of int * simpleexp_t * t
| Id of LongId.t
| Lit of Literal.t
| MLString of string
and exp_node
= Let of VarPat.t list * rhs * t
| Do of SimpleExp.t * t
| Fun of FunDef.t list * t
| Cont of BomId.t * Param.t list option * t * t
| If of SimpleExp.t * t * t
| Case of SimpleExp.t * CaseRule.t list
| Typecase of TyParam.t * TyCaseRule.t list
| Apply of LongId.t * SimpleExp.t list option * SimpleExp.t list option
| Throw of LongId.t * SimpleExp.t list option
| Return of SimpleExp.t list option
and foo_node
= bar
Apologies for the huge code dump, but the content doesn't matter -- just the extremely inconsistent indentation. Attempting to define signatures inside of a structure Foo : sig ... end = struct ... end block is even worse:
structure Attrs : sig
type t
datatype node
= Attributes of string list
include WRAPPED
sharing type node' = node
sharing type obj = t
end
or:
functor DoWrap(type node) : sig
type t = node Wrap.t
include WRAPPED
sharing type node' = node
sharing type obj = t
end = struct
open Wrap
type t = node Wrap.t
type node' = node
type obj = t
end
I'm a diehard Emacs fan and I would hate to have to avoid it on this project, since it's one that I'll be working on the the next few months. However, I can't turn code like this in to my supervisor. The above is the behavior that I get when I select a block of text and indent-region.
I've looked at this answer, but the solution doesn't apply, since this is the behavior M-C-\ gives me, and it would be a huge pain to work on code without being able to auto-indent the whole file.
Is there an easy fix for this? Are there alternative sml-mode implementations?
Presuming that you are using the latest version of SML Mode from GNU ELPA, it looks like you have hit a corner-case in which the built-in SMIE grammar of SML Mode breaks down. I had similar issues with Tuareg Mode for OCaml.
You likely need to downgrade your version of SML Mode to version 4.0 or lower, which can obtain from http://www.iro.umontreal.ca/~monnier/elisp/. These versions do not yet use SMIE for indentation, but some ad-hoc indentation code. At least for OCaml and Tuareg, I found this old-fashioned approach to yield much better and much more reliable indentation.
You may want to report this issue to the maintainer of SML Mode, though, whose mail address you can find in the header of SML Mode, or in the footer of the aforementioned website.
I have a class Test_Class in Ada 2005 which has a parent-linked task property called Primary, from type Primary_Task, defined as:
type Test_Class is tagged limited
record
Info : Integer;
Value : Float;
Primary : Primary_Task (Test_Class'Access);
end record;
I need build a one-step constructor for my class in the form
function Construct (T : access Test_Class) return Test_Class_Ptr is
begin
return new Test_Class'(Info => T.Info + 1,
Value => 0.0,
Primary => [WHAT I WANNA KNOW]);
end Construct;
Currently my code is:
-- test_pkg.ads
package Test_Pkg is
type Test_Class;
type Test_Class_Ptr is access all Test_Class;
task type Primary_Task (This_Test : access Test_Class) is
pragma Storage_Size (1000);
end Primary_Task;
type Test_Class is tagged limited
record
Info : Integer;
Value : Float;
Primary : Primary_Task (Test_Class'Access);
end record;
function Construct (T : access Test_Class) return Test_Class_Ptr;
end Test_Pkg;
-- test_pkg.adb
with Text_IO; use Text_IO;
package body Test_Pkg is
[...]
function Construct (T : access Test_Class) return Test_Class_Ptr is
T_Ptr : constant Test_Class_Ptr := new Test_Class;
begin
T_Ptr.Info := T.Info + 1;
T_Ptr.Value := 0.0;
return T_Ptr;
end Construct;
end Test_Pkg;
So, how can I code it? What should I put in Primary => [...] code? Should I change the definition of Primary : Primary_Task (Test_Class'Access); in Test_Class definition?
I got an answer from Randy Brukardt (thank you) on comp.lang.ada:
In Ada 2005 or later, use "<>" to default initialize a component in an
aggregate (which is the only thing you can do with a task).
(...)
function Construct (T : access Test_Class) return Test_Class_Ptr is
begin
return new Test_Class'(Info => T.Info + 1,
Value => 0.0,
Primary => <>);
end Construct;
However, I tried to compile it using GNAT GPL 2011 and got the GNATBUG below
c:\tst>gnatmake -gnat12 test_pkg.adb
gcc -c -gnat12 test_pkg.adb
+===========================GNAT BUG DETECTED==============================+
| GPL 2011 (20110428) (i686-pc-mingw32) GCC error: |
| in create_tmp_var, at gimplify.c:505 |
| Error detected around test_pkg.adb:20:29 |
| Please submit a bug report by email to report#adacore.com. |
| GAP members can alternatively use GNAT Tracker: |
| http://www.adacore.com/ section 'send a report'. |
| See gnatinfo.txt for full info on procedure for submitting bugs. |
| Use a subject line meaningful to you and us to track the bug. |
| Include the entire contents of this bug box in the report. |
| Include the exact gcc or gnatmake command that you entered. |
| Also include sources listed below in gnatchop format |
| (concatenated together with no headers between files). |
| Use plain ASCII or MIME attachment. |
+==========================================================================+
Please include these source files with error report
Note that list may not be accurate in some cases,
so please double check that the problem can still
be reproduced with the set of files listed.
Consider also -gnatd.n switch (see debug.adb).
test_pkg.adb
test_pkg.ads
raised TYPES.UNRECOVERABLE_ERROR : comperr.adb:423
gnatmake: "test_pkg.adb" compilation error
So GNAT GPL users may have to wait for the next release to use this solution.