[GROUP] Database management operations
353759
def db(ctx):
    """"""

    from hfos import database
    database.initialize(ctx.obj['dbhost'], ctx.obj['dbname'])
    ctx.obj['db'] = database
181662
def execute(self, conn, run_num=-1, logical_file_name="", block_name="", dataset="", trans=False):
        
        sql = self.sql
        binds = {}
	if logical_file_name and "%" not in logical_file_name:
	    sql += """ inner join %sFILES FILES on FILES.FILE_ID = FL.FILE_ID
		    WHERE FILES.LOGICAL_FILE_NAME = :logical_file_name"""%(self.owner)
	    binds["logical_file_name"] = logical_file_name
	elif block_name and "%" not in block_name:
            sql += """ inner join %sFILES FILES on FILES.FILE_ID = FL.FILE_ID
		    inner join %sBLOCKS BLOCKS on BLOCKS.BLOCK_ID = FILES.BLOCK_ID
		    WHERE BLOCKS.BLOCK_NAME = :block_name """%(self.owner, self.owner)
            binds["block_name"] =  block_name
	elif dataset and "%" not in dataset:
	    sql += """ inner join %sFILES FILES on FILES.FILE_ID = FL.FILE_ID
	    inner join %sDATASETS DATASETS on DATASETS.DATASET_ID = FILES.DATASET_ID
	    WHERE DATASETS.DATASET = :dataset """%(self.owner, self.owner)
	    binds["dataset"] = dataset
	else:
	    pass
        
	if run_num != -1:
            andorwhere = ("WHERE", "AND")["WHERE" in sql]
            run_list = []
            wheresql_run_list = ''
            wheresql_run_range = ''
            #
            for r in parseRunRange(run_num):
                if isinstance(r, basestring) or isinstance(r, int) or isinstance(r, long):
                    run_list.append(str(r))
                if isinstance(r, run_tuple):
                    if r[0] == r[1]:
                        dbsExceptionHandler('dbsException-invalid-input', "DBS run_num range must be apart at least by 1.", self.logger.exception)
                    wheresql_run_range = " FL.RUN_NUM between :minrun and :maxrun "
                    binds.update({"minrun":r[0]})
                    binds.update({"maxrun":r[1]})
            # 
            if run_list:
                wheresql_run_list = " fl.RUN_NUM in (SELECT TOKEN FROM TOKEN_GENERATOR) "
                run_generator, run_binds = create_token_generator(run_list)
                sql =  "{run_generator}".format(run_generator=run_generator) + sql
                binds.update(run_binds)

            if wheresql_run_range and wheresql_run_list:
                sql += " %s (" %andorwhere    + wheresql_run_range + " or " +  wheresql_run_list + " )"
            elif wheresql_run_range and not wheresql_run_list:
                sql += " %s " %andorwhere  + wheresql_run_range
            elif not wheresql_run_range and wheresql_run_list:
                sql += " %s " %andorwhere  + wheresql_run_list
        #self.logger.debug(sql)
	cursors = self.dbi.processData(sql, binds, conn, transaction=trans, returnCursor=True)
        result=[]
        for c in cursors:
            result.extend(self.formatCursor(c, size=100))
        return result
1213127
def store_many(self, sql, values):
        """"""
        cursor = self.get_cursor()
        cursor.executemany(sql, values)
        self.conn.commit()
322598
def upsert(db, table, key_cols, update_dict):
    
    with db:
        cur = db.cursor()
        cur.execute(
            'UPDATE {} SET {} WHERE {}'.format(
                table,
                ','.join(_sqlpformat(col) for col in update_dict.keys()),
                ' AND '.join(_sqlpformat(col) for col in key_cols),
            ),
            update_dict,
        )
        if db.changes() == 0:
            keys, values = zip(*update_dict.items())
            cur.execute(
                'INSERT INTO {} ({}) VALUES ({})'.format(
                    table,
                    ','.join(keys),
                    ','.join('?' for _ in values)),
                values)
1038235
public void collectDbStats(ArrayList<com.couchbase.lite.internal.database.sqlite.SQLiteDebug.DbStats> dbStatsList) {
        synchronized (mLock) {
            if (mAvailablePrimaryConnection != null) {
                mAvailablePrimaryConnection.collectDbStats(dbStatsList);
            }

            for (SQLiteConnection connection : mAvailableNonPrimaryConnections) {
                connection.collectDbStats(dbStatsList);
            }

            for (SQLiteConnection connection : mAcquiredConnections.keySet()) {
                connection.collectDbStatsUnsafe(dbStatsList);
            }
        }
    }
138389
private void handleOperations(List<TableInfo> tableInfos)
    {
        SchemaOperationType operationType = SchemaOperationType.getInstance(operation);

        switch (operationType)
        {
        case createdrop:
            create_drop(tableInfos);
            break;
        case create:
            create(tableInfos);
            break;
        case update:
            update(tableInfos);
            break;
        case validate:
            validate(tableInfos);
            break;
        }
    }
19568
public static void executeSQL(final SQLiteDatabase database, List<String> commands) {
		for (String command : commands) {
			executeSQL(database, command);
		}
		// commands.forEach(command -> {
		// executeSQL(database, command);
		// });
	}
1088422
def _createDatabase( self ):
        """"""

        # create experiment metadata table
        command = """
                  CREATE TABLE {tn} (
                  {k}             INT PRIMARY KEY NOT NULL,
                  START_TIME      INT             NOT NULL,
                  END_TIME        INT             NOT NULL,
                  ELAPSED_TIME    INT             NOT NULL,
                  SETUP_TIME      INT             NOT NULL,
                  EXPERIMENT_TIME INT             NOT NULL,
                  TEARDOWN_TIME   INT             NOT NULL,
                  STATUS          BOOLEAN         NOT NULL)
                  """
        self._connection.execute(command.format(tn = self.EXPERIMENT_METADATA_TABLE,
                                                k = self.EXPERIMENT_ID))
        
        # commit the changes
        self.commit()
273170
def running_objects(self):
        """"""
        return [obj for obj in self.database_objects
                if obj.status in [obj.known_statuses.RUNNING]]
572749
@Override
  protected void process(DBIDRef id, ArrayDBIDs ids, DBIDArrayIter it, int n, WritableDBIDDataStore pi, WritableDoubleDataStore lambda, WritableDoubleDataStore m) {
    clinkstep3(id, it, n, pi, lambda, m);
    clinkstep4567(id, ids, it, n, pi, lambda, m);
    clinkstep8(id, it, n, pi, lambda, m);
  }
67364
function runProgramLogic() {
  var entries = db.getCollection("entries");
  var entryCount = entries.count();
  var now = new Date();

  console.log("old number of entries in database : " + entryCount);

  entries.insert({ x: now.getTime(), y: 100 - entryCount });
  entryCount = entries.count();

  console.log("new number of entries in database : " + entryCount);
  console.log("");
  
  // manually save
  db.saveDatabase(function(err) {
    if (err) {
      console.log(err);
    }
    else {
      console.log("saved... it can now be loaded or reloaded with up to date data");
    }
  });
}
229159
def run_workload(database, keys, parameters):
    """"""
    total_weight = 0.0
    weights = []
    operations = []
    latencies_ms = {}
    for operation in OPERATIONS:
        weight = float(parameters[operation])
        if weight <= 0.0:
            continue
        total_weight += weight
        op_code = operation.split('proportion')[0]
        operations.append(op_code)
        weights.append(total_weight)
        latencies_ms[op_code] = []

    threads = []
    start = timeit.default_timer()
    for i in range(int(parameters['num_worker'])):
        thread = WorkloadThread(database, keys, parameters, total_weight,
                                weights, operations)
        thread.start()
        threads.append(thread)

    for thread in threads:
        thread.join()
        end = timeit.default_timer()

    for thread in threads:
        thread_latencies_ms = thread.latencies_ms()
        for key in latencies_ms.keys():
            latencies_ms[key].extend(thread_latencies_ms[key])

    aggregate_metrics(latencies_ms, (end - start) * 1000.0,
                      parameters['num_bucket'])
885167
def handle(self, *args, **options):
        
        self.db = options.get("database", DEFAULT_DB_ALIAS)
        self.current_name = connections[self.db].settings_dict["NAME"]
        self.compare_name = options.get("db_name")
        self.lines = options.get("lines")
        self.ignore = int(options.get('ignore'))

        if not self.compare_name:
            self.compare_name = "%s_compare" % self.current_name
        
        command = NASHVEGAS.get("dumpdb", "pg_dump -s {dbname}")
        
        print "Getting schema for current database..."
        current_sql = Popen(
            command.format(dbname=self.current_name),
            shell=True,
            stdout=PIPE
        ).stdout.readlines()
        
        print "Getting schema for fresh database..."
        self.setup_database()
        connections[self.db].close()
        connections[self.db].settings_dict["NAME"] = self.compare_name
        try:
            call_command("syncdb", interactive=False, verbosity=0, migrations=False)
            new_sql = Popen(
                command.format(dbname=self.compare_name).split(),
                stdout=PIPE
            ).stdout.readlines()
        finally:
            connections[self.db].close()
            connections[self.db].settings_dict["NAME"] = self.current_name
            self.teardown_database()
        
        print "Outputing diff between the two..."
        print "".join(difflib.unified_diff(normalize_sql(current_sql, self.ignore),
                                           normalize_sql(new_sql, self.ignore),
                                           n=int(self.lines)))
1274799
def execute(self, conn, dataset="",  logical_file_name="", app="", release_version="", pset_hash="", 
                output_label ="", block_id=0, global_tag='', transaction = False):
        
	#sql=self.sql	
        binds = {}
	setAnd=False
	#add search only block id only for migration dump block. 
	if block_id==0:
	    sql = self.sql1 + self.sql2
	    if dataset:
		sql += " JOIN %sDATASET_OUTPUT_MOD_CONFIGS DC ON DC.OUTPUT_MOD_CONFIG_ID=O.OUTPUT_MOD_CONFIG_ID" % self.owner
		sql += " JOIN %sDATASETS DS ON DS.DATASET_ID=DC.DATASET_ID" % self.owner
	    if logical_file_name:
		sql += " JOIN %sFILE_OUTPUT_MOD_CONFIGS FC ON FC.OUTPUT_MOD_CONFIG_ID=O.OUTPUT_MOD_CONFIG_ID" % self.owner
		sql += " JOIN %sFILES FS ON FS.FILE_ID=FC.FILE_ID" % self.owner
	    if not app == "":
		op = ("=", "like")["%" in app]
		sql += " WHERE A.APP_NAME %s :app_name" % op
        	binds["app_name"]=app
		setAnd=True
	    if not release_version == "":
		op = ("=", "like")["%" in release_version]
		if setAnd : sql += " AND " 
		else : sql += " WHERE "
		sql += " R.RELEASE_VERSION %s :release_version" % op
		binds["release_version"]=release_version
		setAnd=True
	    if not pset_hash == "":
		op = ("=", "like")["%" in pset_hash]
		if setAnd : sql += " AND "
		else : sql += " WHERE "
		sql += " P.PSET_HASH %s :pset_hash" % op
		binds["pset_hash"]=pset_hash
		setAnd=True
	    if not output_label == "":
		op = ("=", "like")["%" in output_label]
                if setAnd : sql += " AND "
		else : sql += " WHERE "
	        sql += " O.OUTPUT_MODULE_LABEL %s :output_module_label" % op
	        binds["output_module_label"]=output_label
		setAnd=True
            if not global_tag == "":
                op = ("=", "like")["%" in global_tag]
                if setAnd : sql += " AND "
                else : sql += " WHERE "
                sql += " O.GLOBAL_TAG %s :global_tag" % op
                binds["global_tag"]=global_tag
                setAnd=True
	    if dataset:
		if setAnd : sql += " AND "
		else : sql += " WHERE "
		sql += "DS.DATASET=:dataset"
		binds["dataset"]=dataset
		setAnd=True
	    if logical_file_name:
		if setAnd : sql += " AND "
		else : sql += " WHERE "
		sql += "FS.LOGICAL_FILE_NAME=:logical_file_name"
		binds["logical_file_name"]=logical_file_name
		setAnd=True
	else:
	    #select by block id and return config along with LFN
	    sql=  self.sql1 + " , FS.LOGICAL_FILE_NAME LFN " +  self.sql2 \
	          + " JOIN %sFILE_OUTPUT_MOD_CONFIGS FC ON FC.OUTPUT_MOD_CONFIG_ID=O.OUTPUT_MOD_CONFIG_ID" % self.owner \
		  +  " JOIN %sFILES FS ON FS.FILE_ID=FC.FILE_ID" % self.owner \
		  +  "  WHERE FS.BLOCK_ID = :block_id "
	    binds["block_id"]=block_id
	cursors = self.dbi.processData(sql, binds, conn, transaction=False, returnCursor=True)
	#assert len(cursors) == 1, "output module config does not exist"
        result = []
	for c in cursors:
            result.extend(self.formatCursor(c, size=100))
        return result
511702
function (db, id, doc, metadata, cb) {
			var operations = [
				{
					Method: "PUT",
					Document: doc,
					Metadata: metadata,
					Key: id
				}
			];
			request.post({
				url: host + 'databases/' + db + '/bulk_docs',
				headers: {
					'Content-Type': 'application/json; charset=utf-8'
				},
				body: JSON.stringify(operations)
			}, function (error, response, resBody) {
				if (!error && response.statusCode === 200) {
					var result = JSON.parse(resBody);
					cb(null, result);
				} else {
					cb(error || new Error(response.statusCode), null);
				}
			});
		}
1269659
function Batch(db, options) {
  this.options = options || {};
  this.ops = [];
  this.db = db;
  this.written = false;
}
143239
public void addDatabase(Database database)
    {
        if (m_databaseCollection == null)
            m_databaseCollection = new DatabaseCollection(this);
        m_databaseCollection.addDatabase(database);
    }