1 module dpq2.args;
2 
3 @safe:
4 
5 public import dpq2.conv.from_d_types;
6 public import dpq2.conv.from_bson;
7 
8 import dpq2;
9 
10 /// Query parameters
11 struct QueryParams
12 {
13     string sqlCommand; /// SQL command
14     Value[] args; /// SQL command arguments
15     ValueFormat resultFormat = ValueFormat.BINARY; /// Result value format
16 
17     /// Useful for simple text-only query params
18     /// Postgres infers a data type for the parameter in the same way it would do for an untyped literal string.
19     @property void argsFromArray(in string[] arr)
20     {
21         args.length = arr.length;
22 
23         foreach(i, ref a; args)
24             a = toValue(arr[i], ValueFormat.TEXT);
25     }
26 
27     @property string preparedStatementName() const { return sqlCommand; }
28     @property void preparedStatementName(string s){ sqlCommand = s; }
29 }
30 
31 /// Used as parameters by PQexecParams-like functions
32 package struct InternalQueryParams
33 {
34     private
35     {
36         const(string)* sqlCommand;
37         Oid[] oids;
38         int[] formats;
39         int[] lengths;
40         const(ubyte)*[] values;
41     }
42 
43     ValueFormat resultFormat;
44 
45     this(in ref QueryParams qp) pure
46     {
47         sqlCommand = &qp.sqlCommand;
48         resultFormat = qp.resultFormat;
49 
50         oids = new Oid[qp.args.length];
51         formats = new int[qp.args.length];
52         lengths = new int[qp.args.length];
53         values = new const(ubyte)* [qp.args.length];
54 
55         for(int i = 0; i < qp.args.length; ++i)
56         {
57             oids[i] = qp.args[i].oidType;
58             formats[i] = qp.args[i].format;
59 
60             if(!qp.args[i].isNull)
61             {
62                 lengths[i] = qp.args[i].data.length.to!int;
63                 values[i] = qp.args[i].data.ptr;
64             }
65         }
66     }
67 
68     /// Values used by PQexecParams-like functions
69     const(char)* command() pure const
70     {
71         return cast(const(char)*) (*sqlCommand).toStringz;
72     }
73 
74     /// ditto
75     const(char)* stmtName() pure const
76     {
77         return command();
78     }
79 
80     /// ditto
81     int nParams() pure const
82     {
83         return values.length.to!int;
84     }
85 
86     /// ditto
87     const(Oid)* paramTypes() pure
88     {
89         return oids.ptr;
90     }
91 
92     /// ditto
93     const(ubyte*)* paramValues() pure
94     {
95         return values.ptr;
96     }
97 
98     /// ditto
99     const(int)* paramLengths() pure
100     {
101         return lengths.ptr;
102     }
103 
104     /// ditto
105     const(int)* paramFormats() pure
106     {
107         return formats.ptr;
108     }
109 }