#region Copyright
// Advanced.Data.Provider - SQL Console
//
// Copyright (C) 2004 Astrein Engenharia de Manuteno S/A
// Copyright (C) 2004 Everaldo Canuto <everaldo_canuto@yahoo.com.br>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#endregion
using System;
using System.Data;
using System.Text;
using Advanced.Data.Provider;
namespace Advanced.Data.Provider{
class adpsql
{
private static AdpConnection connection;
[STAThread]
static void Main(string[] args)
{
string cmdline = "";
string strcmd = "";
string strpar = "";
Console.WriteLine("ADPSQL - The interactive SQL command-line client for Advanced.Data.Provider");
Console.WriteLine("Copyright (c) 2003-2004 Advanced.NET Team");
SqlHelp();
// Prompt looping!
while (true)
{
Console.Write(">"); cmdline = Console.ReadLine().Trim() + " ";
// Parse command line.
int i = cmdline.IndexOf(" ");
strcmd = cmdline.Substring(0,i);
strpar = cmdline.Substring(i).Trim();
switch (strcmd.ToLower().Trim())
{
case "":
break;
case "exit":
return;
case "help":
SqlHelp();
break;
case "providers":
SqlProviders();
break;
case "connect":
SqlConnect(strpar);
break;
case "close":
SqlClose();
break;
default:
SqlCommand(cmdline);
break;
}
}
}
static void SqlHelp()
{
Console.WriteLine("");
Console.WriteLine("Type:");
Console.WriteLine(" connect [connstr] - Connect to [connstr] database.");
Console.WriteLine(" close - Close active connection.");
Console.WriteLine(" providers - Show providers list.");
Console.WriteLine(" help - Show this message.");
Console.WriteLine(" exit - Exit adpsql.");
Console.WriteLine();
}
static void SqlProviders()
{
foreach (AdpProvider provider in AdpProvider.Providers)
{
Console.WriteLine(provider.Name + " - " + provider.Description);
}
}
static void SqlConnect(string connstr)
{
try
{
connection = new AdpConnection(connstr);
connection.Open();
Console.WriteLine("connected.\n");
}
catch (Exception e)
{
Console.WriteLine("{0}\n", e.Message);
}
}
static void SqlClose()
{
connection.Close();
connection.Dispose();
connection = null;
Console.WriteLine("Connection closed.\n");
}
static void SqlCommand(string connstr)
{
if ((connection == null) || (connection.State != ConnectionState.Open))
{
Console.WriteLine("No connection opened.\n");
return;
}
try
{
AdpCommand comm = new AdpCommand(connstr, connection);
AdpDataReader reader = comm.ExecuteReader();
DisplayQueryResult(reader);
reader.Close();
Console.WriteLine("");
}
catch (Exception e)
{
Console.WriteLine("{0}\n", e.Message);
}
}
static void DisplayQueryResult(AdpDataReader reader)
{
StringBuilder cheader = new StringBuilder();
StringBuilder divider = new StringBuilder();
StringBuilder column = null;
int length = 0;
int c;
DataTable table = reader.GetSchemaTable();
DataRow row;
// Display header.
Console.WriteLine("");
foreach (DataRow schemaRow in table.Rows)
{
string caption = schemaRow["ColumnName"].ToString();
if (caption.Equals("")) caption = "Column" + schemaRow["ColumnOrdinal"].ToString();
switch (schemaRow["DataType"].ToString())
{
case "System.DateTime":
length = 19;
break;
case "System.Boolean":
length = 5;
break;
default:
length = (int) schemaRow["ColumnSize"];
break;
}
length = (caption.Length > length) ? caption.Length : length;
cheader.Append(caption);
cheader.Append(' ', length - caption.Length);
divider.Append('-', length);
cheader.Append(" ");
divider.Append(" ");
}
Console.WriteLine(cheader.ToString());
Console.WriteLine(divider.ToString());
// Display data.
int rows = 0;
while(reader.Read())
{
rows++;
for(c = 0; c < reader.FieldCount; c++)
{
int dataLen = 0;
string dataValue = "";
column = new StringBuilder();
row = table.Rows[c];
string colhdr = (string) row["ColumnName"];
if (colhdr.Equals("")) colhdr = "Column" + row["ColumnOrdinal"].ToString();
switch (row["DataType"].ToString())
{
case "System.DateTime":
length = 19;
break;
case "System.Boolean":
length = 5;
break;
default:
length = (int) row["ColumnSize"];
break;
}
length = (colhdr.Length > length) ? colhdr.Length : length;
if (reader.IsDBNull(c))
{
dataValue = "";
dataLen = 0;
}
else
{
if (row["DataType"].ToString().Equals("System.DateTime"))
{
dataValue = reader.GetDateTime(c).ToString("yyyy-MM-dd HH:mm:ss");
}
else
{
dataValue = reader.GetValue(c).ToString();
}
dataLen = dataValue.Length;
}
length = (length > dataLen) ? length : dataLen;
if (length < colhdr.Length)
{
column.Append(' ', (colhdr.Length - length));
}
if (dataLen < length)
{
column.Append(' ', (length - dataLen));
switch (row["DataType"].ToString())
{
case "System.Int16":
case "System.Int32":
case "System.Int64":
case "System.Single":
case "System.Double":
case "System.Decimal":
Console.Write(column.ToString() + dataValue + " ");
break;
default:
Console.Write(dataValue + column.ToString() + " ");
break;
}
}
else
Console.Write(dataValue + " ");
}
Console.WriteLine();
}
Console.WriteLine("\n{0} rows retrieved.", rows.ToString());
}
}
}
|