📄 xsql.cpp
字号:
/******************************************************************* * * * xsql.cpp * * * * This file is a part of the eXtremeDB source code * * Copyright (c) 2001-2007 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; int pageSize = PAGE_SIZE; int databaseSize = DATABASE_SIZE; int i; for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-page-size") == 0) { pageSize = atoi(argv[++i]); } else if (strcmp(argv[i], "-database-size") == 0) { databaseSize = atoi(argv[++i]); } else if (*argv[i] == '-' || *argv[i] == '?') { fprintf(stderr, "xSQL: eXtremeSQL interactive SQL Utility\n\n" "Usage: xsql [pagesize=N] [databasesize=N] (DATABASE_NAME | @HOST)[:PORT | :*] {SQL_FILE}\n"); return 1; } else { break; } } if (i >= argc) { fprintf(stderr, "xSQL: eXtremeSQL interactive SQL Utility\n\n" "Usage: xsql [-page-size=N] [-database-size=N] (DATABASE_NAME | @HOST)[:PORT | :*] {SQL_FILE}\n"); return 1; } char* databaseName = argv[i++]; 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(), databaseSize, pageSize, MAP_ADDRESS); if (portPart != NULL) { server = new SqlServer(sqlEngine, port); server->start(); } engine = sqlEngine; } if (i < argc) { time_t start_time = msec(); while (i < argc && strcmp(argv[i], "-") != 0) { FILE* f = fopen(argv[i++], "r"); if (f == NULL) { fprintf(stderr, "Failed to open file '%s'\n", argv[i-1]); } else { engine->main(NULL, f, stdout, stderr);; } } printf("Elapsed time: %d milliseconds\n", (int)(msec() - start_time)); if (i < argc) { engine->main("XSQL> ", stdin, stdout, stderr); } } 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 + -