Perform pre-copy sql - such as creating table, truncating, or removing data older than x.
545503
def init_copy(self, connection):
        
        if not self.does_schema_exist(connection):
            logger.info("Creating schema for %s", self.table)
            self.create_schema(connection)

        if not self.does_table_exist(connection):
            logger.info("Creating table %s", self.table)
            self.create_table(connection)

        if self.enable_metadata_columns:
            self._add_metadata_columns(connection)

        if self.do_truncate_table:
            logger.info("Truncating table %s", self.table)
            self.truncate_table(connection)

        if self.do_prune():
            logger.info("Removing %s older than %s from %s", self.prune_column, self.prune_date, self.prune_table)
            self.prune(connection)
794684
def executeSingle( self, conn, daoinput, tablename, transaction = False):			
	""""""
	sql1 = " insert into %s%s( " %(self.owner, tablename)
	sql2 =" values("
	"Now loop over all the input keys. We need to check if all the keys are valid !!!"
        for key in daoinput:
            sql1 += "%s," %key.upper()
            sql2 += ":%s," %key.lower()
        
        sql = sql1.strip(',') + ') ' + sql2.strip(',') + ' )'

        self.dbi.processData(sql, daoinput, conn, transaction)
227001
def setup_sql(self, graphs):
        
        with closing(self.db.cursor()) as cursor:
            for table in graphs:
                cursor.execute(SQL.CREATE_TABLE % (table))
                for index in SQL.INDEXES:
                    cursor.execute(index % (table))
            self.db.commit()
1062015
private void truncate(BatchExecutor batchExecutor, MappingConfig config) throws SQLException {
        DbMapping dbMapping = config.getDbMapping();
        StringBuilder sql = new StringBuilder();
        sql.append("TRUNCATE TABLE ").append(SyncUtil.getDbTableName(dbMapping));
        batchExecutor.execute(sql.toString(), new ArrayList<>());
        if (logger.isTraceEnabled()) {
            logger.trace("Truncate target table, sql: {}", sql);
        }
    }
951427
def truncate(self, async_=False, **kw):
        
        from .tasks import SQLTask
        async_ = kw.get('async', async_)
        task = SQLTask(name='SQLTruncateTableTask', query='truncate table %s.%s' % (self.project.name, self.name))
        instance = self.project.parent[self._client.project].instances.create(task=task)

        if not async_:
            instance.wait_for_success()
        else:
            return instance
630655
def _construct_inserts(self, record, new_columns, rename_columns, retype_columns, resize_columns):

        '''  '''

        insert_kwargs = {}

        for key, value in new_columns.items():

        # retrieve value for key (or from old key name)
            if key in rename_columns.keys():
                record_value = getattr(record, rename_columns[key], None)
            else:
                record_value = getattr(record, key, None)

        # attempt to convert datatype
            if record_value:
                if key in retype_columns.keys():
                    try:
                        old_list = False
                        if isinstance(record_value, bytes):
                            record_value = pickle.loads(record_value)
                            old_list = True
                        if retype_columns[key] == 'boolean':
                            record_value = bool(record_value)
                        elif retype_columns[key] == 'string':
                            if old_list:
                                record_value = ','.join(record_value)
                            else:
                                record_value = str(record_value)
                        elif retype_columns[key] == 'integer':
                            if old_list:
                                record_value = int(record_value[0])
                            else:
                                record_value = int(record_value)
                        elif retype_columns[key] == 'float':
                            if old_list:
                                record_value = int(record_value[0])
                            else:
                                record_value = float(record_value)
                        elif retype_columns[key] == 'list':
                            if isinstance(record_value, str):
                                record_value = pickle.dumps(record_value.split(','))
                            else:
                                record_value = pickle.dumps([record_value])
                    except:
                        record_value = None
        
        # attempt to resize string data 
                if key in resize_columns.keys():
                    max_length = resize_columns[key]
                    try:
                        if len(record_value) > max_length:
                            record_value = record_value[0:max_length]
                    except:
                        record_value = None

                insert_kwargs[key] = record_value

        return insert_kwargs
745525
private synchronized void initCopy(CopyOperationImpl op) throws SQLException, IOException {
    pgStream.receiveInteger4(); // length not used
    int rowFormat = pgStream.receiveChar();
    int numFields = pgStream.receiveInteger2();
    int[] fieldFormats = new int[numFields];

    for (int i = 0; i < numFields; i++) {
      fieldFormats[i] = pgStream.receiveInteger2();
    }

    lock(op);
    op.init(this, rowFormat, fieldFormats);
  }
1256184
@Override
  public void releasePrepareStatement(ServerPrepareResult serverPrepareResult) throws SQLException {
    //If prepared cache is enable, the ServerPrepareResult can be shared in many PrepStatement,
    //so synchronised use count indicator will be decrement.
    serverPrepareResult.decrementShareCounter();

    //deallocate from server if not cached
    if (serverPrepareResult.canBeDeallocate()) {
      forceReleasePrepareStatement(serverPrepareResult.getStatementId());
    }
  }
521585
def copy_table(tbl, start=0, stop=None, blen=None, storage=None,
               create='table', **kwargs):
    """"""

    # setup
    names, columns = _util.check_table_like(tbl)
    storage = _util.get_storage(storage)
    blen = _util.get_blen_table(tbl, blen)
    if stop is None:
        stop = len(columns[0])
    else:
        stop = min(stop, len(columns[0]))
    length = stop - start
    if length < 0:
        raise ValueError('invalid stop/start')

    # copy block-wise
    out = None
    for i in range(start, stop, blen):
        j = min(i+blen, stop)
        res = [c[i:j] for c in columns]
        if out is None:
            out = getattr(storage, create)(res, names=names,
                                           expectedlen=length, **kwargs)
        else:
            out.append(res)

    return out
624606
def copy_database_data(self, source, destination, optimized=False):
        
        # Change database to source
        self.enable_printing = False
        self.change_db(source)
        tables = self.tables

        # Copy database data by executing INSERT and SELECT commands in a single query
        if optimized:
            self._copy_database_data_serverside(source, destination, tables)

        # Generate and execute SELECT and INSERT commands
        else:
            self._copy_database_data_clientside(tables, source, destination)

        self.enable_printing = True
1305889
def prepare_database(delete_existing: bool) -> bool:
    
    settings = Settings()

    conn = psycopg2.connect(
        password=settings.DB_PASSWORD,
        host=settings.DB_HOST,
        port=settings.DB_PORT,
        user=settings.DB_USER,
    )
    conn.autocommit = True
    cur = conn.cursor()
    db_name = settings.DB_NAME
    cur.execute('SELECT EXISTS (SELECT datname FROM pg_catalog.pg_database WHERE datname=%s)', (db_name,))
    already_exists = bool(cur.fetchone()[0])
    if already_exists:
        if not delete_existing:
            print('database "{}" already exists, skipping'.format(db_name))
            return False
        else:
            print('dropping database "{}" as it already exists...'.format(db_name))
            cur.execute('DROP DATABASE {}'.format(db_name))
    else:
        print('database "{}" does not yet exist'.format(db_name))

    print('creating database "{}"...'.format(db_name))
    cur.execute('CREATE DATABASE {}'.format(db_name))
    cur.close()
    conn.close()

    # {% if database.is_pg_sqlalchemy %}
    engine = create_engine(pg_dsn(settings))
    print('creating tables from model definition...')
    Base.metadata.create_all(engine)
    engine.dispose()
    # {% else %}
    # TODO
    # {% endif %}
    return True
505362
public void copyTo(ColumnBuffer dest, LongPredicate timestampTest) {
    dest.clear();

    if (key != null) {
      dest.key = new Key(key);
    }

    for (int i = 0; i < timeStamps.size(); i++) {
      long time = timeStamps.get(i);
      if (timestampTest.test(time)) {
        dest.add(time, values.get(i));
      }
    }
  }
954404
def _copy_database_data_serverside(self, source, destination, tables):
        """"""
        for table in tqdm(tables, total=len(tables), desc='Copying table data (optimized)'):
            self.execute('INSERT INTO {0}.{1} SELECT * FROM {2}.{1}'.format(destination, wrap(table), source))
1068464
void nextExecuted(String sql) throws SQLException
    {
        count++;

        if (_order.contains(sql))
        {
            return;
        }

        String sqlCmd = sql.substring(0, 7);
        String rest = sql.substring(sqlCmd.equals("UPDATE ") ? 7 // "UPDATE "
                : 12); // "INSERT INTO " or "DELETE FROM "
        String tableName = rest.substring(0, rest.indexOf(' '));
        HashSet fkTables = (HashSet) _fkInfo.get(tableName);

        // we should not change order of INSERT/DELETE/UPDATE
        // statements for the same table
        if (_touched.contains(tableName))
        {
            executeBatch();
        }
        if (sqlCmd.equals("INSERT "))
        {
            if (_dontInsert != null && _dontInsert.contains(tableName))
            {
                // one of the previous INSERTs contained a table
                // that references this table.
                // Let's execute that previous INSERT right now so that
                // in the future INSERTs into this table will go first
                // in the _order array.
                executeBatch();
            }
        }
        else
        //if (sqlCmd.equals("DELETE ") || sqlCmd.equals("UPDATE "))
        {
            // We process UPDATEs in the same way as DELETEs
            // because setting FK to NULL in UPDATE is equivalent
            // to DELETE from the referential integrity point of view.

            if (_deleted != null && fkTables != null)
            {
                HashSet intersection = (HashSet) _deleted.clone();

                intersection.retainAll(fkTables);
                if (!intersection.isEmpty())
                {
                    // one of the previous DELETEs contained a table
                    // that is referenced from this table.
                    // Let's execute that previous DELETE right now so that
                    // in the future DELETEs into this table will go first
                    // in the _order array.
                    executeBatch();
                }
            }
        }

        _order.add(sql);

        _touched.add(tableName);
        if (sqlCmd.equals("INSERT "))
        {
            if (fkTables != null)
            {
                if (_dontInsert == null)
                {
                    _dontInsert = new HashSet();
                }
                _dontInsert.addAll(fkTables);
            }
        }
        else if (sqlCmd.equals("DELETE "))
        {
            if (_deleted == null)
            {
                _deleted = new HashSet();
            }
            _deleted.add(tableName);
        }
    }
59097
def truncate_table(self, table_name, database=None):
        
        statement = ddl.TruncateTable(table_name, database=database)
        self._execute(statement, False)
545953
def compile_truncate(self, query):
        
        sql = {
            "DELETE FROM sqlite_sequence WHERE name = %s"
            % self.get_marker(): [query.from__]
        }

        sql["DELETE FROM %s" % self.wrap_table(query.from__)] = []

        return sql
869599
def execute(self, sql, values = ()) :
		""
		sql = sql.strip()
		self._debugActions(sql, values)
		cur = self.connection.cursor()
		cur.execute(sql, values)
		return cur