1 #include "Sql.h"
2 
3 namespace Upp {
4 
Field(const char * name,Ref f,bool * b)5 void S_info_maker::Field(const char *name, Ref f, bool *b)
6 {
7 	if(b) f = Ref(*b);
8 	S_info::Column& c = info.column.Add(name);
9 	c.offset = (byte *)f.GetVoidPtr() - (byte *)s;
10 	c.manager = f.GetManager();
11 	c.width = 0;
12 }
13 
Width(int width)14 void S_info_maker::Width(int width)
15 {
16 	info.column.Top().width = width;
17 }
18 
GetRef(const void * s,int i) const19 Ref S_info::GetRef(const void *s, int i) const
20 {
21 	return Ref((byte *)s + column[i].offset, column[i].manager);
22 }
23 
GetWidth(const SqlId & id) const24 int S_info::GetWidth(const SqlId& id) const
25 {
26 	int q = column.Find(~id);
27 	return q >= 0 ? GetWidth(q) : 0;
28 }
29 
GetRef(const void * s,const SqlId & id) const30 Ref S_info::GetRef(const void *s, const SqlId& id) const
31 {
32 	int q = column.Find(~id);
33 	return q >= 0 ? GetRef(s, q) : Ref();
34 }
35 
Get(const void * s,const SqlId & id) const36 Value S_info::Get(const void *s, const SqlId& id) const
37 {
38 	return ~GetRef(s, id);
39 }
40 
Get(const void * s,int i) const41 Value S_info::Get(const void *s, int i) const
42 {
43 	return ~GetRef(s, i);
44 }
45 
Get(const void * s) const46 ValueMap S_info::Get(const void *s) const
47 {
48 	ValueMap m;
49 	for(int i = 0; i < column.GetCount(); i++)
50 		m.Add(column.GetKey(i), GetRef(s, i));
51 	return m;
52 }
53 
Set(const void * s,int i,const Value & v) const54 void S_info::Set(const void *s, int i, const Value& v) const
55 {
56 	Ref f = GetRef(s, i);
57 	if(f.Is<bool>() && IsString(v)) {
58 		String h = v;
59 		f = !(h == "0" || IsNull(h));
60 	}
61 	else
62 		f = v;
63 }
64 
Set(const void * s,const SqlId & id,const Value & v) const65 void S_info::Set(const void *s, const SqlId& id, const Value& v) const
66 {
67 	int q = column.Find(~id);
68 	if(q >= 0)
69 		Set(s, q, v);
70 }
71 
Set(const void * s,const ValueMap & m) const72 void S_info::Set(const void *s, const ValueMap& m) const
73 {
74 	for(int i = 0; i < m.GetCount(); i++) {
75 		Value v = m.GetKey(i);
76 		if(IsString(v))
77 			Set(s, (String)v, m.GetValue(i));
78 	}
79 }
80 
GetSet(const String & prefix) const81 SqlSet S_info::GetSet(const String& prefix) const
82 {
83 	SqlSet set;
84 	for(int i = 0; i < column.GetCount(); i++)
85 		set << SqlId(prefix + column.GetKey(i));
86 	return set;
87 }
88 
GetOf(const SqlId & table) const89 SqlSet S_info::GetOf(const SqlId& table) const
90 {
91 	SqlSet set;
92 	for(int i = 0; i < ids.GetCount(); i++)
93 		set << SqlId(ids[i].Of(table));
94 	return set;
95 }
96 
Init()97 void S_info::Init()
98 {
99 	column.Shrink();
100 	ids.SetCount(column.GetCount());
101 	for(int i = 0; i < column.GetCount(); i++) {
102 		SqlId id(column.GetKey(i));
103 		ids[i] = id;
104 		set << id;
105 	}
106 }
107 
108 }
109