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