MSSQL package (ODBC based)

git-svn-id: svn://ultimatepp.org/upp/trunk@1083 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2009-04-20 20:45:47 +00:00
parent 1025de3d5d
commit 2f309d3972
9 changed files with 231 additions and 71 deletions

12
uppsrc/MSSQL/MSSQL.cpp Normal file
View file

@ -0,0 +1,12 @@
#include "MSSQL.h"
namespace Upp {
String MsSqlTextType(int width)
{
if(width <= 4000)
return NFormat("varchar(%d)", width);
return "text";
}
};

12
uppsrc/MSSQL/MSSQL.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef _MSSQL_MSSQL_h
#define _MSSQL_MSSQL_h
#include "ODBC/ODBC.h"
namespace Upp {
String MsSqlTextType(int width);
};
#endif

5
uppsrc/MSSQL/MSSQL.upp Normal file
View file

@ -0,0 +1,5 @@
file
MSSQL.h,
MSSQLSchema.h,
MSSQL.cpp;

104
uppsrc/MSSQL/MSSQLSchema.h Normal file
View file

@ -0,0 +1,104 @@
#define BIT(x) COLUMN("bit", int, x, 0, 0)
#define BIT_ARRAY(x, items) COLUMN_ARRAY("bit", int, x, 0, 0, items)
#define BIT_(x) COLUMN_("bit", int, x, 0, 0)
#define BIT_ARRAY_(x, items) COLUMN_ARRAY_("bit", int, x, 0, 0, items)
#define BOOL(x) COLUMN("char(1)", bool, x, 0, 0)
#define BOOL_ARRAY(x, items) COLUMN_ARRAY("char(1)", bool, x, 0, 0, items)
#define BOOL_(x) COLUMN_("char(1)", bool, x, 0, 0)
#define BOOL_ARRAY_(x, items) COLUMN_ARRAY_("char(1)", bool, x, 0, 0, items)
#define INT64(x) COLUMN("bigint", int64, x, 0, 0)
#define INT64_ARRAY(x, items) COLUMN_ARRAY("bigint", int64, x, 0, 0, items)
#define INT64_(x) COLUMN_("bigint", int64, x, 0, 0)
#define INT64_ARRAY_(x, items) COLUMN_ARRAY_("bigint", int64, x, 0, 0, items)
#define INT(x) COLUMN("integer", int, x, 0, 0)
#define INT_ARRAY(x, items) COLUMN_ARRAY("integer", int, x, 0, 0, items)
#define INT_(x) COLUMN_("integer", int, x, 0, 0)
#define INT_ARRAY_(x, items) COLUMN_ARRAY_("integer", int, x, 0, 0, items)
#define DOUBLE(x) COLUMN("double precision", double, x, 0, 0)
#define DOUBLE_ARRAY(x, items) COLUMN_ARRAY("double precision", double, x, 0, 0, items)
#define DOUBLE_(x) COLUMN_("double precision", double, x, 0, 0)
#define DOUBLE_ARRAY_(x, items) COLUMN_ARRAY_("double precision", double, x, 0, 0, items)
#define DATE(x) COLUMN("datetime", Date, x, 0, 0)
#define DATE_ARRAY(x, items) COLUMN_ARRAY("datetime", Date, x, 0, 0, items)
#define DATE_(x) COLUMN_("datetime", Date, x, 0, 0)
#define DATE_ARRAY_(x, items) COLUMN_ARRAY_("datetime", Date, x, 0, 0, items)
#define TIME(x) COLUMN("datetime", Time, x, 0, 0)
#define TIME_ARRAY(x, items) COLUMN_ARRAY("datetime", Time, x, 0, 0, items)
#define TIME_(x) COLUMN_("datetime", Time, x, 0, 0)
#define TIME_ARRAY_(x, items) COLUMN_ARRAY_("datetime", Time, x, 0, 0, items)
#define STRING(x, n) COLUMN(MsSqlTextType(n), String, x, n, 0)
#define STRING_ARRAY(x, n, items) COLUMN_ARRAY(MsSqlTextType(n), String, x, n, 0, items)
#define STRING_(x, n) COLUMN_(MsSqlTextType(n), String, x, n, 0)
#define STRING_ARRAY_(x, n, items) COLUMN_ARRAY_(MsSqlTextType(n), String, x, n, 0, items)
#define LONGRAW(x) COLUMN("varbinary(max)", String, x, 0, 0)
#define LONGRAW_(x) COLUMN_("varbinary(max)", String, x, 0, 0)
#define PRIMARY_KEY INLINE_ATTRIBUTE("primary key")\
ATTRIBUTE("alter table @t add constraint PK_@x primary key "\
"(@c)",\
"alter table @t drop constraint PK_@x;")\
ATTRIBUTE("create index PKX_@x on @t(@c);", \
"drop index PKX_@x;")
#define KEY INLINE_ATTRIBUTE("key")
#define NOT_NULL INLINE_ATTRIBUTE("not null")
#define IDENTITY INLINE_ATTRIBUTE("identity")
#define AUTO_INCREMENT INLINE_ATTRIBUTE("identity")
#define INDEX ATTRIBUTE("create index IDX_@x on @t(@c);", \
"drop index IDX_@x;")
#define UNIQUE ATTRIBUTE("create unique index UNQ_@x on @t(@c);", \
"drop index UNQ_@x;")
#define REFERENCES(x) ATTRIBUTE("alter table @t add constraint FK_@x foreign key "\
"(@c) references @s" #x ";",\
"alter table @t drop constraint FK_@x;")
#define TIMESTAMP(ts) SCHEMA("-- " ts "\n\n", NULL)
#define COMMENT(txt) SCHEMA("-- " #txt "\n", NULL)
#include <Sql/sch_model.h>
#undef INT
#undef INT_ARRAY
#undef INT_
#undef INT_ARRAY_
#undef DOUBLE
#undef DOUBLE_ARRAY
#undef DOUBLE_
#undef DOUBLE_ARRAY_
#undef DATE
#undef DATE_ARRAY
#undef DATE_
#undef DATE_ARRAY_
#undef DATETIME
#undef DATETIME_ARRAY
#undef DATETIME_
#undef DATETIME_ARRAY_
#undef STRING
#undef STRING_ARRAY
#undef STRING_
#undef STRING_ARRAY_
#undef PRIMARY_KEY
#undef KEY
#undef NOT_NULL
#undef IDENTITY
#undef INDEX
#undef UNIQUE
#undef TIMESTAMP
#undef COMMENT

3
uppsrc/MSSQL/init Normal file
View file

@ -0,0 +1,3 @@
#ifndef _MSSQL_icpp_init_stub
#define _MSSQL_icpp_init_stub
#endif

View file

@ -38,11 +38,13 @@ private:
int rowsprocessed;
Vector< Vector<double> > number;
Vector< Vector<int64> > num64;
Vector< Vector<String> > text;
Vector< Vector<Time> > time;
int rowcount;
int rowi;
Vector<Value> fetchrow;
Vector<bool> binary;
bool IsOk(SQLRETURN ret) const;
void Flush();
@ -345,6 +347,7 @@ bool ODBCConnection::Execute()
}
session->current = this;
info.Clear();
binary.Clear();
for(int i = 1; i <= ncol; i++) {
SQLCHAR ColumnName[256];
SQLSMALLINT NameLength;
@ -355,6 +358,7 @@ bool ODBCConnection::Execute()
if(!IsOk(SQLDescribeCol(session->hstmt, i, ColumnName, 255, &NameLength, &DataType,
&ColumnSize, &DecimalDigits, &Nullable)))
return false;
binary.Add(false);
SqlColumnInfo& f = info.Add();
f.nullable = Nullable != SQL_NO_NULLS;
f.precision = DecimalDigits;
@ -371,13 +375,21 @@ bool ODBCConnection::Execute()
case SQL_DOUBLE:
case SQL_BIT:
case SQL_TINYINT:
case SQL_BIGINT:
f.type = DOUBLE_V;
break;
case SQL_BIGINT:
f.type = INT64_V;
break;
case SQL_TYPE_DATE:
case SQL_TYPE_TIMESTAMP:
f.type = TIME_V;
break;
case SQL_BINARY:
case SQL_VARBINARY:
case SQL_LONGVARBINARY:
f.type = STRING_V;
binary.Top() = true;
break;
default:
f.type = STRING_V;
break;
@ -402,6 +414,7 @@ bool ODBCConnection::Fetch0()
return false;
fetchrow.Clear();
double dbl;
int64 n64;
SQL_TIMESTAMP_STRUCT tm;
SQLLEN li;
for(int i = 0; i < info.GetCount(); i++) {
@ -413,6 +426,12 @@ bool ODBCConnection::Fetch0()
if(li != SQL_NULL_DATA)
v = dbl;
break;
case INT64_V:
if(!IsOk(SQLGetData(session->hstmt, i + 1, SQL_C_SBIGINT, &n64, sizeof(dbl), &li)))
break;
if(li != SQL_NULL_DATA)
v = n64;
break;
case TIME_V:
if(!IsOk(SQLGetData(session->hstmt, i + 1, SQL_C_TYPE_TIMESTAMP, &tm, sizeof(tm), &li)))
break;
@ -428,12 +447,13 @@ bool ODBCConnection::Fetch0()
}
break;
default:
if(!IsOk(SQLGetData(session->hstmt, i + 1, SQL_C_CHAR, &tm, 0, &li)))
int ct = binary[i] ? SQL_C_BINARY : SQL_C_CHAR;
if(!IsOk(SQLGetData(session->hstmt, i + 1, ct, &tm, 0, &li)))
break;
if(li != SQL_NULL_DATA) {
StringBuffer sb;
sb.SetLength(li);
if(!IsOk(SQLGetData(session->hstmt, i + 1, SQL_C_CHAR, ~sb, li + 1, &li)))
if(!IsOk(SQLGetData(session->hstmt, i + 1, ct, ~sb, li + 1, &li)))
break;
v = String(sb);
}
@ -457,6 +477,9 @@ bool ODBCConnection::Fetch()
case DOUBLE_V:
v = number[i][rowi];
break;
case INT64_V:
v = num64[i][rowi];
break;
case TIME_V:
v = time[i][rowi];
break;
@ -494,6 +517,9 @@ void ODBCConnection::Flush()
case DOUBLE_V:
number[i].Add(fetchrow[i]);
break;
case INT64_V:
num64[i].Add(fetchrow[i]);
break;
case STRING_V:
text[i].Add(fetchrow[i]);
break;
@ -526,11 +552,4 @@ Value ODBCConnection::GetInsertedId() const
: sql.Select("@@IDENTITY");
}
String MSSQLTextType(int width)
{
if(width <= 4000)
return NFormat("varchar(%d)", width);
return "text";
}
END_UPP_NAMESPACE

View file

@ -1,61 +1,60 @@
#ifndef _ODBC_ODBC_h
#define _ODBC_ODBC_h
#include <Sql/Sql.h>
#include <sql.h>
#include <sqlext.h>
NAMESPACE_UPP
bool ODBCPerformScript(const String& text, StatementExecutor& executor, Gate2<int, int> progress_canceled = false);
String MSSQLTextType(int width);
class ODBCConnection;
class ODBCSession : public SqlSession {
public:
virtual void Begin();
virtual void Commit();
virtual void Rollback();
virtual String Savepoint();
virtual void RollbackTo(const String& savepoint);
virtual bool IsOpen() const;
virtual Vector<String> EnumUsers();
virtual Vector<String> EnumDatabases();
virtual Vector<String> EnumTables(String database);
virtual Vector<String> EnumViews(String database);
virtual Vector<String> EnumSequences(String database);
virtual Vector<String> EnumPrimaryKeys(String database, String table);
virtual String EnumRowID(String database, String table);
virtual RunScript GetRunScript() const { return &ODBCPerformScript; }
protected:
virtual SqlConnection *CreateConnection();
private:
friend class ODBCConnection;
HENV henv;
HDBC hdbc;
HSTMT hstmt;
String user;
int tlevel;
ODBCConnection *current;
void FlushConnections();
bool IsOk(SQLRETURN ret);
public:
bool Connect(const char *cs);
void Close();
ODBCSession();
~ODBCSession();
};
END_UPP_NAMESPACE
#endif
#ifndef _ODBC_ODBC_h
#define _ODBC_ODBC_h
#include <Sql/Sql.h>
#include <sql.h>
#include <sqlext.h>
NAMESPACE_UPP
bool ODBCPerformScript(const String& text, StatementExecutor& executor, Gate2<int, int> progress_canceled = false);
class ODBCConnection;
class ODBCSession : public SqlSession {
public:
virtual void Begin();
virtual void Commit();
virtual void Rollback();
virtual String Savepoint();
virtual void RollbackTo(const String& savepoint);
virtual bool IsOpen() const;
virtual Vector<String> EnumUsers();
virtual Vector<String> EnumDatabases();
virtual Vector<String> EnumTables(String database);
virtual Vector<String> EnumViews(String database);
virtual Vector<String> EnumSequences(String database);
virtual Vector<String> EnumPrimaryKeys(String database, String table);
virtual String EnumRowID(String database, String table);
virtual RunScript GetRunScript() const { return &ODBCPerformScript; }
protected:
virtual SqlConnection *CreateConnection();
private:
friend class ODBCConnection;
HENV henv;
HDBC hdbc;
HSTMT hstmt;
String user;
int tlevel;
ODBCConnection *current;
void FlushConnections();
bool IsOk(SQLRETURN ret);
public:
bool Connect(const char *cs);
void Close();
ODBCSession();
~ODBCSession();
};
END_UPP_NAMESPACE
#endif

View file

@ -1,3 +1,8 @@
#define BIT(x) COLUMN("bit", int, x, 0, 0)
#define BIT_ARRAY(x, items) COLUMN_ARRAY("bit", int, x, 0, 0, items)
#define BIT_(x) COLUMN_("bit", int, x, 0, 0)
#define BIT_ARRAY_(x, items) COLUMN_ARRAY_("bit", int, x, 0, 0, items)
#define BOOL(x) COLUMN("bit", int, x, 0, 0)
#define BOOL_ARRAY(x, items) COLUMN_ARRAY("bit", int, x, 0, 0, items)
#define BOOL_(x) COLUMN_("bit", int, x, 0, 0)

View file

@ -34,6 +34,7 @@ umk
Updater
TextDiffCtrl
ODBC
MSSQL
usvn
art
Painter