Ejemplo n.º 1
0
LocalyticsDatabase::LocalyticsDatabase(QObject *parent) : QObject(parent)
{
    // Attempt to open database. It will be created if it does not exist, already.

  _databaseConnection = QSqlDatabase::addDatabase( QLatin1String("QSQLITE") );
  _databaseConnection.setDatabaseName(pathToDatabaseFile());
  bool success = _databaseConnection.open();
  if (!success)
    {
      qDebug() << _databaseConnection.lastError();
      qFatal( "Failed to connect." );
    }

    qDebug( "Connected!" );

//    // If we were unable to open the database, it is likely corrupted. Clobber it and move on.
//    if (code != SQLITE_OK) {
//        [[NSFileManager defaultManager] removeItemAtPath:dbPath error:nil];
//        code =  sqlite3_open([dbPath UTF8String], &_databaseConnection);
//    }

    // Enable foreign key constraints.
    if (success) {
        QSqlQuery q(_databaseConnection);
        success = q.exec(QLatin1String("PRAGMA foreign_keys = ON;"));
    }

    if (schemaVersion() < 7) {
        createSchema();
    }
}
Ejemplo n.º 2
0
Schema *testSchema(void)
{
	Schema *result;
	char *names[] = { "a", "b", "c" };
	DataType dt[] = { DT_INT, DT_STRING, DT_INT };
	int sizes[] = { 0, 4, 0 };
	int keys[] = { 0 };
	int i;
	char **cpNames = (char **)malloc(sizeof(char*) * 3);
	DataType *cpDt = (DataType *)malloc(sizeof(DataType) * 3);
	int *cpSizes = (int *)malloc(sizeof(int) * 3);
	int *cpKeys = (int *)malloc(sizeof(int));

	for (i = 0; i < 3; i++)
	{
		cpNames[i] = (char *)malloc(2);
		strcpy(cpNames[i], names[i]);
	}
	memcpy(cpDt, dt, sizeof(DataType) * 3);
	memcpy(cpSizes, sizes, sizeof(int) * 3);
	memcpy(cpKeys, keys, sizeof(int));

	result = createSchema(3, cpNames, cpDt, cpSizes, 1, cpKeys);

	return result;
}
Ejemplo n.º 3
0
SQLiteStore::SQLiteStore(uv_loop_t *loop, const std::string &path)
    : thread_id(uv_thread_self()),
      db(std::make_shared<Database>(path.c_str(), ReadWrite | Create)) {
    createSchema();
    worker = new uv_worker_t;
    uv_worker_init(worker, loop, 1, "SQLite");
}
Ejemplo n.º 4
0
void LogSQLite::open() throw(std::runtime_error) {
	if(connection == NULL) {
		if(sqlite3_open(filename.c_str(), &connection) != SQLITE_OK) {
			throw std::runtime_error(std::string("SQLite error: ") + sqlite3_errmsg(connection));
		}
		createSchema();
	} // else: this is just NOP
}
Ejemplo n.º 5
0
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    if (! (createConnection() && createSchema()))
        return 1;

    MainWindow window;
    QSqlQueryModel plainModel;
//    initializeModel(&plainModel);
//    createView(QObject::tr("Simulation de prêt"), &plainModel);


    window.show();
    return app.exec();
}
Ejemplo n.º 6
0
bool Repo::initSchema(int repoId, bool& isWritable) {
  if (!schemaExists(repoId)) {
    if (createSchema(repoId)) {
      // Check whether this failure is due to losing the schema
      // initialization race with another process.
      if (!schemaExists(repoId)) {
        return true;
      }
    } else {
      // createSchema() successfully wrote to the database, so no further
      // verification is necessary.
      return false;
    }
  }
  if (isWritable) {
    isWritable = writable(repoId);
  }
  return false;
}
Ejemplo n.º 7
0
Schema *
testSchema ( int numAttr, char *names[],  DataType dt[],int sizes[],int keys[])
{
  Schema *result;
  int i;
  char **cpNames = (char **) malloc(sizeof(char*) * numAttr);
  DataType *cpDt = (DataType *) malloc(sizeof(DataType) * numAttr);
  int *cpSizes = (int *) malloc(sizeof(int) * numAttr);
  int *cpKeys = (int *) malloc(sizeof(int));

  for(i = 0; i < numAttr; i++)
    {
      cpNames[i] = (char *) malloc(2);
      strcpy(cpNames[i], names[i]);
    }
  memcpy(cpDt, dt, sizeof(DataType) * numAttr);
  memcpy(cpSizes, sizes, sizeof(int) * numAttr);
  memcpy(cpKeys, keys, sizeof(int));

  result = createSchema(numAttr, cpNames, cpDt, cpSizes, 1, cpKeys);

  return result;
}
void testUserInterface()
{
	int attrNum=0;
	int keys[]={};

	printf("Enter the number of attributes: \n");
	scanf("%d",&attrNum);
	int attrTypes[attrNum];
	int attrSize[attrNum];
	DataType dt[attrNum];
	char *attrName[attrNum];
	int numRec;
	int j;

	for(j=0 ; j< attrNum ; j++){
		printf("Enter the attribute name for %d attribute :",j+1);
		attrName[j]=malloc(100);
		scanf("%s",attrName[j]);

		printf("Enter the type of attribute for %d attribute \n int : 0 \n string : 1 \n Float : 2 \n Boolean : 3 \n",j+1);
		scanf("%d",&attrTypes[j]);

		if(attrTypes[j]==0)
			dt[j] = DT_INT;
		else if(attrTypes[j]==1)
			dt[j] = DT_STRING;
		else if(attrTypes[j]==2)
			dt[j] = DT_FLOAT;
		else if(attrTypes[j]==3)
			dt[j] = DT_BOOL;

		if(attrTypes[j]!= 1){
			attrSize[j] = 0;
		}else{
			printf("Enter the size of the string attribute \n");
			scanf("%d",&attrSize[j]);
		}

	}


	Schema *schema;
	schema = createSchema(attrNum, attrName, dt, attrSize, 0 ,keys);

	RM_TableData *table = (RM_TableData *) malloc(sizeof(RM_TableData));
	  TestRecord inserts[] = {
	    {1, "aaaa", 3},
	    {2, "bbbb", 2},
	    {3, "cccc", 1},
	    {4, "dddd", 3},
	    {5, "eeee", 5},
	    {6, "ffff", 1},
	    {7, "gggg", 3},
	    {8, "hhhh", 3},
	    {9, "iiii", 2}
	  };
	  int numInserts = 9, i;
	  Record *r;
	  RID *rids;
	  testName = "testing user interface";

	  rids = (RID *) malloc(sizeof(RID) * numInserts);

	  TEST_CHECK(initRecordManager(NULL));
	  TEST_CHECK(createTable("test_table_r",schema));
	  TEST_CHECK(openTable(table, "test_table_r"));

	  // insert rows into table
	  for(i = 0; i < numInserts; i++)
	    {
	      r = fromTestRecord(schema, inserts[i]);
	      TEST_CHECK(insertRecord(table,r));
	      rids[i] = r->id;
	    }

	  TEST_CHECK(closeTable(table));
	  TEST_CHECK(openTable(table, "test_table_r"));

	  // randomly retrieve records from the table and compare to inserted ones with (int,string,int) format.
	  if(attrNum <= 3)
	  {
		for(i = 0; i < 1000; i++)
	    {
	      int pos = rand() % numInserts;
	      RID rid = rids[pos];
	      TEST_CHECK(getRecord(table, rid, r));
	      ASSERT_EQUALS_RECORDS(fromTestRecord(schema, inserts[pos]), r, schema, "compare records");
	    }
	  }

	  TEST_CHECK(closeTable(table));
	  TEST_CHECK(deleteTable("test_table_r"));
	  TEST_CHECK(shutdownRecordManager());

	  free(rids);
	  free(table);
	  TEST_DONE();

}
Ejemplo n.º 9
0
int
main (void) 
{
    initRecordManager(NULL);
    int in;
    int cl = 0;
    while(cl == 0){
        printf("What to do:\n1.Add new table\n2.insert\n3.update\n4.delete\n5.scan\n6.print table\n7.close\n");
        in=0;
        scanf("%d",&in);
        RM_TableData *table;
        Record *result,*r;
        Value *value,*v;
        int i,j;
        switch(in)
        {
            case 0:
                printf("invalid input\n");
                break;
            case 1:
                printf("\nAdding new table:\n");
                printf("Table name: ");
                char tbn[50];
                scanf("%s",tbn);
                char* tbnp = (char *) malloc(sizeof(char)*sizeof(tbn));
                memcpy(tbnp,tbn,sizeof(tbn));
                printf("number of attributes: ");
                int nat = 0;
                scanf("%d",&nat);
                printf("number of keys: ");
                int nk = 0;
                scanf("%d",&nk);
                Schema *sch;
                char **names = (char **) malloc(sizeof(char*) * nat);
                DataType* dt = (DataType *) malloc(sizeof(DataType) * nat);
                int* sizes = (int *) malloc(sizeof(int) * nat);
                int* keys = (int *) malloc(sizeof(int) * nk);
                int kptr = 0;
                for(i=0;i<nat;i++){
                    printf("Attribute %d name:  ", i+1);
                    char buf[100];
                    scanf("%s", buf);
                    char* tmp = (char *) malloc(sizeof(char)*sizeof(buf));
                    memcpy(tmp,buf,sizeof(buf));
                    names[i] = tmp;
                    printf("Attribute %d datatype: [I/S/F/B]: ", i+1);
                    char dtp;
                    scanf(" %c", &dtp);
                    //printf("\ndtp = %c", dtp);
                    switch(dtp)
                    {
                        case 'I':
                            dt[i] = DT_INT;
                            sizes[i] = 0;
                            break;
                        case 'S':
                            dt[i] = DT_STRING;
                            printf("Attribute %d length:  ", i+1);
                            scanf("%d",&sizes[i]);
                            printf("length is: %d\n", sizes[i]);
                            break;
                        case 'F':
                            dt[i] = DT_FLOAT;
                            sizes[i] = 0;
                            break;
                        case 'B':
                            dt[i] = DT_BOOL;
                            sizes[i] = 0;
                            break;
                            
                    }
                    printf("Is Attribute %d a key?: [Y/N] ", i+1);
                    char isk;
                    scanf(" %c", &isk);
                    if(isk == 'Y'){
                        keys[kptr] = i;
                        kptr++;
                    }
                    //printf("schema add: %s,%d,%c\n",names[i],sizes[i],isk);
                }
                sch = createSchema(nat,names,dt,sizes,nk,keys);
                createTable(tbnp,sch);
                break;
            case 2:
                printf("Insert record\n");
                printf("table name: ");
                char buf2[100];
                scanf("%s", buf2);
                char* tmp2 = (char *) malloc(sizeof(char)*sizeof(buf2));
                memcpy(tmp2,buf2,sizeof(buf2));
                table = (RM_TableData *) malloc(sizeof(RM_TableData));
                openTable(table, tmp2);
                createRecord(&result, table->schema);
               for(i=0;i<table->schema->numAttr;i++){
                    if(table->schema->dataTypes[i] == DT_INT)
                    {
                        printf("intput int attr[%d/%d] value -- %s: ", i+1,table->schema->numAttr,table->schema->attrNames[i]);
                        int intt = 0;
                        scanf("%d",&intt);
                        //printf("\n%d is %d\n", i, intt);
                        MAKE_VALUE(value,DT_INT,intt);
                        setAttr(result, table->schema, i, value);
                        freeVal(value);
                    } else if (table->schema->dataTypes[i] == DT_STRING)
                    {
                        printf("intput string attr[%d/%d] value -- %s: ", i+1,table->schema->numAttr,table->schema->attrNames[i]);
                        char instrbuf[100];
                        scanf("%s",instrbuf);
                        char* instr = (char *) malloc(sizeof(char)*sizeof(instrbuf));
                        memcpy(instr,instrbuf,sizeof(instrbuf));
                        MAKE_STRING_VALUE(value,instr);
                        setAttr(result, table->schema, i, value);
                        freeVal(value);
                    } else if (table->schema->dataTypes[i] == DT_FLOAT)
                    {
                        printf("intput float attr[%d/%d] value -- %s: ", i+1,table->schema->numAttr,table->schema->attrNames[i]);
                        float infl;
                        scanf("%fl",&infl);
                        MAKE_VALUE(value,DT_FLOAT,infl);
                        setAttr(result, table->schema, i, value);
                        freeVal(value);
                    } else if (table->schema->dataTypes[i] == DT_BOOL)
                    {
                        printf("intput boolean attr[%d/%d] value -- %s [T/F]: ", i+1,table->schema->numAttr,table->schema->attrNames[i]);
                        char inbl;
                        scanf(" %c",&inbl);
                        if(inbl == 'T'){
                            MAKE_VALUE(value,DT_BOOL,true);
                        }
                        else if (inbl == 'F'){
                            MAKE_VALUE(value,DT_BOOL,false);
                        }
                        setAttr(result, table->schema, i, value);
                        freeVal(value);
                    }
                    if(i==table->schema->numAttr-1){
                        printf("\n");
                    }
                }
                insertRecord(table,result);
                
                break;
            case 3:
                printf("update record\n");
                printf("table name: ");
                char buf3[100];
                scanf("%s", buf3);
                char* tmp3 = (char *) malloc(sizeof(char)*sizeof(buf3));
                memcpy(tmp3,buf3,sizeof(buf3));
                table = (RM_TableData *) malloc(sizeof(RM_TableData));
                openTable(table, tmp3);
                createRecord(&result, table->schema);
                
                r = (Record *) malloc(sizeof(Record));
                RM_ScanHandle *sc = (RM_ScanHandle *) malloc(sizeof(RM_ScanHandle));
                startScan(table, sc, NULL);
                
                printf("Select record to update: \n");
                j=0;
                int rids[100][2];
                while(next(sc, r) == RC_OK){
                    printf("(%d)   ",j+1);
                    rids[j][0] = r->id.page;
                    rids[j][1] = r->id.slot;
                    for(i=0;i<table->schema->numAttr;i++){
                        if(table->schema->dataTypes[i] == DT_INT)
                        {
                            getAttr(r,table->schema,i,&v);
                            printf("%-15d",v->v.intV);
                        } else if(table->schema->dataTypes[i] == DT_STRING)
                        {
                            getAttr(r,table->schema,i,&v);
                            printf("%-15s",v->v.stringV);
                        } else if(table->schema->dataTypes[i] == DT_FLOAT)
                        {
                            getAttr(r,table->schema,i,&v);
                            printf("%-15fl",v->v.floatV);
                        } else if(table->schema->dataTypes[i] == DT_BOOL)
                        {
                            getAttr(r,table->schema,i,&v);
                            printf("%-15s",v->v.boolV ? "true" : "false");
                        }
                    }
                    printf("\n");
                    j++;
                }
                
                int sele;
                scanf("%d", &sele);
                sele--;
                if(sele >= 0 && sele < j+1){
                    createRecord(&result, table->schema);
                    for(i=0;i<table->schema->numAttr;i++){
                        if(table->schema->dataTypes[i] == DT_INT)
                        {
                            printf("intput int attr[%d/%d] value -- %s: ", i+1,table->schema->numAttr,table->schema->attrNames[i]);
                            int intt = 0;
                            scanf("%d",&intt);
                            //printf("\n%d is %d\n", i, intt);
                            MAKE_VALUE(value,DT_INT,intt);
                            setAttr(result, table->schema, i, value);
                            freeVal(value);
                        } else if (table->schema->dataTypes[i] == DT_STRING)
                        {
                            printf("intput string attr[%d/%d] value -- %s: ", i+1,table->schema->numAttr,table->schema->attrNames[i]);
                            char instrbuf[100];
                            scanf("%s",instrbuf);
                            char* instr = (char *) malloc(sizeof(char)*sizeof(instrbuf));
                            memcpy(instr,instrbuf,sizeof(instrbuf));
                            MAKE_STRING_VALUE(value,instr);
                            setAttr(result, table->schema, i, value);
                            freeVal(value);
                        } else if (table->schema->dataTypes[i] == DT_FLOAT)
                        {
                            printf("intput float attr[%d/%d] value -- %s: ", i+1,table->schema->numAttr,table->schema->attrNames[i]);
                            float infl;
                            scanf("%fl",&infl);
                            MAKE_VALUE(value,DT_FLOAT,infl);
                            setAttr(result, table->schema, i, value);
                            freeVal(value);
                        } else if (table->schema->dataTypes[i] == DT_BOOL)
                        {
                            printf("intput boolean attr[%d/%d] value -- %s [T/F]: ", i+1,table->schema->numAttr,table->schema->attrNames[i]);
                            char inbl;
                            scanf(" %c",&inbl);
                            if(inbl == 'T'){
                                MAKE_VALUE(value,DT_BOOL,true);
                            }
                            else if (inbl == 'F'){
                                MAKE_VALUE(value,DT_BOOL,false);
                            }
                            setAttr(result, table->schema, i, value);
                            freeVal(value);
                        }
                        if(i==table->schema->numAttr-1){
                            printf("\n");
                        }
                    }
                    result->id.page = rids[sele][0];
                    result->id.slot = rids[sele][1];
                    updateRecord(table,result);
                }
                break;
            case 4:
                printf("delete record\n");
                printf("table name: ");
                char buf4[100];
                scanf("%s", buf4);
                char* tmp4 = (char *) malloc(sizeof(char)*sizeof(buf3));
                memcpy(tmp4,buf4,sizeof(buf4));
                table = (RM_TableData *) malloc(sizeof(RM_TableData));
                openTable(table, tmp4);
                createRecord(&result, table->schema);
                
                r = (Record *) malloc(sizeof(Record));
                RM_ScanHandle *sc2 = (RM_ScanHandle *) malloc(sizeof(RM_ScanHandle));
                startScan(table, sc2, NULL);
                
                printf("Select record to delete: \n");
                j=0;
                int rids2[100][2];
                while(next(sc2, r) == RC_OK){
                    printf("(%d)   ",j+1);
                    rids2[j][0] = r->id.page;
                    rids2[j][1] = r->id.slot;
                    for(i=0;i<table->schema->numAttr;i++){
                        if(table->schema->dataTypes[i] == DT_INT)
                        {
                            getAttr(r,table->schema,i,&v);
                            printf("%-15d",v->v.intV);
                        } else if(table->schema->dataTypes[i] == DT_STRING)
                        {
                            getAttr(r,table->schema,i,&v);
                            printf("%-15s",v->v.stringV);
                        } else if(table->schema->dataTypes[i] == DT_FLOAT)
                        {
                            getAttr(r,table->schema,i,&v);
                            printf("%-15fl",v->v.floatV);
                        } else if(table->schema->dataTypes[i] == DT_BOOL)
                        {
                            getAttr(r,table->schema,i,&v);
                            printf("%-15s",v->v.boolV ? "true" : "false");
                        }
                    }
                    printf("\n");
                    j++;
                }
                
                int sele2;
                scanf("%d", &sele2);
                sele2--;
                if(sele2 >= 0 && sele2 < j+1){
                    RID rid;
                    rid.page = rids[sele2][0];
                    rid.slot = rids[sele2][1];
                    deleteRecord(table,rid);
                }
                break;
            case 5:
                printf("scan\n");
                break;
            case 6:
                printf("table name: ");
                char buf[100];
                scanf("%s", buf);
                char* tmp = (char *) malloc(sizeof(char)*sizeof(buf));
                memcpy(tmp,buf,sizeof(buf));
                printtable(buf);
                break;
            case 7:
                cl = 1;
                break;
            default:
                printf("invalid input\n");
                break;
        }
    }
}