3 * Copyright (C)2007-2008 Versabanq Innovations Inc. and contributors.
4 * See the included file named LICENSE for license information.
7 using System
.Collections
.Generic
;
9 using System
.Data
.Common
;
12 using Wv
.Mono
.Terminal
;
18 public class VxDbException
: DbException
20 public VxDbException(string msg
) : base(msg
)
26 public class WvDbi_Versaplex
: WvDbi
35 public short precision
;
40 public static void wvmoniker_register()
42 WvMoniker
<WvDbi
>.register("vx",
43 (string m
, object o
) => new WvDbi_Versaplex(m
));
46 public WvDbi_Versaplex(string busurl
)
48 WvUrl url
= new WvUrl(busurl
);
50 if (url
.host
.e() || url
.host
== "session")
51 bus
= WvDbus
.session_bus
;
53 bus
= new WvDbus(wv
.fmt("tcp:host={0},port={1}",
57 public override WvSqlRows
select(string sql
, params object[] args
)
59 var call
= new WvDbusCall("vx.versaplexd", "/db", "vx.db",
60 "ExecRecordset", "s");
61 WvDbusWriter writer
= new WvDbusWriter();
65 WvDbusMsg reply
= bus
.send_and_wait(call
);
66 if (reply
.err
== "vx.db.sqlerror")
67 throw new VxDbException(reply
.iter().pop());
68 reply
.check("a(issnny)vaay");
69 var it
= reply
.iter();
71 // decode the raw column info
72 var l
= new List
<WvColInfo
>();
73 foreach (IEnumerable
<WvAutoCast
> c
in it
.pop())
77 short precision
, scale
;
79 c
.ToArray().assignto(out size
, out name
, out type
,
80 out precision
, out scale
,
83 l
.Add(new WvColInfo(name
, typeof(string),
85 size
, precision
, scale
));
88 WvColInfo
[] colinfo
= l
.ToArray();
89 var rows
= new List
<WvSqlRow
>();
90 foreach (var r
in it
.pop())
91 rows
.Add(new WvSqlRow(r
.Cast
<object>().ToArray(),
94 return new WvSqlRows_Versaplex(rows
.ToArray(), colinfo
);
97 public override int execute(string sql
, params object[] args
)
99 using (select(sql
, args
))
104 class WvSqlRows_Versaplex
: WvSqlRows
, IEnumerable
<WvSqlRow
>
109 public WvSqlRows_Versaplex(WvSqlRow
[] rows
, WvColInfo
[] schema
)
112 this.schema
= schema
;
115 public override IEnumerable
<WvColInfo
> columns
116 { get { return schema; }
}
118 public override IEnumerator
<WvSqlRow
> GetEnumerator()
120 foreach (var row
in rows
)
126 public static class VxCli
128 public static int Main(string[] args
)
130 WvLog
.maxlevel
= WvLog
.L
.Debug
;
131 WvLog log
= new WvLog("vxcli");
133 if (args
.Length
!= 1)
135 Console
.Error
.WriteLine("Usage: vxcli <db-connection-string>");
139 string moniker
= args
[0];
141 WvIni vxini
= new WvIni("versaplexd.ini");
142 if (vxini
.get("Connections", moniker
) != null)
143 moniker
= vxini
.get("Connections", moniker
);
145 WvIni bookmarks
= new WvIni(
146 wv
.PathCombine(wv
.getenv("HOME"), ".wvdbi.ini"));
147 if (!moniker
.Contains(":")
148 && bookmarks
["Bookmarks"].ContainsKey(moniker
))
150 moniker
= bookmarks
["Bookmarks"][moniker
];
154 // not found in existing bookmarks, so see if we can parse and
156 WvUrl url
= new WvUrl(moniker
);
157 string path
= url
.path
;
158 if (path
.StartsWith("/"))
159 path
= path
.Substring(1);
160 if (path
!= "" && url
.host
!= null)
162 log
.print("Creating bookmark '{0}'\n", path
);
163 bookmarks
.set("Bookmarks", path
, moniker
);
166 } catch (IOException
) {
167 // not a big deal if we can't save our bookmarks.
172 using (var dbi
= WvDbi
.create(moniker
))
174 LineEditor le
= new LineEditor("VxCli");
180 inp
= le
.Edit("vx> ", "");
181 if (inp
== null) break;
182 if (inp
== "") continue;
185 using (var result
= dbi
.select(inp
))
188 from c
in result
.columns
189 select c
.name
.ToUpper();
190 Console
.Write(wv
.fmt("{0}\n\n",
191 colnames
.join(",")));
193 foreach (var row
in result
)
194 Console
.Write(wv
.fmt("{0}\n", row
.join(",")));
197 catch (DbException e
)
199 Console
.Write(wv
.fmt("ERROR: {0}\n", e
.Short()));