📄 xsql.cpp
字号:
/******************************************************************* * * * xsql.cpp * * * * This file is a part of the eXtremeDB source code * * Copyright (c) 2001-2006 McObject LLC * * All Rights Reserved * * * *******************************************************************/#include "mcosql.h"#include "xsqldb.h"#include "sqlcln.h"#include "sqlsrv.h"#include "platform.h"using namespace McoSql;#ifndef MCO_PLATFORM_X64size_t const PAGE_SIZE = 128; // define eXtremeDB page size#elsesize_t const PAGE_SIZE = 256;#endifsize_t const DATABASE_SIZE = 4*1024*1024;void* const MAP_ADDRESS = (void*)0x20000000;int const DEFAULT_PORT = 5001;int main(int argc, char* argv[]) { SqlEngine* engine; SqlServer* server = NULL; if (argc < 2) { fprintf(stderr, "xSQL: eXtremeSQL interactive SQL Utility\n\n" "Usage: xsql (DATABASE_NAME | @HOST)[:PORT | :*] {SQL_FILE}\n"); return 1; } char* databaseName = argv[1]; char* portPart = strchr(databaseName, ':'); int port = DEFAULT_PORT; if (portPart != NULL) { *portPart = '\0'; if (portPart[1] != '*') { if (sscanf(portPart+1, "%d", &port) != 1) { fprintf(stderr, "Invalid port number\n"); return 1; } } } if (*databaseName == '@') { // client RemoteSqlEngine* client = new RemoteSqlEngine(); client->open(databaseName+1, port); engine = client; } else { McoMultithreadedSqlEngine* sqlEngine = new McoMultithreadedSqlEngine(); sqlEngine->open(databaseName, xsqldb_get_dictionary(), DATABASE_SIZE, PAGE_SIZE, MAP_ADDRESS); if (portPart != NULL) { server = new SqlServer(sqlEngine, port); server->start(); } engine = sqlEngine; } if (argc > 2) { time_t start_time = msec(); for (int i = 2; i < argc; i++) { FILE* f = fopen(argv[i], "r"); if (f == NULL) { fprintf(stderr, "Failed to open file '%s'\n", argv[i]); } else { engine->main(NULL, f, stdout, stderr);; } } printf("Elapsed time: %d milliseconds\n", (int)(msec() - start_time)); } else { engine->main("XSQL> ", stdin, stdout, stderr); } if (server != NULL) { server->stop(); } engine->close(); delete server; delete engine; return 0;} // Test of user defined functionsstatic Value* mod(Value* a, Value* b) { if (a->isNull() || b->isNull()) { return NULL; } return new IntValue(a->intValue() % b->intValue());}static SqlFunctionDeclaration f1(tpInt, "mod", (void*)mod, 2);#ifndef NO_FLOATING_POINTstatic Value* rnd() { return new RealValue((double)rand()/RAND_MAX);}static SqlFunctionDeclaration f2(tpReal, "rnd", (void*)rnd, 0);#endifstatic Value* vxor(Vector<Value>* params) { int64_t s = 0; for (int i = 0; i < params->length; i++) { if (!params->items[i]->isNull()) { s ^= params->items[i]->intValue(); } } return new IntValue(s);}static SqlFunctionDeclaration f3(tpInt, "xor", (void*)vxor, -1);static String* env(Value* str) { return String::create(getenv(str->stringValue()->cstr()));}static SqlFunctionDeclaration f4(tpString, "env", (void*)env, 1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -