From 450e861f08c5edcdc26fca3ab38ff2a8b60b179c Mon Sep 17 00:00:00 2001 From: cxl Date: Wed, 20 Aug 2008 08:06:54 +0000 Subject: [PATCH] new fn ExportSch in Sql exports .sch file from SqlSession git-svn-id: svn://ultimatepp.org/upp/trunk@359 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Sql/ExportSch.cpp | 64 ++++++++++++++++++++++++++++++++++++++++ uppsrc/Sql/Sql.upp | 1 + uppsrc/Sql/SqlSchema.h | 3 ++ 3 files changed, 68 insertions(+) create mode 100644 uppsrc/Sql/ExportSch.cpp diff --git a/uppsrc/Sql/ExportSch.cpp b/uppsrc/Sql/ExportSch.cpp new file mode 100644 index 000000000..dd7bcf17c --- /dev/null +++ b/uppsrc/Sql/ExportSch.cpp @@ -0,0 +1,64 @@ +#include "Sql.h" + +NAMESPACE_UPP + +static String sPutId(const char *type, Index& id, String n, int w = 0) +{ + String r = type; + if(id.Find(n) < 0) { + id.Add(n); + r << '_'; + } + r << String(' ', max(0, w - r.GetLength())) << '(' << n; + return r; +} + +String ExportSch(SqlSession& session, const String& database) +{ + String r; + Vector tab = session.EnumTables(database); + Index id; + for(int i = 0; i < tab.GetCount(); i++) { + r << sPutId("TABLE", id, tab[i]) << ")\r\n"; + Vector c = session.EnumColumns(database, tab[i]); + for(int i = 0; i < c.GetCount(); i++) { + String type; + int width = Null; + switch(c[i].type) { + case INT_V: + type = "INT"; + break; + case DOUBLE_V: + type = "DOUBLE"; + break; + case DATE_V: + type = "DATE"; + break; + case TIME_V: + type = "TIME"; + break; + case STRING_V: + type = "STRING"; + width = c[i].width; + break; + default: + type = "STRING"; + width = 200; + break; + } + r << '\t' << sPutId(type, id, c[i].name, 8); + if(width > 0 && width < 4000) + r << ", " << width; + r << ")\r\n"; + } + r << "END_TABLE\r\n\r\n"; + } + return r; +} + +String ExportSch(const String& database) +{ + return ExportSch(SQL.GetSession(), database); +} + +END_UPP_NAMESPACE diff --git a/uppsrc/Sql/Sql.upp b/uppsrc/Sql/Sql.upp index 60d55d8d7..dcac94c1f 100644 --- a/uppsrc/Sql/Sql.upp +++ b/uppsrc/Sql/Sql.upp @@ -24,6 +24,7 @@ file sch_source.h, sch_schema.h, util_td.cpp, + ExportSch.cpp, Info readonly separator, Copying, Copying-plain, diff --git a/uppsrc/Sql/SqlSchema.h b/uppsrc/Sql/SqlSchema.h index 57d4e5c33..d6a7316d1 100644 --- a/uppsrc/Sql/SqlSchema.h +++ b/uppsrc/Sql/SqlSchema.h @@ -107,3 +107,6 @@ inline void SqlSchemaClear(T *a, int n) { while(n--) SqlSchemaClear(*a++); } + +String ExportSch(SqlSession& session, const String& database); +String ExportSch(const String& database);